0

I want to use the current user in the postFlush method of a Proposals service, so I've just added SecurityContextInterface $securityContext as a parameter of the __construct method and as a property of the Proposals service class:

use Symfony\Component\Security\Core\SecurityContextInterface;

/**
 * @DI\Service("pro.proposals")
 * @DI\Tag("doctrine.event_listener", attributes = {"event": "onFlush", "lazy": true})
 * @DI\Tag("doctrine.event_listener", attributes = {"event": "postFlush", "lazy": true})
 */
class Proposals
{
    private $doctrine;
    private $translator;
    private $templating;
    private $notifications;
    private $proposalsWithTypeChanged;
    private $securityContext;


    /**
     * @DI\InjectParams({"notifications" = @DI\Inject("pro.notifications")})
     */
    function __construct(Registry $doctrine, TranslatorInterface $translator, Notifications $notifications, Templating $templating, SecurityContextInterface $securityContext)
    {
        $this->doctrine = $doctrine;
        $this->translator = $translator;
        $this->templating = $templating;
        $this->notifications = $notifications;
        $this->securityContext = $securityContext;
    }

but this is giving me this error:

ServiceNotFoundException: The service "pro.proposals" has a dependency on a non-existent service "security_context".

I've also tried passing the whole container as suggested on this post, but neither works. Any suggestion?

Community
  • 1
  • 1
Manolo
  • 24,020
  • 20
  • 85
  • 130

1 Answers1

0

Replace

/**
 * @DI\InjectParams({"notifications" = @DI\Inject("pro.notifications")})
 */

with

/**
 * @DI\InjectParams( {
 *     "notifications" = @DI\Inject("pro.notifications"),
 *     "securityContext" = @DI\Inject("security.context")
 * } )
 */

I think the DIExtraBundle tried to guess the service name based on the argument's name. You must specify the service id explicitly instead.

If your security context relies on doctrine, you can still inject the container as stated in the other post, for example:

/**
 * @DI\Service("pro.proposals")
 */
class Test {

    /**
     * @var SecurityContextInterface
     */
    protected $securityContext;

    /**
     * @DI\InjectParams({"container" = @DI\Inject("service_container")})
     */
    function __construct( ContainerInterface $container ) {
        $this->securityContext = $container->get( 'security.context' );
    }
}