Skip to content

Commit

Permalink
asdasdsa
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Aug 7, 2021
1 parent b52f686 commit 4e94df4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 56 deletions.
29 changes: 6 additions & 23 deletions DependencyInjection/Compiler/CustomHandlersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use JMS\Serializer\GraphNavigatorInterface;
use JMS\Serializer\Handler\HandlerRegistry;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

Expand All @@ -16,15 +15,6 @@
*/
final class CustomHandlersPass implements CompilerPassInterface
{
public const REGISTER_HANDLERS = 1;
public const REGISTER_LOCATOR = 2;
private $mode;

public function __construct(int $mode = 3)
{
$this->mode = $mode;
}

public function process(ContainerBuilder $container)
{
$handlersByDirection = $this->findHandlers($container);
Expand All @@ -35,24 +25,17 @@ public function process(ContainerBuilder $container)
foreach ($handlersByType as $type => $handlersByFormat) {
foreach ($handlersByFormat as $format => $handlerCallable) {
$id = (string) $handlerCallable[0];
if (class_exists(ServiceLocatorTagPass::class) || $container->findDefinition($id)->isPublic()) {
$handlerServices[$id] = $handlerCallable[0];
$handlerCallable[0] = $id;
}

if (self::REGISTER_HANDLERS & $this->mode) {
$handlerRegistryDef->addMethodCall('registerHandler', [$direction, $type, $format, $handlerCallable]);
}
$handlerServices[$id] = $handlerCallable[0];
$handlerCallable[0] = $id;

$handlerRegistryDef->addMethodCall('registerHandler', [$direction, $type, $format, $handlerCallable]);
}
}
}

if (self::REGISTER_LOCATOR & $this->mode) {
if (class_exists(ServiceLocatorTagPass::class) && count($handlerServices)) {
$serviceLocator = ServiceLocatorTagPass::register($container, $handlerServices);
$handlerRegistryDef->replaceArgument(0, $serviceLocator);
}
}
$container->findDefinition('jms_serializer.handler_registry.service_locator')
->setArgument(0, $handlerServices);
}

private function findHandlers(ContainerBuilder $container): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace JMS\SerializerBundle\DependencyInjection\Compiler;

use JMS\Serializer\EventDispatcher\EventDispatcher;
use JMS\Serializer\EventDispatcher\LazyEventDispatcher;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

Expand All @@ -16,15 +14,6 @@
*/
final class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
{
public const REGISTER_LISTENERS = 1;
public const REGISTER_LOCATOR = 2;
private $mode;

public function __construct(int $mode = 3)
{
$this->mode = $mode;
}

public function process(ContainerBuilder $container)
{
$listeners = $this->findListeners($container);
Expand All @@ -33,29 +22,22 @@ public function process(ContainerBuilder $container)
$listenerServices = [];

foreach ($listeners as &$events) {
$events = call_user_func_array('array_merge', $events);
$events = array_merge(...$events);
}

foreach ($listeners as $event => $listenersPerEvent) {
foreach ($listenersPerEvent as $singleListener) {
$id = (string) $singleListener[0][0];
if ((is_a($dispatcherDef->getClass(), LazyEventDispatcher::class, true) && class_exists(ServiceLocatorTagPass::class)) || $container->findDefinition($id)->isPublic()) {
$listenerServices[$id] = $singleListener[0][0];
$singleListener[0][0] = $id;
}

if (self::REGISTER_LISTENERS & $this->mode) {
$dispatcherDef->addMethodCall('addListener', array_merge([$event], $singleListener));
}
}
}
$listenerServices[$id] = $singleListener[0][0];
$singleListener[0][0] = $id;

if (self::REGISTER_LOCATOR & $this->mode) {
if (class_exists(ServiceLocatorTagPass::class) && count($listenerServices)) {
$serviceLocator = ServiceLocatorTagPass::register($container, $listenerServices);
$dispatcherDef->replaceArgument(0, $serviceLocator);
$dispatcherDef->addMethodCall('addListener', array_merge([$event], $singleListener));
}
}

$container->findDefinition('jms_serializer.event_dispatcher.service_locator')
->setArgument(0, $listenerServices);
}

private function findListeners(ContainerBuilder $container): array
Expand Down
7 changes: 2 additions & 5 deletions JMSSerializerBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ function (ContainerBuilder $container, $def) {
$builder->addCompilerPass(new ExpressionFunctionProviderPass());
$builder->addCompilerPass(new DoctrinePass());

$builder->addCompilerPass(new RegisterEventListenersAndSubscribersPass(RegisterEventListenersAndSubscribersPass::REGISTER_LOCATOR), PassConfig::TYPE_BEFORE_OPTIMIZATION, -256);
$builder->addCompilerPass(new RegisterEventListenersAndSubscribersPass(RegisterEventListenersAndSubscribersPass::REGISTER_LISTENERS), PassConfig::TYPE_OPTIMIZE, -256);

$builder->addCompilerPass(new CustomHandlersPass(CustomHandlersPass::REGISTER_LOCATOR), PassConfig::TYPE_BEFORE_OPTIMIZATION, -256);
$builder->addCompilerPass(new CustomHandlersPass(CustomHandlersPass::REGISTER_HANDLERS), PassConfig::TYPE_OPTIMIZE, -256);
$builder->addCompilerPass(new RegisterEventListenersAndSubscribersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -256);
$builder->addCompilerPass(new CustomHandlersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -256);
}

private function getServiceMapPass($tagName, $keyAttributeName, $callable)
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/debug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</service>

<service id="jms_serializer.traceable_event_dispatcher" class="JMS\SerializerBundle\Debug\TraceableEventDispatcher" public="false">
<argument type="service" id="service_container" />
<argument type="service" id="jms_serializer.event_dispatcher.service_locator" />
</service>

<service id="jms_serializer.traceable_runs_listener" class="JMS\SerializerBundle\Debug\RunsListener" public="false">
Expand Down
15 changes: 13 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
<services>
<!-- Event Dispatcher -->
<service id="jms_serializer.event_dispatcher" class="JMS\Serializer\EventDispatcher\LazyEventDispatcher" public="false">
<argument type="service" id="service_container" />
<argument type="service" id="jms_serializer.event_dispatcher.service_locator" />
</service>

<service id="jms_serializer.event_dispatcher.service_locator" class="Symfony\Component\DependencyInjection\ServiceLocator" public="false">
<argument type="collection"/>
<tag name="container.service_locator" />
</service>

<service id="jms_serializer.doctrine_proxy_subscriber" class="JMS\Serializer\EventDispatcher\Subscriber\DoctrineProxySubscriber" public="false">
Expand All @@ -23,8 +28,14 @@

<!-- Handlers -->
<service id="jms_serializer.handler_registry" class="JMS\Serializer\Handler\LazyHandlerRegistry">
<argument type="service" id="service_container" />
<argument type="service" id="jms_serializer.handler_registry.service_locator" />
</service>

<service id="jms_serializer.handler_registry.service_locator" class="Symfony\Component\DependencyInjection\ServiceLocator" public="false">
<argument type="collection"/>
<tag name="container.service_locator" />
</service>

<service id="jms_serializer.array_collection_handler" class="JMS\Serializer\Handler\ArrayCollectionHandler" public="false">
<argument type="constant">false</argument>
<tag name="jms_serializer.subscribing_handler" />
Expand Down

0 comments on commit 4e94df4

Please sign in to comment.