deb0rian
deb0rian

Reputation: 976

How can you call a static method in Symfony's YAML config?

I have an application that uses Symfony components. Everything went great until I hit a wall when I was needed to add Symfony's request object to dependency injection container.

Here's my config:

parameters:
  config.project: %project_cfg%
  config.module: %module_cfg%
  config.mysql: %mysql_cfg%
  config.couch: %couch_cfg%
  request: %http_request%

services:
  request:
    class: Symfony\Component\HttpFoundation\Request
    calls:
      - [createFromGlobals]
  db:
    class: App\Core\AlarisDb
    arguments: ["%mysql_cfg%"]
  func:
    class: App\Core\AlarisFunctions
    calls:
      - [setTree, ["@tree"]]
  tree:
    class: App\Core\AlarisTree

Everything works perfect, except that request is not created properly, because in PHP it should be called like this:

$request = Request::createFromGlobals();

Whereas YAML config calls it as object's method. Is there a way to tell it to call it as a static method?

Upvotes: 5

Views: 9384

Answers (2)

Kamil Adryjanek
Kamil Adryjanek

Reputation: 3338

I think that "Synthetic Services" is what you are looking for:

Synthetic services are services that are injected into the container instead of being created by the container.

More: http://symfony.com/doc/current/components/dependency_injection/advanced.html#synthetic-services

Upvotes: 1

qooplmao
qooplmao

Reputation: 17759

I might be wrong but I think this needs a factory call rather than a "calls" call, like..

request:
    class: Symfony\Component\HttpFoundation\Request
    factory_class:  Symfony\Component\HttpFoundation\Request
    factory_method: createFromGlobals

For more on factories see the docs.

Upvotes: 4

Related Questions