php - Is it possible to inject a method into a service in symfony services.yml -
there plenty of documentation on internet injecting service service this: http://symfony.com/doc/current/components/dependency_injection/introduction.html
however, have service called objectcache
configured in symfony's services.yml this:
object_cache: class: app\bundle\apibundle\service\objectcache
this service has 2 methods getting , setting user object. example:
$user = new user(); // assume entity database $this->get('object_cache')->setuser($user); // ... $this->get('object_cache')->getuser(); // instance of $user
i want create new service depends on user, makes sense inject user @ service creation:
class someservice { public function __construct(user $user) { } }
how configure services.yml such user injected new service?
object_cache: class: app\bundle\apibundle\service\objectcache some_service: class: app\bundle\apibundle\service\someservice arguments: [@object_cache->getuser()????]
this didn't work, , symfony yaml documentation sketchy least.
am forced creating user-only flavour of objectcache , injecting someservice or expecting someservice receive objectcache , call getuser once in constructor?
credit qooplmao comment in helping me find answer this looking for. thought i'd answer own question benefit of others i have working, plus corrections syntax in comment.
what should have been looking symfony's expression language allows precisely granularity of control looking for.
the resulting configuration looks this:
object_cache: class: app\bundle\apibundle\service\objectcache some_service: class: app\bundle\apibundle\service\someservice arguments: [@=service('object_cache').getuser()]
for more information on expression syntax, here detailed documentation: http://symfony.com/doc/2.7/components/expression_language/syntax.html
(if symfony docs had courtesy provide links such crucial information on pages make reference it!)
Comments
Post a Comment