Skip to content

Commit

Permalink
Read event
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain committed Feb 10, 2019
1 parent bf1cb5e commit 2e69d85
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -17,6 +17,7 @@
"doctrine/inflector": "^1.0",
"psr/cache": "^1.0",
"psr/container": "^1.0",
"symfony/event-dispatcher": "^3.4 || ^4.0",
"symfony/http-foundation": "^3.4 || ^4.0",
"symfony/http-kernel": "^3.4 || ^4.0",
"symfony/property-access": "^3.4 || ^4.0",
Expand Down Expand Up @@ -58,7 +59,6 @@
"symfony/debug": "^3.4 || ^4.0",
"symfony/dependency-injection": "^3.4 || ^4.0",
"symfony/doctrine-bridge": "^3.4 || ^4.0",
"symfony/event-dispatcher": "^3.4 || ^4.0",
"symfony/expression-language": "^3.4 || ^4.0",
"symfony/finder": "^3.4 || ^4.0",
"symfony/form": "^3.4 || ^4.0",
Expand Down
Expand Up @@ -101,6 +101,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('api.xml');
$loader->load('data_persister.xml');
$loader->load('data_provider.xml');
$loader->load('event_dispatcher.xml');
$loader->load('filter.xml');

$container->registerForAutoconfiguration(DataPersisterInterface::class)
Expand Down
47 changes: 47 additions & 0 deletions src/Bridge/Symfony/Bundle/EventListener/EventDispatcher.php
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

/**
* @author Alan Poulain <contact@alanpoulain.eu>
*/
final class EventDispatcher
{
private $eventName;
private $eventClass;
private $dispatcher;

public function __construct(string $eventName, string $eventClass, EventDispatcherInterface $dispatcher)
{
$this->eventName = $eventName;
$this->eventClass = $eventClass;
$this->dispatcher = $dispatcher;
}

public function dispatch(Event $event): void
{
$internalEvent = null;

switch ($event) {
case $event instanceof GetResponseEvent:
$internalEvent = new $this->eventClass(null, ['request' => $event->getRequest()]);
}

$this->dispatcher->dispatch($this->eventName, $internalEvent);
}
}
2 changes: 1 addition & 1 deletion src/Bridge/Symfony/Bundle/Resources/config/api.xml
Expand Up @@ -150,7 +150,7 @@
<argument type="service" id="api_platform.serializer.context_builder" />
<argument type="service" id="api_platform.identifier.converter" />

<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="4" />
<tag name="kernel.event_listener" event="api_platform.read" method="handleEvent" />
</service>

<service id="api_platform.listener.view.write" class="ApiPlatform\Core\EventListener\WriteListener">
Expand Down
31 changes: 31 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/event_dispatcher.xml
@@ -0,0 +1,31 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="api_platform.dispatcher.request.pre_read" class="ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener\EventDispatcher">
<argument type="string">api_platform.pre_read</argument>
<argument type="string">ApiPlatform\Core\Event\PreReadEvent</argument>
<argument type="service" id="event_dispatcher" />

<tag name="kernel.event_listener" event="kernel.request" method="dispatch" priority="5" />
</service>

<service id="api_platform.dispatcher.request.read" class="ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener\EventDispatcher">
<argument type="string">api_platform.read</argument>
<argument type="string">ApiPlatform\Core\Event\ReadEvent</argument>
<argument type="service" id="event_dispatcher" />

<tag name="kernel.event_listener" event="kernel.request" method="dispatch" priority="4" />
</service>

<service id="api_platform.dispatcher.request.post_read" class="ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener\EventDispatcher">
<argument type="string">api_platform.post_read</argument>
<argument type="string">ApiPlatform\Core\Event\PostReadEvent</argument>
<argument type="service" id="event_dispatcher" />

<tag name="kernel.event_listener" event="kernel.request" method="dispatch" priority="3" />
</service>
</services>
</container>
51 changes: 51 additions & 0 deletions src/Event/Event.php
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Event;

use Symfony\Component\EventDispatcher\Event as BaseEvent;

/**
* @author Alan Poulain <contact@alanpoulain.eu>
*/
abstract class Event extends BaseEvent implements EventInterface
{
private $data;
private $context;

public function __construct($data, array $context = [])
{
$this->data = $data;
$this->context = $context;
}

public function getData()
{
return $this->data;
}

public function setData($data): void
{
$this->data = $data;
}

public function getContext(): array
{
return $this->context;
}

public function setContext(array $context): void
{
$this->context = $context;
}
}
28 changes: 28 additions & 0 deletions src/Event/EventInterface.php
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Event;

/**
* @author Alan Poulain <contact@alanpoulain.eu>
*/
interface EventInterface
{
public function getData();

public function setData($data): void;

public function getContext(): array;

public function setContext(array $context): void;
}
21 changes: 21 additions & 0 deletions src/Event/PostReadEvent.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Event;

/**
* @author Alan Poulain <contact@alanpoulain.eu>
*/
final class PostReadEvent extends Event
{
}
21 changes: 21 additions & 0 deletions src/Event/PreReadEvent.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Event;

/**
* @author Alan Poulain <contact@alanpoulain.eu>
*/
final class PreReadEvent extends Event
{
}
21 changes: 21 additions & 0 deletions src/Event/ReadEvent.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Event;

/**
* @author Alan Poulain <contact@alanpoulain.eu>
*/
final class ReadEvent extends Event
{
}
7 changes: 3 additions & 4 deletions src/EventListener/ReadListener.php
Expand Up @@ -17,14 +17,13 @@
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\OperationDataProviderTrait;
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
use ApiPlatform\Core\Event\EventInterface;
use ApiPlatform\Core\Exception\InvalidIdentifierException;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
use ApiPlatform\Core\Util\RequestAttributesExtractor;
use ApiPlatform\Core\Util\RequestParser;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
Expand Down Expand Up @@ -52,9 +51,9 @@ public function __construct(CollectionDataProviderInterface $collectionDataProvi
*
* @throws NotFoundHttpException
*/
public function onKernelRequest(GetResponseEvent $event)
public function handleEvent(EventInterface $event)
{
$request = $event->getRequest();
$request = $event->getContext()['request'];
if (
!($attributes = RequestAttributesExtractor::extractAttributes($request))
|| !$attributes['receive']
Expand Down
40 changes: 40 additions & 0 deletions src/Events.php
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core;

/**
* @author Alan Poulain <contact@alanpoulain.eu>
*/
final class Events
{
public const PRE_READ = 'api_platform.pre_read';
public const READ = 'api_platform.read';
public const POST_READ = 'api_platform.post_read';

public const PRE_DESERIALIZE = 'api_platform.pre_deserialize';
public const DESERIALIZE = 'api_platform.deserialize';
public const POST_DESERIALIZE = 'api_platform.post_deserialize';

public const PRE_VALIDATE = 'api_platform.pre_validate';
public const VALIDATE = 'api_platform.validate';
public const POST_VALIDATE = 'api_platform.post_validate';

public const PRE_WRITE = 'api_platform.pre_write';
public const WRITE = 'api_platform.write';
public const POST_WRITE = 'api_platform.post_write';

public const PRE_SERIALIZE = 'api_platform.pre_serialize';
public const SERIALIZE = 'api_platform.serialize';
public const POST_SERIALIZE = 'api_platform.post_serialize';
}

0 comments on commit 2e69d85

Please sign in to comment.