/** * Returns the application DIC. * * @return \SystemContainer */ public function createContainer() { $c = $this->buildContainer(); LimitedScope::evaluate($c); $container = new $this->parameters['container']['class'](); $container->initialize(); return $container; }
/** * Renders template to output. * @return void */ public function render() { $cache = new Caching\Cache($storage = $this->getCacheStorage(), 'Nette.Template'); $cached = $compiled = $cache->load($this->source); if ($compiled === NULL) { $compiled = $this->compile(); $cache->save($this->source, $compiled, array(Caching\Cache::CONSTS => 'Nette\\Framework::REVISION')); $cached = $cache->load($this->source); } if ($cached !== NULL && $storage instanceof Caching\Storages\PhpFileStorage) { Nette\Utils\LimitedScope::load($cached['file'], $this->getParameters()); } else { Nette\Utils\LimitedScope::evaluate($compiled, $this->getParameters()); } }
public function beforeCompile() { $container = $this->getContainerBuilder(); $code = ''; foreach ($container->findByTag('proxy') as $factory => $meta) { $definition = $container->getDefinition($factory); $class = substr($definition->class, strrpos($definition->class, '\\') + 1); $namespace = substr($definition->class, 0, strrpos($definition->class, '\\')); $extend = ltrim($meta, '\\'); $code .= $this->generateCode($class, $extend, $namespace); } if ($code) { $cache = $this->getCache(); $cache->save('file', $code); // find the file $cached = $cache->load('file'); $this->classesFile = $cached['file']; \Nette\Utils\LimitedScope::evaluate($code); } }
/** * Returns system DI container. * @return \SystemContainer */ public function createContainer() { if ($cacheDir = $this->getCacheDirectory()) { $cache = new Cache(new Nette\Caching\Storages\PhpFileStorage($cacheDir), 'Nette.Configurator'); $cacheKey = array($this->parameters, $this->files); $cached = $cache->load($cacheKey); if (!$cached) { $code = $this->buildContainer($dependencies); $cache->save($cacheKey, $code, array(Cache::FILES => $dependencies)); $cached = $cache->load($cacheKey); } Nette\Utils\LimitedScope::load($cached['file'], TRUE); } elseif ($this->files) { throw new Nette\InvalidStateException("Set path to temporary directory using setTempDirectory()."); } else { Nette\Utils\LimitedScope::evaluate($this->buildContainer()); // back compatibility with Environment } $container = new $this->parameters['container']['class'](); $container->initialize(); Nette\Environment::setContext($container); // back compatibility return $container; }
/** * Renders template to output. * * @return void */ public function render() { if ($this->file == null) { // intentionally == throw new Nette\InvalidStateException("Template file name was not specified."); } $cache = new Caching\Cache($storage = $this->getCacheStorage(), 'Nette.FileTemplate'); if ($storage instanceof Caching\Storages\PhpFileStorage) { $storage->hint = str_replace(dirname(dirname($this->file)), '', $this->file); } $cached = $compiled = $cache->load($this->file); if ($compiled === null) { try { $compiled = "<?php\n\n// source file: {$this->file}\n\n?>" . $this->compile(); } catch (FilterException $e) { $e->setSourceFile($this->file); throw $e; } $cache->save($this->file, $compiled, array(Caching\Cache::FILES => $this->file, Caching\Cache::CONSTS => 'Nette\\Framework::REVISION')); $cached = $cache->load($this->file); } if ($cached !== null && $storage instanceof Caching\Storages\PhpFileStorage) { Nette\Utils\LimitedScope::load($cached['file'], $this->getParameters()); } else { Nette\Utils\LimitedScope::evaluate($compiled, $this->getParameters()); } }
/** * Loads configuration from file and process it. * @return DI\Container */ public function loadConfig($file, $section = NULL) { if ($file === NULL) { $file = $this->defaultConfigFile; } $container = $this->container; $file = $container->expand($file); if (!is_file($file)) { $file = preg_replace('#\\.neon$#', '.ini', $file); // back compatibility } if ($section === NULL) { if (PHP_SAPI === 'cli') { $section = Environment::CONSOLE; } else { $section = $container->params['productionMode'] ? Environment::PRODUCTION : Environment::DEVELOPMENT; } } $cache = new Cache($container->templateCacheStorage, 'Nette.Configurator'); $cacheKey = array((array) $container->params, $file, $section); $cached = $cache->load($cacheKey); if ($cached) { require $cached['file']; fclose($cached['handle']); return $this->container; } $config = Nette\Config\Config::fromFile($file, $section); $code = "<?php\n// source file {$file}\n\n"; // back compatibility with singular names foreach (array('service', 'variable') as $item) { if (isset($config[$item])) { trigger_error(basename($file) . ": Section '{$item}' is deprecated; use plural form '{$item}s' instead.", E_USER_WARNING); $config[$item . 's'] = $config[$item]; unset($config[$item]); } } // process services if (isset($config['services'])) { foreach ($config['services'] as $key => &$def) { if (preg_match('#^Nette\\\\.*\\\\I?([a-zA-Z]+)$#', strtr($key, '-', '\\'), $m)) { // back compatibility $m[1][0] = strtolower($m[1][0]); trigger_error(basename($file) . ": service name '{$key}' has been renamed to '{$m['1']}'", E_USER_WARNING); $key = $m[1]; } if (is_array($def)) { if (method_exists(get_called_class(), "createService{$key}") && !isset($def['factory']) && !isset($def['class'])) { $def['factory'] = array(get_called_class(), "createService{$key}"); } if (isset($def['option'])) { $def['arguments'][] = $def['option']; } if (!empty($def['run'])) { $def['tags'] = array('run'); } } } $builder = new DI\ContainerBuilder(); $code .= $builder->generateCode($config['services']); unset($config['services']); } // consolidate variables if (!isset($config['variables'])) { $config['variables'] = array(); } foreach ($config as $key => $value) { if (!in_array($key, array('variables', 'services', 'php', 'const', 'mode'))) { $config['variables'][$key] = $value; } } // pre-expand variables at compile-time $variables = $config['variables']; array_walk_recursive($config, function (&$val) use($variables) { $val = Configurator::preExpand($val, $variables); }); // add variables foreach ($config['variables'] as $key => $value) { $code .= $this->generateCode('$container->params[?] = ?', $key, $value); } // PHP settings if (isset($config['php'])) { foreach ($config['php'] as $key => $value) { if (is_array($value)) { // back compatibility - flatten INI dots foreach ($value as $k => $v) { $code .= $this->configurePhp("{$key}.{$k}", $v); } } else { $code .= $this->configurePhp($key, $value); } } } // define constants if (isset($config['const'])) { foreach ($config['const'] as $key => $value) { $code .= $this->generateCode('define', $key, $value); } } // set modes - back compatibility if (isset($config['mode'])) { foreach ($config['mode'] as $mode => $state) { trigger_error(basename($file) . ": Section 'mode' is deprecated; use '{$mode}Mode' in section 'variables' instead.", E_USER_WARNING); $code .= $this->generateCode('$container->params[?] = ?', $mode . 'Mode', (bool) $state); } } // pre-loading $code .= self::preloadEnvironment($container); // auto-start services $code .= 'foreach ($container->getServiceNamesByTag("run") as $name => $foo) { $container->getService($name); }' . "\n"; $cache->save($cacheKey, $code, array(Cache::FILES => $file)); Nette\Utils\LimitedScope::evaluate($code, array('container' => $container)); return $this->container; }
/** * Loads configuration from file and process it. * @return DI\Container */ public function loadConfig($file, $section = NULL) { if ($file === NULL) { $file = $this->defaultConfigFile; } $container = $this->container; $file = $container->expand($file); if (!is_file($file)) { $file = preg_replace('#\.neon$#', '.ini', $file); // back compatibility } if ($section === NULL) { if (PHP_SAPI === 'cli') { $section = Environment::CONSOLE; } else { $section = $container->params['productionMode'] ? Environment::PRODUCTION : Environment::DEVELOPMENT; } } $cache = new Cache($container->templateCacheStorage, 'Nette.Configurator'); $cacheKey = array((array) $container->params, $file, $section); $cached = $cache->load($cacheKey); if ($cached) { require $cached['file']; fclose($cached['handle']); return $this->container; } $config = Nette\Config\Config::fromFile($file, $section); $code = "<?php\n// source file $file\n\n"; // back compatibility with singular names foreach (array('service', 'variable') as $item) { if (isset($config[$item])) { trigger_error(basename($file) . ": Section '$item' is deprecated; use plural form '{$item}s' instead.", E_USER_WARNING); $config[$item . 's'] = $config[$item]; unset($config[$item]); } } // add expanded variables while (!empty($config['variables'])) { $old = $config['variables']; foreach ($config['variables'] as $key => $value) { try { $code .= $this->generateCode('$container->params[?] = ?', $key, $container->params[$key] = $container->expand($value)); unset($config['variables'][$key]); } catch (Nette\InvalidArgumentException $e) {} } if ($old === $config['variables']) { throw new InvalidStateException("Unable to expand variables: " . implode(', ', array_keys($old)) . "."); } } unset($config['variables']); // process services if (isset($config['services'])) { foreach ($config['services'] as $key => & $def) { if (preg_match('#^Nette\\\\.*\\\\I?([a-zA-Z]+)$#', strtr($key, '-', '\\'), $m)) { // back compatibility $m[1][0] = strtolower($m[1][0]); trigger_error(basename($file) . ": service name '$key' has been renamed to '$m[1]'", E_USER_WARNING); $key = $m[1]; } if (is_array($def)) { if (method_exists(get_called_class(), "createService$key") && !isset($def['factory']) && !isset($def['class'])) { $def['factory'] = array(get_called_class(), "createService$key"); } if (isset($def['option'])) { $def['arguments'][] = $def['option']; } if (!empty($def['run'])) { $def['tags'] = array('run'); } } } $builder = new DI\ContainerBuilder; /**/$code .= $builder->generateCode($config['services']);/**/ /*5.2* $code .= $this->generateCode('$builder = new '.get_class($builder).'; $builder->addDefinitions($container, ?)', $config['services']);*/ unset($config['services']); } // expand variables array_walk_recursive($config, function(&$val) use ($container) { $val = $container->expand($val); }); // PHP settings if (isset($config['php'])) { foreach ($config['php'] as $key => $value) { if (is_array($value)) { // back compatibility - flatten INI dots foreach ($value as $k => $v) { $code .= $this->configurePhp("$key.$k", $v); } } else { $code .= $this->configurePhp($key, $value); } } unset($config['php']); } // define constants if (isset($config['const'])) { foreach ($config['const'] as $key => $value) { $code .= $this->generateCode('define', $key, $value); } unset($config['const']); } // set modes - back compatibility if (isset($config['mode'])) { trigger_error(basename($file) . ": Section 'mode' is deprecated; use 'params' instead.", E_USER_WARNING); foreach ($config['mode'] as $mode => $state) { $code .= $this->generateCode('$container->params[?] = ?', $mode . 'Mode', (bool) $state); } unset($config['mode']); } // other foreach ($config as $key => $value) { $code .= $this->generateCode('$container->params[?] = ' . (is_array($value) ? 'Nette\ArrayHash::from(?)' : '?'), $key, $value); } // pre-loading $code .= self::preloadEnvironment($container); // auto-start services $code .= 'foreach ($container->getServiceNamesByTag("run") as $name => $foo) { $container->getService($name); }' . "\n"; $cache->save($cacheKey, $code, array( Cache::FILES => $file, )); Nette\Utils\LimitedScope::evaluate($code, array('container' => $container)); return $this->container; }
public function reloadContainer() { $this->presenterFactory = NULL; $container = $this->getContainer(); $configurator = $this->getConfigurator(); $class = $container->parameters['container']['class'] . '_test_' . $this->containerCounter++; \Nette\Utils\LimitedScope::evaluate($configurator->buildContainer($dependencies, $class)); $this->container = new $class(); $this->container->initialize(); $this->container->addService('configurator', $configurator); }
/** * Reload system container. */ protected function reloadSystemContainer() { /** @var $configurator Configurator */ $configurator = $this->context->configurator; $class = $this->context->parameters['container']['class'] . $this->_systemContainer++; LimitedScope::evaluate($configurator->buildContainer($dependencies, $class)); /** @var context Container */ $this->context = new $class(); $this->context->parameters = (include $this->configDir . '/settings.php') + $this->context->parameters; $this->context->initialize(); $this->context->addService("configurator", $configurator); }
/** * @param $class * @param array $types * @return string * @throws \Nette\InvalidArgumentException */ public function prepareType($class, array $types = array()) { $class = trim($class, '\\'); $key = serialize(array('class' => $class, 'types' => $types)); if (!isset($this->loaded[$key])) { $newClass = $this->prepareClassName($class, $types); if ($this->storage) { $cache = new Cache($this->storage, 'Venne.Generics'); $data = $cache->load($key); if (!$data) { $data = $this->prepareClassTemplate($class, $newClass, $types); $cache->save($key, $data); $data = $cache->load($key); } if ($this->storage instanceof PhpFileStorage) { \Nette\Utils\LimitedScope::load($data['file']); } else { \Nette\Utils\LimitedScope::evaluate($data); } } else { $data = $this->prepareClassTemplate($class, $newClass, $types); \Nette\Utils\LimitedScope::evaluate($data); } $this->loaded[$key] = $newClass; } return $this->loaded[$key]; }