当前位置: 首页>>代码示例>>PHP>>正文


PHP ContainerBuilder::getDefinition方法代码示例

本文整理汇总了PHP中Nette\DI\ContainerBuilder::getDefinition方法的典型用法代码示例。如果您正苦于以下问题:PHP ContainerBuilder::getDefinition方法的具体用法?PHP ContainerBuilder::getDefinition怎么用?PHP ContainerBuilder::getDefinition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Nette\DI\ContainerBuilder的用法示例。


在下文中一共展示了ContainerBuilder::getDefinition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: generateService

 /**
  * Generates body of service method.
  * @return string
  */
 private function generateService($name)
 {
     $def = $this->builder->getDefinition($name);
     if ($def->isDynamic()) {
         return PhpHelpers::formatArgs('throw new Nette\\DI\\ServiceCreationException(?);', ["Unable to create dynamic service '{$name}', it must be added using addService()"]);
     }
     $entity = $def->getFactory()->getEntity();
     $serviceRef = $this->builder->getServiceName($entity);
     $factory = $serviceRef && !$def->getFactory()->arguments && !$def->getSetup() && $def->getImplementMode() !== $def::IMPLEMENT_MODE_CREATE ? new Statement(['@' . ContainerBuilder::THIS_CONTAINER, 'getService'], [$serviceRef]) : $def->getFactory();
     $this->currentService = NULL;
     $code = '$service = ' . $this->formatStatement($factory) . ";\n";
     if (($class = $def->getClass()) && !$serviceRef && $class !== $entity && !(is_string($entity) && preg_match('#^[\\w\\\\]+\\z#', $entity) && is_subclass_of($entity, $class))) {
         $code .= PhpHelpers::formatArgs("if (!\$service instanceof {$class}) {\n" . "\tthrow new Nette\\UnexpectedValueException(?);\n}\n", ["Unable to create service '{$name}', value returned by factory is not {$class} type."]);
     }
     $this->currentService = $name;
     foreach ($def->getSetup() as $setup) {
         $code .= $this->formatStatement($setup) . ";\n";
     }
     $code .= 'return $service;';
     if (!$def->getImplement()) {
         return $code;
     }
     $factoryClass = new Nette\PhpGenerator\ClassType();
     $factoryClass->setName('($this)')->addImplement($def->getImplement());
     $factoryClass->addProperty('container')->setVisibility('private');
     $factoryClass->addMethod('__construct')->addBody('$this->container = $container;')->addParameter('container')->setTypeHint($this->className);
     $factoryClass->addMethod($def->getImplementMode())->setParameters($this->convertParameters($def->parameters))->setBody(str_replace('$this', '$this->container', $code))->setReturnType(PHP_VERSION_ID >= 70000 ? $def->getClass() : NULL);
     if (PHP_VERSION_ID < 70000) {
         $this->generatedClasses[] = $factoryClass;
         $factoryClass->setName(str_replace(['\\', '.'], '_', "{$this->className}_{$def->getImplement()}Impl_{$name}"));
         return "return new {$factoryClass->getName()}(\$this);";
     }
     return 'return new ' . rtrim($factoryClass) . ';';
 }
开发者ID:nette,项目名称:di,代码行数:38,代码来源:PhpGenerator.php

示例2: 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')]);
 }
开发者ID:mouse-over,项目名称:storage,代码行数:18,代码来源:StorageExtension.php

示例3: install

 public function install(Nette\DI\ContainerBuilder $builder)
 {
     foreach ($this->listener as $class_name => $on) {
         $definition = $builder->getDefinition("annevents." . $class_name);
         foreach ($on as $on_name => $data) {
             foreach ($data['call'] as $add) {
                 $definition->addSetup('$service->' . $on_name . '[]=?->' . $add['method'], array($builder->getDefinition('annevents.' . str_replace('\\', '_', $add['class']))));
             }
         }
     }
 }
开发者ID:dipcom,项目名称:annevents,代码行数:11,代码来源:Guideline.php

