Sets whether this definition is synthetic, that is not constructed by the
container, but dynamically injected.
public setSynthetic ( boolean $boolean ) : |
||
$boolean | boolean | |
리턴 | the current instance |
/** * Ensures that the creator returns null if a synthetic service is requested. */ public function testCreatorReturnsNullIfSyntheticServiceIsRequested() { $definition = new Definition(); $definition->setSynthetic(true); $this->container->setDefinition('my.service', $definition); $this->assertNull($this->creator->create('my.service')); }
/** * @test */ public function ifDefinitionIsSyntheticSkipsValidation() { $definition = new Definition(); $definition->setSynthetic(true); $argumentsValidator = $this->createMockArgumentsValidator(); $argumentsValidator->expects($this->never())->method('validate'); $validator = new DefinitionArgumentsValidator($this->createMockConstructorResolver(), $argumentsValidator); $validator->validate($definition); }
/** * test every new and overrided method provided by BackBee\DependencyInjection\Container. * * @covers ::get * @covers ::getContainerValues * @covers ::getContainerParameters * @covers ::getContainerServices * @covers ::isLoaded */ public function testContainerOverridedAndNewMethods() { // build container to apply every tests on $container = new Container(); // setting random parameters $container->setParameter('random_parameter', 'this is a test'); // creating a definition without any tag (definition of service with id `date_without_tag`) $definition = new Definition('DateTime'); $container->setDefinition('date_without_tag', $definition); // creating a definition with one tag, `test` (definition of service with id `date_with_tag`) $definition = new Definition('BackBee\\DependencyInjection\\Tests\\ContainerTest_Resources\\DateTime'); $definition->addTag('test'); $container->setDefinition('date_with_tag', $definition); // creating a definition for a synthetic service (definition of service with id `synthetic_service`) $definition = new Definition(); $definition->setSynthetic(true); $container->setDefinition('synthetic_service', $definition); // setting into container a service as listener with id `listener` $container->set('listener', new TestListener()); // creating a new event dispatcher and prepare it for testing the dispatch of event on get of tagged service // from the container $event_dispatcher = new Dispatcher(); $event_dispatcher->setContainer($container); $event_dispatcher->addListener('service.tagged.test', array('@listener', 'onGetServiceTaggedTestEvent')); $container->set('event.dispatcher', $event_dispatcher); // basic test for the listener $this->assertEquals('bar', $container->get('listener')->getFoo()); // tests of Container::isLoaded() method $this->assertFalse($container->isLoaded('date_without_tag')); $container->get('date_without_tag'); $this->assertTrue($container->isLoaded('date_without_tag')); // checks that getting a service without the `test` tag won't change listener's foo value $this->assertEquals('bar', $container->get('listener')->getFoo()); // tests to get a service with tag to check if the value of foo from listener has changed // and if the timestamp of the service `date_with_tag` has changed correctly $container->get('date_with_tag'); $this->assertEquals('foo', $container->get('listener')->getFoo()); $this->assertEquals(self::NEW_DATE_WITH_TAG_VALUE, $container->get('date_with_tag')->getTimestamp()); // tests that if we get a synthetic service which we didn't define it yet Container::get() will return null $this->assertEquals(null, $container->get('synthetic_service')); // test for Container::getContainerValues() $paramter_key_string = '%random_parameter%'; $this->assertEquals('this is a test', $container->getContainerValues($paramter_key_string)); $service_string_id = '@listener'; $this->assertEquals($container->get('listener')->getFoo(), $container->getContainerValues($service_string_id)->getFoo()); // Test that there is no events to dispatch if the tag name is `dumpable` $container->set('listener', new TestListener()); $event_dispatcher->addListener('service.tagged.dumpable', array('@listener', 'onGetServiceTaggedTestEvent')); $definition = new Definition('DateTime'); $definition->addTag('dumpable', array('dispatch_event' => false)); $container->setDefinition('dumpable_date', $definition); $container->get('dumpable_date'); $this->assertEquals('bar', $container->get('listener')->getFoo()); }
public function testSyntheticDefinitionCanHaveNoClass() { $definition = new Definition(); $definition->setSynthetic(true); $containerBuilder = new ContainerBuilder(); $validator = new ServiceDefinitionValidator($containerBuilder, $this->createMockDefinitionArgumentsValidator(), $this->createMockMethodCallsValidator()); try { $validator->validate($definition); } catch (DefinitionHasNoClassException $e) { $this->fail('Synthetic definitions should be allowed to have no class'); } }
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); }
/** * Parses a definition. * * @param string $id * @param array $service * @param string $file * * @throws InvalidArgumentException When tags are invalid */ private function parseDefinition($id, $service, $file) { if (is_string($service) && 0 === strpos($service, '@')) { $this->container->setAlias($id, substr($service, 1)); return; } if (!is_array($service)) { throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but %s found for service "%s" in %s. Check your YAML syntax.', gettype($service), $id, $file)); } static::checkDefinition($id, $service, $file); if (isset($service['alias'])) { $public = !array_key_exists('public', $service) || (bool) $service['public']; $this->container->setAlias($id, new Alias($service['alias'], $public)); foreach ($service as $key => $value) { if (!in_array($key, array('alias', 'public'))) { @trigger_error(sprintf('The configuration key "%s" is unsupported for alias definition "%s" in "%s". Allowed configuration keys are "alias" and "public". The YamlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported attributes.', $key, $id, $file), E_USER_DEPRECATED); } } return; } if (isset($service['parent'])) { $definition = new DefinitionDecorator($service['parent']); } else { $definition = new Definition(); } if (isset($service['class'])) { $definition->setClass($service['class']); } if (isset($service['shared'])) { $definition->setShared($service['shared']); } if (isset($service['synthetic'])) { $definition->setSynthetic($service['synthetic']); } if (isset($service['lazy'])) { $definition->setLazy($service['lazy']); } if (isset($service['public'])) { $definition->setPublic($service['public']); } if (isset($service['abstract'])) { $definition->setAbstract($service['abstract']); } if (array_key_exists('deprecated', $service)) { $definition->setDeprecated(true, $service['deprecated']); } if (isset($service['factory'])) { $definition->setFactory($this->parseCallable($service['factory'], 'factory', $id, $file)); } if (isset($service['file'])) { $definition->setFile($service['file']); } if (isset($service['arguments'])) { $definition->setArguments($this->resolveServices($service['arguments'])); } if (isset($service['properties'])) { $definition->setProperties($this->resolveServices($service['properties'])); } if (isset($service['configurator'])) { $definition->setConfigurator($this->parseCallable($service['configurator'], 'configurator', $id, $file)); } if (isset($service['calls'])) { if (!is_array($service['calls'])) { throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); } foreach ($service['calls'] as $call) { if (isset($call['method'])) { $method = $call['method']; $args = isset($call['arguments']) ? $this->resolveServices($call['arguments']) : array(); } else { $method = $call[0]; $args = isset($call[1]) ? $this->resolveServices($call[1]) : array(); } $definition->addMethodCall($method, $args); } } if (isset($service['tags'])) { if (!is_array($service['tags'])) { throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); } foreach ($service['tags'] as $tag) { if (!is_array($tag)) { throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); } if (!isset($tag['name'])) { throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file)); } if (!is_string($tag['name']) || '' === $tag['name']) { throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file)); } $name = $tag['name']; unset($tag['name']); foreach ($tag as $attribute => $value) { if (!is_scalar($value) && null !== $value) { throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.', $id, $name, $attribute, $file)); } } $definition->addTag($name, $tag); } } if (isset($service['decorates'])) { if ('' !== $service['decorates'] && '@' === $service['decorates'][0]) { throw new InvalidArgumentException(sprintf('The value of the "decorates" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['decorates'], substr($service['decorates'], 1))); } $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null; $priority = isset($service['decoration_priority']) ? $service['decoration_priority'] : 0; $definition->setDecoratedService($service['decorates'], $renameId, $priority); } if (isset($service['autowire'])) { $definition->setAutowired($service['autowire']); } if (isset($service['autowiring_types'])) { if (is_string($service['autowiring_types'])) { $definition->addAutowiringType($service['autowiring_types']); } else { if (!is_array($service['autowiring_types'])) { throw new InvalidArgumentException(sprintf('Parameter "autowiring_types" must be a string or an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); } foreach ($service['autowiring_types'] as $autowiringType) { if (!is_string($autowiringType)) { throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file)); } $definition->addAutowiringType($autowiringType); } } } $this->container->setDefinition($id, $definition); }
/** * @param array $loadedApplications * * @return ApplicationContainerBuilder */ public function getApplicationContainer(&$loadedApplications = array()) { if (!$this->containerBuilderCompiled) { if (count($loadedApplications) == 0) { $parent = true; } else { $parent = false; } foreach ($this->parentApplications as $app) { if (in_array(get_class($app), $loadedApplications)) { continue; } $loadedApplications[] = get_class($app); $this->containerBuilder->merge($app->getApplicationContainer($loadedApplications)); } $this->configure($this->containerBuilder); foreach ($this->getConfigurationFiles() as $file) { $this->containerBuilder->import($file); } if ($parent) { //setting syntetic services $definition = new Definition(); $definition->setSynthetic(true); //inserting container as service $this->containerBuilder->setDefinition('container', $definition); $this->containerBuilder->set('container', $this->containerBuilder); //inserting application as service $this->containerBuilder->setDefinition('application', clone $definition); $this->containerBuilder->set('application', $this); $this->containerBuilder->compile(); } } return $this->containerBuilder; }
private function createSyntheticDefinition() { $syntheticDefinition = new Definition(); $syntheticDefinition->setSynthetic(true); return $syntheticDefinition; }
/** * Parses a definition. * * @param string $id * @param array $service * @param string $file * * @throws InvalidArgumentException When tags are invalid */ private function parseDefinition($id, $service, $file) { if (is_string($service) && 0 === strpos($service, '@')) { $this->container->setAlias($id, substr($service, 1)); return; } if (!is_array($service)) { throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but %s found for service "%s" in %s. Check your YAML syntax.', gettype($service), $id, $file)); } if (isset($service['alias'])) { $public = !array_key_exists('public', $service) || (bool) $service['public']; $this->container->setAlias($id, new Alias($service['alias'], $public)); return; } if (isset($service['parent'])) { $definition = new DefinitionDecorator($service['parent']); } else { $definition = new Definition(); } if (isset($service['class'])) { $definition->setClass($service['class']); } if (isset($service['scope'])) { $definition->setScope($service['scope']); } if (isset($service['synthetic'])) { $definition->setSynthetic($service['synthetic']); } if (isset($service['synchronized'])) { $definition->setSynchronized($service['synchronized']); } if (isset($service['lazy'])) { $definition->setLazy($service['lazy']); } if (isset($service['public'])) { $definition->setPublic($service['public']); } if (isset($service['abstract'])) { $definition->setAbstract($service['abstract']); } if (isset($service['factory'])) { if (is_string($service['factory'])) { if (strpos($service['factory'], ':') !== false && strpos($service['factory'], '::') === false) { $parts = explode(':', $service['factory']); $definition->setFactory(array($this->resolveServices('@' . $parts[0]), $parts[1])); } else { $definition->setFactory($service['factory']); } } else { $definition->setFactory(array($this->resolveServices($service['factory'][0]), $service['factory'][1])); } } if (isset($service['factory_class'])) { $definition->setFactoryClass($service['factory_class']); } if (isset($service['factory_method'])) { $definition->setFactoryMethod($service['factory_method']); } if (isset($service['factory_service'])) { $definition->setFactoryService($service['factory_service']); } if (isset($service['file'])) { $definition->setFile($service['file']); } if (isset($service['arguments'])) { $definition->setArguments($this->resolveServices($service['arguments'])); } if (isset($service['properties'])) { $definition->setProperties($this->resolveServices($service['properties'])); } if (isset($service['configurator'])) { if (is_string($service['configurator'])) { $definition->setConfigurator($service['configurator']); } else { $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1])); } } if (isset($service['calls'])) { if (!is_array($service['calls'])) { throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); } foreach ($service['calls'] as $call) { $args = isset($call[1]) ? $this->resolveServices($call[1]) : array(); $definition->addMethodCall($call[0], $args); } } if (isset($service['tags'])) { if (!is_array($service['tags'])) { throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); } foreach ($service['tags'] as $tag) { if (!is_array($tag)) { throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); } if (!isset($tag['name'])) { throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file)); } $name = $tag['name']; unset($tag['name']); foreach ($tag as $attribute => $value) { if (!is_scalar($value) && null !== $value) { throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.', $id, $name, $attribute, $file)); } } $definition->addTag($name, $tag); } } if (isset($service['decorates'])) { $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null; $definition->setDecoratedService($service['decorates'], $renameId); } $this->container->setDefinition($id, $definition); }
/** * Initializes service container * @see http://symfony.com/doc/current/components/dependency_injection/index.html */ protected function initializeContainer() { $container = new ContainerBuilder(); $clientDefinition = new Definition(); $clientDefinition->setSynthetic(true); $container->setDefinition('client', $clientDefinition); $container->set('client', $this); $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/Resources/config')); $loader->load('services.yml'); $loader->load('api_services.yml'); $loader->load('mapper_services.yml'); $this->container = $container; }
/** * You can modify the container here before it is dumped to PHP code. * * @param ContainerBuilder $container * * @api */ public function process(ContainerBuilder $container) { $twigDefinition = new Definition('Twig_Environment'); $twigDefinition->setSynthetic(true); $container->setDefinition('twig', $twigDefinition); }
public function testHasSyntheticService() { $this->given($definition = new DependencyInjection\Definition('stdClass'), $definition->setSynthetic(false), $syntheticDef = new DependencyInjection\Definition('stdClass'), $syntheticDef->setSynthetic(true), $container = new DependencyInjection\ContainerBuilder(), $container->addDefinitions(['service' => $definition, 'serviceSynthetic' => $syntheticDef]))->if($asserter = $this->newTestedInstance())->and($asserter->setWith($container))->then->object($asserter->hasSyntheticService('serviceSynthetic'))->isIdenticalTo($asserter)->exception(function () use($asserter) { $asserter->hasSyntheticService('service'); })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage('Service "service" is not synthetic'); }
/** * Build a definition from the definition's array provided as current method parameter. * * @param array $array the raw definition's array * * @return Symfony\Component\DependencyInjection\Definition the definition object */ private function buildDefinition(array $array) { $definition = null; if (isset($array['parent'])) { $definition = new DefinitionDecorator($array['parent']); } else { $definition = new Definition(); } if (isset($array['synthetic'])) { $definition->setSynthetic($array['synthetic']); } $this->setDefinitionClass($definition, $array); $this->setDefinitionArguments($definition, $array); $this->setDefinitionFactory($definition, $array); $this->setDefinitionTags($definition, $array); $this->setDefinitionMethodCalls($definition, $array); $this->setDefinitionProperties($definition, $array); $this->setDefinitionConfigurator($definition, $array); return $definition; }
protected function parseDefinition($id, $service, $file) { if (is_string($service) && 0 === strpos($service, '@')) { $this->container->setAlias($id, substr($service, 1)); return; } else { if (isset($service['alias'])) { $public = !array_key_exists('public', $service) || (bool) $service['public']; $this->container->setAlias($id, new Alias($service['alias'], $public)); return; } } if (isset($service['parent'])) { $definition = new DefinitionDecorator($service['parent']); } else { $definition = new Definition(); } if (isset($service['class'])) { $definition->setClass($service['class']); } if (isset($service['scope'])) { $definition->setScope($service['scope']); } if (isset($service['synthetic'])) { $definition->setSynthetic($service['synthetic']); } if (isset($service['public'])) { $definition->setPublic($service['public']); } if (isset($service['abstract'])) { $definition->setAbstract($service['abstract']); } if (isset($service['factory_method'])) { $definition->setFactoryMethod($service['factory_method']); } if (isset($service['factory_service'])) { $definition->setFactoryService($service['factory_service']); } if (isset($service['file'])) { $definition->setFile($service['file']); } if (isset($service['arguments'])) { $definition->setArguments($this->resolveServices($service['arguments'])); } if (isset($service['configurator'])) { if (is_string($service['configurator'])) { $definition->setConfigurator($service['configurator']); } else { $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1])); } } if (isset($service['calls'])) { foreach ($service['calls'] as $call) { $definition->addMethodCall($call[0], $this->resolveServices($call[1])); } } if (isset($service['tags'])) { foreach ($service['tags'] as $tag) { $name = $tag['name']; unset($tag['name']); $definition->addTag($name, $tag); } } $this->container->setDefinition($id, $definition); }
public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer() { $container = new ContainerBuilder(); $def = new Definition('stdClass'); $def->setSynthetic(true); $container->setDefinition('a', $def); $container->compile(); $container->set('a', $a = new \stdClass()); $this->assertEquals($a, $container->get('a')); }
/** * Parses a definition. * * @param string $id * @param array $service * @param string $file * * @throws InvalidArgumentException When tags are invalid */ private function parseDefinition($id, $service, $file) { if (is_string($service) && 0 === strpos($service, '@')) { $this->container->setAlias($id, substr($service, 1)); return; } elseif (isset($service['alias'])) { $public = !array_key_exists('public', $service) || (Boolean) $service['public']; $this->container->setAlias($id, new Alias($service['alias'], $public)); return; } if (isset($service['parent'])) { $definition = new DefinitionDecorator($service['parent']); } else { $definition = new Definition(); } if (isset($service['class'])) { $definition->setClass($service['class']); } if (isset($service['scope'])) { $definition->setScope($service['scope']); } if (isset($service['synthetic'])) { $definition->setSynthetic($service['synthetic']); } if (isset($service['public'])) { $definition->setPublic($service['public']); } if (isset($service['abstract'])) { $definition->setAbstract($service['abstract']); } if (isset($service['factory_class'])) { $definition->setFactoryClass($service['factory_class']); } if (isset($service['factory_method'])) { $definition->setFactoryMethod($service['factory_method']); } if (isset($service['factory_service'])) { $definition->setFactoryService($service['factory_service']); } if (isset($service['file'])) { $definition->setFile($service['file']); } if (isset($service['arguments'])) { $definition->setArguments($this->resolveServices($service['arguments'])); } if (isset($service['properties'])) { $definition->setProperties($this->resolveServices($service['properties'])); } if (isset($service['configurator'])) { if (is_string($service['configurator'])) { $definition->setConfigurator($service['configurator']); } else { $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1])); } } if (isset($service['calls'])) { foreach ($service['calls'] as $call) { $args = isset($call[1]) ? $this->resolveServices($call[1]) : array(); $definition->addMethodCall($call[0], $args); } } if (isset($service['tags'])) { if (!is_array($service['tags'])) { throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s.', $id, $file)); } foreach ($service['tags'] as $tag) { if (!isset($tag['name'])) { throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file)); } $name = $tag['name']; unset($tag['name']); foreach ($tag as $attribute => $value) { if (!is_scalar($value)) { throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s" in %s.', $id, $name, $file)); } } $definition->addTag($name, $tag); } } $this->container->setDefinition($id, $definition); }
/** * Parses a definition. * * @param string $id * @param array $service * @param string $file * * @throws InvalidArgumentException When tags are invalid */ private function parseDefinition($id, $service, $file) { if (is_string($service) && 0 === strpos($service, '@')) { $this->container->setAlias($id, substr($service, 1)); return; } if (!is_array($service)) { throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but %s found for service "%s" in %s. Check your YAML syntax.', gettype($service), $id, $file)); } if (isset($service['alias'])) { $public = !array_key_exists('public', $service) || (bool) $service['public']; $this->container->setAlias($id, new Alias($service['alias'], $public)); return; } if (isset($service['parent'])) { $definition = new DefinitionDecorator($service['parent']); } else { $definition = new Definition(); } if (isset($service['class'])) { $definition->setClass($service['class']); } if (isset($service['scope'])) { $definition->setScope($service['scope']); } if (isset($service['synthetic'])) { $definition->setSynthetic($service['synthetic']); } if (isset($service['synchronized'])) { @trigger_error(sprintf('The "synchronized" key of service "%s" in file "%s" is deprecated since version 2.7 and will be removed in 3.0.', $id, $file), E_USER_DEPRECATED); $definition->setSynchronized($service['synchronized'], 'request' !== $id); } if (isset($service['lazy'])) { $definition->setLazy($service['lazy']); } if (isset($service['public'])) { $definition->setPublic($service['public']); } if (isset($service['abstract'])) { $definition->setAbstract($service['abstract']); } if (isset($service['factory'])) { if (is_string($service['factory'])) { if (strpos($service['factory'], ':') !== false && strpos($service['factory'], '::') === false) { $parts = explode(':', $service['factory']); $definition->setFactory(array($this->resolveServices('@' . $parts[0]), $parts[1])); } else { $definition->setFactory($service['factory']); } } else { $definition->setFactory(array($this->resolveServices($service['factory'][0]), $service['factory'][1])); } } if (isset($service['factory_class'])) { @trigger_error(sprintf('The "factory_class" key of service "%s" in file "%s" is deprecated since version 2.6 and will be removed in 3.0. Use "factory" instead.', $id, $file), E_USER_DEPRECATED); $definition->setFactoryClass($service['factory_class']); } if (isset($service['factory_method'])) { @trigger_error(sprintf('The "factory_method" key of service "%s" in file "%s" is deprecated since version 2.6 and will be removed in 3.0. Use "factory" instead.', $id, $file), E_USER_DEPRECATED); $definition->setFactoryMethod($service['factory_method']); } if (isset($service['factory_service'])) { @trigger_error(sprintf('The "factory_service" key of service "%s" in file "%s" is deprecated since version 2.6 and will be removed in 3.0. Use "factory" instead.', $id, $file), E_USER_DEPRECATED); $definition->setFactoryService($service['factory_service']); } if (isset($service['file'])) { $definition->setFile($service['file']); } if (isset($service['arguments'])) { $definition->setArguments($this->resolveServices($service['arguments'])); } if (isset($service['properties'])) { $definition->setProperties($this->resolveServices($service['properties'])); } if (isset($service['configurator'])) { if (is_string($service['configurator'])) { $definition->setConfigurator($service['configurator']); } else { $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1])); } } if (isset($service['calls'])) { if (!is_array($service['calls'])) { throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); } foreach ($service['calls'] as $call) { if (isset($call['method'])) { $method = $call['method']; $args = isset($call['arguments']) ? $this->resolveServices($call['arguments']) : array(); } else { $method = $call[0]; $args = isset($call[1]) ? $this->resolveServices($call[1]) : array(); } $definition->addMethodCall($method, $args); } } if (isset($service['tags'])) { if (!is_array($service['tags'])) { throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); } foreach ($service['tags'] as $tag) { if (!is_array($tag)) { throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); } if (!isset($tag['name'])) { throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file)); } if (!is_string($tag['name']) || '' === $tag['name']) { throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file)); } $name = $tag['name']; unset($tag['name']); foreach ($tag as $attribute => $value) { if (!is_scalar($value) && null !== $value) { throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.', $id, $name, $attribute, $file)); } } $definition->addTag($name, $tag); } } if (isset($service['decorates'])) { if ('' !== $service['decorates'] && '@' === $service['decorates'][0]) { throw new InvalidArgumentException(sprintf('The value of the "decorates" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['decorates'], substr($service['decorates'], 1))); } $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null; $definition->setDecoratedService($service['decorates'], $renameId); } $this->container->setDefinition($id, $definition); }
public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer() { if (!class_exists('Symfony\\Component\\Config\\Resource\\FileResource')) { $this->markTestSkipped('The "Config" component is not available'); } $container = new ContainerBuilder(); $def = new Definition('stdClass'); $def->setSynthetic(true); $container->setDefinition('a', $def); $container->compile(); $container->set('a', $a = new \stdClass()); $this->assertEquals($a, $container->get('a')); }
/** * Parses a definition * * @param string $id The service ID * @param array $service The service definition * @param string $file The file path * * @throws InvalidArgumentException When tags are invalid */ protected function parseDefinition($id, $service, $file) { if (is_string($service) && 0 === strpos($service, '@')) { $this->container->setAlias($id, substr($service, 1)); return; } if (!is_array($service)) { $message = sprintf('A service definition must be an array or a string starting with "@" but %s found for ' . 'service "%s" in %s. Check your JSON syntax', gettype($service), $id, $file); throw new InvalidArgumentException($message); } static::checkDefinition($id, $service, $file); if (isset($service['alias'])) { $public = !array_key_exists('public', $service) || (bool) $service['public']; $this->container->setAlias($id, new Alias($service['alias'], $public)); return; } if (isset($service['parent'])) { $definition = new DefinitionDecorator($service['parent']); } else { $definition = new Definition(); } if (isset($service['class'])) { $definition->setClass($service['class']); } if (isset($service['shared'])) { $definition->setShared($service['shared']); } if (isset($service['synthetic'])) { $definition->setSynthetic($service['synthetic']); } if (isset($service['lazy'])) { $definition->setLazy($service['lazy']); } if (isset($service['public'])) { $definition->setPublic($service['public']); } if (isset($service['abstract'])) { $definition->setAbstract($service['abstract']); } if (array_key_exists('deprecated', $service)) { $definition->setDeprecated(true, $service['deprecated']); } if (isset($service['factory'])) { $definition->setFactory($this->parseCallable($service['factory'], 'factory', $id, $file)); } if (isset($service['file'])) { $definition->setFile($service['file']); } if (isset($service['arguments'])) { $definition->setArguments($this->resolveServices($service['arguments'])); } if (isset($service['properties'])) { $definition->setProperties($this->resolveServices($service['properties'])); } if (isset($service['configurator'])) { $definition->setConfigurator($this->parseCallable($service['configurator'], 'configurator', $id, $file)); } if (isset($service['calls'])) { if (!is_array($service['calls'])) { $message = sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your JSON syntax', $id, $file); throw new InvalidArgumentException($message); } foreach ($service['calls'] as $call) { if (isset($call['method'])) { $method = $call['method']; $args = isset($call['arguments']) ? $this->resolveServices($call['arguments']) : array(); } else { $method = $call[0]; $args = isset($call[1]) ? $this->resolveServices($call[1]) : array(); } $definition->addMethodCall($method, $args); } } if (isset($service['tags'])) { if (!is_array($service['tags'])) { $message = sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your JSON syntax', $id, $file); throw new InvalidArgumentException($message); } foreach ($service['tags'] as $tag) { if (!is_array($tag)) { $message = sprintf('A "tags" entry must be an array for service "%s" in %s. Check your JSON syntax', $id, $file); throw new InvalidArgumentException($message); } if (!isset($tag['name'])) { $message = sprintf('A "tags" entry is missing a "name" key for service "%s" in %s', $id, $file); throw new InvalidArgumentException($message); } if (!is_string($tag['name']) || '' === $tag['name']) { $message = sprintf('The tag name for service "%s" in %s must be a non-empty string', $id, $file); throw new InvalidArgumentException($message); } $name = $tag['name']; unset($tag['name']); foreach ($tag as $attribute => $value) { if (!is_scalar($value) && null !== $value) { $message = sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", ' . 'attribute "%s" in %s. Check your JSON syntax', $id, $name, $attribute, $file); throw new InvalidArgumentException($message); } } $definition->addTag($name, $tag); } } if (isset($service['decorates'])) { if ('' !== $service['decorates'] && '@' === $service['decorates'][0]) { $message = sprintf('The value of the "decorates" option for the "%s" service must be the id of the service without ' . 'the "@" prefix (replace "%s" with "%s")', $id, $service['decorates'], substr($service['decorates'], 1)); throw new InvalidArgumentException($message); } $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null; $priority = isset($service['decoration_priority']) ? $service['decoration_priority'] : 0; $definition->setDecoratedService($service['decorates'], $renameId, $priority); } if (isset($service['autowire'])) { $definition->setAutowired($service['autowire']); } if (isset($service['autowiring_types'])) { if (is_string($service['autowiring_types'])) { $definition->addAutowiringType($service['autowiring_types']); } else { if (!is_array($service['autowiring_types'])) { $message = sprintf('Parameter "autowiring_types" must be a string or an array for service "%s" in %s. ' . 'Check your JSON syntax', $id, $file); throw new InvalidArgumentException($message); } foreach ($service['autowiring_types'] as $autowiringType) { if (!is_string($autowiringType)) { $message = sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. ' . 'Check your JSON syntax', $id, $file); throw new InvalidArgumentException($message); } $definition->addAutowiringType($autowiringType); } } } $this->container->setDefinition($id, $definition); }