• Stars
    star
    849
  • Rank 53,688 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 3 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A flat-file database driver for Eloquent. πŸ—„

Orbit

Laravel v9.x PHP 8.0

Orbit is a flat-file driver for Laravel Eloquent. It allows you to replace your generic database with real files that you can manipulate using the methods you're familiar with.

Installation

To install Orbit, run the following command in your project:

composer require ryangjchandler/orbit

Usage

To begin using Orbit, create a Laravel model and use the Orbit\Concerns\Orbital trait.

class Post extends Model
{
    use \Orbit\Concerns\Orbital;
}

The Orbital trait is responsible for hooking into the Eloquent lifecycle and ensuring all of your interactions are handled correctly.

Defining the Schema

Just like a database migration, you need to define the different pieces of data that your Orbit model can have. Add a public static function schema(Blueprint $table) method to your model.

This method will need to accept an instance of Illuminate\Database\Schema\Blueprint, just like a migration.

use Illuminate\Database\Schema\Blueprint;

class Post extends Model
{
    use \Orbit\Concerns\Orbital;

    public static function schema(Blueprint $table)
    {
        $table->string('title');
        $table->string('slug');
        $table->timestamp('published_at');
    }
}

If some of your data is optional, make sure the corresponding column is nullable.

Storing content

By default, all content is stored inside of a content folder in the root of your application. If you wish to change this, publish the orbit.php configuration file and change the orbit.paths.content option.

Orbit will transform the base name of your model into a pluralized snake-cased string and use that as the main folder name, e.g. Post -> content/posts, PostCategory => content/post_categories.

🚨 Changing the name of a model will prevent Orbit from finding any existing records in the old folder. If you wish to change the name of the folder, overwrite the public static function getOrbitalName method on your model class and return the old name instead.

Any time you call Model::create(), Model::update or Model::delete, Orbit will intercept those calls and forward to the necessary driver method. The driver is then responsible for performing the necessary file system calls.

Changing the primary key

Just like a normal Eloquent model, you can change the primary key of your Orbit model. Overwrite the Model::getKeyName method and return the name of your new model.

class Post extends Model
{
    use Orbital;

    public function getKeyName()
    {
        return 'slug';
    }
    
    public function getIncrementing()
    {
        return false;
    }
}

If your model's primary key (the key you defined in getKeyName) doesn't need to automatically increment, you should either define public $incrementing = false on the model or overwrite the getIncrementing method.

Standard Orbit drivers will respect the new key name and use that when creating, updating and deleting files on disk, e.g. a Post with the slug hello-world will write to the content/posts/hello-world.md file.

🚨 Changing the key for a model after records already exist can cause damage. Be sure to rename your files afterwards so that Orbit doesn't create duplicate content.

Soft Deletes

Since Orbit needs to update files on disk when your model is updated, the standard Eloquent SoftDeletes trait doesn't quite work. If you want to add soft delete support to your Orbit model, you can instead use the Orbit\Concerns\SoftDeletes trait.

This trait uses the Eloquent one under-the-hood, so you can still access all of your normal SoftDeletes methods, including isForceDeleting() and forceDelete().

The Orbit version adds in the necessary hooks to perform file system operations as well as ensure you don't completely delete your content.

Validation Rules

When dealing with validation rules that check against a database like exists and unique, you should use the fully-qualified namespace (FQN) of the model instead of the table name.

This is because Orbit runs on a separate database connection - using the FQN will allow Laravel to correctly resolve the qualified table name.

class StorePostRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'slug' => 'required|alpha_dash|unique:App\Post,id',
            // 'slug' => ['required', 'alpha_dash', Rule::unique(Post::class)],
            'title' => 'required',
            'description' => 'required',
        ];
    }
  }

Testing

As previously mentioned in the Validation Rules section, Orbit operates on a custom connection called orbit. This means that Laravel's database testing utilities will work, as long as you specify the connection name.

$this->assertDatabaseCount('posts', 5, 'orbit');

$this->assertDatabaseHas('posts', [
    'title' => 'Hello, world',
    'slug' => 'hello-world',
], 'orbit');

