Hi Juan,

On 19 September 2014 19:20, Juan Pedro Gonzalez <[email protected]>
wrote:

> Hi,
> Can a service be initialized at bootstrap automatically?
> I've written a multilingual library which loads a custom router to take
> care of the locale which can be located at
> https://github.com/JPG-Consulting/LocaleManager. The problem here is I
> must inject some instances to the router class so it works fine. This is
> done throw the service fctory of the LocaleManager service... As you can
> see in the Module.php file I do this by providing a bootstrap method and
> getting my service  so it gets created.
>

I would advise _AGAINST_ using any of this context-switching done at
runtime: instead, pass in whatever router you get, and do the decisions in
the router implementation. To be more clear:

// bad example
if ($thinger->hasThing()) {
    return $serviceA;
} else {
    return $serviceB;
}

// good example
class GoodService
{
    public function __construct(Thinger $thinger, ...) {
        $this->thinger = $thinger;
        // ...
    }

    public function doFoo() {
        if ($this->thinger->hasThing()) {
            return $this->serviceA->doFoo();
        }

        return $this->serviceB->doFoo();
}

This is a very subtle but VERY important difference.


> Most of this "module" can be configured right away in the
> module.config.php of any of the modules not needing to register the
> module... But in order to do so i need a way to initialize my service
> automatically within the application and not the module itself. Is there a
> way to accomplish this task?
>

Inject the service that will give you the data you need into the
"aggregate" "GoodService", then do the decision by fetching the data at
runtime.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

Reply via email to