Skip to content

Commit

Permalink
Merge pull request #8 from ryangjchandler/feature/json-driver
Browse files Browse the repository at this point in the history
feature: `Json` driver
  • Loading branch information
ryangjchandler authored Mar 22, 2021
2 parents 2837b77 + 1037fea commit 07a43b3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/orbit.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

'drivers' => [
'md' => \Orbit\Drivers\Markdown::class,
'json' => \Orbit\Drivers\Json::class,
],

'paths' => [
Expand Down
10 changes: 9 additions & 1 deletion src/Concerns/Orbital.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,15 @@ public function migrate()

$driver = Orbit::driver(static::getOrbitalDriver());

$driver->all(static::getOrbitalPath())->each(fn ($row) => static::insert($row));
$driver->all(static::getOrbitalPath())->each(function ($row) {
foreach ($row as $key => $value) {
$this->setAttribute($key, $value);

$row[$key] = $this->attributes[$key];
}

static::insert($row);
});
}

protected static function getOrbitalDriver()
Expand Down
71 changes: 71 additions & 0 deletions src/Drivers/Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Orbit\Drivers;

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

class Json implements Driver
{
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
{
$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;
}

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

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

return true;
}

public function all(string $directory): Collection
{
$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;
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function save(Model $model, string $directory): bool
}

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

$matter = array_filter($model->getAttributes(), function ($value, $key) {
Expand Down

0 comments on commit 07a43b3

Please sign in to comment.