/** * @return \Nette\DI\Container */ function netteFromConfig($config) { $loader = new \Nette\DI\ContainerLoader(TEMP_DIR); $class = $loader->load('', function (\Nette\DI\Compiler $compiler) use($config) { $compiler->addExtension('php', new \Nette\DI\Extensions\PhpExtension()); $compiler->addExtension('decorator', new \Nette\DI\Extensions\DecoratorExtension()); $compiler->addExtension('inject', new \Nette\DI\Extensions\InjectExtension()); $compiler->loadConfig(asfile('config.neon', $config)); }); return new $class(); }
/** * @param string $file * @param array $parameters * * @return \Nette\DI\Container */ function createContainer($file, array $parameters = []) { $loader = new Nette\DI\ContainerLoader(TEMP_DIR, true); $class = $loader->load('', function (\Nette\DI\Compiler $compiler) use($file, $parameters) { $compiler->addExtension('extensions', new \Nette\DI\Extensions\ExtensionsExtension()); if ($parameters) { $compiler->addConfig(['parameters' => $parameters]); } $compiler->loadConfig($file); }); return new $class(); }
<?php require_once __DIR__ . '/bootstrap.php'; $loader = new \Nette\DI\ContainerLoader(TEMP_DIR); $class = $loader->load('', function (\Nette\DI\Compiler $compiler) { $builder = $compiler->getContainerBuilder(); $builder->parameters['mailer']['transport'] = 'sendmail'; $builder->addDefinition('mailer')->setClass(\Project\Mailer::class)->setArguments([$builder->parameters['mailer']['transport']]); $builder->addDefinition('newsletter_manager')->setClass('Project\\NewsletterManager')->setArguments(['@mailer']); }); /** @var \Nette\DI\Container $container */ $container = new $class(); dump($container->getService('newsletter_manager'));
<?php $t1 = microtime(true); $loader = new Nette\DI\ContainerLoader(__DIR__ . '/temp', TRUE); $class = $loader->load('', function ($compiler) { $compiler->loadConfig(__DIR__ . '/config/services.neon'); }); $container = new $class(); for ($i = 0; $i < 10000; $i++) { $a = $container->createServiceA(); } $t2 = microtime(true); $results = ['time' => $t2 - $t1, 'files' => count(get_included_files()), 'memory' => memory_get_peak_usage() / 1024 / 1024]; echo json_encode($results);
<?php require_once __DIR__ . '/bootstrap.php'; $loader = new \Nette\DI\ContainerLoader(TEMP_DIR); $class = $loader->load('', function (\Nette\DI\Compiler $compiler) { $compiler->loadConfig(asfile('config.neon', <<<NEON parameters: mailer: transport: sendmail services: mailer: class: Project\\Mailer arguments: [%mailer.transport%] newsletter_manager: class: Project\\NewsletterManager arguments: [@mailer] NEON )); }); /** @var \Nette\DI\Container $container */ $container = new $class(); dump($container->getService('newsletter_manager'));