From 3db8bdd41e330956555c7e9cbbc1886426cfa0bb Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sun, 31 Aug 2025 00:41:11 +0300 Subject: [PATCH] Working with files is put into a separate class --- src/Services/Filesystem.php | 81 +++++++++++++++++++ src/Services/Generator.php | 43 ++-------- .../export_with_data_set____false__.snap | 4 +- .../export_with_data_set____true__.snap | 4 +- .../export_with_data_set____false__.snap | 4 +- .../export_with_data_set____true__.snap | 4 +- tests/Feature/FullTest.php | 4 +- tests/Feature/PartialTest.php | 4 +- .../Feeds/{FilledFeed.php => FullFeed.php} | 4 +- workbench/app/Feeds/PartialFeed.php | 36 +++++++++ workbench/config/feeds.php | 7 +- 11 files changed, 143 insertions(+), 52 deletions(-) create mode 100644 src/Services/Filesystem.php rename workbench/app/Feeds/{FilledFeed.php => FullFeed.php} (92%) create mode 100644 workbench/app/Feeds/PartialFeed.php diff --git a/src/Services/Filesystem.php b/src/Services/Filesystem.php new file mode 100644 index 0000000..bff317d --- /dev/null +++ b/src/Services/Filesystem.php @@ -0,0 +1,81 @@ +draft($path); + + $this->ensureFileDelete($path); + $this->ensureDirectory($path); + + return fopen($path, 'ab'); + } + + public function append($resource, string $content): void + { + if (! empty($content)) { + fwrite($resource, $content); + } + } + + /** + * @param resource $resource + */ + public function release($resource, string $path): void + { + $this->close($resource); + + if ($this->file->exists($path)) { + $this->file->delete($path); + } + + $this->file->move( + $this->draft($path), + $path + ); + } + + /** + * @param resource $resource + */ + public function close($resource): void + { + fclose($resource); + } + + protected function ensureFileDelete(string $path): void + { + if ($this->file->exists($path)) { + $this->file->delete($path); + } + } + + protected function ensureDirectory(string $path): void + { + $this->file->ensureDirectoryExists(dirname($path)); + } + + protected function draft(string $path): string + { + return $path . '.draft'; + } +} diff --git a/src/Services/Generator.php b/src/Services/Generator.php index 2eec712..c076666 100644 --- a/src/Services/Generator.php +++ b/src/Services/Generator.php @@ -7,13 +7,8 @@ use DragonCode\LaravelFeed\Data\ElementData; use DragonCode\LaravelFeed\Feeds\Feed; use Illuminate\Database\Eloquent\Collection; -use Illuminate\Filesystem\Filesystem; use function collect; -use function dirname; -use function fclose; -use function fopen; -use function fwrite; use function implode; use function sprintf; @@ -27,15 +22,14 @@ public function __construct( public function feed(Feed $feed): void { $file = $this->openFile( - $filename = $this->draft($feed) + $path = $feed->path() ); $this->performHeader($file, $feed); $this->performItem($file, $feed); $this->performFooter($file, $feed); - $this->closeFile($file); - $this->release($feed, $filename); + $this->release($file, $path); } protected function performItem($file, Feed $feed): void @@ -86,39 +80,16 @@ protected function makeRootAttributes(ElementData $item): string protected function append($file, string $content): void { - if (! empty($content)) { - fwrite($file, $content); - } - } - - protected function release(Feed $feed, string $draft): void - { - if ($this->filesystem->exists($feed->path())) { - $this->filesystem->delete($feed->path()); - } - - $this->filesystem->move($draft, $feed->path()); - } - - protected function openFile(string $filename) - { - $this->ensureDirectory($filename); - - return fopen($filename, 'ab'); - } - - protected function closeFile($file): void - { - fclose($file); + $this->filesystem->append($file, $content); } - protected function ensureDirectory(string $filename): void + protected function release($file, string $path): void { - $this->filesystem->ensureDirectoryExists(dirname($filename)); + $this->filesystem->release($file, $path); } - protected function draft(Feed $feed): string + protected function openFile(string $path) { - return $feed->path() . '.draft'; + return $this->filesystem->open($path); } } diff --git a/tests/.pest/snapshots/Feature/FullTest/export_with_data_set____false__.snap b/tests/.pest/snapshots/Feature/FullTest/export_with_data_set____false__.snap index 185915d..10fc567 100644 --- a/tests/.pest/snapshots/Feature/FullTest/export_with_data_set____false__.snap +++ b/tests/.pest/snapshots/Feature/FullTest/export_with_data_set____false__.snap @@ -1,6 +1,6 @@ - + [NEWS]:Some 1Some content 1Some extra dataLuke SkywalkerLightsaberSauron]]>Evil Eye [NEWS]:Some 2Some content 2Some extra dataLuke SkywalkerLightsaberSauron]]>Evil Eye [NEWS]:Some 3Some content 3Some extra dataLuke SkywalkerLightsaberSauron]]>Evil Eye - + diff --git a/tests/.pest/snapshots/Feature/FullTest/export_with_data_set____true__.snap b/tests/.pest/snapshots/Feature/FullTest/export_with_data_set____true__.snap index 531c4b0..8b7a43e 100644 --- a/tests/.pest/snapshots/Feature/FullTest/export_with_data_set____true__.snap +++ b/tests/.pest/snapshots/Feature/FullTest/export_with_data_set____true__.snap @@ -1,5 +1,5 @@ - + [NEWS]:Some 1 Some content 1 @@ -45,4 +45,4 @@ - + diff --git a/tests/.pest/snapshots/Feature/PartialTest/export_with_data_set____false__.snap b/tests/.pest/snapshots/Feature/PartialTest/export_with_data_set____false__.snap index 185915d..9931cdf 100644 --- a/tests/.pest/snapshots/Feature/PartialTest/export_with_data_set____false__.snap +++ b/tests/.pest/snapshots/Feature/PartialTest/export_with_data_set____false__.snap @@ -1,6 +1,6 @@ - + [NEWS]:Some 1Some content 1Some extra dataLuke SkywalkerLightsaberSauron]]>Evil Eye [NEWS]:Some 2Some content 2Some extra dataLuke SkywalkerLightsaberSauron]]>Evil Eye [NEWS]:Some 3Some content 3Some extra dataLuke SkywalkerLightsaberSauron]]>Evil Eye - + diff --git a/tests/.pest/snapshots/Feature/PartialTest/export_with_data_set____true__.snap b/tests/.pest/snapshots/Feature/PartialTest/export_with_data_set____true__.snap index 531c4b0..181d385 100644 --- a/tests/.pest/snapshots/Feature/PartialTest/export_with_data_set____true__.snap +++ b/tests/.pest/snapshots/Feature/PartialTest/export_with_data_set____true__.snap @@ -1,5 +1,5 @@ - + [NEWS]:Some 1 Some content 1 @@ -45,4 +45,4 @@ - + diff --git a/tests/Feature/FullTest.php b/tests/Feature/FullTest.php index a41d856..daad8b4 100644 --- a/tests/Feature/FullTest.php +++ b/tests/Feature/FullTest.php @@ -4,7 +4,7 @@ use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand; use Workbench\App\Data\NewsFakeData; -use Workbench\App\Feeds\FilledFeed; +use Workbench\App\Feeds\FullFeed; use function Pest\Laravel\artisan; @@ -13,7 +13,7 @@ createNews(...NewsFakeData::toArray()); - $feed = app()->make(FilledFeed::class); + $feed = app()->make(FullFeed::class); artisan(FeedGenerateCommand::class)->run(); diff --git a/tests/Feature/PartialTest.php b/tests/Feature/PartialTest.php index 908e67e..ae2d9cf 100644 --- a/tests/Feature/PartialTest.php +++ b/tests/Feature/PartialTest.php @@ -4,7 +4,7 @@ use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand; use Workbench\App\Data\NewsFakeData; -use Workbench\App\Feeds\FilledFeed; +use Workbench\App\Feeds\PartialFeed; use function Pest\Laravel\artisan; @@ -17,7 +17,7 @@ createNews(...NewsFakeData::toArray()); - $feed = app()->make(FilledFeed::class); + $feed = app()->make(PartialFeed::class); artisan(FeedGenerateCommand::class)->run(); diff --git a/workbench/app/Feeds/FilledFeed.php b/workbench/app/Feeds/FullFeed.php similarity index 92% rename from workbench/app/Feeds/FilledFeed.php rename to workbench/app/Feeds/FullFeed.php index e0071a7..d821b71 100644 --- a/workbench/app/Feeds/FilledFeed.php +++ b/workbench/app/Feeds/FullFeed.php @@ -15,7 +15,7 @@ use function class_basename; use function now; -class FilledFeed extends Feed +class FullFeed extends Feed { public function builder(): Builder { @@ -31,7 +31,7 @@ class_basename($this) public function filename(): string { - return 'nested/filled.xml'; + return 'nested/full.xml'; } public function item(Model $model): FeedItem diff --git a/workbench/app/Feeds/PartialFeed.php b/workbench/app/Feeds/PartialFeed.php new file mode 100644 index 0000000..dcacaa7 --- /dev/null +++ b/workbench/app/Feeds/PartialFeed.php @@ -0,0 +1,36 @@ +where('updated_at', '>', now()->subDay()); + } + + public function root(): ElementData + { + return new ElementData( + class_basename($this) + ); + } + + public function item(Model $model): FeedItem + { + return new NewsFeedItem($model); + } +} diff --git a/workbench/config/feeds.php b/workbench/config/feeds.php index 69b0c5a..82e517a 100644 --- a/workbench/config/feeds.php +++ b/workbench/config/feeds.php @@ -3,14 +3,17 @@ declare(strict_types=1); use Workbench\App\Feeds\EmptyFeed; -use Workbench\App\Feeds\FilledFeed; +use Workbench\App\Feeds\FullFeed; +use Workbench\App\Feeds\PartialFeed; use Workbench\App\Feeds\SitemapFeed; use Workbench\App\Feeds\YandexFeed; return [ 'channels' => [ EmptyFeed::class => true, - FilledFeed::class => true, + FullFeed::class => true, + PartialFeed::class => true, + SitemapFeed::class => true, YandexFeed::class => true, ],