本文整理汇总了PHP中Symfony\Component\DependencyInjection\Definition::getFactoryClass方法的典型用法代码示例。如果您正苦于以下问题:PHP Definition::getFactoryClass方法的具体用法?PHP Definition::getFactoryClass怎么用?PHP Definition::getFactoryClass使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\DependencyInjection\Definition
的用法示例。
在下文中一共展示了Definition::getFactoryClass方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: autowireFactory
/**
* @param Definition $definition
* @param string[] $classes
* @param string $id
* @param ContainerBuilder $container
*/
private function autowireFactory(Definition $definition, array $classes, $id, ContainerBuilder $container)
{
if ($definition->getFactoryClass()) {
$factoryClass = $definition->getFactoryClass();
} else {
$factoryName = $definition->getFactoryService();
$factoryClass = $container->getDefinition($factoryName)->getClass();
}
if ($factoryClass) {
$parameterBag = $container->getParameterBag();
$method = new ReflectionMethod($parameterBag->resolveValue($factoryClass), $definition->getFactoryMethod());
$autowiredArgs = $this->autowireMethod($method, $definition->getArguments(), $classes, $id, $container);
$definition->setArguments($autowiredArgs);
}
}
示例2: testSetGetFactoryClass
public function testSetGetFactoryClass()
{
$def = new Definition('stdClass');
$this->assertNull($def->getFactoryClass());
$this->assertSame($def, $def->setFactoryClass('stdClass2'), '->setFactoryClass() implements a fluent interface.');
$this->assertEquals('stdClass2', $def->getFactoryClass(), '->getFactoryClass() returns current class to construct this service.');
}
示例3: processDefinition
/**
* @param Definition $definition
*/
protected function processDefinition(Definition $definition)
{
if ($definition->isSynthetic()) {
return;
}
if ($definition->getFactoryService() || $definition->getFactoryClass()) {
return;
}
if ($file = $definition->getFile()) {
require_once $file;
}
if (!class_exists($definition->getClass())) {
return;
}
$class = new \ReflectionClass($definition->getClass());
if (!$class->implementsInterface(static::INTERFACE_CLASS)) {
return;
}
$metadata = $this->getMetadataFactory()->getMetadataForClass($definition->getClass());
if (!$metadata instanceof ClassMetadata) {
return;
}
$namespace = $metadata->getNamespace() ?: static::ROOT_NAMESPACE;
$serviceName = static::PROVIDER_PREFIX . substr(sha1($namespace), 0, 10);
if (!$this->container->hasDefinition($serviceName)) {
$cacher = new Definition('Werkint\\Bundle\\CacheBundle\\Service\\CacheProvider', [$this->container->getParameter('kernel.cache_dir') . '/werkint_cache']);
$cacher->setPublic(true);
$cacher->addMethodCall('setNamespace', [$namespace]);
$this->container->setDefinition($serviceName, $cacher);
}
$definition->addMethodCall('setCacheProvider', [new Reference($serviceName)]);
}
示例4: validateFactoryClass
private function validateFactoryClass(Definition $definition)
{
$factoryClass = $definition->getFactoryClass();
if (!$factoryClass) {
return;
}
$factoryMethod = $definition->getFactoryMethod();
if ($factoryClass && !$factoryMethod) {
throw new MissingFactoryMethodException();
}
$factoryClass = $this->resolveValue($factoryClass);
$this->validateFactoryClassAndMethod($factoryClass, $factoryMethod);
}
示例5: addNewInstance
private function addNewInstance($id, Definition $definition, $return, $instantiation)
{
$class = $this->dumpValue($definition->getClass());
$arguments = array();
foreach ($definition->getArguments() as $value) {
$arguments[] = $this->dumpValue($value);
}
if (null !== $definition->getFactory()) {
$callable = $definition->getFactory();
if (is_array($callable)) {
if (!preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $callable[1])) {
throw new RuntimeException(sprintf('Cannot dump definition because of invalid factory method (%s)', $callable[1] ?: 'n/a'));
}
if ($callable[0] instanceof Reference || $callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0])) {
return sprintf(" {$return}{$instantiation}%s->%s(%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
}
$class = $this->dumpValue($callable[0]);
// If the class is a string we can optimize call_user_func away
if (strpos($class, "'") === 0) {
return sprintf(" {$return}{$instantiation}%s::%s(%s);\n", $this->dumpLiteralClass($class), $callable[1], $arguments ? implode(', ', $arguments) : '');
}
return sprintf(" {$return}{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? ', ' . implode(', ', $arguments) : '');
}
return sprintf(" {$return}{$instantiation}\\%s(%s);\n", $callable, $arguments ? implode(', ', $arguments) : '');
} elseif (null !== $definition->getFactoryMethod(false)) {
if (null !== $definition->getFactoryClass(false)) {
$class = $this->dumpValue($definition->getFactoryClass(false));
// If the class is a string we can optimize call_user_func away
if (strpos($class, "'") === 0) {
return sprintf(" {$return}{$instantiation}%s::%s(%s);\n", $this->dumpLiteralClass($class), $definition->getFactoryMethod(false), $arguments ? implode(', ', $arguments) : '');
}
return sprintf(" {$return}{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($definition->getFactoryClass(false)), $definition->getFactoryMethod(false), $arguments ? ', ' . implode(', ', $arguments) : '');
}
if (null !== $definition->getFactoryService(false)) {
return sprintf(" {$return}{$instantiation}%s->%s(%s);\n", $this->getServiceCall($definition->getFactoryService(false)), $definition->getFactoryMethod(false), implode(', ', $arguments));
}
throw new RuntimeException(sprintf('Factory method requires a factory service or factory class in service definition for %s', $id));
}
if (false !== strpos($class, '$')) {
return sprintf(" \$class = %s;\n\n {$return}{$instantiation}new \$class(%s);\n", $class, implode(', ', $arguments));
}
return sprintf(" {$return}{$instantiation}new %s(%s);\n", $this->dumpLiteralClass($class), implode(', ', $arguments));
}
示例6: describeContainerDefinition
/**
* {@inheritdoc}
*/
protected function describeContainerDefinition(Definition $definition, array $options = array())
{
$output = '- Class: `' . $definition->getClass() . '`' . "\n" . '- Scope: `' . $definition->getScope() . '`' . "\n" . '- Public: ' . ($definition->isPublic() ? 'yes' : 'no') . "\n" . '- Synthetic: ' . ($definition->isSynthetic() ? 'yes' : 'no') . "\n" . '- Lazy: ' . ($definition->isLazy() ? 'yes' : 'no') . "\n" . '- Synchronized: ' . ($definition->isSynchronized() ? 'yes' : 'no') . "\n" . '- Abstract: ' . ($definition->isAbstract() ? 'yes' : 'no');
if ($definition->getFile()) {
$output .= "\n" . '- File: `' . $definition->getFile() . '`';
}
if ($definition->getFactoryClass()) {
$output .= "\n" . '- Factory Class: `' . $definition->getFactoryClass() . '`';
}
if ($definition->getFactoryService()) {
$output .= "\n" . '- Factory Service: `' . $definition->getFactoryService() . '`';
}
if ($definition->getFactoryMethod()) {
$output .= "\n" . '- Factory Method: `' . $definition->getFactoryMethod() . '`';
}
if ($factory = $definition->getFactory()) {
if (is_array($factory)) {
if ($factory[0] instanceof Reference) {
$output .= "\n" . '- Factory Service: `' . $factory[0] . '`';
} elseif ($factory[0] instanceof Definition) {
throw new \InvalidArgumentException('Factory is not describable.');
} else {
$output .= "\n" . '- Factory Class: `' . $factory[0] . '`';
}
$output .= "\n" . '- Factory Method: `' . $factory[1] . '`';
} else {
$output .= "\n" . '- Factory Function: `' . $factory . '`';
}
}
if (!(isset($options['omit_tags']) && $options['omit_tags'])) {
foreach ($definition->getTags() as $tagName => $tagData) {
foreach ($tagData as $parameters) {
$output .= "\n" . '- Tag: `' . $tagName . '`';
foreach ($parameters as $name => $value) {
$output .= "\n" . ' - ' . ucfirst($name) . ': ' . $value;
}
}
}
}
$this->write(isset($options['id']) ? sprintf("%s\n%s\n\n%s\n", $options['id'], str_repeat('~', strlen($options['id'])), $output) : $output);
}
示例7: addService
/**
* Adds a service
*
* @param string $id
* @param Definition $definition
*
* @return string
*/
private function addService($id, $definition)
{
$name = Container::camelize($id);
$this->definitionVariables = new \SplObjectStorage();
$this->referenceVariables = array();
$this->variableCount = 0;
$return = '';
if ($definition->isSynthetic()) {
$return = sprintf('@throws RuntimeException always since this service is expected to be injected dynamically');
} elseif ($class = $definition->getClass()) {
$return = sprintf("@return %s A %s instance.", 0 === strpos($class, '%') ? 'Object' : $class, $class);
} elseif ($definition->getFactoryClass()) {
$return = sprintf('@return Object An instance returned by %s::%s().', $definition->getFactoryClass(), $definition->getFactoryMethod());
} elseif ($definition->getFactoryService()) {
$return = sprintf('@return Object An instance returned by %s::%s().', $definition->getFactoryService(), $definition->getFactoryMethod());
}
$doc = '';
if (ContainerInterface::SCOPE_PROTOTYPE !== $definition->getScope()) {
$doc .= <<<EOF
*
* This service is shared.
* This method always returns the same instance of the service.
EOF;
}
if (!$definition->isPublic()) {
$doc .= <<<EOF
*
* This service is private.
* If you want to be able to request this service from the container directly,
* make it public, otherwise you might end up with broken code.
EOF;
}
$code = <<<EOF
/**
* Gets the '{$id}' service.{$doc}
*
* {$return}
*/
protected function get{$name}Service()
{
EOF;
$scope = $definition->getScope();
if (ContainerInterface::SCOPE_CONTAINER !== $scope && ContainerInterface::SCOPE_PROTOTYPE !== $scope) {
$code .= <<<EOF
if (!isset(\$this->scopedServices['{$scope}'])) {
throw new InactiveScopeException('{$id}', '{$scope}');
}
EOF;
}
if ($definition->isSynthetic()) {
$code .= sprintf(" throw new RuntimeException('You have requested a synthetic service (\"%s\"). The DIC does not know how to construct this service.');\n }\n", $id);
} else {
$code .= $this->addServiceInclude($id, $definition) . $this->addServiceLocalTempVariables($id, $definition) . $this->addServiceInlinedDefinitions($id, $definition) . $this->addServiceInstance($id, $definition) . $this->addServiceInlinedDefinitionsSetup($id, $definition) . $this->addServiceMethodCalls($id, $definition) . $this->addServiceProperties($id, $definition) . $this->addServiceConfigurator($id, $definition) . $this->addServiceReturn($id, $definition);
}
$this->definitionVariables = null;
$this->referenceVariables = null;
return $code;
}
示例8: addService
/**
* Adds a service.
*
* @param string $id
* @param Definition $definition
*
* @return string
*/
private function addService($id, $definition)
{
$code = " {$id}:\n";
if ($class = $definition->getClass()) {
if ('\\' === substr($class, 0, 1)) {
$class = substr($class, 1);
}
$code .= sprintf(" class: %s\n", $this->dumper->dump($class));
}
if (!$definition->isPublic()) {
$code .= " public: false\n";
}
$tagsCode = '';
foreach ($definition->getTags() as $name => $tags) {
foreach ($tags as $attributes) {
$att = array();
foreach ($attributes as $key => $value) {
$att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value));
}
$att = $att ? ', ' . implode(', ', $att) : '';
$tagsCode .= sprintf(" - { name: %s%s }\n", $this->dumper->dump($name), $att);
}
}
if ($tagsCode) {
$code .= " tags:\n" . $tagsCode;
}
if ($definition->getFile()) {
$code .= sprintf(" file: %s\n", $this->dumper->dump($definition->getFile()));
}
if ($definition->isSynthetic()) {
$code .= sprintf(" synthetic: true\n");
}
if ($definition->isSynchronized(false)) {
$code .= sprintf(" synchronized: true\n");
}
if ($definition->isDeprecated()) {
$code .= sprintf(" deprecated: %s\n", $definition->getDeprecationMessage('%service_id%'));
}
if ($definition->isAutowired()) {
$code .= " autowire: true\n";
}
$autowiringTypesCode = '';
foreach ($definition->getAutowiringTypes() as $autowiringType) {
$autowiringTypesCode .= sprintf(" - %s\n", $this->dumper->dump($autowiringType));
}
if ($autowiringTypesCode) {
$code .= sprintf(" autowiring_types:\n%s", $autowiringTypesCode);
}
if ($definition->getFactoryClass(false)) {
$code .= sprintf(" factory_class: %s\n", $this->dumper->dump($definition->getFactoryClass(false)));
}
if ($definition->isLazy()) {
$code .= sprintf(" lazy: true\n");
}
if ($definition->getFactoryMethod(false)) {
$code .= sprintf(" factory_method: %s\n", $this->dumper->dump($definition->getFactoryMethod(false)));
}
if ($definition->getFactoryService(false)) {
$code .= sprintf(" factory_service: %s\n", $this->dumper->dump($definition->getFactoryService(false)));
}
if ($definition->getArguments()) {
$code .= sprintf(" arguments: %s\n", $this->dumper->dump($this->dumpValue($definition->getArguments()), 0));
}
if ($definition->getProperties()) {
$code .= sprintf(" properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0));
}
if ($definition->getMethodCalls()) {
$code .= sprintf(" calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12));
}
if (!$definition->isShared()) {
$code .= " shared: false\n";
}
if (ContainerInterface::SCOPE_CONTAINER !== ($scope = $definition->getScope(false))) {
$code .= sprintf(" scope: %s\n", $this->dumper->dump($scope));
}
if (null !== ($decorated = $definition->getDecoratedService())) {
list($decorated, $renamedId, $priority) = $decorated;
$code .= sprintf(" decorates: %s\n", $decorated);
if (null !== $renamedId) {
$code .= sprintf(" decoration_inner_name: %s\n", $renamedId);
}
if (0 !== $priority) {
$code .= sprintf(" decoration_priority: %s\n", $priority);
}
}
if ($callable = $definition->getFactory()) {
$code .= sprintf(" factory: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
}
if ($callable = $definition->getConfigurator()) {
$code .= sprintf(" configurator: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
}
return $code;
//.........这里部分代码省略.........
示例9: addNewInstance
private function addNewInstance($id, Definition $definition, $return, $instantiation)
{
$class = $this->dumpValue($definition->getClass());
$arguments = array();
foreach ($definition->getArguments() as $value) {
$arguments[] = $this->dumpValue($value);
}
if (null !== $definition->getFactoryMethod()) {
if (null !== $definition->getFactoryClass()) {
return sprintf(" {$return}{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($definition->getFactoryClass()), $definition->getFactoryMethod(), $arguments ? ', ' . implode(', ', $arguments) : '');
}
if (null !== $definition->getFactoryService()) {
return sprintf(" {$return}{$instantiation}%s->%s(%s);\n", $this->getServiceCall($definition->getFactoryService()), $definition->getFactoryMethod(), implode(', ', $arguments));
}
throw new RuntimeException(sprintf('Factory method requires a factory service or factory class in service definition for %s', $id));
}
if (false !== strpos($class, '$')) {
return sprintf(" \$class = %s;\n\n {$return}{$instantiation}new \$class(%s);\n", $class, implode(', ', $arguments));
}
return sprintf(" {$return}{$instantiation}new \\%s(%s);\n", substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
}
示例10: getContainerDefinitionData
/**
* @param Definition $definition
* @param bool $omitTags
*
* @return array
*/
private function getContainerDefinitionData(Definition $definition, $omitTags = false)
{
$data = array('class' => (string) $definition->getClass(), 'scope' => $definition->getScope(), 'public' => $definition->isPublic(), 'synthetic' => $definition->isSynthetic(), 'lazy' => $definition->isLazy(), 'synchronized' => $definition->isSynchronized(), 'abstract' => $definition->isAbstract(), 'file' => $definition->getFile());
if ($definition->getFactoryClass()) {
$data['factory_class'] = $definition->getFactoryClass();
}
if ($definition->getFactoryService()) {
$data['factory_service'] = $definition->getFactoryService();
}
if ($definition->getFactoryMethod()) {
$data['factory_method'] = $definition->getFactoryMethod();
}
if ($factory = $definition->getFactory()) {
if (is_array($factory)) {
if ($factory[0] instanceof Reference) {
$data['factory_service'] = (string) $factory[0];
} elseif ($factory[0] instanceof Definition) {
throw new \InvalidArgumentException('Factory is not describable.');
} else {
$data['factory_class'] = $factory[0];
}
$data['factory_method'] = $factory[1];
} else {
$data['factory_function'] = $factory;
}
}
if (!$omitTags) {
$data['tags'] = array();
if (count($definition->getTags())) {
foreach ($definition->getTags() as $tagName => $tagData) {
foreach ($tagData as $parameters) {
$data['tags'][] = array('name' => $tagName, 'parameters' => $parameters);
}
}
}
}
return $data;
}
示例11: addService
/**
* Adds a service.
*
* @param Definition $definition
* @param string $id
* @param \DOMElement $parent
*/
private function addService($definition, $id, \DOMElement $parent)
{
$service = $this->document->createElement('service');
if (null !== $id) {
$service->setAttribute('id', $id);
}
if ($class = $definition->getClass()) {
if ('\\' === substr($class, 0, 1)) {
$class = substr($class, 1);
}
$service->setAttribute('class', $class);
}
if ($definition->getFactoryMethod(false)) {
$service->setAttribute('factory-method', $definition->getFactoryMethod(false));
}
if ($definition->getFactoryClass(false)) {
$service->setAttribute('factory-class', $definition->getFactoryClass(false));
}
if ($definition->getFactoryService(false)) {
$service->setAttribute('factory-service', $definition->getFactoryService(false));
}
if (!$definition->isShared()) {
$service->setAttribute('shared', 'false');
}
if (ContainerInterface::SCOPE_CONTAINER !== ($scope = $definition->getScope(false))) {
$service->setAttribute('scope', $scope);
}
if (!$definition->isPublic()) {
$service->setAttribute('public', 'false');
}
if ($definition->isSynthetic()) {
$service->setAttribute('synthetic', 'true');
}
if ($definition->isSynchronized(false)) {
$service->setAttribute('synchronized', 'true');
}
if ($definition->isLazy()) {
$service->setAttribute('lazy', 'true');
}
if (null !== ($decorated = $definition->getDecoratedService())) {
list($decorated, $renamedId, $priority) = $decorated;
$service->setAttribute('decorates', $decorated);
if (null !== $renamedId) {
$service->setAttribute('decoration-inner-name', $renamedId);
}
if (0 !== $priority) {
$service->setAttribute('decoration-priority', $priority);
}
}
foreach ($definition->getTags() as $name => $tags) {
foreach ($tags as $attributes) {
$tag = $this->document->createElement('tag');
$tag->setAttribute('name', $name);
foreach ($attributes as $key => $value) {
$tag->setAttribute($key, $value);
}
$service->appendChild($tag);
}
}
if ($definition->getFile()) {
$file = $this->document->createElement('file');
$file->appendChild($this->document->createTextNode($definition->getFile()));
$service->appendChild($file);
}
if ($parameters = $definition->getArguments()) {
$this->convertParameters($parameters, 'argument', $service);
}
if ($parameters = $definition->getProperties()) {
$this->convertParameters($parameters, 'property', $service, 'name');
}
$this->addMethodCalls($definition->getMethodCalls(), $service);
if ($callable = $definition->getFactory()) {
$factory = $this->document->createElement('factory');
if (is_array($callable) && $callable[0] instanceof Definition) {
$this->addService($callable[0], null, $factory);
$factory->setAttribute('method', $callable[1]);
} elseif (is_array($callable)) {
$factory->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
$factory->setAttribute('method', $callable[1]);
} else {
$factory->setAttribute('function', $callable);
}
$service->appendChild($factory);
}
if ($definition->isDeprecated()) {
$deprecated = $this->document->createElement('deprecated');
$deprecated->appendChild($this->document->createTextNode($definition->getDeprecationMessage('%service_id%')));
$service->appendChild($deprecated);
}
if ($definition->isAutowired()) {
$service->setAttribute('autowire', 'true');
}
foreach ($definition->getAutowiringTypes() as $autowiringTypeValue) {
//.........这里部分代码省略.........
示例12: processDefinition
private function processDefinition(Definition $definition, $pointcuts, &$interceptors)
{
if ($definition->isSynthetic()) {
return;
}
if ($definition->getFactoryService() || $definition->getFactoryClass()) {
return;
}
if ($file = $definition->getFile()) {
require_once $file;
}
$class = new \ReflectionClass($definition->getClass());
// check if class is matched
$matchingPointcuts = array();
foreach ($pointcuts as $interceptor => $pointcut) {
if ($pointcut->matchesClass($class)) {
$matchingPointcuts[$interceptor] = $pointcut;
}
}
if (empty($matchingPointcuts)) {
return;
}
$this->addResources($class, $this->container);
if ($class->isFinal()) {
return;
}
$classAdvices = array();
foreach ($class->getMethods(\ReflectionMethod::IS_PROTECTED | \ReflectionMethod::IS_PUBLIC) as $method) {
if ($method->isFinal()) {
continue;
}
$advices = array();
foreach ($matchingPointcuts as $interceptor => $pointcut) {
if ($pointcut->matchesMethod($method)) {
$advices[] = $interceptor;
}
}
if (empty($advices)) {
continue;
}
$classAdvices[$method->name] = $advices;
}
if (empty($classAdvices)) {
return;
}
$interceptors[ClassUtils::getUserClass($class->name)] = $classAdvices;
$generator = new InterceptionGenerator();
$generator->setFilter(function(\ReflectionMethod $method) use ($classAdvices) {
return isset($classAdvices[$method->name]);
});
if ($file) {
$generator->setRequiredFile($file);
}
$enhancer = new Enhancer($class, array(), array(
$generator
));
$enhancer->writeClass($filename = $this->cacheDir.'/'.str_replace('\\', '-', $class->name).'.php');
$definition->setFile($filename);
$definition->setClass($enhancer->getClassName($class));
$definition->addMethodCall('__CGInterception__setLoader', array(
new Reference('jms_aop.interceptor_loader')
));
}
示例13: canDefinitionBeAutowired
/**
* @param string $serviceId
* @param Definition $definition
* @return boolean
*/
private function canDefinitionBeAutowired($serviceId, Definition $definition)
{
foreach ($this->getIgnoredServicePatterns() as $pattern) {
if ($pattern[0] === "/" && preg_match($pattern, $serviceId) || strcasecmp($serviceId, $pattern) == 0) {
return false;
}
}
if ($definition->isAbstract() || $definition->isSynthetic() || !$definition->isPublic() || !$definition->getClass() || $definition->getFactory() || $definition->getFactoryClass(false) || $definition->getFactoryService(false) || $definition->getFactoryMethod(false)) {
return false;
}
return true;
}
示例14: createService
/**
* Creates a service for a service definition.
*
* @param Definition $definition A service definition instance
* @param string $id The service identifier
*
* @return object The service described by the service definition
*
* @throws RuntimeException When the scope is inactive
* @throws RuntimeException When the factory definition is incomplete
* @throws RuntimeException When the service is a synthetic service
* @throws InvalidArgumentException When configure callable is not callable
*/
private function createService(Definition $definition, $id)
{
if ($definition->isSynthetic()) {
throw new RuntimeException(sprintf('You have requested a synthetic service ("%s"). The DIC does not know how to construct this service.', $id));
}
$parameterBag = $this->getParameterBag();
if (null !== $definition->getFile()) {
require_once $parameterBag->resolveValue($definition->getFile());
}
$arguments = $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())));
if (null !== $definition->getFactoryMethod()) {
if (null !== $definition->getFactoryClass()) {
$factory = $parameterBag->resolveValue($definition->getFactoryClass());
} elseif (null !== $definition->getFactoryService()) {
$factory = $this->get($parameterBag->resolveValue($definition->getFactoryService()));
} else {
throw new RuntimeException(sprintf('Cannot create service "%s" from factory method without a factory service or factory class.', $id));
}
$service = call_user_func_array(array($factory, $definition->getFactoryMethod()), $arguments);
} else {
$r = new \ReflectionClass($parameterBag->resolveValue($definition->getClass()));
$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);
}
if (self::SCOPE_PROTOTYPE !== ($scope = $definition->getScope())) {
if (self::SCOPE_CONTAINER !== $scope && !isset($this->scopedServices[$scope])) {
throw new RuntimeException(sprintf('You tried to create the "%s" service of an inactive scope.', $id));
}
$this->services[$lowerId = strtolower($id)] = $service;
if (self::SCOPE_CONTAINER !== $scope) {
$this->scopedServices[$scope][$lowerId] = $service;
}
}
foreach ($definition->getMethodCalls() as $call) {
$this->callMethod($service, $call);
}
$properties = $this->resolveServices($parameterBag->resolveValue($definition->getProperties()));
foreach ($properties as $name => $value) {
$service->{$name} = $value;
}
if ($callable = $definition->getConfigurator()) {
if (is_array($callable)) {
$callable[0] = $callable[0] instanceof Reference ? $this->get((string) $callable[0]) : $parameterBag->resolveValue($callable[0]);
}
if (!is_callable($callable)) {
throw new InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', get_class($service)));
}
call_user_func($callable, $service);
}
return $service;
}
示例15: processDefinition
/**
* @param array<PointcutInterface> $pointcuts
* @param array<string,string> $interceptors
*/
private function processDefinition(Definition $definition, $pointcuts, &$interceptors)
{
if ($definition->isSynthetic()) {
return;
}
if ($definition->getFactoryService() || $definition->getFactoryClass()) {
return;
}
if ($originalFilename = $definition->getFile()) {
require_once $originalFilename;
}
if (!class_exists($definition->getClass())) {
return;
}
$class = new \ReflectionClass($definition->getClass());
// check if class is matched
$matchingPointcuts = array();
foreach ($pointcuts as $interceptor => $pointcut) {
if ($pointcut->matchesClass($class)) {
$matchingPointcuts[$interceptor] = $pointcut;
}
}
if (empty($matchingPointcuts)) {
return;
}
$this->addResources($class, $this->container);
if ($class->isFinal()) {
return;
}
$classAdvices = array();
foreach (ReflectionUtils::getOverrideableMethods($class) as $method) {
if ('__construct' === $method->name) {
continue;
}
$advices = array();
foreach ($matchingPointcuts as $interceptor => $pointcut) {
if ($pointcut->matchesMethod($method)) {
$advices[] = $interceptor;
}
}
if (empty($advices)) {
continue;
}
$classAdvices[$method->name] = $advices;
}
if (empty($classAdvices)) {
return;
}
$interceptors[ClassUtils::getUserClass($class->name)] = $classAdvices;
$proxyFilename = $this->cacheDir . '/' . str_replace('\\', '-', $class->name) . '.php';
$generator = new InterceptionGenerator();
$generator->setFilter(function (\ReflectionMethod $method) use($classAdvices) {
return isset($classAdvices[$method->name]);
});
if ($originalFilename) {
$relativeOriginalFilename = $this->relativizePath($proxyFilename, $originalFilename);
if ($relativeOriginalFilename[0] === '.') {
$generator->setRequiredFile(new RelativePath($relativeOriginalFilename));
} else {
$generator->setRequiredFile($relativeOriginalFilename);
}
}
$enhancer = new Enhancer($class, array(), array($generator));
$enhancer->setNamingStrategy(new DefaultNamingStrategy('EnhancedProxy' . substr(md5($this->container->getParameter('jms_aop.cache_dir')), 0, 8)));
$enhancer->writeClass($proxyFilename);
$definition->setFile($proxyFilename);
$definition->setClass($enhancer->getClassName($class));
$definition->addMethodCall('__CGInterception__setLoader', array(new Reference('jms_aop.interceptor_loader')));
}