-
-
Notifications
You must be signed in to change notification settings - Fork 887
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf1cb5e
commit 2e69d85
Showing
12 changed files
with
266 additions
and
6 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
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
47 changes: 47 additions & 0 deletions
47
src/Bridge/Symfony/Bundle/EventListener/EventDispatcher.php
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,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); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
src/Bridge/Symfony/Bundle/Resources/config/event_dispatcher.xml
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,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> |
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,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; | ||
} | ||
} |
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,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; | ||
} |
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 | ||
|
||
/* | ||
* 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 | ||
{ | ||
} |
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 | ||
|
||
/* | ||
* 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 | ||
{ | ||
} |
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 | ||
|
||
/* | ||
* 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 | ||
{ | ||
} |
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
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,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'; | ||
} |