-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added first implementation of event listeners for API Platform
- Loading branch information
Showing
17 changed files
with
306 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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 | ||
<?php | ||
declare(strict_types=1); | ||
|
||
// src/EventListener/DoSomethingAfterWriting.php | ||
|
||
namespace App\EventListener; | ||
|
||
use Symfony\Component\HttpKernel\Event\ViewEvent; | ||
use TimoBakx\ApiEventsBundle\ApiPlatformListeners\AfterWrite; | ||
|
||
final class DoSomethingAfterWriting implements AfterWrite | ||
{ | ||
public function __invoke(ViewEvent $event): void | ||
{ | ||
// Check for the correct object, method and/or operation name | ||
|
||
// Do something | ||
} | ||
} | ||
``` | ||
|
||
The event listener will be automatically configured for the correct tag, | ||
including the correct event and priority. |
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,32 @@ | ||
{ | ||
"name": "timobakx/api-events-bundle", | ||
"description": "Symfony bundle to add some easier to use event listeners for API Platform", | ||
"type": "symfony-bundle", | ||
"license": "MIT", | ||
"keywords": [ | ||
"Symfony", | ||
"bundle", | ||
"events", | ||
"API Platform" | ||
], | ||
"require": { | ||
"php": "^7.4 || ^8.0", | ||
"api-platform/core": "^2.5", | ||
"symfony/dependency-injection": "^5.2", | ||
"symfony/http-kernel": "^5.2" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.5", | ||
"roave/security-advisories": "dev-latest" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"TimoBakx\\ApiEventsBundle\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"TimoBakx\\ApiEventsBundle\\": "tests/" | ||
} | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; | ||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
final class ApiEventsBundle extends Bundle | ||
{ | ||
public function build(ContainerBuilder $container): void | ||
{ | ||
parent::build($container); | ||
} | ||
|
||
public function getContainerExtension(): ExtensionInterface | ||
{ | ||
return new ApiEventsExtension(); | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle; | ||
|
||
use ApiPlatform\Core\EventListener\EventPriorities; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Extension\Extension; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use TimoBakx\ApiEventsBundle\ApiPlatformListeners\AfterDeserialize; | ||
use TimoBakx\ApiEventsBundle\ApiPlatformListeners\AfterRead; | ||
use TimoBakx\ApiEventsBundle\ApiPlatformListeners\AfterRespond; | ||
use TimoBakx\ApiEventsBundle\ApiPlatformListeners\AfterSerialize; | ||
use TimoBakx\ApiEventsBundle\ApiPlatformListeners\AfterValidate; | ||
use TimoBakx\ApiEventsBundle\ApiPlatformListeners\AfterWrite; | ||
use TimoBakx\ApiEventsBundle\ApiPlatformListeners\BeforeDeserialize; | ||
use TimoBakx\ApiEventsBundle\ApiPlatformListeners\BeforeRead; | ||
use TimoBakx\ApiEventsBundle\ApiPlatformListeners\BeforeSerialize; | ||
use TimoBakx\ApiEventsBundle\ApiPlatformListeners\BeforeValidate; | ||
use TimoBakx\ApiEventsBundle\ApiPlatformListeners\BeforeWrite; | ||
|
||
final class ApiEventsExtension extends Extension | ||
{ | ||
public function load(array $configs, ContainerBuilder $container): void | ||
{ | ||
$this->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]); | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle\ApiPlatformListeners; | ||
|
||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
|
||
interface AfterDeserialize | ||
{ | ||
public function __invoke(RequestEvent $event): void; | ||
} |
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle\ApiPlatformListeners; | ||
|
||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
|
||
interface AfterRead | ||
{ | ||
public function __invoke(RequestEvent $event): void; | ||
} |
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle\ApiPlatformListeners; | ||
|
||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
|
||
interface AfterRespond | ||
{ | ||
public function __invoke(ResponseEvent $event): void; | ||
} |
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle\ApiPlatformListeners; | ||
|
||
use Symfony\Component\HttpKernel\Event\ViewEvent; | ||
|
||
interface AfterSerialize | ||
{ | ||
public function __invoke(ViewEvent $event): void; | ||
} |
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle\ApiPlatformListeners; | ||
|
||
use Symfony\Component\HttpKernel\Event\ViewEvent; | ||
|
||
interface AfterValidate | ||
{ | ||
public function __invoke(ViewEvent $event): void; | ||
} |
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle\ApiPlatformListeners; | ||
|
||
use Symfony\Component\HttpKernel\Event\ViewEvent; | ||
|
||
interface AfterWrite | ||
{ | ||
public function __invoke(ViewEvent $event): void; | ||
} |
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle\ApiPlatformListeners; | ||
|
||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
|
||
interface BeforeDeserialize | ||
{ | ||
public function __invoke(RequestEvent $event): void; | ||
} |
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle\ApiPlatformListeners; | ||
|
||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
|
||
interface BeforeRead | ||
{ | ||
public function __invoke(RequestEvent $event): void; | ||
} |
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle\ApiPlatformListeners; | ||
|
||
use Symfony\Component\HttpKernel\Event\ViewEvent; | ||
|
||
interface BeforeRespond | ||
{ | ||
public function __invoke(ViewEvent $event): void; | ||
} |
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle\ApiPlatformListeners; | ||
|
||
use Symfony\Component\HttpKernel\Event\ViewEvent; | ||
|
||
interface BeforeSerialize | ||
{ | ||
public function __invoke(ViewEvent $event): void; | ||
} |
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle\ApiPlatformListeners; | ||
|
||
use Symfony\Component\HttpKernel\Event\ViewEvent; | ||
|
||
interface BeforeValidate | ||
{ | ||
public function __invoke(ViewEvent $event): void; | ||
} |
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace TimoBakx\ApiEventsBundle\ApiPlatformListeners; | ||
|
||
use Symfony\Component\HttpKernel\Event\ViewEvent; | ||
|
||
interface BeforeWrite | ||
{ | ||
public function __invoke(ViewEvent $event): void; | ||
} |