Skip to content

Commit

Permalink
Added first implementation of event listeners for API Platform
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoBakx committed Apr 12, 2021
1 parent aaa29d6 commit 143f61d
Show file tree
Hide file tree
Showing 17 changed files with 306 additions and 51 deletions.
50 changes: 1 addition & 49 deletions .gitignore
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
73 changes: 71 additions & 2 deletions README.md
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.
32 changes: 32 additions & 0 deletions composer.json
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/"
}
}
}
21 changes: 21 additions & 0 deletions src/ApiEventsBundle.php
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();
}
}
49 changes: 49 additions & 0 deletions src/ApiEventsExtension.php
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]);
}
}
11 changes: 11 additions & 0 deletions src/ApiPlatformListeners/AfterDeserialize.php
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;
}
11 changes: 11 additions & 0 deletions src/ApiPlatformListeners/AfterRead.php
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;
}
11 changes: 11 additions & 0 deletions src/ApiPlatformListeners/AfterRespond.php
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;
}
11 changes: 11 additions & 0 deletions src/ApiPlatformListeners/AfterSerialize.php
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;
}
11 changes: 11 additions & 0 deletions src/ApiPlatformListeners/AfterValidate.php
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;
}
11 changes: 11 additions & 0 deletions src/ApiPlatformListeners/AfterWrite.php
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;
}
11 changes: 11 additions & 0 deletions src/ApiPlatformListeners/BeforeDeserialize.php
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;
}
11 changes: 11 additions & 0 deletions src/ApiPlatformListeners/BeforeRead.php
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;
}
11 changes: 11 additions & 0 deletions src/ApiPlatformListeners/BeforeRespond.php
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;
}
11 changes: 11 additions & 0 deletions src/ApiPlatformListeners/BeforeSerialize.php
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;
}
11 changes: 11 additions & 0 deletions src/ApiPlatformListeners/BeforeValidate.php
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;
}
11 changes: 11 additions & 0 deletions src/ApiPlatformListeners/BeforeWrite.php
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;
}

0 comments on commit 143f61d

Please sign in to comment.