nette / bootstrap

đź…± The simple way to configure and bootstrap your Nette application.
https://doc.nette.org/bootstrap
Other
663 stars 36 forks source link

Configurator: added $onCreate event #48

Closed JanTvrdik closed 8 years ago

JanTvrdik commented 8 years ago

I need to replace in runtime some services based on their type before initialize is called. This allows me to do stuff like

$configurator->onCreate[] = function (Nette\Configurator $configurator, Nette\DI\Container $dic) {
    $name = $dic->findByType(SomeService::class)[0];
    $mockedService = Mockery::mock(SomeService::class);
    $configurator->addServices([$name => $mockedService]);
};
JanTvrdik commented 8 years ago

Alternative solution is to split createContainer into createContainer and createContainerClass which would return the container class name but did not instantiate it.

/**
 * Creates system DI container class and returns the class name.
 * @return string
 */
public function createContainerClass()
{
    $loader = new DI\ContainerLoader(
        $this->getCacheDirectory() . '/Nette.Configurator',
        $this->parameters['debugMode']
    );

    return $loader->load(
        [$this->parameters, $this->files, PHP_VERSION_ID - PHP_RELEASE_VERSION],
        [$this, 'generateContainer']
    );
}
JanTvrdik commented 8 years ago

@dg Which solution do you like more?

dg commented 8 years ago

ad $onCreate: Bad is that it duplicates purpose of addServices…

createContainerClass: sounds good, but name is not ideal

JanTvrdik commented 8 years ago

createContainerClass: sounds good, but name is not ideal

The only alternative name I can think of is loadContainer / loadContainerClass.


Now we hove a nice triad – createContainer / loadContainer / generateContainer.