-
-
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
d86f956
commit b7dbb8f
Showing
39 changed files
with
1,014 additions
and
255 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
88 changes: 88 additions & 0 deletions
88
src/Bridge/Symfony/Bundle/EventSubscriber/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,88 @@ | ||
<?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\EventSubscriber; | ||
|
||
use ApiPlatform\Core\Event\DeserializeEvent; | ||
use ApiPlatform\Core\Event\FormatAddEvent; | ||
use ApiPlatform\Core\Event\PostDeserializeEvent; | ||
use ApiPlatform\Core\Event\PostReadEvent; | ||
use ApiPlatform\Core\Event\PreDeserializeEvent; | ||
use ApiPlatform\Core\Event\PreReadEvent; | ||
use ApiPlatform\Core\Event\QueryParameterValidateEvent; | ||
use ApiPlatform\Core\Event\ReadEvent; | ||
use ApiPlatform\Core\Events; | ||
use Symfony\Component\EventDispatcher\Event; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @author Alan Poulain <contact@alanpoulain.eu> | ||
*/ | ||
final class EventDispatcher implements EventSubscriberInterface | ||
{ | ||
private const INTERNAL_EVENTS_CONFIGURATION = [ | ||
KernelEvents::REQUEST => [ | ||
QueryParameterValidateEvent::class => Events::QUERY_PARAMETER_VALIDATE, | ||
FormatAddEvent::class => Events::FORMAT_ADD, | ||
|
||
PreReadEvent::class => Events::PRE_READ, | ||
ReadEvent::class => Events::READ, | ||
PostReadEvent::class => Events::POST_READ, | ||
|
||
PreDeserializeEvent::class => Events::PRE_DESERIALIZE, | ||
DeserializeEvent::class => Events::DESERIALIZE, | ||
PostDeserializeEvent::class => Events::POST_DESERIALIZE, | ||
], | ||
]; | ||
|
||
private $dispatcher; | ||
|
||
public function __construct(EventDispatcherInterface $dispatcher) | ||
{ | ||
$this->dispatcher = $dispatcher; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
KernelEvents::REQUEST => 'dispatch', | ||
]; | ||
} | ||
|
||
public function dispatch(Event $event, string $eventName): void | ||
{ | ||
$internalEventData = null; | ||
$internalEventContext = []; | ||
|
||
switch (true) { | ||
case $event instanceof GetResponseEvent: | ||
$internalEventContext = ['request' => $event->getRequest()]; | ||
} | ||
|
||
foreach ($this->getInternalEventData($eventName) as $internalEventClass => $internalEventName) { | ||
$internalEvent = new $internalEventClass($internalEventData, $internalEventContext); | ||
|
||
$this->dispatcher->dispatch($internalEventName, $internalEvent); | ||
} | ||
} | ||
|
||
private function getInternalEventData(string $eventName): \Generator | ||
{ | ||
yield from self::INTERNAL_EVENTS_CONFIGURATION[$eventName]; | ||
} | ||
} |
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
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
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,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 DeserializeEvent 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,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 FormatAddEvent 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 PostDeserializeEvent extends Event | ||
{ | ||
} |
Oops, something went wrong.