From 143f61d714f365c1bbc1c4141f8faed41091a636 Mon Sep 17 00:00:00 2001 From: Timo Bakx Date: Mon, 12 Apr 2021 09:24:44 +0200 Subject: [PATCH] Added first implementation of event listeners for API Platform --- .gitignore | 50 +------------ README.md | 73 ++++++++++++++++++- composer.json | 32 ++++++++ src/ApiEventsBundle.php | 21 ++++++ src/ApiEventsExtension.php | 49 +++++++++++++ src/ApiPlatformListeners/AfterDeserialize.php | 11 +++ src/ApiPlatformListeners/AfterRead.php | 11 +++ src/ApiPlatformListeners/AfterRespond.php | 11 +++ src/ApiPlatformListeners/AfterSerialize.php | 11 +++ src/ApiPlatformListeners/AfterValidate.php | 11 +++ src/ApiPlatformListeners/AfterWrite.php | 11 +++ .../BeforeDeserialize.php | 11 +++ src/ApiPlatformListeners/BeforeRead.php | 11 +++ src/ApiPlatformListeners/BeforeRespond.php | 11 +++ src/ApiPlatformListeners/BeforeSerialize.php | 11 +++ src/ApiPlatformListeners/BeforeValidate.php | 11 +++ src/ApiPlatformListeners/BeforeWrite.php | 11 +++ 17 files changed, 306 insertions(+), 51 deletions(-) create mode 100644 composer.json create mode 100644 src/ApiEventsBundle.php create mode 100644 src/ApiEventsExtension.php create mode 100644 src/ApiPlatformListeners/AfterDeserialize.php create mode 100644 src/ApiPlatformListeners/AfterRead.php create mode 100644 src/ApiPlatformListeners/AfterRespond.php create mode 100644 src/ApiPlatformListeners/AfterSerialize.php create mode 100644 src/ApiPlatformListeners/AfterValidate.php create mode 100644 src/ApiPlatformListeners/AfterWrite.php create mode 100644 src/ApiPlatformListeners/BeforeDeserialize.php create mode 100644 src/ApiPlatformListeners/BeforeRead.php create mode 100644 src/ApiPlatformListeners/BeforeRespond.php create mode 100644 src/ApiPlatformListeners/BeforeSerialize.php create mode 100644 src/ApiPlatformListeners/BeforeValidate.php create mode 100644 src/ApiPlatformListeners/BeforeWrite.php diff --git a/.gitignore b/.gitignore index 3dab634..5538a03 100644 --- a/.gitignore +++ b/.gitignore @@ -1,52 +1,4 @@ -# Cache and logs (Symfony2) -/app/cache/* -/app/logs/* -!app/cache/.gitkeep -!app/logs/.gitkeep - -# Email spool folder -/app/spool/* - -# Cache, session files and logs (Symfony3) -/var/cache/* -/var/logs/* -/var/sessions/* -!var/cache/.gitkeep -!var/logs/.gitkeep -!var/sessions/.gitkeep - -# Logs (Symfony4) -/var/log/* -!var/log/.gitkeep - -# Parameters -/app/config/parameters.yml -/app/config/parameters.ini - -# Managed by Composer -/app/bootstrap.php.cache -/var/bootstrap.php.cache -/bin/* -!bin/console -!bin/symfony_requirements /vendor/ -# Assets and user uploads -/web/bundles/ -/web/uploads/ - -# PHPUnit -/app/phpunit.xml +/composer.lock /phpunit.xml - -# Build data -/build/ - -# Composer PHAR -/composer.phar - -# Backup entities generated with doctrine:generate:entities command -**/Entity/*~ - -# Embedded web-server pid file -/.web-server-pid diff --git a/README.md b/README.md index 1c3e88b..63f6c10 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,71 @@ -# apiplatform-events -Easier to use event listeners for API-Platform +# ApiEventsBundle + +## About + +This Symfony bundle provides autoconfigured interfaces to more easily autowire +lazy event listeners for API Platform. + +## Installation + +Make sure Composer is installed globally, as explained in the +[installation chapter](https://getcomposer.org/doc/00-intro.md) +of the Composer documentation. + +### Applications that use Symfony Flex + +Open a command console, enter your project directory and execute: + +```console +$ composer require timobakx/api-events-bundle +``` + +### Applications that don't use Symfony Flex + +Open a command console, enter your project directory and execute the +following command to download the latest stable version of this bundle: + +```console +$ composer require timobakx/api-events-bundle +``` + +Then, enable the bundle by adding it to the list of registered bundles +in the `config/bundles.php` file of your project: + +```php +// config/bundles.php + +return [ + // ... + TimoBakx\ApiEventsBundle\ApiEventsBundle::class => ['all' => true], +]; +``` + +## Usage + +Create an event listener and implement one of the interfaces from the +`TimoBakx\ApiEventsBundle\ApiPlatformListeners` namespace: + +```php +registerListener($container, BeforeRead::class, KernelEvents::REQUEST, EventPriorities::PRE_READ); + $this->registerListener($container, AfterRead::class, KernelEvents::REQUEST, EventPriorities::POST_READ); + + $this->registerListener($container, BeforeDeserialize::class, KernelEvents::REQUEST, EventPriorities::PRE_DESERIALIZE); + $this->registerListener($container, AfterDeserialize::class, KernelEvents::REQUEST, EventPriorities::POST_DESERIALIZE); + + $this->registerListener($container, BeforeValidate::class, KernelEvents::VIEW, EventPriorities::PRE_VALIDATE); + $this->registerListener($container, AfterValidate::class, KernelEvents::VIEW, EventPriorities::POST_VALIDATE); + + $this->registerListener($container, BeforeWrite::class, KernelEvents::VIEW, EventPriorities::PRE_WRITE); + $this->registerListener($container, AfterWrite::class, KernelEvents::VIEW, EventPriorities::POST_WRITE); + + $this->registerListener($container, BeforeSerialize::class, KernelEvents::VIEW, EventPriorities::PRE_SERIALIZE); + $this->registerListener($container, AfterSerialize::class, KernelEvents::VIEW, EventPriorities::POST_SERIALIZE); + + $this->registerListener($container, AfterRespond::class, KernelEvents::RESPONSE, EventPriorities::POST_RESPOND); + } + + private function registerListener(ContainerBuilder $container, string $interface, string $event, int $priority): void + { + $container->registerForAutoconfiguration($interface) + ->addTag('kernel.event_listener', ['event' => $event, 'priority' => $priority, 'lazy' => true]); + } +} diff --git a/src/ApiPlatformListeners/AfterDeserialize.php b/src/ApiPlatformListeners/AfterDeserialize.php new file mode 100644 index 0000000..8504224 --- /dev/null +++ b/src/ApiPlatformListeners/AfterDeserialize.php @@ -0,0 +1,11 @@ +