示例4: prepareNetteDatabase

 private function prepareNetteDatabase(\Nette\DI\ContainerBuilder $builder)
 {
     $ndbConnectionSectionKeys = ['dsn' => NULL, 'user' => NULL, 'password' => NULL];
     /** @var \Nette\DI\CompilerExtension $extension */
     foreach ($this->compiler->getExtensions('Nette\\Bridges\\DatabaseDI\\DatabaseExtension') as $extension) {
         if (array_intersect_key($extension->config, $ndbConnectionSectionKeys)) {
             $extensionConfig = $extension->config;
             $definitionName = $extension->name . '.default.connection';
             $builder->getDefinition($definitionName)->setClass('Testbench\\Mocks\\NetteDatabaseConnectionMock', [$extensionConfig['dsn'], $extensionConfig['user'], $extensionConfig['password'], isset($extensionConfig['options']) ? $extensionConfig['options'] + ['lazy' => TRUE] : []]);
         } else {
             foreach ($extension->config as $sectionName => $sectionConfig) {
                 $definitionName = $extension->name . '.' . $sectionName . '.connection';
                 $builder->getDefinition($definitionName)->setClass('Testbench\\Mocks\\NetteDatabaseConnectionMock', [$sectionConfig['dsn'], $sectionConfig['user'], $sectionConfig['password'], isset($sectionConfig['options']) ? $sectionConfig['options'] + ['lazy' => TRUE] : []]);
             }
         }
     }
 }
开发者ID:mrtnzlml,项目名称:testbench,代码行数:17,代码来源:TestbenchExtension.php

示例5: 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;
     }
 }
开发者ID:radekdvorak,项目名称:nette-proxy-service,代码行数:25,代码来源:ProxyServiceExtension.php

示例6: createSubscribersMap

 private function createSubscribersMap(\Nette\DI\ContainerBuilder $builder)
 {
     $map = [];
     foreach ($builder->findByTag(self::TAG_SUBSCRIBER) as $serviceName => $tagProperties) {
         $def = $builder->getDefinition($serviceName);
         $class = $def->getClass();
         $events = $class::getSubscribedEvents();
         foreach ($events as $eventName => $callbacks) {
             foreach ($callbacks as $function) {
                 $map[$eventName][] = [$serviceName, $function];
             }
         }
     }
     return $map;
 }
开发者ID:bazo,项目名称:nette-events-extension,代码行数:15,代码来源:EventsExtension.php

示例7: processPackages

 /**
  * @param ContainerBuilder $builder
  * @throws \movi\InvalidArgumentException
  */
 private function processPackages(ContainerBuilder $builder)
 {
     $manager = $builder->getDefinition($this->prefix('manager'));
     $packages = $builder->parameters['packages'];
     if (count($packages) > 0) {
         foreach (array_values($packages) as $data) {
             $class = $this->getPackageClass($data);
             if (class_exists($class)) {
                 $package = new $class($data);
                 if (!$package instanceof Package) {
                     throw new InvalidArgumentException("Package class must be an instance of movi\\Packages\\Package.");
                 }
             } else {
                 $package = new Package($data);
             }
             // Compile the package
             $package->onCompile($this->compiler);
             $this->processExtensions($package->extensions);
             $manager->addSetup('addPackage', [$package]);
         }
     }
 }
开发者ID:peterzadori,项目名称:movi,代码行数:26,代码来源:PackagesExtension.php

示例8: setupRouting

 private function setupRouting(ContainerBuilder $container, array $config)
 {
     $router = $container->addDefinition('router')->setClass('Nette\\Application\\Routers\\RouteList');
     foreach ($config['routes'] as $mask => $action) {
         $router->addSetup('$service[] = new Nette\\Application\\Routers\\Route(?, ?);', array($mask, $action));
     }
     if ($container->parameters['debugMode'] && $config['debugger']) {
         $container->getDefinition('application')->addSetup('Nette\\Diagnostics\\Debugger::getBar()->addPanel(?)', array(new Nette\DI\Statement('Nette\\Application\\Diagnostics\\RoutingPanel')));
     }
 }
开发者ID:jurasm2,项目名称:nette,代码行数:10,代码来源:NetteExtension.php

