/** * Generates PHP classes. First class is the container. * @return Nette\PhpGenerator\ClassType[] */ public function generate($className) { $this->builder->complete(); $this->generatedClasses = []; $this->className = $className; $containerClass = $this->generatedClasses[] = new Nette\PhpGenerator\ClassType($this->className); $containerClass->setExtends(Container::class); $containerClass->addMethod('__construct')->addBody('parent::__construct(?);', [$this->builder->parameters]); $definitions = $this->builder->getDefinitions(); ksort($definitions); $meta = $containerClass->addProperty('meta')->setVisibility('protected')->setValue([Container::TYPES => $this->builder->getClassList()]); foreach ($definitions as $name => $def) { $meta->value[Container::SERVICES][$name] = $def->getClass() ?: NULL; foreach ($def->getTags() as $tag => $value) { $meta->value[Container::TAGS][$tag][$name] = $value; } } foreach ($definitions as $name => $def) { try { $name = (string) $name; $methodName = Container::getMethodName($name); if (!PhpHelpers::isIdentifier($methodName)) { throw new ServiceCreationException('Name contains invalid characters.'); } $containerClass->addMethod($methodName)->addComment('@return ' . ($def->getImplement() ?: $def->getClass()))->setBody($name === ContainerBuilder::THIS_CONTAINER ? 'return $this;' : $this->generateService($name))->setParameters($def->getImplement() ? [] : $this->convertParameters($def->parameters)); } catch (\Exception $e) { throw new ServiceCreationException("Service '{$name}': " . $e->getMessage(), 0, $e); } } $aliases = $this->builder->getAliases(); ksort($aliases); $meta->value[Container::ALIASES] = $aliases; return $this->generatedClasses; }
public function afterCompile(Nette\PhpGenerator\ClassType $class) { $initialize = $class->getMethod('initialize'); $container = $this->getContainerBuilder(); if ($this->debugMode && $this->config['debugger']) { $initialize->addBody($container->formatPhp('?;', array(new Nette\DI\Statement('@Tracy\\Bar::addPanel', array(new Nette\DI\Statement('Nette\\Bridges\\DITracy\\ContainerPanel')))))); } foreach (array_filter($container->findByTag('run')) as $name => $on) { $initialize->addBody('$this->getService(?);', array($name)); } if (!empty($this->config['accessors'])) { $definitions = $container->getDefinitions(); ksort($definitions); foreach ($definitions as $name => $def) { if (Nette\PhpGenerator\Helpers::isIdentifier($name)) { $type = $def->getImplement() ?: $def->getClass(); $class->addDocument("@property {$type} \${$name}"); } } } }
public function afterCompile(Nette\PhpGenerator\ClassType $class) { $initialize = $class->methods['initialize']; $container = $this->getContainerBuilder(); $config = $this->getConfig($this->defaults); // debugger foreach (array('email', 'editor', 'browser', 'strictMode', 'maxLen', 'maxDepth', 'showLocation', 'scream') as $key) { if (isset($config['debugger'][$key])) { $initialize->addBody('Nette\\Diagnostics\\Debugger::$? = ?;', array($key, $config['debugger'][$key])); } } if ($container->parameters['debugMode']) { if ($config['container']['debugger']) { $config['debugger']['bar'][] = 'Nette\\DI\\Diagnostics\\ContainerPanel'; } foreach ((array) $config['debugger']['bar'] as $item) { $initialize->addBody($container->formatPhp('Nette\\Diagnostics\\Debugger::getBar()->addPanel(?);', Nette\DI\Compiler::filterArguments(array(is_string($item) ? new Nette\DI\Statement($item) : $item)))); } foreach ((array) $config['debugger']['blueScreen'] as $item) { $initialize->addBody($container->formatPhp('Nette\\Diagnostics\\Debugger::getBlueScreen()->addPanel(?);', Nette\DI\Compiler::filterArguments(array($item)))); } } if (!empty($container->parameters['tempDir'])) { $initialize->addBody('Nette\\Caching\\Storages\\FileStorage::$useDirectories = ?;', array($this->checkTempDir($container->expand('%tempDir%/cache')))); } foreach ((array) $config['forms']['messages'] as $name => $text) { $initialize->addBody('Nette\\Forms\\Rules::$defaultMessages[Nette\\Forms\\Form::?] = ?;', array($name, $text)); } if ($config['session']['autoStart'] === 'smart') { $initialize->addBody('$this->getByType("Nette\\Http\\Session")->exists() && $this->getByType("Nette\\Http\\Session")->start();'); } elseif ($config['session']['autoStart']) { $initialize->addBody('$this->getByType("Nette\\Http\\Session")->start();'); } if ($config['latte']['xhtml']) { $initialize->addBody('Nette\\Utils\\Html::$xhtml = ?;', array(TRUE)); } if (isset($config['security']['frames']) && $config['security']['frames'] !== TRUE) { $frames = $config['security']['frames']; if ($frames === FALSE) { $frames = 'DENY'; } elseif (preg_match('#^https?:#', $frames)) { $frames = "ALLOW-FROM {$frames}"; } $initialize->addBody('header(?);', array("X-Frame-Options: {$frames}")); } foreach ($container->findByTag('run') as $name => $on) { if ($on) { $initialize->addBody('$this->getService(?);', array($name)); } } if (!empty($config['container']['accessors'])) { $definitions = $container->definitions; ksort($definitions); foreach ($definitions as $name => $def) { if (Nette\PhpGenerator\Helpers::isIdentifier($name)) { $type = $def->implement ?: $def->class; $class->addDocument("@property {$type} \${$name}"); } } } $initialize->addBody("@header('Content-Type: text/html; charset=utf-8');"); $initialize->addBody('Nette\\Utils\\SafeStream::register();'); }
/** * Generates PHP classes. First class is the container. * @return Nette\PhpGenerator\ClassType[] */ public function generateClasses($className = 'Container', $parentName = 'Nette\\DI\\Container') { unset($this->definitions[self::THIS_CONTAINER]); $this->addDefinition(self::THIS_CONTAINER)->setClass('Nette\\DI\\Container'); $this->generatedClasses = array(); $this->prepareClassList(); $containerClass = $this->generatedClasses[] = new Nette\PhpGenerator\ClassType($className); $containerClass->setExtends($parentName); $containerClass->addMethod('__construct')->addBody('parent::__construct(?);', array($this->parameters)); $definitions = $this->definitions; ksort($definitions); $meta = $containerClass->addProperty('meta', array())->setVisibility('protected')->setValue(array(Container::TYPES => $this->classes)); foreach ($definitions as $name => $def) { foreach ($def->tags as $tag => $value) { $meta->value[Container::TAGS][$tag][$name] = $value; } } foreach ($definitions as $name => $def) { try { $name = (string) $name; $methodName = Container::getMethodName($name); if (!PhpHelpers::isIdentifier($methodName)) { throw new ServiceCreationException('Name contains invalid characters.'); } $containerClass->addMethod($methodName)->addDocument("@return " . ($def->implement ?: $def->class))->setBody($name === self::THIS_CONTAINER ? 'return $this;' : $this->generateService($name))->setParameters($def->implement ? array() : $this->convertParameters($def->parameters)); } catch (\Exception $e) { throw new ServiceCreationException("Service '{$name}': " . $e->getMessage(), NULL, $e); } } return $this->generatedClasses; }