From d534d8dfb1e382d9f727d611b7f9c8c47c596b34 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sat, 6 Sep 2025 02:29:29 +0300 Subject: [PATCH] Added `FeedStartingEvent` and `FeedFinishedEvent` --- docs/laravel-feeds.tree | 3 +++ docs/topics/events.topic | 27 ++++++++++++++++++++++++ src/Events/FeedFinishedEvent.php | 22 ++++++++++++++++++++ src/Events/FeedStartingEvent.php | 20 ++++++++++++++++++ src/Services/GeneratorService.php | 21 +++++++++++++++---- tests/Feature/Events/SuccessTest.php | 30 +++++++++++++++++++++++++++ tests/Unit/Architecture/EventTest.php | 7 +++++++ 7 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 docs/topics/events.topic create mode 100644 src/Events/FeedFinishedEvent.php create mode 100644 src/Events/FeedStartingEvent.php create mode 100644 tests/Feature/Events/SuccessTest.php create mode 100644 tests/Unit/Architecture/EventTest.php diff --git a/docs/laravel-feeds.tree b/docs/laravel-feeds.tree index 5614054..1739284 100644 --- a/docs/laravel-feeds.tree +++ b/docs/laravel-feeds.tree @@ -20,6 +20,9 @@ + + + diff --git a/docs/topics/events.topic b/docs/topics/events.topic new file mode 100644 index 0000000..46411d3 --- /dev/null +++ b/docs/topics/events.topic @@ -0,0 +1,27 @@ + + + + + Events dispatched during feed generation + Events dispatched during feed generation + Events dispatched during feed generation + + + +

+ Feeds dispatch two events when running the command: +

+ + + + The FeedStartingEvent is dispatched immediately before the draft file is created. + + + The FeedFinishedEvent is dispatched right after the feed file generation has completed. + + +
diff --git a/src/Events/FeedFinishedEvent.php b/src/Events/FeedFinishedEvent.php new file mode 100644 index 0000000..6b81138 --- /dev/null +++ b/src/Events/FeedFinishedEvent.php @@ -0,0 +1,22 @@ + $feed Reference to the feed class + * @param string $path Path to the generated feed file + * @return void + */ + public function __construct( + public string $feed, + public string $path, + ) {} +} diff --git a/src/Events/FeedStartingEvent.php b/src/Events/FeedStartingEvent.php new file mode 100644 index 0000000..ef33e02 --- /dev/null +++ b/src/Events/FeedStartingEvent.php @@ -0,0 +1,20 @@ + $feed Reference to the feed class + * @return void + */ + public function __construct( + public string $feed, + ) {} +} diff --git a/src/Services/GeneratorService.php b/src/Services/GeneratorService.php index aae996f..ea06574 100644 --- a/src/Services/GeneratorService.php +++ b/src/Services/GeneratorService.php @@ -5,6 +5,8 @@ namespace DragonCode\LaravelFeed\Services; use DragonCode\LaravelFeed\Converters\Converter; +use DragonCode\LaravelFeed\Events\FeedFinishedEvent; +use DragonCode\LaravelFeed\Events\FeedStartingEvent; use DragonCode\LaravelFeed\Exceptions\FeedGenerationException; use DragonCode\LaravelFeed\Feeds\Feed; use DragonCode\LaravelFeed\Helpers\ConverterHelper; @@ -15,6 +17,7 @@ use Throwable; use function blank; +use function event; use function get_class; use function implode; @@ -28,10 +31,9 @@ public function __construct( public function feed(Feed $feed, ?OutputStyle $output = null): void { - $file = $this->openFile( - $path = $feed->path() - ); try { + $this->started($feed); + $file = $this->openFile( $path = $feed->path() ); @@ -45,8 +47,9 @@ public function feed(Feed $feed, ?OutputStyle $output = null): void $this->release($file, $path); - $this->setLastActivity($feed); $this->setLastActivity($feed); + + $this->finished($feed, $path); } catch (Throwable $e) { throw new FeedGenerationException(get_class($feed), $e); } @@ -162,4 +165,14 @@ protected function progressBar(int $count, ?OutputStyle $output): ?ProgressBar { return $output?->createProgressBar($count); } + + protected function started(Feed $feed): void + { + event(new FeedStartingEvent(get_class($feed))); + } + + protected function finished(Feed $feed, string $path): void + { + event(new FeedFinishedEvent(get_class($feed), $path)); + } } diff --git a/tests/Feature/Events/SuccessTest.php b/tests/Feature/Events/SuccessTest.php new file mode 100644 index 0000000..9abaac2 --- /dev/null +++ b/tests/Feature/Events/SuccessTest.php @@ -0,0 +1,30 @@ +assertSuccessful() + ->run(); + + getAllFeeds()->each(function (Feed $feed) { + Event::assertDispatched(FeedStartingEvent::class, static function (FeedStartingEvent $event) use ($feed) { + return $event->feed === $feed->class; + }); + + Event::assertDispatched(FeedFinishedEvent::class, static function (FeedFinishedEvent $event) use ($feed) { + return $event->feed === $feed->class + && $event->path === app($feed->class)->path(); + }); + }); +}); diff --git a/tests/Unit/Architecture/EventTest.php b/tests/Unit/Architecture/EventTest.php new file mode 100644 index 0000000..69bd9a0 --- /dev/null +++ b/tests/Unit/Architecture/EventTest.php @@ -0,0 +1,7 @@ +expect('DragonCode\LaravelFeed\Events') + ->toHaveSuffix('Event');