14. ->createContainer()
Zkusí jestli už zkompilovaný container existuje
- pokud ano, tak vytvoří instanci.
- pokud ne, tak zkompiluje container
a vytvoří instanci
15. Kompilace pohledem z dálky
- nakonfiguruje se ContainerBuilder
- Nette vygeneruje PHP třídu
- uloží do tempu
- profit
16. Co se vygeneruje?
class SystemContainer extends NetteDIContainer {
protected $meta = array(
'types' => array(),
'services' => array(),
'tags' => array()
);
public function __construct() {
parent::__construct(array(/* parameters */));
}
17. Co se vygeneruje?
class SystemContainer extends NetteDIContainer {
public function createServiceApplication()
{
$service = new NetteApplicationApplication(
$this->getService('nette.presenterFactory'), ...
);
$service->catchExceptions = FALSE;
$service->errorPresenter = 'Front:Error';
return $service;
}
18. Jak se to používá?
$app = $container
->getService('application');
use NetteApplicationApplication;
$app = $container
->getByType(Application::class);
28. Každé rozšíření
- pracuje s ContainerBuilder
- dědí od NetteDICompilerExtension
- má “dynamické” jméno
- má vlastní sekci v configu
29. ContainerBuilder
class ContainerBuilder
{
/** @return ServiceDefinition */
function addDefinition($name, $definition = NULL);
/** @return ServiceDefinition */
function getDefinition($name);
/** @return ServiceDefinition[] */
function getDefinitions();
function removeDefinition($name);
30. ContainerBuilder
class ContainerBuilder
{
/** @return string service name or NULL */
function getByType($class);
/** @return string[] */
function findByType($class, $autowired = TRUE);
/** @return array of [service name => tag attributes] */
function findByTag($tag);
31. ContainerBuilder
class ContainerBuilder
{
public $parameters = array();
function expand($value);
function addExcludedClasses(array $classes);
function addDependency($file);
/** @return NettePhpGeneratorPhpLiteral */
static function literal($phpCode);
32. ServiceDefinition
class ServiceDefinition extends NetteObject {
public $class;
public $factory;
public $setup = array();
public $parameters = array();
public $tags = array();
public $autowired = TRUE;
public $dynamic = FALSE;
public $implement;
public $implementType;
34. Extension points of extensions
- loadConfiguration()
- pouze na zpracování configu a přidávání služeb
- beforeCompiler()
- modifikace existujících služeb, nepřidávat nové!
- afterCompile()
- modifikace vygenerovaného kódu
40. afterCompile($class)
use NettePhpGenerator as Code;
public function afterCompile(CodeClassType $class)
{
$config = $this->getConfig();
$initialize = $class->methods['initialize'];
$initialize->addBody('date_default_timezone_set(?);',
array($config['date.timezone']));
41. Závislosti rozšíření
- například BlogExtension, který potřebuje
OrmExtension
- vyřešilo se to změnou přístupu
- dva přístupy
- ask (starý)
- tell (lepší)
42. Závislosti rozšíření - ask
use KdybyDoctrineDIIDatabaseTypeProvider;
$exts = $this->compiler
->getExtensions(IDatabaseTypeProvider::class);
foreach ($exts as $ext) {
$types = $ext->getDatabaseTypes();
// ...
43. Závislosti rozšíření - tell
use KdybyDoctrineDIOrmExtension;
$exts = $this->compiler
->getExtensions(OrmExtension::class);
$exts[0]->addDatabaseType($metadata);