title: How to use services defined in PHP-DI in the Symfony's services.yaml?
body: <p>I'm writing an application, that has a structure of a Symfony application and some Symfony functionality, but should be decoupled from the framework as possible. So instead of the framework's DI, I'm using <a href="http://php-di.org/" rel="nofollow noreferrer">PHP-DI</a>.</p>
  
  <p>Now I need Symfony's <code>EventDispatcher</code> (a bitter pill for my framework-decoupled architecture, but anyway...). My Problem is, that I'm not getting referencing from the <code>services.yaml</code> to the PHP-DI container working -- and my listeners aren't gettin attached to the <code>EventDispatcher</code>.</p>
  
  <p><strong>How to use PHP-DI container's services in the <code>services.yaml</code> (especially for the <code>EventDispatcher</code>, if it has any specifics)?</strong></p>
  
  <hr>
  
  <p>Here is some code:</p>
  
  <p><strong><code>services.yaml</code></strong> (completely)</p>
  
  <pre><code>parameters:
  
  services:
      _defaults:
          autowire: true
          autoconfigure: true
          public: false
  
      # events
      # App\Process\SystemEventHandler: &lt;-- No errors, but also no effect.
      @system.event_handler:
          tags:
              - { name: system.event_handler, event: general.user_message_received, method: handle }
              - { name: system.event_handler, event: xxx.foo, method: handle }
              - { name: system.event_handler, event: yyy.foo, method: handle }
              - { name: system.event_handler, event: zzz.foo, method: handle }
  </code></pre>
  
  <p><strong><code>App\Kernel</code></strong> (completely)</p>
  
  <pre><code>namespace App;
  
  use DI\Bridge\Symfony\Kernel as PhpDIBridgeSymfonyKernel;
  use DI\Container;
  use DI\ContainerBuilder as PhpDiContainerBuilder;
  use Exception;
  use Psr\Container\ContainerInterface;
  use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
  use Symfony\Component\Config\Exception\FileLoaderLoadException;
  use Symfony\Component\Config\Loader\LoaderInterface;
  use Symfony\Component\Config\Resource\FileResource;
  use Symfony\Component\DependencyInjection\ContainerBuilder;
  use Symfony\Component\Routing\RouteCollectionBuilder;
  
  class Kernel extends PhpDIBridgeSymfonyKernel
  {
      use MicroKernelTrait;
  
      const CONFIG_EXTS = '.{php,xml,yaml,yml}';
  
      public function getCacheDir()
      {
          return $this-&gt;getProjectDir().'/var/cache/'.$this-&gt;environment;
      }
  
      public function getLogDir()
      {
          return $this-&gt;getProjectDir().'/var/log';
      }
  
      public function registerBundles()
      {
          $contents = require $this-&gt;getProjectDir() . '/config/bundles.php';
          foreach ($contents as $class =&gt; $envs) {
              if (isset($envs['all']) || isset($envs[$this-&gt;environment])) {
                  yield new $class();
              }
          }
      }
  
      /**
       * @param ContainerBuilder $container
       * @param LoaderInterface $loader
       * @throws Exception
       */
      protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
      {
          $container-&gt;addResource(new FileResource($this-&gt;getProjectDir().'/config/bundles.php'));
          // Feel free to remove the "container.autowiring.strict_mode" parameter
          // if you are using symfony/dependency-injection 4.0+ as it's the default behavior
          $container-&gt;setParameter('container.autowiring.strict_mode', true);
          $container-&gt;setParameter('container.dumper.inline_class_loader', true);
          $confDir = $this-&gt;getProjectDir().'/config';
  
          $loader-&gt;load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob');
          $loader-&gt;load($confDir.'/{packages}/'.$this-&gt;environment.'/**/*'.self::CONFIG_EXTS, 'glob');
          $loader-&gt;load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob');
          $loader-&gt;load($confDir.'/{services}_'.$this-&gt;environment.self::CONFIG_EXTS, 'glob');
      }
  
      /**
       * @param RouteCollectionBuilder $routes
       * @throws FileLoaderLoadException
       */
      protected function configureRoutes(RouteCollectionBuilder $routes)
      {
          $confDir = $this-&gt;getProjectDir().'/config';
  
          $routes-&gt;import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob');
          $routes-&gt;import($confDir.'/{routes}/'.$this-&gt;environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
          $routes-&gt;import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
      }
  
      /**
       * @param PhpDiContainerBuilder $builder
       * @return Container|ContainerInterface
       * @throws Exception
       */
      protected function buildPHPDIContainer(PhpDiContainerBuilder $builder)
      {
          // Configure your container here
          // http://php-di.org/doc/container-configuration.html
          $builder-&gt;addDefinitions($this-&gt;getProjectDir() . '/config/dependencies/common.php');
  
          return $builder-&gt;build();
      }
  
  }
  </code></pre>
  
  <p><strong><code>/config/dependencies/common.php</code></strong> (file with the dependencies, relevant extract)</p>
  
  <pre><code>...
  return [
      FooServiceInterface::class =&gt; DI\autowire(FooBService::class),
      BarServiceInterface::class =&gt; DI\autowire(BarService::class),
      ...
      EntityManagerInterface::class =&gt; function () {
          ...
      },
      'process_handler.xxx' =&gt; DI\autowire(XxxHandler::class),
      'process_handler.yyy' =&gt; DI\autowire(YyyHandler::class),
      'process_handler.zzz' =&gt; DI\autowire(ZzzHandler::class),
      EventHandlerInterface::class =&gt; DI\factory(function(ContainerInterface $container) {
          return new SystemEventHandler(
              $container-&gt;get('process_handler.xxx'),
              $container-&gt;get('process_handler.yyy'),
              $container-&gt;get('process_handler.zzz')
          );
      }),
      'system.event_handler' =&gt; DI\get(EventHandlerInterface::class),
      EventDispatcherInterface::class =&gt; DI\get('event_dispatcher'),
  ];
  </code></pre>