Drivers

Orbit is a driver-based package, making it very easy to change the storage format of your data.

Out of the box, Orbit provides the following drivers:

  • md -> Orbit\Drivers\Markdown
  • json => Orbit\Drivers\Json
  • yaml => Orbit\Drivers\Yaml

md

This is a Markdown that is capable of parsing Markdown files, as well as YAML front-matter.

When Orbit loads files using this driver, it will map each front-matter key into a column in your models schema.

By default, the Markdown driver will also add a TEXT content column to your schema. This is used to store the Markdown body from the file.

πŸ’‘ If you wish to customise the name of the content column, you can use the Markdown::contentColumn() method and provide a new column name. This is applied to all models that use the Markdown driver.

json

This is a JSON driver that is capable of parsing .json files. Each key in the file is mapped to a column in your schema.

yaml

This is a YAML driver that is capable of parsing .yml files. Each key in the file is mapped to a column in your schema.

Registering drivers

You can register custom Orbit drivers by using the Orbit::extend method. You should call this method from the boot method in a ServiceProvider.

\Orbit\Facades\Orbit::extend('json', function ($app) {
    return new \App\Drivers\JsonDriver($app);
})

All drivers must implement the Orbit\Contracts\Driver contract, or extend the Orbit\Drivers\FileDriver class. This enforces drivers to have at least 4 methods:

interface Driver
{
    public function shouldRestoreCache(string $directory): bool;

    public function save(Model $model, string $directory): bool;

    public function delete(Model $model, string $directory): bool;

    public function all(string $directory): Collection;
}

Here is what each method is used for:

  • shouldRestoreCache - used to determine if the file cache should be updated.
  • save - used to persist model changes to a file on disk, or create a new file.
  • delete - used to delete an existing file on disk
  • all - used to retrieve all records from disk and cache.

Extending FileDriver

Extending the Orbit\Drivers\FileDriver class is useful when you want some of the heavy lifting done for you. To work with this base class, you should do the following:

  1. Implement an extension(): string method that returns the file extension as a string, i.e. return 'md' for Markdown.
  2. Implement a dumpContent(Model $model): string method. This method should return the updated content for the file.
  3. Implement a parseContent(SplFileInfo $file): array method. This method should return an array of key => value pairs, where each key is a column in the schema.

Changing drivers

If you wish to use a different driver for one of your models, you can add a public static $driver property to your model and set the value to the name of a driver.

class Post extends Model
{
    use Orbital;

    public static $driver = 'json';
}

Driver names are determined when they are registered with Orbit. You should always use the string name of the driver instead of the fully-qualified class name.

Disabling Orbit

If you have a model that uses the Orbital trait and would like to temporarily disable Orbit's functionality, you can override the enableOrbit(): bool method on your model:

class Post extends Model
{
    use Orbital;

    public static function enableOrbit(): bool
    {
        return false;
    }
}

More Repositories

1

spruce

A lightweight state management layer for Alpine.js. 🌲
JavaScript
911
star
2

alpine-clipboard

Simply copy to the users clipboard in your Alpine.js components. πŸ“‹
JavaScript
348
star
3

alpine-tooltip

Add tooltips to your Alpine 3.x components with a custom directive.
JavaScript
315
star
4

laravel-cloudflare-turnstile

A simple package to help integrate Cloudflare Turnstile.
PHP
227
star
5

laravel-comments

A dead-simple comments package for Laravel.
PHP
211
star
6

blade-cache-directive

Cache chunks of your Blade markup with ease. πŸ”ͺ
PHP
196
star
7

alpine-hooks

A collection of hooks for use with Alpine.js.
JavaScript
178
star
8

laravel-feature-flags

An opinionated feature flags package for Laravel.
PHP
167
star
9

filament-navigation

Build structured navigation menus in Filament.
PHP
161
star
10

prettier-plugin-blade

Adds support for Blade templates to Prettier. (work in progress)
TypeScript
138
star
11

filament-profile

A simple profile management page for Filament. ✨
PHP
100
star
12

bearer

Minimalistic token-based authorization for Laravel API endpoints.
PHP
83
star
13

blade-tabler-icons

A Blade icon pack for the Tabler icon set.
PHP
76
star
14

forge-previewer

Create preview deployments for pull requests with Laravel Forge.
PHP
74
star
15

blade-capture-directive

Create inline partials in your Blade templates with ease.
PHP
66
star
16

alpine-mask

A clean integration between Cleave.js and Alpine. ✨
JavaScript
60
star
17

filament-progress-column

Add a progress bar column to your Filament tables.
PHP
60
star
18

cpx

Quickly execute Composer package binaries from anywhere. ⚑️
PHP
58
star
19

using

Enforced disposal of objects in PHP. 🐘
PHP
53
star
20

is-php-dead.lol

Is PHP dead?
PHP
48
star
21

filament-feature-flags

Control your Laravel feature flags through a clean Filament interface.
PHP
47
star
22

lexical

Quickly build lexers in PHP.
PHP
44
star
23

laravel-uuid

A small package for adding UUIDs to Eloquent models.
PHP
40
star
24

color

A simple Color object for PHP packages and applications. 🎨
PHP
38
star
25

cursed-html

Who said HTML wasn't a programming language?
JavaScript
37
star
26

filament-tools

Add a general-purpose tools page to your Filament project. πŸ› 
PHP
37
star
27

standalone-blade

Use Laravel's Blade templating engine outside of Laravel.
PHP
36
star
28

phpast.com

A web tool to explore the ASTs generated by PHP-Parser.
PHP
35
star
29

laravel-helpers

A collection of helper functions that I use across my projects.
PHP
34
star
30

laravel-json-settings

Store your Laravel application settings in an on-disk JSON file. βš™οΈ
PHP
31
star
31

lagoon

A dynamic, weakly-typed and minimal scripting language. 🏞
Rust
29
star
32

computed-properties

A small package to add computed properties to any PHP class. 🐘
PHP
27
star
33

fern

Persisted global stores for Alpine 3.x.
JavaScript
26
star
34

laravel-slug

Simple slugs for your Laravel models.
PHP
25
star
35

skeleton-laravel

PHP
25
star
36

x-else

An `x-else` directive for use in Alpine components. ✨
JavaScript
25
star
37

filament-minimal-tabs

A clean and minimal design for the Tabs component in Filament.
CSS
24
star
38

ryangjchandler.co.uk

My personal website, blog and project documentation.
PHP
22
star
39

laravel-expose

A clean integration between Laravel and Expose. ⚑️
PHP
21
star
40

alpine-parent

Adds a handy $parent magic property to your Alpine components.
JavaScript
18
star
41

alpine-toggle

Quickly toggle / negate a property in your Alpine.js components.
JavaScript
17
star
42

puny

Make unit testing in PHP simpler again. πŸ‘Œ
PHP
17
star
43

laravel-orphan-controller

Quickly identify controller methods with no route in your Laravel applications.
PHP
16
star
44

filament-log

A simplistic log viewer for your Filament apps.
PHP
15
star
45

git

A small PHP wrapper for Git. ✨
PHP
15
star
46

filament-user-resource

A simple resource for managing users in Filament.
PHP
14
star
47

laravel-make-user

Quickly create `User` models with Artisan. ⚑️
PHP
14
star
48

fn-inspector

A utility package that helps inspect functions in PHP.
PHP
14
star
49

stencil

A simple template engine for PHP written for a tutorial blog post.
PHP
14
star
50

tonic

An elegant language for script-kiddies and terminal squatters.
Rust
12
star
51

bytes

A class to help convert bytes into other units (kb, mb, etc).
PHP
12
star
52

laravel-make-view

A simple `make:view` command for Laravel applications.
PHP
10
star
53

laravel-bunny-fonts

Manage Bunny Fonts programatically in your Laravel projects.
PHP
10
star
54

laravel-rql

I don't like writing queries for CSV exports.
PHP
9
star
55

filament-color-palette

Let users pick a color from a fixed list of options.
PHP
8
star
56

fakeinator

Quickly generate CSV files full of fake data. πŸ’½
HTML
7
star
57