示例9: parseServices

 static function parseServices(Nette\DI\ContainerBuilder $container, array $config)
 {
     $all = isset($config['services']) ? $config['services'] : array();
     $all += isset($config['factories']) ? $config['factories'] : array();
     uasort($all, function ($a, $b) {
         return strcmp(Config::isInheriting($a), Config::isInheriting($b));
     });
     foreach ($all as $name => $def) {
         if ($parent = Config::takeParent($def)) {
             $container->removeDefinition($name);
             $definition = $container->addDefinition($name);
             if ($parent !== Config::OVERWRITE) {
                 foreach ($container->getDefinition($parent) as $k => $v) {
                     $definition->{$k} = $v;
                 }
             }
         } elseif ($container->hasDefinition($name)) {
             $definition = $container->getDefinition($name);
         } else {
             $definition = $container->addDefinition($name);
         }
         try {
             static::parseService($definition, $def, isset($config['services'][$name]));
         } catch (\Exception $e) {
             throw new Nette\DI\ServiceCreationException("Service '{$name}': " . $e->getMessage(), NULL, $e);
         }
     }
 }
开发者ID:skapytek,项目名称:scripts,代码行数:28,代码来源:nette.min.php

示例10: setPresenterMapping

 /**
  * $mapping => ['NAMESPACE' => 'NAMESPACE\\*Module\\Presenters\\*Presenter']
  *
  * @param ContainerBuilder $builder
  * @param array $mapping
  */
 public function setPresenterMapping(ContainerBuilder $builder, array $mapping)
 {
     $builder->getDefinition($builder->getByType(IPresenterFactory::class))->addSetup('setMapping', [$mapping]);
 }
开发者ID:blitzik,项目名称:CMS,代码行数:10,代码来源:CompilerExtension.php

示例11: 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}'.");
         }
     }
 }
开发者ID:DIPcom,项目名称:Sandmin,代码行数:56,代码来源:Compiler.php

示例12: setupRouting

	private function setupRouting(ContainerBuilder $container, array $config)
	{
		$this->validate($config, $this->defaults['routing'], 'nette.routing');

		$router = $container->addDefinition('router') // no namespace for back compatibility
			->setClass('Nette\Application\Routers\RouteList');

		foreach ($config['routes'] as $mask => $action) {
			$router->addSetup('$service[] = new Nette\Application\Routers\Route(?, ?);', array($mask, $action));
		}

		if ($container->parameters['debugMode'] && $config['debugger']) {
			$container->getDefinition('application')->addSetup('Tracy\Debugger::getBar()->addPanel(?)', array(
				new Nette\DI\Statement('Nette\Bridges\ApplicationTracy\RoutingPanel')
			));
		}
	}
开发者ID:nakoukal,项目名称:fakturace,代码行数:17,代码来源:NetteExtension.php

示例13: 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);
         }
     }
 }
开发者ID:kukulich,项目名称:di,代码行数:50,代码来源:Compiler.php

示例14: 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);
         }
     }
 }
开发者ID:BozzaCoon,项目名称:SPHERE-Framework,代码行数:44,代码来源:Compiler.php

示例15: loadResourceRoutePanel

 /**
  * @param ContainerBuilder $container
  * @param array $config
  */
 private function loadResourceRoutePanel(ContainerBuilder $container, $config)
 {
     $container->addDefinition($this->prefix('panel'))->setClass('Drahak\\Restful\\Diagnostics\\ResourceRouterPanel')->setArguments(array($config['security']['privateKey'], isset($config['security']['requestTimeKey']) ? $config['security']['requestTimeKey'] : 'timestamp'))->addSetup('Nette\\Diagnostics\\Debugger::getBar()->addPanel(?)', array('@self'));
     $container->getDefinition('application')->addSetup('$service->onStartup[] = ?', array(array($this->prefix('@panel'), 'getTab')));
 }
开发者ID:michallohnisky,项目名称:Restful,代码行数:9,代码来源:RestfulExtension.php


注:本文中的Nette\DI\ContainerBuilder::getDefinition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。