From 8a29e938058651ee8a1f1d0747fe7dc139ae7a08 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Fri, 26 Mar 2021 20:47:06 +0000 Subject: [PATCH 1/6] wip [skip-ci] --- src/Concerns/Orbital.php | 1 + src/Drivers/Json.php | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Concerns/Orbital.php b/src/Concerns/Orbital.php index e9f0990..fd9681d 100644 --- a/src/Concerns/Orbital.php +++ b/src/Concerns/Orbital.php @@ -127,6 +127,7 @@ public function migrate() $columns = $schema->getColumnListing($table); $driver->all(static::getOrbitalPath()) + ->filter() ->map(function ($row) use ($columns) { return collect($row) ->filter(fn ($_, $key) => in_array($key, $columns)) diff --git a/src/Drivers/Json.php b/src/Drivers/Json.php index fe82c46..3945dd7 100644 --- a/src/Drivers/Json.php +++ b/src/Drivers/Json.php @@ -16,7 +16,13 @@ protected function dumpContent(Model $model): string protected function parseContent(SplFileInfo $file): array { - return json_decode(file_get_contents($file->getPathname()), true); + $contents = file_get_contents($file->getPathname()); + + if (! $contents) { + return []; + } + + return json_decode($contents, true); } protected function extension(): string From 541ac3f24adb02ab62faf65b6f09e2860c3cf778 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Fri, 26 Mar 2021 20:47:19 +0000 Subject: [PATCH 2/6] wip [skip-ci] --- src/Drivers/FileDriver.php | 7 +++++++ src/Drivers/Json.php | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Drivers/FileDriver.php b/src/Drivers/FileDriver.php index 05a0502..b95d8e0 100644 --- a/src/Drivers/FileDriver.php +++ b/src/Drivers/FileDriver.php @@ -67,6 +67,13 @@ protected function filepath(string $directory, string $key): string return $directory . DIRECTORY_SEPARATOR . $key . '.' . $this->extension(); } + protected function getModelAttributes(Model $model) + { + return collect($model->getAttributes()) + ->map(fn ($_, $key) => $model->{$key}) + ->toArray(); + } + abstract protected function extension(): string; abstract protected function dumpContent(Model $model): string; diff --git a/src/Drivers/Json.php b/src/Drivers/Json.php index 3945dd7..cd16e21 100644 --- a/src/Drivers/Json.php +++ b/src/Drivers/Json.php @@ -9,7 +9,7 @@ class Json extends FileDriver { protected function dumpContent(Model $model): string { - $data = array_filter($model->attributesToArray()); + $data = array_filter($this->getModelAttributes($model)); return json_encode($data, JSON_PRETTY_PRINT); } From 29398b9ebf25302f3ef667609e38b7dc12db0185 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Fri, 26 Mar 2021 20:47:47 +0000 Subject: [PATCH 3/6] wip [skip-ci] --- src/Drivers/Markdown.php | 2 +- src/Drivers/MarkdownJson.php | 2 +- src/Drivers/Yaml.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Drivers/Markdown.php b/src/Drivers/Markdown.php index 5ad3de3..15d3683 100644 --- a/src/Drivers/Markdown.php +++ b/src/Drivers/Markdown.php @@ -14,7 +14,7 @@ class Markdown extends FileDriver protected function dumpContent(Model $model): string { - $matter = array_filter($model->attributesToArray(), function ($value, $key) { + $matter = array_filter($this->getModelAttributes($model), function ($value, $key) { return $key !== 'content' && $value !== null; }, ARRAY_FILTER_USE_BOTH); diff --git a/src/Drivers/MarkdownJson.php b/src/Drivers/MarkdownJson.php index 0bfcd68..1c85079 100644 --- a/src/Drivers/MarkdownJson.php +++ b/src/Drivers/MarkdownJson.php @@ -10,7 +10,7 @@ class MarkdownJson extends Markdown { protected function dumpContent(Model $model): string { - $matter = array_filter($model->attributesToArray(), function ($value, $key) { + $matter = array_filter($this->getModelAttributes($model), function ($value, $key) { return $key !== 'content' && $value !== null; }, ARRAY_FILTER_USE_BOTH); diff --git a/src/Drivers/Yaml.php b/src/Drivers/Yaml.php index 6d2bda7..c8b9893 100644 --- a/src/Drivers/Yaml.php +++ b/src/Drivers/Yaml.php @@ -10,7 +10,7 @@ class Yaml extends FileDriver { protected function dumpContent(Model $model): string { - $data = array_filter($model->getAttributes()); + $data = array_filter($this->getModelAttributes($model)); return SymfonyYaml::dump($data); } From af60501a9c05d25a48e145d83ac84f48132d9c56 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Fri, 26 Mar 2021 20:50:11 +0000 Subject: [PATCH 4/6] wip [skip-ci] --- tests/SoftDeletesTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/SoftDeletesTest.php b/tests/SoftDeletesTest.php index e0a84a0..7054150 100644 --- a/tests/SoftDeletesTest.php +++ b/tests/SoftDeletesTest.php @@ -23,7 +23,7 @@ public function test_it_will_update_deleted_at_when_deleting() $file = file_get_contents(__DIR__.'/content/soft_deleted_posts/'.$post->id.'.md'); - $this->assertStringContainsString(sprintf('deleted_at: \'%s\'', $post->deleted_at->toIsoString()), $file); + $this->assertStringContainsString(sprintf('deleted_at: %s', $post->deleted_at->toIso8601String()), $file); } public function test_it_will_delete_file_when_force_deleting() @@ -38,7 +38,7 @@ public function test_it_will_delete_file_when_force_deleting() $file = file_get_contents(__DIR__.'/content/soft_deleted_posts/'.$post->id.'.md'); - $this->assertStringContainsString(sprintf('deleted_at: \'%s\'', $post->deleted_at->toIsoString()), $file); + $this->assertStringContainsString(sprintf('deleted_at: %s', $post->deleted_at->toIso8601String()), $file); $post->forceDelete(); @@ -57,7 +57,7 @@ public function test_it_will_remove_deleted_at_when_restoring_file() $file = file_get_contents(__DIR__.'/content/soft_deleted_posts/'.$post->id.'.md'); - $this->assertStringContainsString(sprintf('deleted_at: \'%s\'', $post->deleted_at->toIsoString()), $file); + $this->assertStringContainsString(sprintf('deleted_at: %s', $post->deleted_at->toIso8601String()), $file); $post->restore(); From f72a3ff6cc804843197cae7282dee8ed9e8db0ff Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Fri, 26 Mar 2021 20:52:45 +0000 Subject: [PATCH 5/6] fix: write hidden props to file --- tests/AdvancedOrbitalTest.php | 16 ++++++++++++++++ tests/Fixtures/Post.php | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/tests/AdvancedOrbitalTest.php b/tests/AdvancedOrbitalTest.php index 1d9aa80..febb46e 100644 --- a/tests/AdvancedOrbitalTest.php +++ b/tests/AdvancedOrbitalTest.php @@ -2,6 +2,7 @@ namespace Orbit\Tests; +use Orbit\Tests\Fixtures\Post; use Orbit\Tests\Fixtures\CustomKey; use Orbit\Tests\Fixtures\JsonModel; use Orbit\Tests\Fixtures\MarkdownJsonModel; @@ -14,6 +15,7 @@ public function tearDown(): void CustomKey::all()->each->delete(); JsonModel::all()->each->delete(); YamlModel::all()->each->delete(); + Post::all()->each->delete(); } public function test_it_can_create_files_using_custom_primary_key() @@ -67,4 +69,18 @@ public function test_it_can_use_markdown_json_driver() $this->assertFileExists(__DIR__.'/content/markdown_json_models/'.$md->getKey().'.md'); } + + public function test_it_writes_hidden_columns() + { + $post = Post::create([ + 'title' => 'Ryan', + 'slug' => 'ryan', + ]); + + $this->assertFileExists($path = __DIR__.'/content/posts/'.$post->getKey().'.md'); + + $contents = file_get_contents($path); + + $this->assertStringContainsString('slug', $contents); + } } diff --git a/tests/Fixtures/Post.php b/tests/Fixtures/Post.php index fc3f7a4..c06304c 100644 --- a/tests/Fixtures/Post.php +++ b/tests/Fixtures/Post.php @@ -12,10 +12,15 @@ class Post extends Model protected $guarded = []; + protected $hidden = [ + 'slug', + ]; + public static function schema(Blueprint $table) { $table->id(); $table->string('title'); + $table->string('slug')->nullable(); $table->text('content')->nullable(); } } From 282c2de2d329ffa392fc823b4edf1471ea867b32 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Fri, 26 Mar 2021 20:54:05 +0000 Subject: [PATCH 6/6] delete: psalm workflow --- .github/workflows/psalm.yml | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 .github/workflows/psalm.yml diff --git a/.github/workflows/psalm.yml b/.github/workflows/psalm.yml deleted file mode 100644 index ca33952..0000000 --- a/.github/workflows/psalm.yml +++ /dev/null @@ -1,33 +0,0 @@ -name: Psalm - -on: - push: - paths: - - '**.php' - - 'psalm.xml.dist' - -jobs: - psalm: - name: psalm - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '7.4' - extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick - coverage: none - - - name: Cache composer dependencies - uses: actions/cache@v2 - with: - path: vendor - key: composer-${{ hashFiles('composer.json') }} - - - name: Run composer install - run: composer install -n --prefer-dist - - - name: Run psalm - run: ./vendor/bin/psalm --output-format=github