container

A service container for PHP.
PHP
7
star
58

proxy

Proxy method and property interactions in PHP. ⚑️
PHP
7
star
59

laravel-auto-validate-models

A small package to auto-validate your Laravel models.
PHP
7
star
60

krypt

Share short-lived encrypted text messages with others.
PHP
6
star
61

uptime-checker

My personal uptime checker powered by Filament. ⚑️
PHP
6
star
62

blade-lint

Validate and analyse Blade templates in your Laravel project.
PHP
6
star
63

repository

πŸ“¦ A dead-simple repository class for use with Laravel.
PHP
5
star
64

rgjc.me

A disgustingly simple URL shortener powered by Laravel. πŸ“Ά
PHP
5
star
65

laravel-restore-mix

A small CLI script that removes Vite in favour of Laravel Mix.
PHP
5
star
66

microphp

A small subset of PHP implemented in Rust. 🐘
Rust
5
star
67

rss-reader

An RSS reader application built with Laravel.
PHP
5
star
68

forge-previewer-action

A quick and easy way to get Forge Previewer running in GitHub Actions.
Dockerfile
5
star
69

paddle-demultiplexer

A simple serverless function to demultiplex Paddle webhooks.
TypeScript
4
star
70

filament-data-studio

Low configuration exports and data analysis for Filament.
PHP
4
star
71

phpbyexample.dev

A simple introduction to PHP with annotated code snippets.
CSS
4
star
72

template-rust-cli

A template for command-line Rust projects.
Python
4
star
73

peso

An elephant-ish esoteric programming language inspired by Brainf*ck.
PHP
4
star
74

notepal

My personal note-taking application.
PHP
3
star
75

typewrite.cc

A bring-your-own-email-first newsletter service. ✨
PHP
3
star
76

filament-plugin-search

Add a page to your Filament panel to search for plugins.
PHP
3
star
77

filament-simple-repeater

A single-field repeater for Filament. ⚑️
PHP
3
star
78

php-ffi-example

An example of using PHP + FFI with C, Rust and Go.
PHP
3
star
79

advent-of-code

Advent of Code Solutions in PHP
PHP
3
star
80

c-in-php

An incredibly cursed C interpreter written in PHP.
PHP
3
star
81

laravel-worldwide-demo

The demo used for my talk at October's Laravel Worldwide Meetup.
PHP
2
star
82

rook-lang

A small scripting language running on V8.
Rust
2
star
83

curl-to-laravel-http

PHP
2
star
84

filament-passwordless-login

PHP
2
star
85

60-second-laravel

The demo application used in the 60 Second Laravel series.
PHP
2
star
86

pl

A toy language implemented in Rust. πŸ¦€
Rust
2
star
87

filament-plausible-page

Embed your site's Plausible dashboard inside of Filament.
PHP
2
star
88

laravel-slack-webhook

A fluent package for building Slack webhook payloads.
PHP
2
star
89

mathexpr

A tiny math expression evaluator written in PHP.
PHP
2
star
90

alpinejs-mobile

Swift
2
star
91

polling-app

A simple polling application built with the TALL stack. πŸ—Ό
PHP
2
star
92

laravel-restore-command

An Artisan command to quickly restore soft-deleted models. ⚑
PHP
2
star
93

php-parser-specs

A set of specification tests for PHP parsers, used to improve compliance and acceptance.
PHP
2
star
94

phiki

Syntax highlighting powered by TextMate grammars in PHP.
PHP
2
star
95

laravel-synchro

Easily sync and anonymise your data between environments.
PHP
2
star
96

blade-validator

Run rudimentary linting checks on your Blade templates.
PHP
1
star
97

expr

A powerful expression engine designed for business rules and sandboxed execution. πŸ”‘
PHP
1
star
98

blade-in-directive

A tidy conditional Blade directive for checking if something is in an array.
PHP
1
star
99

chains

Gold, silver, diamonds, tiny ol' JavaScript framework.
JavaScript
1
star
100

filament-sanctum

Manage your Sanctum tokens inside of Filament. ✨
PHP
1
star