private function getAllParameters() { $service = new Definition(); $service->setClass('stdClass'); $service->setAbstract(true); $service->setFactoryClass('stdClass'); $service->setFactoryMethod('get'); $service->setFactoryService('service.fact'); $service->setFile('/file.php'); $service->setLazy(true); $service->addMethodCall('methodCall1'); $service->addMethodCall('methodCall2', array('arg1', 'arg2')); $service->addMethodCall('methodCall3', array(new Reference('arg.one'))); $service->setProperty('prop1', 'val1'); $service->setProperty('prop2', new Reference('prop.one')); $service->setPublic(false); $service->setScope('request'); $service->setSynchronized(true); $service->setSynthetic(true); return new ServiceElement('service.two', $service); }
private function convertObjectDefinition(ContainerBuilder $container, ObjectDefinitionInterface $definition) { $symfonyDefinition = new Definition(); $symfonyDefinition->setClass($definition->getClassName()); $symfonyDefinition->setArguments($this->convertArguments($container, $definition->getConstructorArguments())); foreach ($definition->getPropertyAssignments() as $propertyAssignment) { $symfonyDefinition->setProperty($propertyAssignment->getPropertyName(), $this->convertArguments($container, $propertyAssignment->getValue())); } foreach ($definition->getMethodCalls() as $methodCall) { $symfonyDefinition->addMethodCall($methodCall->getMethodName(), $this->convertArguments($container, $methodCall->getArguments())); } return $symfonyDefinition; }
/** * @param ContainerBuilder $container * @param $config */ protected function configureReader(ContainerBuilder $container, $config) { $container->setParameter('evlz_php_exif.type', $config['type']); foreach (array('native', 'exiftool') as $reader) { if ($container->hasParameter('evlz_php_exif.reader.' . $reader . '.class')) { $class = $container->getParameter('evlz_php_exif.reader.' . $reader . '.class'); $definition = new Definition($class); $definition->setProperty('private', true); $container->setDefinition('evlz_php_exif.reader.' . $reader, $definition); } } $container->setAlias('evlz_php_exif.reader.default', 'evlz_php_exif.reader.' . $container->getParameter('evlz_php_exif.type')); }
/** * {@inheritDoc} */ public function load(array $configs, ContainerBuilder $container) { $this->container = $container; $configuration = new Configuration(); $this->config = $this->processConfiguration($configuration, $configs); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $loader->load('services.yml'); // create pipeline definition and store it into a PipelineProvider $pipeline = (new PipelineFactory())->buildPipeline($this->config); $pipelineProviderDefinition = new Definition('Wizbii\\PipelineBundle\\Service\\PipelineProvider', [$this->config]); $container->setDefinition("pipeline.provider", $pipelineProviderDefinition); // Load Connection $this->loadConnection(); // create global producer $internalProducerDefinition = new Definition("%pipeline.producer.class%"); $pipelineExchangeName = $pipeline->getName(); $internalProducerDefinition->addTag('old_sound_rabbit_mq.base_amqp')->addTag('old_sound_rabbit_mq.producer')->addMethodCall('setExchangeOptions', [["name" => $pipelineExchangeName, "type" => "direct", "passive" => false, "durable" => true]])->addMethodCall('setQueueOptions', [["name" => null]])->addArgument(new Reference('old_sound_rabbit_mq.connection.default'))->setProperty("logger", new Reference("monolog.logger.pipeline")); $internalProducerId = 'old_sound_rabbit_mq.internal_pipeline_producer'; $this->container->setDefinition($internalProducerId, $internalProducerDefinition); // create event consumers for each incoming event foreach ($pipeline->getIncomingEvents() as $event) { $frontConsumerDefinition = new Definition("%pipeline.consumer.front.class%"); $frontConsumerDefinition->setProperty("eventName", $event->getName())->setProperty("producer", new Reference($internalProducerId))->setPublic(false); $frontConsumerId = sprintf('pipeline.consumer.front.%s_consumer', $event->getName()); $this->container->setDefinition($frontConsumerId, $frontConsumerDefinition); $amqpConsumer = new Definition('%pipeline.consumer.command.class%'); $amqpConsumer->addTag('old_sound_rabbit_mq.base_amqp')->addTag('old_sound_rabbit_mq.consumer')->addTag('pipeline.front.consumer')->addMethodCall('setExchangeOptions', [["name" => $event->getName(), "type" => "direct"]])->addMethodCall('setQueueOptions', [["name" => $event->getName()]])->addMethodCall('setCallback', [[new Reference($frontConsumerId), "execute"]])->addArgument(new Reference('old_sound_rabbit_mq.connection.default')); $name = sprintf('old_sound_rabbit_mq.%s_consumer', $event->getName()); $this->container->setDefinition($name, $amqpConsumer); } // create event producers for each outgoing event foreach ($pipeline->getOutgoingEvents() as $event) { $producerDefinition = new Definition("%pipeline.producer.class%"); $producerDefinition->setProperty("logger", new Reference("monolog.logger.pipeline"))->addTag('old_sound_rabbit_mq.producer')->addTag('pipeline.back.producer')->addMethodCall('setExchangeOptions', [["name" => $event->getName(), "type" => "direct", "passive" => false, "durable" => true]])->addMethodCall('setQueueOptions', [["name" => null]])->addArgument(new Reference('old_sound_rabbit_mq.connection.default')); $producerId = sprintf('pipeline.producer.%s', $event->getName()); $this->container->setDefinition($producerId, $producerDefinition); } // create backend consumer $pipelineQueueName = $pipeline->getName(); $backConsumer = new Definition('%old_sound_rabbit_mq.consumer.class%'); $backConsumer->addTag('old_sound_rabbit_mq.base_amqp')->addTag('old_sound_rabbit_mq.consumer')->addMethodCall('setExchangeOptions', [["name" => $pipelineExchangeName, "type" => "direct"]])->addMethodCall('setQueueOptions', [["name" => $pipelineQueueName]])->addMethodCall('setCallback', [[new Reference("pipeline.consumer.back"), "execute"]])->addArgument(new Reference('old_sound_rabbit_mq.connection.default')); $this->container->setDefinition("old_sound_rabbit_mq.pipeline_back_consumer", $backConsumer); }
/** * Resolves the definition. * * @param ContainerBuilder $container The ContainerBuilder * @param DefinitionDecorator $definition * * @return Definition * * @throws \RuntimeException When the definition is invalid */ private function resolveDefinition(ContainerBuilder $container, DefinitionDecorator $definition) { if (!$container->hasDefinition($parent = $definition->getParent())) { throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $this->currentId)); } $parentDef = $container->getDefinition($parent); if ($parentDef instanceof DefinitionDecorator) { $id = $this->currentId; $this->currentId = $parent; $parentDef = $this->resolveDefinition($container, $parentDef); $container->setDefinition($parent, $parentDef); $this->currentId = $id; } $this->compiler->addLogMessage($this->formatter->formatResolveInheritance($this, $this->currentId, $parent)); $def = new Definition(); // merge in parent definition // purposely ignored attributes: scope, abstract, tags $def->setClass($parentDef->getClass()); $def->setArguments($parentDef->getArguments()); $def->setMethodCalls($parentDef->getMethodCalls()); $def->setProperties($parentDef->getProperties()); $def->setAutowiringTypes($parentDef->getAutowiringTypes()); if ($parentDef->getFactoryClass(false)) { $def->setFactoryClass($parentDef->getFactoryClass(false)); } if ($parentDef->getFactoryMethod(false)) { $def->setFactoryMethod($parentDef->getFactoryMethod(false)); } if ($parentDef->getFactoryService(false)) { $def->setFactoryService($parentDef->getFactoryService(false)); } if ($parentDef->isDeprecated()) { $def->setDeprecated(true, $parentDef->getDeprecationMessage('%service_id%')); } $def->setFactory($parentDef->getFactory()); $def->setConfigurator($parentDef->getConfigurator()); $def->setFile($parentDef->getFile()); $def->setPublic($parentDef->isPublic()); $def->setLazy($parentDef->isLazy()); // overwrite with values specified in the decorator $changes = $definition->getChanges(); if (isset($changes['class'])) { $def->setClass($definition->getClass()); } if (isset($changes['factory_class'])) { $def->setFactoryClass($definition->getFactoryClass(false)); } if (isset($changes['factory_method'])) { $def->setFactoryMethod($definition->getFactoryMethod(false)); } if (isset($changes['factory_service'])) { $def->setFactoryService($definition->getFactoryService(false)); } if (isset($changes['factory'])) { $def->setFactory($definition->getFactory()); } if (isset($changes['configurator'])) { $def->setConfigurator($definition->getConfigurator()); } if (isset($changes['file'])) { $def->setFile($definition->getFile()); } if (isset($changes['public'])) { $def->setPublic($definition->isPublic()); } if (isset($changes['lazy'])) { $def->setLazy($definition->isLazy()); } if (isset($changes['deprecated'])) { $def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%')); } if (isset($changes['decorated_service'])) { $decoratedService = $definition->getDecoratedService(); if (null === $decoratedService) { $def->setDecoratedService($decoratedService); } else { $def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2]); } } // merge arguments foreach ($definition->getArguments() as $k => $v) { if (is_numeric($k)) { $def->addArgument($v); continue; } if (0 !== strpos($k, 'index_')) { throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k)); } $index = (int) substr($k, strlen('index_')); $def->replaceArgument($index, $v); } // merge properties foreach ($definition->getProperties() as $k => $v) { $def->setProperty($k, $v); } // append method calls if (count($calls = $definition->getMethodCalls()) > 0) { $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls)); } // merge autowiring types foreach ($definition->getAutowiringTypes() as $autowiringType) { $def->addAutowiringType($autowiringType); } // these attributes are always taken from the child $def->setAbstract($definition->isAbstract()); $def->setScope($definition->getScope(false), false); $def->setShared($definition->isShared()); $def->setTags($definition->getTags()); return $def; }
/** * Resolves the definition * * @param string $id The definition identifier * @param DefinitionDecorator $definition * * @return Definition */ private function resolveDefinition($id, DefinitionDecorator $definition) { if (!$this->container->hasDefinition($parent = $definition->getParent())) { throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $id)); } $parentDef = $this->container->getDefinition($parent); if ($parentDef instanceof DefinitionDecorator) { $parentDef = $this->resolveDefinition($parent, $parentDef); } $this->compiler->addLogMessage($this->formatter->formatResolveInheritance($this, $id, $parent)); $def = new Definition(); // merge in parent definition // purposely ignored attributes: scope, abstract, tags $def->setClass($parentDef->getClass()); $def->setArguments($parentDef->getArguments()); $def->setMethodCalls($parentDef->getMethodCalls()); $def->setProperties($parentDef->getProperties()); $def->setFactoryClass($parentDef->getFactoryClass()); $def->setFactoryMethod($parentDef->getFactoryMethod()); $def->setFactoryService($parentDef->getFactoryService()); $def->setConfigurator($parentDef->getConfigurator()); $def->setFile($parentDef->getFile()); $def->setPublic($parentDef->isPublic()); // overwrite with values specified in the decorator $changes = $definition->getChanges(); if (isset($changes['class'])) { $def->setClass($definition->getClass()); } if (isset($changes['factory_class'])) { $def->setFactoryClass($definition->getFactoryClass()); } if (isset($changes['factory_method'])) { $def->setFactoryMethod($definition->getFactoryMethod()); } if (isset($changes['factory_service'])) { $def->setFactoryService($definition->getFactoryService()); } if (isset($changes['configurator'])) { $def->setConfigurator($definition->getConfigurator()); } if (isset($changes['file'])) { $def->setFile($definition->getFile()); } if (isset($changes['public'])) { $def->setPublic($definition->isPublic()); } // merge arguments foreach ($definition->getArguments() as $k => $v) { if (is_numeric($k)) { $def->addArgument($v); continue; } if (0 !== strpos($k, 'index_')) { throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k)); } $index = (int) substr($k, strlen('index_')); $def->replaceArgument($index, $v); } // merge properties foreach ($definition->getProperties() as $k => $v) { $def->setProperty($k, $v); } // append method calls if (count($calls = $definition->getMethodCalls()) > 0) { $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls)); } // these attributes are always taken from the child $def->setAbstract($definition->isAbstract()); $def->setScope($definition->getScope()); $def->setTags($definition->getTags()); // set new definition on container $this->container->setDefinition($id, $def); return $def; }
/** * @param Bean $bean * @return mixed */ public function create(Bean $bean) { $useConfigurator = true; $originalClass = $class = ltrim($bean->class, '\\'); if ($bean->factoryMethod) { // We don't have a clue what what the real class is, fake it and hope nothing breaks; $class = "stdClass"; } else { if (class_exists($class)) { $rClass = new \ReflectionClass($class); if (!$rClass->implementsInterface('MooDev\\Bounce\\Config\\Configurable')) { // The class definitely doesn't need the configurator, so we can disable it. $useConfigurator = false; } } } $usesLookupMethods = false; if ($bean->lookupMethods) { if (!$this->proxyGeneratorFactory) { throw new BounceException("Proxy generator not configured, cannot use lookup-method"); } // If we have lookup methods then the class is actually a generated proxy. $class = ltrim($this->proxyGeneratorFactory->loadProxy($bean), '\\'); $usesLookupMethods = true; } $def = new Definition($class); if ($usesLookupMethods) { // The proxy will take an additional, first, constructor arg which is expected to be a bean factory. $def->addArgument($this->getBeanFactory()); } if ($useConfigurator) { // We use the configurator if we know the class of the bean and it implements Configurable // or if we have no idea what the class of the bean is (there's a factory method.) $def->setConfigurator($this->getConfigurator()); } if ($bean->scope) { // This is getting killed off in Symfony 3. Sigh. // TODO: deal with Symfony 3. switch ($bean->scope) { case "singleton": $def->setScope(ContainerBuilder::SCOPE_CONTAINER); break; case "prototype": $def->setScope(ContainerBuilder::SCOPE_PROTOTYPE); break; default: $def->setScope($bean->scope); } } foreach ($bean->constructorArguments as $constructorArgument) { $def->addArgument($this->convertValueProviderToValue($constructorArgument)); } foreach ($bean->properties as $name => $property) { // TODO: Could support setter injection using Reflection here? $def->setProperty($name, $this->convertValueProviderToValue($property)); } if ($bean->factoryBean) { $def->setFactoryService($bean->factoryBean); $def->setFactoryMethod($bean->factoryMethod); } elseif ($bean->factoryMethod) { $def->setFactoryClass($originalClass); $def->setFactoryMethod($bean->factoryMethod); } return $def; }
/** * Set a property on the service. * * @param string $name The property name. * @param mixed $value The property value. * * @api * @since 4.0.0 */ public function setProperty($name, $value) { $this->_underlyingSymfonyDefinition->setProperty($name, $value); return $this; }
/** * @param string $className * @param ReflectionClass $reflectionClass * @param Definition $definition * @param string $fastAnnotationChecksRegex * @param string[] $preferredServices * @param ParameterBagInterface $parameterBag */ private function autowireClass($className, ReflectionClass $reflectionClass, Definition $definition, $fastAnnotationChecksRegex, $preferredServices, ParameterBagInterface $parameterBag) { // constructor - autowire always if ($reflectionClass->getConstructor()) { $definition->setArguments($this->autowireMethod($className, $reflectionClass->getConstructor(), $definition->getArguments(), $preferredServices)); } if ($fastAnnotationChecksRegex === null || $reflectionClass->getDocComment() && preg_match($fastAnnotationChecksRegex, $reflectionClass->getDocComment())) { // method calls @Autowired foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { if ($reflectionMethod->getName() === "__construct") { continue; } if ($definition->hasMethodCall($reflectionMethod->getName())) { continue; } if (strpos($reflectionMethod->getDocComment(), "@Autowired") === false) { continue; } /** @var Autowired $annotation */ $annotation = $this->annotationReader->getMethodAnnotation($reflectionMethod, "Skrz\\Bundle\\AutowiringBundle\\Annotation\\Autowired"); if ($annotation === null) { continue; } if ($annotation->name !== null) { throw new AutowiringException(sprintf("@Autowired parameter can be used only on properties. %s::%s(...)", $className, $reflectionMethod->getName())); } $definition->addMethodCall($reflectionMethod->getName(), $this->autowireMethod($className, $reflectionMethod, [], $preferredServices)); } // properties @Autowired, @Value $manualProperties = $definition->getProperties(); foreach ($reflectionClass->getProperties() as $reflectionProperty) { if (isset($manualProperties[$reflectionProperty->getName()])) { continue; } if (strpos($reflectionProperty->getDocComment(), "@Autowired") === false && strpos($reflectionProperty->getDocComment(), "@Value") === false) { continue; } $annotations = $this->annotationReader->getPropertyAnnotations($reflectionProperty); $autowiredAnnotation = false; $valueAnnotation = false; $incorrectUsage = false; foreach ($annotations as $annotation) { if ($annotation instanceof Autowired) { if ($valueAnnotation) { $incorrectUsage = true; break; } $autowiredAnnotation = true; try { if ($annotation->name !== null) { $definition->setProperty($reflectionProperty->getName(), new Reference($annotation->name)); } else { $definition->setProperty($reflectionProperty->getName(), $this->getValue($reflectionProperty->getDeclaringClass(), $reflectionProperty->getDocComment(), null, null, false, null, $preferredServices)); } } catch (AutowiringException $exception) { throw new AutowiringException(sprintf("%s (Property %s::\$%s)", $exception->getMessage(), $className, $reflectionProperty->getName()), $exception->getCode(), $exception); } } elseif ($annotation instanceof Value) { if ($autowiredAnnotation) { $incorrectUsage = true; break; } try { $definition->setProperty($reflectionProperty->getName(), $parameterBag->resolveValue($annotation->value)); } catch (RuntimeException $exception) { throw new AutowiringException(sprintf("%s (Property %s::\$%s)", $exception->getMessage(), $className, $reflectionProperty->getName()), $exception->getCode(), $exception); } } } if ($incorrectUsage) { throw new AutowiringException(sprintf("Property can have either @Autowired, or @Value annotation, not both. (Property %s::\$%s)", $className, $reflectionProperty->getName())); } } } }