-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2dc260f
commit 62648be
Showing
18 changed files
with
272 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
return [ | ||
|
||
'paths' => [ | ||
'database' => base_path('framework/cache/orbit/database.sqlite'), | ||
'content' => base_path('content'), | ||
], | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Orbit\Actions; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Orbit\Contracts\Orbit; | ||
|
||
class InitialiseOrbitalTable | ||
{ | ||
public function hasTable(Orbit & Model $model): bool | ||
{ | ||
$schemaBuilder = $model->resolveConnection()->getSchemaBuilder(); | ||
|
||
return $schemaBuilder->hasTable($model->getTable()); | ||
} | ||
|
||
public function migrate(Orbit & Model $model): void | ||
{ | ||
$table = $model->getTable(); | ||
$schemaBuilder = $model->resolveConnection()->getSchemaBuilder(); | ||
|
||
if ($schemaBuilder->hasTable($table)) { | ||
$schemaBuilder->drop($table); | ||
} | ||
|
||
$blueprint = null; | ||
|
||
$schemaBuilder->create($table, static function (Blueprint $table) use (&$blueprint, $model) { | ||
$blueprint = $table; | ||
|
||
$model->schema($blueprint); | ||
|
||
if ($model->usesTimestamps()) { | ||
$blueprint->timestamps(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Orbit\Actions; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Filesystem\Filesystem; | ||
use Orbit\Contracts\Orbit; | ||
|
||
class MaybeCreateOrbitDirectories | ||
{ | ||
public function execute(Orbit & Model $model) | ||
{ | ||
$fs = new Filesystem(); | ||
|
||
$fs->ensureDirectoryExists(config('orbit.paths.content')); | ||
$fs->ensureDirectoryExists(dirname(config('orbit.paths.database'))); | ||
|
||
if (! $fs->exists(config('orbit.paths.database'))) { | ||
$fs->put(config('orbit.paths.database'), ''); | ||
} | ||
|
||
$modelDirectory = config('orbit.paths.content') . DIRECTORY_SEPARATOR . $model->getTable(); | ||
|
||
$fs->ensureDirectoryExists($modelDirectory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Orbit\Actions; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Filesystem\Filesystem; | ||
use Orbit\Contracts\Driver; | ||
use Orbit\Contracts\Orbit; | ||
|
||
class SaveCompiledAttributesToFile | ||
{ | ||
public function execute(Orbit & Model $model, string $compiledAttributes, Driver $driver): void | ||
{ | ||
$directory = config('orbit.paths.content') . DIRECTORY_SEPARATOR . $model->getTable(); | ||
$filename = "{$model->getKey()}.{$driver->extension()}"; | ||
|
||
$fs = new Filesystem(); | ||
$fs->put($directory . DIRECTORY_SEPARATOR . $filename, $compiledAttributes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Orbit\Concerns; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Orbit\Actions\InitialiseOrbitalTable; | ||
use Orbit\Actions\MaybeCreateOrbitDirectories; | ||
use Orbit\Actions\SaveCompiledAttributesToFile; | ||
use Orbit\Contracts\Driver; | ||
use Orbit\Contracts\Orbit; | ||
use Orbit\Drivers\Markdown; | ||
use Orbit\Exceptions\InvalidDriverException; | ||
use Orbit\Support\ModelAttributeFormatter; | ||
|
||
/** | ||
* @mixin \Illuminate\Database\Eloquent\Model | ||
* @mixin \Orbit\Contracts\Orbit | ||
*/ | ||
trait Orbital | ||
{ | ||
public static function bootOrbital() | ||
{ | ||
$model = new static(); | ||
$maybeCreateOrbitDirectories = new MaybeCreateOrbitDirectories(); | ||
$maybeCreateOrbitDirectories->execute($model); | ||
|
||
$driver = $model->getOrbitDriver(); | ||
|
||
if (!class_exists($driver)) { | ||
throw InvalidDriverException::make($driver); | ||
} | ||
|
||
$driver = app($driver); | ||
|
||
if (!$driver instanceof Driver) { | ||
throw InvalidDriverException::make($driver::class); | ||
} | ||
|
||
$initialiseOrbitTable = new InitialiseOrbitalTable(); | ||
|
||
if (! $initialiseOrbitTable->hasTable($model)) { | ||
$initialiseOrbitTable->migrate($model); | ||
} | ||
|
||
$saveCompiledAttributesToFile = new SaveCompiledAttributesToFile(); | ||
|
||
static::created(function (Orbit & Model $model) use ($driver, $saveCompiledAttributesToFile) { | ||
$model->refresh(); | ||
|
||
$attributes = ModelAttributeFormatter::format($model, $model->getAttributes()); | ||
$compiledAttributes = $driver->compile($attributes); | ||
|
||
$saveCompiledAttributesToFile->execute($model, $compiledAttributes, $driver); | ||
}); | ||
} | ||
|
||
public static function resolveConnection($connection = null) | ||
{ | ||
return static::$resolver->connection('orbit'); | ||
} | ||
|
||
public function getConnectionName() | ||
{ | ||
return 'orbit'; | ||
} | ||
|
||
public function getOrbitDriver(): string | ||
{ | ||
return Markdown::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Orbit\Exceptions; | ||
|
||
use Exception; | ||
|
||
class InvalidDriverException extends Exception | ||
{ | ||
public static function make(string $driver): static | ||
{ | ||
return new static("Driver {$driver} is invalid or does not implements the \\Orbit\\Contracts\\Driver interface."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Orbit\Support; | ||
|
||
use BackedEnum; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Orbit\Contracts\Orbit; | ||
|
||
class ModelAttributeFormatter | ||
{ | ||
public static function format(Orbit & Model $model, array $attributes): array | ||
{ | ||
$formatted = []; | ||
|
||
foreach ($attributes as $key => $value) { | ||
$value = $model->{$key}; | ||
|
||
$formatted[$key] = match (true) { | ||
$value instanceof BackedEnum => $value->value, | ||
default => $value, | ||
}; | ||
} | ||
|
||
return $formatted; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
providers: | ||
- Orbit\OrbitServiceProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
use Tests\Fixtures\Models\Post; | ||
|
||
it('creates a new file when a model is created', function () { | ||
$post = Post::create([ | ||
'title' => 'Example Post', | ||
]); | ||
|
||
expect(base_path("content/posts/{$post->id}.md")) | ||
->toBeFile() | ||
->and(file_get_contents(base_path("content/posts/{$post->id}.md"))) | ||
->toContain(<<<MD | ||
id: $post->id | ||
title: 'Example Post' | ||
MD); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Tests\Fixtures\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Orbit\Concerns\Orbital; | ||
use Orbit\Contracts\Orbit; | ||
|
||
class Post extends Model implements Orbit | ||
{ | ||
use Orbital; | ||
|
||
protected $guarded = []; | ||
|
||
public function schema(Blueprint $table): void | ||
{ | ||
$table->id(); | ||
$table->string('title'); | ||
$table->longText('content')->nullable(); | ||
} | ||
} |
Oops, something went wrong.