title: Symfony4 - How to pass a TokenStorageInterface to an EntityListener?
body: <p>I'm currently working on a project created with api-platform and using Symfony4.</p>
  
  <p>My goal is to be able to use a TokenStorage inside of an EntityListener in order to know the identity of my user.</p>
  
  <p>Unfortunately, I can't find a way to pass a TokenStorageInterface as an argument.
  And the autowiring system doesn't seem to work.</p>
  
  <p>services.yaml :</p>
  
  <pre><code># https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
  parameters:
      locale: 'en'
  
  services:
      # default configuration for services in *this* file
      _defaults:
          autowire: true      # Automatically injects dependencies in your services.
          autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
          public: false       # Allows optimizing the container by removing unused services; this also means
                              # fetching services directly from the container via $container-&gt;get() won't work.
                              # The best practice is to be explicit about your dependencies anyway.
  
  
      # makes classes in src/ available to be used as services
      # this creates a service per class whose id is the fully-qualified class name
      App\:
          resource: '../src/*'
          exclude: '../src/{Entity,Migrations,Tests}'
  
      # controllers are imported separately to make sure services can be injected
      # as action arguments even if you don't extend any base controller class
      App\Controller\:
          resource: '../src/Controller'
          tags: ['controller.service_arguments']
  
      # add more service definitions when explicit configuration is needed
      # please note that last definitions always *replace* previous ones
      App\EntityListener\KpiDateListener:
          public: true,
          arguments:
              $tokenStorage: '@security.token_storage'
  </code></pre>
  
  <p>KpiDateListener.php :</p>
  
  <pre><code>&lt;?php
  
  namespace App\EntityListener;
  
  use ApiPlatform\Core\Tests\Fixtures\TestBundle\Manager\UserManager;
  use App\Entity\KpiDate;
  use App\Entity\StateDate;
  use App\Security\User\UserProvider;
  use Doctrine\ORM\Event\LifecycleEventArgs;
  use Doctrine\ORM\OptimisticLockException;
  use Doctrine\ORM\ORMException;
  use Psr\Log\LoggerInterface;
  use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
  use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  use Symfony\Component\Security\Core\User\UserInterface;
  use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  
  class KpiDateListener
  {
      private $tokenStorage;
  
      public function __construct(TokenStorageInterface $tokenStorage)
      {
          $this-&gt;tokenStorage = $tokenStorage;
      }
  
      public function postPersist(KpiDate $kpiDate, LifecycleEventArgs $args)
      {
          //Here is my function
      }
  }
  </code></pre>
  
  <p>I always have the same error :</p>
  
  <blockquote>
    <p>Too few arguments to function App\EntityListener\KpiDateListener::__construct(), 0 passed in /srv/api/vendor/doctrine/doctrine-bundle/Mapping/ContainerAwareEntityListenerResolver.php on line 76 and exactly 1 expected</p>
  </blockquote>
  
  <p>Is there something wrong or missing ?</p>
  
  <p>Thank you</p>