Skip to content

Commit

Permalink
Merge pull request #19 from Gummibeer/yaml-driver
Browse files Browse the repository at this point in the history
add yaml driver
  • Loading branch information
ryangjchandler authored Mar 22, 2021
2 parents 4dc4bee + 6b50411 commit 79e7805
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 109 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"illuminate/events": "^8.0",
"illuminate/support": "^8.0",
"spatie/yaml-front-matter": "^2.0",
"symfony/yaml": "^3.0 || ^4.0 || ^5.0",
"symplify/git-wrapper": "^9.2"
},
"require-dev": {
Expand Down
1 change: 1 addition & 0 deletions config/orbit.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'drivers' => [
'md' => \Orbit\Drivers\Markdown::class,
'json' => \Orbit\Drivers\Json::class,
'yaml' => \Orbit\Drivers\Yaml::class,
],

'paths' => [
Expand Down
75 changes: 75 additions & 0 deletions src/Drivers/FileDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Orbit\Drivers;

use FilesystemIterator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Orbit\Facades\Orbit;
use Orbit\Contracts\Driver as DriverContract;
use SplFileInfo;

abstract class FileDriver implements DriverContract
{
public function shouldRestoreCache(string $directory): bool
{
$highest = 0;

foreach (new FilesystemIterator($directory) as $file) {
if ($file->getMTime() > $highest) {
$highest = $file->getMTime();
}
}

return $highest > filemtime(Orbit::getDatabasePath());
}

public function save(Model $model, string $directory): bool
{
if ($model->wasChanged($model->getKeyName())) {
unlink($this->filepath($directory, $model->getOriginal($model->getKeyName())));
}

$path = $this->filepath($directory, $model->getKey());

if (! file_exists($path)) {
file_put_contents($path, '');
}

file_put_contents($path, $this->dumpContent($model));

return true;
}

public function delete(Model $model, string $directory): bool
{
unlink($this->filepath($directory, $model->getKey()));

return true;
}

public function all(string $directory): Collection
{
$collection = Collection::make();
$files = new FilesystemIterator($directory);

foreach ($files as $file) {
if ($file->getExtension() !== $this->extension()) continue;

$collection->push($this->parseContent($file));
}

return $collection;
}

protected function filepath(string $directory, string $key): string
{
return $directory . DIRECTORY_SEPARATOR . $key . '.' . $this->extension();
}

abstract protected function extension(): string;

abstract protected function dumpContent(Model $model): string;

abstract protected function parseContent(SplFileInfo $file): array;
}
61 changes: 8 additions & 53 deletions src/Drivers/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,25 @@

namespace Orbit\Drivers;

use FilesystemIterator;
use Orbit\Facades\Orbit;
use Orbit\Contracts\Driver;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use SplFileInfo;

class Json implements Driver
class Json extends FileDriver
{
public function shouldRestoreCache(string $directory): bool
protected function dumpContent(Model $model): string
{
$highest = 0;

foreach (new FilesystemIterator($directory) as $file) {
if ($file->getMTime() > $highest) {
$highest = $file->getMTime();
}
}

return $highest > filemtime(Orbit::getDatabasePath());
}

public function save(Model $model, string $directory): bool
{
$key = $model->getKey();

if ($model->wasChanged($model->getKeyName())) {
unlink($directory . DIRECTORY_SEPARATOR . $model->getOriginal($model->getKeyName()) . '.json');
}

if (! file_exists($path = $directory . DIRECTORY_SEPARATOR . $key . '.json')) {
file_put_contents($path, '');
}

$data = array_filter($model->getAttributes());

$json = json_encode($data, JSON_PRETTY_PRINT);

file_put_contents($path, $json);

return true;
return json_encode($data, JSON_PRETTY_PRINT);
}

public function delete(Model $model, string $directory): bool
protected function parseContent(SplFileInfo $file): array
{
$key = $model->getKey();

unlink($directory . DIRECTORY_SEPARATOR . $key . '.json');

return true;
return json_decode(file_get_contents($file->getPathname()), true);
}

public function all(string $directory): Collection
protected function extension(): string
{
$collection = Collection::make();
$files = new FilesystemIterator($directory);

foreach ($files as $file) {
if ($file->getExtension() !== 'json') continue;

$collection->push(
json_decode(file_get_contents($file->getPathname()), true)
);
}

return $collection;
return 'json';
}
}
67 changes: 12 additions & 55 deletions src/Drivers/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,41 @@

namespace Orbit\Drivers;

use FilesystemIterator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Orbit\Contracts\Driver;
use Orbit\Facades\Orbit;
use Spatie\YamlFrontMatter\YamlFrontMatter;
use SplFileInfo;
use Symfony\Component\Yaml\Yaml;

class Markdown implements Driver
class Markdown extends FileDriver
{
public function shouldRestoreCache(string $directory): bool
protected function dumpContent(Model $model): string
{
$highest = 0;

foreach (new FilesystemIterator($directory) as $file) {
if ($file->getMTime() > $highest) {
$highest = $file->getMTime();
}
}

return $highest > filemtime(Orbit::getDatabasePath());
}

public function save(Model $model, string $directory): bool
{
$key = $model->getKey();

if ($model->wasChanged($model->getKeyName())) {
unlink($directory . DIRECTORY_SEPARATOR . $model->getOriginal($model->getKeyName()) . '.md');
}

if (! file_exists($path = $directory . DIRECTORY_SEPARATOR . $key . '.md')) {
file_put_contents($path, '');
}

$matter = array_filter($model->attributesToArray(), function ($value, $key) {
return $key !== 'content' && $value !== null;
}, ARRAY_FILTER_USE_BOTH);

$yaml = Yaml::dump($matter);

$contents = implode(PHP_EOL, [
return implode(PHP_EOL, [
'---',
rtrim($yaml, PHP_EOL),
'---',
$model->getAttribute('content'),
]);

file_put_contents($path, $contents);

return true;
}

public function delete(Model $model, string $directory): bool
protected function parseContent(SplFileInfo $file): array
{
$key = $model->getKey();
$document = YamlFrontMatter::parseFile($file->getPathname());

unlink($directory . DIRECTORY_SEPARATOR . $key . '.md');

return true;
return array_merge(
$document->matter(),
['content' => trim($document->body())]
);
}

public function all(string $directory): Collection
protected function extension(): string
{
$collection = Collection::make();
$files = new FilesystemIterator($directory);

foreach ($files as $file) {
if ($file->getExtension() !== 'md') continue;

$document = YamlFrontMatter::parseFile($file->getPathname());

$collection->push(array_merge(
$document->matter(),
['content' => trim($document->body())]
));
}

return $collection;
return 'md';
}
}
27 changes: 27 additions & 0 deletions src/Drivers/Yaml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Orbit\Drivers;

use Illuminate\Database\Eloquent\Model;
use SplFileInfo;
use Symfony\Component\Yaml\Yaml as SymfonyYaml;

class Yaml extends FileDriver
{
protected function dumpContent(Model $model): string
{
$data = array_filter($model->getAttributes());

return SymfonyYaml::dump($data);
}

protected function parseContent(SplFileInfo $file): array
{
return SymfonyYaml::parseFile($file->getPathname());
}

protected function extension(): string
{
return 'yml';
}
}
13 changes: 12 additions & 1 deletion tests/AdvancedOrbitalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Orbit\Tests\Fixtures\CustomKey;
use Orbit\Tests\Fixtures\JsonModel;
use Orbit\Tests\Fixtures\YamlModel;

class AdvancedOrbitalTest extends TestCase
{
public function tearDown(): void
{
CustomKey::all()->each->delete();
JsonModel::all()->each->delete();
YamlModel::all()->each->delete();
}

public function test_it_can_create_files_using_custom_primary_key()
Expand Down Expand Up @@ -38,12 +40,21 @@ public function test_it_can_update_files_using_custom_primary_key()
$this->assertFileExists(__DIR__.'/content/custom_keys/'.$custom->name.'.md');
}

public function test_it_can_use_a_custom_driver()
public function test_it_can_use_json_driver()
{
$json = JsonModel::create([
'name' => 'Ryan',
]);

$this->assertFileExists(__DIR__.'/content/json_models/'.$json->getKey().'.json');
}

public function test_it_can_use_yaml_driver()
{
$yaml = YamlModel::create([
'name' => 'Ryan',
]);

$this->assertFileExists(__DIR__.'/content/yaml_models/'.$yaml->getKey().'.yml');
}
}
31 changes: 31 additions & 0 deletions tests/Fixtures/YamlModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Orbit\Tests\Fixtures;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Orbit\Concerns\Orbital;

class YamlModel extends Model
{
use Orbital;

protected $guarded = [];

protected static $driver = 'yaml';

public static function schema(Blueprint $table)
{
$table->string('name');
}

public function getKeyName()
{
return 'name';
}

public function getIncrementing()
{
return false;
}
}

0 comments on commit 79e7805

Please sign in to comment.