-
-
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.
Merge pull request #19 from Gummibeer/yaml-driver
add yaml driver
- Loading branch information
Showing
8 changed files
with
167 additions
and
109 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,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; | ||
} |
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,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'; | ||
} | ||
} |
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,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; | ||
} | ||
} |