本文整理汇总了PHP中Nette\DI\ContainerBuilder::addDefinition方法的典型用法代码示例。如果您正苦于以下问题:PHP ContainerBuilder::addDefinition方法的具体用法?PHP ContainerBuilder::addDefinition怎么用?PHP ContainerBuilder::addDefinition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Nette\DI\ContainerBuilder
的用法示例。
在下文中一共展示了ContainerBuilder::addDefinition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addWebLoader
private function addWebLoader(\Nette\DI\ContainerBuilder $builder, $name, $config)
{
$filesServiceName = $this->prefix($name . 'Files');
$files = $builder->addDefinition($filesServiceName)->setClass('WebLoader\\FileCollection')->setArguments(array($config['sourceDir']));
foreach ($config['files'] as $file) {
// finder support
if (is_array($file) && isset($file['files']) && (isset($file['in']) || isset($file['from']))) {
$finder = \Nette\Utils\Finder::findFiles($file['files']);
unset($file['files']);
foreach ($file as $method => $params) {
call_user_func_array(array($finder, $method), array($params));
}
foreach ($finder as $foundFile) {
$files->addSetup('addFile', array((string) $foundFile));
}
} else {
$files->addSetup('addFile', array($file));
}
}
$files->addSetup('addRemoteFiles', array($config['remoteFiles']));
$compiler = $builder->addDefinition($this->prefix($name . 'Compiler'))->setClass('WebLoader\\Compiler')->setArguments(array('@' . $filesServiceName, $config['namingConvention'], $config['tempDir']));
$compiler->addSetup('setJoinFiles', array($config['joinFiles']));
foreach ($config['filters'] as $filter) {
$compiler->addSetup('addFilter', array($filter));
}
foreach ($config['fileFilters'] as $filter) {
$compiler->addSetup('addFileFilter', array($filter));
}
// todo css media
}
示例2: addWebLoader
private function addWebLoader(ContainerBuilder $builder, $name, $config)
{
$filesServiceName = $this->prefix($name . 'Files');
$files = $builder->addDefinition($filesServiceName)->setClass('WebLoader\\FileCollection')->setArguments(array($config['sourceDir']));
foreach ($this->findFiles($config['files'], $config['sourceDir']) as $file) {
$files->addSetup('addFile', array($file));
}
foreach ($this->findFiles($config['watchFiles'], $config['sourceDir']) as $file) {
$files->addSetup('addWatchFile', array($file));
}
$files->addSetup('addRemoteFiles', array($config['remoteFiles']));
$compiler = $builder->addDefinition($this->prefix($name . 'Compiler'))->setClass('WebLoader\\Compiler')->setArguments(array('@' . $filesServiceName, $config['namingConvention'], $config['tempDir']));
$compiler->addSetup('setJoinFiles', array($config['joinFiles']));
if ($builder->parameters['webloader']['debugger']) {
$compiler->addSetup('@' . $this->prefix('tracyPanel') . '::addLoader', array($name, '@' . $this->prefix($name . 'Compiler')));
}
foreach ($config['filters'] as $filter) {
$compiler->addSetup('addFilter', array($filter));
}
foreach ($config['fileFilters'] as $filter) {
$compiler->addSetup('addFileFilter', array($filter));
}
if (isset($config['debug']) && $config['debug']) {
$compiler->addSetup('enableDebugging');
}
$compiler->addSetup('setCheckLastModified', array($config['checkLastModified']));
// todo css media
}
示例3: loadRoutes
/**
* Routes definition
*
* @param ContainerBuilder $container Container
* @param array $config Config
*
* @return void
*/
protected function loadRoutes(ContainerBuilder $container, $config)
{
$container->addDefinition($this->prefix('storages'))->setClass('MouseOver\\Storage\\Application\\StorageList');
$container->addDefinition($this->prefix('responder'))->setClass('MouseOver\\Storage\\Application\\StorageResponder');
$container->addDefinition($this->prefix('linkResolver'))->setClass('MouseOver\\Storage\\Application\\StorageLinkResolver', [$config['module']]);
$container->getDefinition('nette.latte')->addSetup('MouseOver\\Storage\\Application\\StorageMacros::install($service->getCompiler(), ?)', array($this->prefix('@linkResolver')));
$routesList = $container->addDefinition($this->prefix('storageRoutes'))->setClass('Nette\\Application\\Routers\\RouteList')->setAutowired(false);
$routesList->addSetup('$service[] = new Nette\\Application\\Routers\\Route(?, function ($presenter, $storage, $file) { return ?->handle($storage, $file, $presenter->request->getParameters()); })', array($config['module'] . '/<storage>/<file [a-zA-Z0-9\\-_.\\/]+>', $this->prefix('@responder')));
$container->getDefinition('router')->addSetup('MouseOver\\Storage\\DI\\StorageExtension::prependTo($service, ?)', [$this->prefix('@storageRoutes')]);
}
示例4: setupTemplating
/**
* @param \Nette\DI\ContainerBuilder $container
* @param array $config
*/
private function setupTemplating(ContainerBuilder $container, array $config)
{
$def = $container->addDefinition($this->prefix('templateFilesFormatter'))->setClass('Lohini\\Templating\\TemplateFilesFormatter')->addSetup('$skin', [$config['skin']]);
foreach ($config['dirs'] as $dir => $priority) {
$def->addSetup('addDir', Validators::isNumericInt($dir) ? [$priority] : [$container->expand($dir), $priority]);
}
}
示例5: processAuthenticators
/**
* @param ContainerBuilder $container
* @param array $config
*/
private function processAuthenticators(ContainerBuilder $container, array $config)
{
$authentication = $container->addDefinition($this->prefix('authentication'))->setClass('Flame\\Rest\\Security\\Authentication');
if (!$container->parameters['debugMode'] && count($config['ips'])) {
$ipAuthenticator = $container->addDefinition($this->prefix('ipAuthenticator'))->setClass('Flame\\Rest\\Security\\Authenticators\\IpAuthenticator')->setArguments(array($config['ips']));
$authentication->addSetup('addAuthenticator', array($ipAuthenticator));
}
if (!$container->parameters['debugMode'] && count($config['referers'])) {
$refererAuthenticator = $container->addDefinition($this->prefix('refererAuthenticator'))->setClass('Flame\\Rest\\Security\\Authenticators\\RefererAuthenticator')->setArguments(array($config['referers']));
$authentication->addSetup('addAuthenticator', array($refererAuthenticator));
}
foreach ($config['authenticators'] as $k => $authenticatorConfig) {
$authenticator = $container->addDefinition($this->prefix('authenticator' . $k))->setClass($authenticatorConfig);
$authentication->addSetup('addAuthenticator', array($authenticator));
}
}
示例6: setupCompileTemplatesCommand
private function setupCompileTemplatesCommand(ContainerBuilder $builder, array $config)
{
$config = $this->validateConfig($this->compileTemplatesDefaults, $config, 'compileTemplates');
Validators::assertField($config, 'source', 'string|array');
$builder->addDefinition($this->prefix('compileTemplates'))->setClass(CompileTemplatesCommand::class, [array_map(function ($directory) use($builder) {
Validators::assert($directory, 'string');
return Helpers::expand($directory, $builder->parameters);
}, (array) $config['source'])])->addTag(ConsoleExtension::TAG_COMMAND)->setAutowired(false);
}
示例7: configMapper
/**
* Adds mapper service into container
* @return ServiceDefinition
*/
protected function configMapper(ContainerBuilder $builder, array $config)
{
if ($config['defaultEntityNamespace'] !== NULL && !is_string($config['defaultEntityNamespace'])) {
throw new \RuntimeException('DefaultEntityNamespace must be NULL or string, ' . gettype($config['defaultEntityNamespace']) . ' given');
}
$mapper = $builder->addDefinition($this->prefix('mapper'))->setClass($config['mapper'], array($config['defaultEntityNamespace']));
$this->processEntityProviders($mapper, $config);
$this->processUserEntities($mapper, $config);
return $mapper;
}
示例8: createEagerProxy
/**
* @param string $serviceName
* @param string $cacheDirectory
*/
private function createEagerProxy($serviceName, $cacheDirectory)
{
$definition = $this->builder->getDefinition($serviceName);
$this->builder->removeDefinition($serviceName);
$hiddenServiceName = $this->prefix($serviceName);
$proxyClassName = $definition->getClass() . md5(serialize($definition));
$this->builder->addDefinition($serviceName)->setClass($proxyClassName)->setFactory('@' . $this->prefix('eagerServiceFactory') . '::create', array($hiddenServiceName, $proxyClassName));
$this->builder->addDefinition($hiddenServiceName, $definition)->setAutowired(false);
if (!class_exists($proxyClassName)) {
$classGenerator = new ClassGenerator();
$this->proxyGenerator->generate(new \ReflectionClass($definition->getClass()), $classGenerator);
$classGenerator->setName($proxyClassName);
$code = '<?php' . ClassGenerator::LINE_FEED . $classGenerator->generate();
$tempPath = $cacheDirectory . uniqid() . ".php";
$backslashPos = strrpos($proxyClassName, '\\');
$newPath = $cacheDirectory . substr($proxyClassName, $backslashPos ? $backslashPos + 1 : 0) . ".php";
file_put_contents($tempPath, $code, LOCK_EX);
rename($tempPath, $newPath);
require_once $newPath;
}
}
示例9: setupDatabase
private function setupDatabase(ContainerBuilder $container, array $config)
{
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, 'flunorette');
$info += array('autowired' => $autowired) + $this->databaseDefaults;
$autowired = false;
foreach (array('transactionCounter', 'delimiteMode', 'lazy', 'driverClass') as $option) {
if (isset($info[$option])) {
$info['options'][$option] = $info[$option];
}
}
foreach ((array) $info['options'] as $key => $value) {
if (preg_match('#^PDO::\\w+\\z#', $key)) {
unset($info['options'][$key]);
$info['options'][constant($key)] = $value;
}
}
if (!$info['reflection']) {
$reflection = null;
} elseif (is_string($info['reflection'])) {
$reflection = new Statement(preg_match('#^[a-z]+\\z#', $info['reflection']) ? 'Flunorette\\Reflections\\' . ucfirst($info['reflection']) . 'Reflection' : $info['reflection'], strtolower($info['reflection']) === 'discovered' ? array('@self') : array());
} else {
$tmp = Compiler::filterArguments(array($info['reflection']));
$reflection = reset($tmp);
}
$connection = $container->addDefinition($this->prefix($name))->setClass('Flunorette\\Connection', array($info['dsn'], $info['user'], $info['password'], $info['options']))->setAutowired($info['autowired'])->addSetup('setCacheStorage')->addSetup('Nette\\Diagnostics\\Debugger::getBlueScreen()->addPanel(?)', array('Flunorette\\Bridges\\Nette\\Diagnostics\\ConnectionPanel::renderException'));
if ($container->parameters['debugMode'] && $info['debugger']) {
$connection->addSetup('Flunorette\\Helpers::createDebugPanel', array($connection, !empty($info['explain']), $name));
}
}
}
示例10: parseServices
/**
* Parses section 'services' from (unexpanded) configuration file.
* @return void
*/
public static function parseServices(ContainerBuilder $builder, array $config, $namespace = NULL)
{
$services = isset($config['services']) ? $config['services'] : array();
$factories = isset($config['factories']) ? $config['factories'] : array();
$all = array_merge($services, $factories);
uasort($all, function ($a, $b) {
return strcmp(Config\Helpers::isInheriting($a), Config\Helpers::isInheriting($b));
});
if (!empty($config['factories'])) {
trigger_error("Section 'factories' is deprecated, move definitions to section 'services' and append key 'autowired: no'.", E_USER_DEPRECATED);
}
foreach ($all as $origName => $def) {
if ((string) (int) $origName === (string) $origName) {
$name = count($builder->getDefinitions()) . preg_replace('#\\W+#', '_', $def instanceof \stdClass ? ".{$def->value}" : (is_scalar($def) ? ".{$def}" : ''));
} elseif (array_key_exists($origName, $services) && array_key_exists($origName, $factories)) {
throw new ServiceCreationException("It is not allowed to use services and factories with the same name: '{$origName}'.");
} else {
$name = ($namespace ? $namespace . '.' : '') . strtr($origName, '\\', '_');
}
$params = $builder->parameters;
if (is_array($def) && isset($def['parameters'])) {
foreach ((array) $def['parameters'] as $k => $v) {
$v = explode(' ', is_int($k) ? $v : $k);
$params[end($v)] = $builder::literal('$' . end($v));
}
}
$def = Helpers::expand($def, $params);
if (($parent = Config\Helpers::takeParent($def)) && $parent !== $name) {
$builder->removeDefinition($name);
$definition = $builder->addDefinition($name, $parent === Config\Helpers::OVERWRITE ? NULL : unserialize(serialize($builder->getDefinition($parent))));
} elseif ($builder->hasDefinition($name)) {
$definition = $builder->getDefinition($name);
} else {
$definition = $builder->addDefinition($name);
}
try {
static::parseService($definition, $def);
} catch (\Exception $e) {
throw new ServiceCreationException("Service '{$name}': " . $e->getMessage(), NULL, $e);
}
if (array_key_exists($origName, $factories)) {
$definition->setAutowired(FALSE);
}
if ($definition->class === 'self') {
$definition->class = $origName;
trigger_error("Replace service definition '{$origName}: self' with '- {$origName}'.", E_USER_DEPRECATED);
}
if ($definition->factory && $definition->factory->entity === 'self') {
$definition->factory->entity = $origName;
trigger_error("Replace service definition '{$origName}: self' with '- {$origName}'.", E_USER_DEPRECATED);
}
}
}
示例11: loadDefinitions
private function loadDefinitions(ContainerBuilder $builder)
{
$classes = $this->robotLoader->getIndexedClasses();
$markerInterface = new ClassType(IServiceMarker::class);
foreach ($classes as $key => $val) {
if (Strings::endsWith($key, 'Service')) {
$reflection = new ClassType($key);
$serviceName = '_auto.' . str_replace("\\", "_", $key);
if (!$reflection->isAbstract() && $reflection->isSubclassOf($markerInterface)) {
$builder->addDefinition($serviceName)->setClass($key);
}
}
}
}
示例12: parseServices
/**
* Parses section 'services' from configuration file.
*
* @return void
*/
public static function parseServices(Nette\DI\ContainerBuilder $container, array $config, $namespace = null)
{
$services = isset($config['services']) ? $config['services'] : array();
$factories = isset($config['factories']) ? $config['factories'] : array();
if ($tmp = array_intersect_key($services, $factories)) {
$tmp = implode("', '", array_keys($tmp));
throw new Nette\DI\ServiceCreationException("It is not allowed to use services and factories with the same names: '{$tmp}'.");
}
$all = $services + $factories;
uasort($all, function ($a, $b) {
return strcmp(Helpers::isInheriting($a), Helpers::isInheriting($b));
});
foreach ($all as $name => $def) {
$shared = array_key_exists($name, $services);
$name = ($namespace ? $namespace . '.' : '') . $name;
if (($parent = Helpers::takeParent($def)) && $parent !== $name) {
$container->removeDefinition($name);
$definition = $container->addDefinition($name);
if ($parent !== Helpers::OVERWRITE) {
foreach ($container->getDefinition($parent) as $k => $v) {
$definition->{$k} = unserialize(serialize($v));
// deep clone
}
}
} elseif ($container->hasDefinition($name)) {
$definition = $container->getDefinition($name);
if ($definition->shared !== $shared) {
throw new Nette\DI\ServiceCreationException("It is not allowed to use service and factory with the same name '{$name}'.");
}
} else {
$definition = $container->addDefinition($name);
}
try {
static::parseService($definition, $def, $shared);
} catch (\Exception $e) {
throw new Nette\DI\ServiceCreationException("Service '{$name}': " . $e->getMessage(), null, $e);
}
}
}
示例13: createTimestampStorage
/**
* @param ContainerBuilder $containerBuilder
*
* @return \Nette\DI\ServiceDefinition
*/
protected function createTimestampStorage(ContainerBuilder $containerBuilder)
{
return $containerBuilder->addDefinition($this->prefix('timestampStorage'))->setAutowired(FALSE)->setInject(FALSE);
}
示例14: setupDatabase
private function setupDatabase(ContainerBuilder $container, array $config)
{
if (isset($config['dsn'])) {
$config = array('default' => $config);
}
$autowired = TRUE;
foreach ((array) $config as $name => $info) {
if (!is_array($info)) {
continue;
}
$info += $this->databaseDefaults + array('autowired' => $autowired);
$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;
}
}
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('@self') : array());
} else {
$tmp = Nette\DI\Compiler::filterArguments(array($info['reflection']));
$reflection = reset($tmp);
}
$connection = $container->addDefinition($this->prefix("database.{$name}"))->setClass('Nette\\Database\\Connection', array($info['dsn'], $info['user'], $info['password'], $info['options']))->setAutowired($info['autowired'])->addSetup('setContext', array(new Nette\DI\Statement('Nette\\Database\\Context', array('@self', $reflection))))->addSetup('Nette\\Diagnostics\\Debugger::getBlueScreen()->addPanel(?)', array('Nette\\Database\\Diagnostics\\ConnectionPanel::renderException'));
$container->addDefinition($this->prefix("database.{$name}.context"))->setClass('Nette\\Database\\Context')->setFactory(array($connection, 'getContext'))->setAutowired($info['autowired']);
if ($container->parameters['debugMode'] && $info['debugger']) {
$connection->addSetup('Nette\\Database\\Helpers::createDebugPanel', array($connection, !empty($info['explain']), $name));
}
}
}
示例15: addDefinition
/**
*
* @param string $class_name
* @param Nette\DI\CompilerExtension $builder
* @return string
*/
public function addDefinition($class_name, Nette\DI\ContainerBuilder $builder)
{
$name = str_replace('\\', '_', $class_name);
$builder->addDefinition($this->prefix(str_replace('\\', '_', $name)))->setClass($class_name)->setInject(true);
return $name;
}