Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 ScopeCrossingInjectionException when you use prototype scope

My aim is to add data_collector to my classes for displaying some useful information on the developer toolbar. My service:

services:
    my_api.auth.login:
        class: YO\ApiV1\Services\Auth\Login
        arguments:
            - requestId
            - "@old_sound_rabbit_mq.login_rpc"
            - "@service_container"
        scope: prototype

I need scope prototype to have different instance for every new call. By the way, service @old_sound_rabbit_mq.login_rpc has scope "prototype" as well. And, I'd like to attach data_collector, which could be done with:

tags:
    - { name: data_collector, template: "AcmeDebug:Collector:templatename", id: "your_collector_name" }

But then I got an exception:

ScopeCrossingInjectionException: Scope Crossing Injection detected: The definition "profiler" references the service "my_api.auth.login" which belongs to another scope hierarchy. This service might not be available consistently. Generally, it is safer to either move the definition "profiler" to scope "prototype", or declare "container" as a child scope of "prototype". If you can be sure that the other scope is always active, you can set the reference to strict=false to get rid of this error.

And it confuses me, because I don't know what to do. I tried to set property "strict=false", but nothing happens.

like image 994
maectpo Avatar asked Apr 04 '12 08:04

maectpo


2 Answers

I guess symfony's synchronized services may help you: doc

otherwise you can set "strict=false" in this way:

services:
    my.service.definition:
        class: Acme\Services\BlaService
        arguments:
            - "@any_other_service_from_narrower_scope="
        scope: prototype

The "=" at the end of the service-definition when injecting a service from a narrower scope, will turn "strict" to false

like image 122
Andy Rosslau Avatar answered Oct 05 '22 03:10

Andy Rosslau


Container scope since Symfony 2.8 was deprecated.

scope: prototype was replaced by shared: false

http://symfony.com/doc/2.8/cookbook/service_container/shared.html

like image 27
Aistis Avatar answered Oct 05 '22 05:10

Aistis