本文整理汇总了PHP中Nette\DI\Compiler类的典型用法代码示例。如果您正苦于以下问题:PHP Compiler类的具体用法?PHP Compiler怎么用?PHP Compiler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Compiler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fixCompile
/**
* @param \Nette\DI\Compiler $compiler
* @return string
*/
protected function fixCompile(\Nette\DI\Compiler $compiler)
{
$code = $this->compiler->compile();
if (!is_array($code)) {
return $code;
}
return implode("\n\n\n", $code);
}
示例2: generate
/**
* @return array of (code, file[])
*/
protected function generate($class, $generator)
{
$compiler = new Compiler();
$compiler->getContainerBuilder()->setClassName($class);
$code = call_user_func_array($generator, [&$compiler]);
$code = $code ?: implode("\n\n\n", $compiler->compile());
$files = $compiler->getDependencies();
$files = call_user_func($this->filter, $files);
$files = array_combine($files, $files);
return array("<?php\n{$code}", serialize(@array_map('filemtime', $files)));
}
示例3: registerAdditionalExtension
/**
* Register extension if not registered by user.
*/
private static function registerAdditionalExtension(\Nette\DI\Compiler $compiler, $name, $newExtension)
{
$extensions = [];
foreach (isset($compiler->config['extensions']) ? $compiler->config['extensions'] : [] as $extension) {
if (is_string($extension)) {
$extensions[] = $extension;
} elseif ($extension instanceof \Nette\DI\Statement) {
$extensions[] = $extension->getEntity();
}
}
if ($extensions && !in_array(get_class($newExtension), $extensions)) {
$compiler->addExtension($name, $newExtension);
}
}
示例4: loadConfiguration
public function loadConfiguration()
{
$builder = $this->getContainerBuilder();
$config = $this->getConfig($this->defaults);
$helperClasses = array('Symfony\\Component\\Console\\Helper\\FormatterHelper', 'Symfony\\Component\\Console\\Helper\\QuestionHelper', 'Kdyby\\Console\\Helpers\\PresenterHelper');
$helperClasses = array_map(function ($class) {
return new Nette\DI\Statement($class);
}, $helperClasses);
if (class_exists('Symfony\\Component\\Console\\Helper\\ProgressHelper')) {
$helperClasses[] = new Nette\DI\Statement('Symfony\\Component\\Console\\Helper\\ProgressHelper', array(false));
}
if (class_exists('Symfony\\Component\\Console\\Helper\\DialogHelper')) {
$helperClasses[] = new Nette\DI\Statement('Symfony\\Component\\Console\\Helper\\DialogHelper', array(false));
}
$builder->addDefinition($this->prefix('helperSet'))->setClass('Symfony\\Component\\Console\\Helper\\HelperSet', array($helperClasses))->setInject(FALSE);
$builder->addDefinition($this->prefix('application'))->setClass('Kdyby\\Console\\Application', array($config['name'], $config['version']))->addSetup('setHelperSet', array($this->prefix('@helperSet')))->addSetup('injectServiceLocator')->setInject(FALSE);
$builder->addDefinition($this->prefix('dicHelper'))->setClass('Kdyby\\Console\\ContainerHelper')->addTag(self::TAG_HELPER, 'dic');
if ($config['disabled']) {
return;
}
$builder->addDefinition($this->prefix('router'))->setClass('Kdyby\\Console\\CliRouter')->setAutowired(FALSE)->setInject(FALSE);
Nette\Utils\Validators::assert($config, 'array');
foreach ($config['commands'] as $command) {
$def = $builder->addDefinition($this->prefix('command.' . md5(Nette\Utils\Json::encode($command))));
list($def->factory) = Nette\DI\Compiler::filterArguments(array(is_string($command) ? new Nette\DI\Statement($command) : $command));
if (class_exists($def->factory->entity)) {
$def->class = $def->factory->entity;
}
$def->setAutowired(FALSE);
$def->setInject(FALSE);
$def->addTag(self::TAG_COMMAND);
}
}
示例5: load
public function load(Nette\DI\Compiler $compiler, Nette\DI\ContainerBuilder $containerBuilder)
{
$aspects = [];
foreach ($this->aspectsList as $def) {
if (!is_array($def)) {
if (!is_string($def) && (!$def instanceof \stdClass || empty($def->value)) && !$def instanceof Nette\DI\Statement) {
$serialised = Nette\Utils\Json::encode($def);
throw new Kdyby\Aop\UnexpectedValueException("The service definition {$serialised} is expected to be an array or Neon entity.");
}
$def = ['factory' => $def];
}
$def['tags'][] = AspectsExtension::ASPECT_TAG;
$aspects[] = $def;
}
$compiler->parseServices($containerBuilder, ['services' => $aspects], $this->prefix ? substr($this->extension->prefix('self'), 0, -5) : NULL);
}
示例6: loadConfiguration
public function loadConfiguration()
{
$builder = $this->getContainerBuilder();
$config = $this->getConfig($this->defaults);
$builder->addDefinition($this->prefix('helperSet'))->setClass('Symfony\\Component\\Console\\Helper\\HelperSet', array(array(new Nette\DI\Statement('Symfony\\Component\\Console\\Helper\\DialogHelper'), new Nette\DI\Statement('Symfony\\Component\\Console\\Helper\\FormatterHelper'), new Nette\DI\Statement('Symfony\\Component\\Console\\Helper\\ProgressHelper'), new Nette\DI\Statement('Kdyby\\Console\\Helpers\\PresenterHelper'))))->setInject(FALSE);
$builder->addDefinition($this->prefix('application'))->setClass('Kdyby\\Console\\Application', array($config['name'], $config['version']))->addSetup('setHelperSet', array($this->prefix('@helperSet')))->setInject(FALSE);
$builder->addDefinition($this->prefix('router'))->setClass('Kdyby\\Console\\CliRouter')->setAutowired(FALSE)->setInject(FALSE);
$builder->getDefinition('router')->addSetup('Kdyby\\Console\\CliRouter::prependTo($service, ?)', array($this->prefix('@router')));
$builder->getDefinition('nette.presenterFactory')->addSetup('if (method_exists($service, ?)) { $service->setMapping(array(? => ?)); } ' . 'elseif (property_exists($service, ?)) { $service->mapping[?] = ?; }', array('setMapping', 'Kdyby', 'KdybyModule\\*\\*Presenter', 'mapping', 'Kdyby', 'KdybyModule\\*\\*Presenter'));
if (!empty($config['url'])) {
if (!preg_match('~^https?://[^/]+\\.[a-z]+(/.*)?$~', $config['url'])) {
throw new Nette\Utils\AssertionException("The url '{$config['url']}' is not valid, please use this format: 'http://domain.tld/path'.");
}
$builder->getDefinition('nette.httpRequestFactory')->setClass('Kdyby\\Console\\HttpRequestFactory')->addSetup('setFakeRequestUrl', array($config['url']));
}
$builder->addDefinition($this->prefix('dicHelper'))->setClass('Kdyby\\Console\\ContainerHelper')->addTag(self::HELPER_TAG, 'dic');
Nette\Utils\Validators::assert($config, 'array');
foreach ($config['commands'] as $command) {
$def = $builder->addDefinition($this->prefix('command.' . md5(Nette\Utils\Json::encode($command))));
list($def->factory) = Nette\DI\Compiler::filterArguments(array(is_string($command) ? new Nette\DI\Statement($command) : $command));
if (class_exists($def->factory->entity)) {
$def->class = $def->factory->entity;
}
$def->setAutowired(FALSE);
$def->setInject(FALSE);
$def->addTag(self::COMMAND_TAG);
}
}
示例7: afterCompile
public function afterCompile(Nette\PhpGenerator\ClassType $class)
{
$initialize = $class->getMethod('initialize');
$builder = $this->getContainerBuilder();
$options = $this->config;
unset($options['bar'], $options['blueScreen']);
if (isset($options['logSeverity'])) {
$res = 0;
foreach ((array) $options['logSeverity'] as $level) {
$res |= is_int($level) ? $level : constant($level);
}
$options['logSeverity'] = $res;
}
foreach ($options as $key => $value) {
if ($value !== NULL) {
$key = ($key === 'fromEmail' ? 'getLogger()->' : '$') . $key;
$initialize->addBody($builder->formatPhp('Tracy\\Debugger::' . $key . ' = ?;', Nette\DI\Compiler::filterArguments([$value])));
}
}
$logger = $builder->getDefinition($this->prefix('logger'));
if ($logger->getFactory()->getEntity() !== 'Tracy\\Debugger::getLogger') {
$initialize->addBody($builder->formatPhp('Tracy\\Debugger::setLogger(?);', [$logger]));
}
if ($this->debugMode) {
foreach ((array) $this->config['bar'] as $item) {
$initialize->addBody($builder->formatPhp('$this->getService(?)->addPanel(?);', Nette\DI\Compiler::filterArguments([$this->prefix('bar'), is_string($item) ? new Nette\DI\Statement($item) : $item])));
}
if (!$this->cliMode) {
$initialize->addBody('if ($tmp = $this->getByType("Nette\\Http\\Session", FALSE)) { $tmp->start(); Tracy\\Debugger::dispatch(); };');
}
}
foreach ((array) $this->config['blueScreen'] as $item) {
$initialize->addBody($builder->formatPhp('$this->getService(?)->addPanel(?);', Nette\DI\Compiler::filterArguments([$this->prefix('blueScreen'), $item])));
}
}
示例8: afterCompile
/**
* @param Code\ClassType $class
*/
public function afterCompile(Code\ClassType $class)
{
$container = $this->getContainerBuilder();
$init = $class->methods['initialize'];
/** @var Code\Method $init */
$blueScreen = 'Nette\\Diagnostics\\Debugger::' . (method_exists('Nette\\Diagnostics\\Debugger', 'getBlueScreen') ? 'getBlueScreen()' : '$blueScreen');
$init->addBody($container->formatPhp($blueScreen . '->addPanel(?);', Nette\DI\Compiler::filterArguments(array('Kdyby\\PayPalExpress\\Diagnostics\\Panel::renderException'))));
}
示例9: setupMetadataParserFactory
/**
* @param string|array $config
*/
protected function setupMetadataParserFactory($config)
{
$builder = $this->getContainerBuilder();
if (is_array($config) && empty($config['class'])) {
$config['class'] = $this->defaults['metadataParserFactory'];
}
Compiler::parseService($builder->addDefinition($this->prefix('metadataParserFactory')), $config);
}
示例10: getDbalFactory
private function getDbalFactory($dbal)
{
if ($dbal instanceof Nette\DI\Statement) {
return Nette\DI\Compiler::filterArguments([$dbal])[0];
} elseif (is_string($dbal) && isset($this->dbals[$dbal])) {
return $this->dbals[$dbal];
} else {
return NULL;
}
}
示例11: configureBuilder
/**
* @param array
*/
protected function configureBuilder($config)
{
$builder = $this->getContainerBuilder();
$builder->addDefinition($this->prefix('formBuilderFactory'))->setClass('Librette\\Doctrine\\Forms\\Builder\\FormBuilderFactory');
$chain = $builder->addDefinition($this->prefix('builderChainHandler'))->setClass('Librette\\Doctrine\\Forms\\Builder\\Handlers\\ChainHandler');
foreach (array_merge($config['handlers'], $this->defaultBuilderHandlers) as $i => $handler) {
Compiler::parseService($def = $builder->addDefinition($this->prefix("builderHandler{$i}")), $handler);
$def->setAutowired(FALSE);
$chain->addSetup('add', [$def, FALSE]);
}
$builder->addDefinition($this->prefix('builderConfiguration'))->setClass('Librette\\Doctrine\\Forms\\Builder\\Configuration');
}
示例12: setCompiler
public function setCompiler(Nette\DI\Compiler $compiler, $name) : self
{
$application = current($compiler->getExtensions(Nette\Bridges\ApplicationDI\ApplicationExtension::class));
if ($application instanceof Nette\Bridges\ApplicationDI\ApplicationExtension) {
$application->defaults['errorPresenter'] = FALSE;
$application->defaults['scanDirs'] = FALSE;
$application->defaults['mapping'] = ['*' => 'Ytnuk\\*\\*'];
}
$session = current($compiler->getExtensions(Nette\Bridges\HttpDI\SessionExtension::class));
if ($session instanceof Nette\Bridges\HttpDI\SessionExtension) {
$session->defaults['debugger'] = $this->debugMode;
}
$http = current($compiler->getExtensions(Nette\Bridges\HttpDI\HttpExtension::class));
if ($http instanceof Nette\Bridges\HttpDI\HttpExtension) {
$http->defaults['headers']['X-Powered-By'] = __NAMESPACE__;
$contentSecurityPolicy = ['default-src' => '\'self\'', 'form-action' => '\'self\'', 'img-src' => '\'self\' data:'];
if ($this->debugMode) {
$contentSecurityPolicy += ['script-src' => '\'self\' \'unsafe-inline\' \'unsafe-eval\'', 'style-src' => '\'self\' \'unsafe-inline\''];
}
$http->defaults['headers']['Content-Security-Policy'] = implode(';', array_map(function (string $key, string $value) {
return implode(' ', [$key, $value]);
}, array_keys($contentSecurityPolicy), array_values($contentSecurityPolicy)));
}
$di = current($compiler->getExtensions(Nette\DI\Extensions\DIExtension::class));
if ($di instanceof Nette\DI\Extensions\DIExtension) {
$di->defaults['debugger'] = $this->debugMode;
}
return parent::setCompiler($compiler, $name);
}
示例13: buildDefinition
/**
* @param string $value
* @return ServiceDefinition|NULL
*/
protected function buildDefinition($value)
{
if ($value === NULL) {
return NULL;
}
$builder = $this->getContainerBuilder();
$definition = $builder->addDefinition($this->prefix(md5($value)));
list($definition->factory) = Compiler::filterArguments(array(is_string($value) ? new Statement($value) : $value));
list($resolverClass) = (array) $builder->normalizeEntity($definition->getFactory()->getEntity());
if (class_exists($resolverClass)) {
$definition->setClass($resolverClass);
}
return $definition;
}
示例14: loadJobs
protected function loadJobs()
{
$builder = $this->getContainerBuilder();
$config = $this->getConfig($this->defaults);
foreach ($config['jobs'] as $subscriber) {
$def = $builder->addDefinition($this->prefix('job.' . md5(Json::encode($subscriber))));
list($def->factory) = Compiler::filterArguments(array(is_string($subscriber) ? new Statement($subscriber) : $subscriber));
list($subscriberClass) = (array) $builder->normalizeEntity($def->factory->entity);
if (class_exists($subscriberClass)) {
$def->class = $subscriberClass;
}
$def->setAutowired(false);
$def->addTag($config['cronJobTag']);
}
}
示例15: loadConfiguration
public function loadConfiguration()
{
$container = $this->getContainerBuilder();
$config = $this->compiler->getConfig();
if (isset($config['nette']['database'])) {
// back compatibility
$config = $config['nette']['database'];
$prefix = 'nette.';
} else {
$config = isset($config[$this->name]) ? $config[$this->name] : array();
$prefix = '';
}
if (isset($config['dsn'])) {
$config = array('default' => $config);
}
$autowired = TRUE;
foreach ((array) $config as $name => $info) {
if (!is_array($info)) {
continue;
}
$this->validate($info, $this->databaseDefaults, 'database');
$info += array('autowired' => $autowired) + $this->databaseDefaults;
$autowired = FALSE;
foreach ((array) $info['options'] as $key => $value) {
if (preg_match('#^PDO::\\w+\\z#', $key)) {
unset($info['options'][$key]);
$info['options'][constant($key)] = $value;
}
}
$connection = $container->addDefinition($prefix . $this->prefix($name))->setClass('Nette\\Database\\Connection', array($info['dsn'], $info['user'], $info['password'], $info['options']))->setAutowired($info['autowired'])->addSetup('Tracy\\Debugger::getBlueScreen()->addPanel(?)', array('Nette\\Bridges\\DatabaseTracy\\ConnectionPanel::renderException'));
if (!$info['reflection']) {
$reflection = NULL;
} elseif (is_string($info['reflection'])) {
$reflection = new Nette\DI\Statement(preg_match('#^[a-z]+\\z#', $info['reflection']) ? 'Nette\\Database\\Reflection\\' . ucfirst($info['reflection']) . 'Reflection' : $info['reflection'], strtolower($info['reflection']) === 'discovered' ? array($connection) : array());
} else {
$tmp = Nette\DI\Compiler::filterArguments(array($info['reflection']));
$reflection = reset($tmp);
}
$container->addDefinition($prefix . $this->prefix("{$name}.context"))->setClass('Nette\\Database\\Context', array($connection, $reflection))->setAutowired($info['autowired']);
if ($container->parameters['debugMode'] && $info['debugger']) {
$connection->addSetup('Nette\\Database\\Helpers::createDebugPanel', array($connection, !empty($info['explain']), $name));
}
}
}