Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Deprecation for event_dispatcher service is still triggered #23054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
garak opened this issue Jun 3, 2017 · 3 comments
Closed

Deprecation for event_dispatcher service is still triggered #23054

garak opened this issue Jun 3, 2017 · 3 comments

Comments

@garak
Copy link
Contributor

garak commented Jun 3, 2017

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.3.0

As mentioned in #22223, it looks like the deprecation notice about ContainerAwareEventDispatcher is still emerging in some cases.
Step to reproduce:

  • install standard edition via create-project symfony/framework-standard-edition foo
  • enter project with cd foo
  • install bundle via composer require knplabs/knp-paginator-bundle
  • enable bundle by adding new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle() in registerBundles of AppKernel

Expected behavior: launching bin/console server:run and navigating to http://127.0.0.1:8000/, no deprecations about ContainerAwareEventDispatcher should be triggered.
Actual behavior: launching bin/console server:run and navigating to http://127.0.0.1:8000/, I see in debug toolbar "User Deprecated: The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since version 3.3 and will be removed in 4.0. Use EventDispatcher with closure-proxy injection instead."

The relevant part of source code is in var/cache/dev/appDevDebugProjectContainer.php and it's following

protected function getDebug_EventDispatcherService()
{    
    $this->services['debug.event_dispatcher'] = $instance = new \Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher(new \Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher($this), ${($_ = isset($this->services['debug.stopwatch']) ? $this->services['debug.stopwatch'] : $this->get('debug.stopwatch')) && false ?: '_'}, ${($_ = isset($this->services['monolog.logger.event']) ? $this->services['monolog.logger.event'] : $this->get('monolog.logger.event', ContainerInterface::NULL_ON_INVALID_REFERENCE)) && false ?: '_'});
   // ...
}
@robfrawley
Copy link
Contributor

robfrawley commented Jun 3, 2017

Thank god someone narrowed down the cause of this deprecation. I'd been getting it hundreds of times per request in a few projects after upgrading to Symfony 3.3 but didn't yet have time to narrow down the cause.

I can confirm that the reproduction steps do cause the described deprecation.

@ogizanagi
Copy link
Contributor

ogizanagi commented Jun 4, 2017

The problem comes from this pass, so on KnpPaginatorBundle's side. I guess it should be updated for sf 3.3 to something similar to what's done in RegisterListenersPass, or simply re-use it internally (but needs to keep BC):

Show code
@@ -2,8 +2,9 @@
 
 namespace Knp\Bundle\PaginatorBundle\DependencyInjection\Compiler;
 
-use Symfony\Component\DependencyInjection\ContainerBuilder;
 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
 
 class PaginatorConfigurationPass implements CompilerPassInterface
 {
@@ -19,30 +20,6 @@
             return;
         }
 
-        $definition = $container->findDefinition('event_dispatcher');
-
-        foreach ($container->findTaggedServiceIds('knp_paginator.subscriber') as $id => $attributes) {
-            // We must assume that the class value has been correctly filled, even if the service is created by a factory
-            $class = $container->getDefinition($id)->getClass();
-
-            $refClass = new \ReflectionClass($class);
-            $interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
-            if (!$refClass->implementsInterface($interface)) {
-                throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
-            }
-
-            foreach ($class::getSubscribedEvents() as $event => $options) {
-                if (!is_array($options)) {
-                    $options = array($options, 0);
-                }
-                $definition->addMethodCall('addListenerService', array(
-                    $event,
-                    array($id, $options[0]),
-                    $options[1]
-                ));
-            }
-            // sf 2.1.x only
-            //$definition->addMethodCall('addSubscriberService', array($id, $class));
-        }
+        (new RegisterListenersPass('event_dispatcher', 'knp_paginator.listener', 'knp_paginator.subscriber'))->process($container);
     }
 }

Maybe you could have a look? (I can't do a PR right now)

@garak
Copy link
Contributor Author

garak commented Jun 9, 2017

The PR proposed for KnpPaginatorBundle was merged and the deprecation warning disappeared. Thanks for your help @ogizanagi!

@garak garak closed this as completed Jun 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants