本文整理汇总了PHP中Nette\DI\ContainerBuilder::getDefinitions方法的典型用法代码示例。如果您正苦于以下问题:PHP ContainerBuilder::getDefinitions方法的具体用法?PHP ContainerBuilder::getDefinitions怎么用?PHP ContainerBuilder::getDefinitions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Nette\DI\ContainerBuilder
的用法示例。
在下文中一共展示了ContainerBuilder::getDefinitions方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: generate
/**
* 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;
}
示例2: generateCode
public function generateCode($className, $parentName)
{
foreach ($this->extensions as $extension) {
$extension->beforeCompile();
$this->container->addDependency(Nette\Reflection\ClassType::from($extension)->getFileName());
}
$classes[] = $class = $this->container->generateClass($parentName);
$class->setName($className)->addMethod('initialize');
foreach ($this->extensions as $extension) {
$extension->afterCompile($class);
}
$defs = $this->container->getDefinitions();
ksort($defs);
$list = array_keys($defs);
foreach (array_reverse($defs, true) as $name => $def) {
if ($def->class === 'Nette\\DI\\NestedAccessor' && ($found = preg_grep('#^' . $name . '\\.#i', $list))) {
$list = array_diff($list, $found);
$def->class = $className . '_' . preg_replace('#\\W+#', '_', $name);
$class->documents = preg_replace("#\\S+(?= \\\${$name}\$)#", $def->class, $class->documents);
$classes[] = $accessor = new Nette\Utils\PhpGenerator\ClassType($def->class);
foreach ($found as $item) {
if ($defs[$item]->internal) {
continue;
}
$short = substr($item, strlen($name) + 1);
$accessor->addDocument($defs[$item]->shared ? "@property {$defs[$item]->class} \${$short}" : "@method {$defs[$item]->class} create" . ucfirst("{$short}()"));
}
}
}
return implode("\n\n\n", $classes);
}
示例3: getByType
/**
* @param ContainerBuilder $container
* @param string $type
* @return ServiceDefinition|null
*/
private function getByType(ContainerBuilder $container, $type)
{
$definitionas = $container->getDefinitions();
foreach ($definitionas as $definition) {
if ($definition->class === $type) {
return $definition;
}
}
return NULL;
}
示例4: 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();
$all = array_merge($services, $factories);
uasort($all, function ($a, $b) {
return strcmp(Helpers::isInheriting($a), Helpers::isInheriting($b));
});
foreach ($all as $origName => $def) {
$shared = array_key_exists($origName, $services);
if ((string) (int) $origName === (string) $origName) {
$name = (string) (count($container->getDefinitions()) + 1);
} elseif ($shared && array_key_exists($origName, $factories)) {
throw new Nette\DI\ServiceCreationException("It is not allowed to use services and factories with the same name: '{$origName}'.");
} else {
$name = ($namespace ? $namespace . '.' : '') . strtr($origName, '\\', '_');
}
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);
}
}
}
示例5: parseServices
/**
* Parses section 'services' from (unexpanded) configuration file.
* @return void
*/
public static function parseServices(ContainerBuilder $builder, array $config, string $namespace = NULL)
{
$services = isset($config['services']) ? $config['services'] : [];
$depths = [];
foreach ($services as $name => $def) {
$path = [];
while (Config\Helpers::isInheriting($def)) {
$path[] = $def;
$def = isset($services[$def[Config\Helpers::EXTENDS_KEY]]) ? $services[$def[Config\Helpers::EXTENDS_KEY]] : [];
if (in_array($def, $path, TRUE)) {
throw new ServiceCreationException("Circular reference detected for service '{$name}'.");
}
}
$depths[$name] = count($path);
}
array_multisort($depths, $services);
foreach ($services as $name => $def) {
if ((string) (int) $name === (string) $name) {
$postfix = $def instanceof Statement && is_string($def->getEntity()) ? '.' . $def->getEntity() : (is_scalar($def) ? ".{$def}" : '');
$name = count($builder->getDefinitions()) + 1 . preg_replace('#\\W+#', '_', $postfix);
} elseif ($namespace) {
$name = $namespace . '.' . $name;
}
$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(), 0, $e);
}
}
}
示例6: 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);
}
}
}
示例7: parseServices
/**
* Parses section 'services' from (unexpanded) configuration file.
* @return void
*/
public static function parseServices(ContainerBuilder $builder, array $config, $namespace = NULL)
{
if (!empty($config['factories'])) {
throw new Nette\DeprecatedException("Section 'factories' is deprecated, move definitions to section 'services' and append key 'autowired: no'.");
}
$services = isset($config['services']) ? $config['services'] : array();
$depths = array();
foreach ($services as $name => $def) {
$path = array();
while (Config\Helpers::isInheriting($def)) {
$path[] = $def;
$def = isset($services[$def[Config\Helpers::EXTENDS_KEY]]) ? $services[$def[Config\Helpers::EXTENDS_KEY]] : array();
if (in_array($def, $path, TRUE)) {
throw new ServiceCreationException("Circular reference detected for service '{$name}'.");
}
}
$depths[$name] = count($path);
}
array_multisort($depths, $services);
foreach ($services as $origName => $def) {
if ((string) (int) $origName === (string) $origName) {
$postfix = $def instanceof Statement && is_string($def->getEntity()) ? '.' . $def->getEntity() : (is_scalar($def) ? ".{$def}" : '');
$name = count($builder->getDefinitions()) + 1 . preg_replace('#\\W+#', '_', $postfix);
} 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 ($definition->getClass() === 'self' || $definition->getFactory() && $definition->getFactory()->getEntity() === 'self') {
throw new Nette\DeprecatedException("Replace service definition '{$origName}: self' with '- {$origName}'.");
}
}
}
示例8: scrambledServiceName
/**
* @param ContainerBuilder $builder
* @param string $className
* @return string
*/
private static function scrambledServiceName(ContainerBuilder $builder, $className)
{
return count($builder->getDefinitions()) + 1 . preg_replace('#\\W+#', '_', $className);
}
示例9: optimizeListeners
/**
* @param \Nette\DI\ContainerBuilder $builder
*/
private function optimizeListeners(Nette\DI\ContainerBuilder $builder)
{
$listeners = array();
foreach ($this->listeners as $serviceName => $eventNames) {
foreach ($eventNames as $eventName) {
list($namespace, $event) = Kdyby\Events\Event::parseName($eventName);
$listeners[$eventName][] = $serviceName;
if (!$namespace || !class_exists($namespace)) {
continue;
// it might not even be a "classname" event namespace
}
// find all subclasses and register the listener to all the classes dispatching them
foreach ($builder->getDefinitions() as $def) {
if (!($class = $def->getClass())) {
continue;
// ignore unresolved classes
}
if (is_subclass_of($class, $namespace)) {
$listeners["{$class}::{$event}"][] = $serviceName;
}
}
}
}
foreach ($listeners as $id => $subscribers) {
$listeners[$id] = array_unique($subscribers);
}
$builder->getDefinition($this->prefix('manager'))->setClass('Kdyby\\Events\\LazyEventManager', array($listeners))->setup = $this->allowedManagerSetup;
}