private function then_references_are_replaced_with_services() { $methodCalls = $this->apiDefinition->getMethodCalls(); $defaultParams = $methodCalls[0][1][0]; $this->assertSame('my_value', $defaultParams['my_param']); $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $defaultParams[0]); $this->assertSame('my_param_service', (string) $defaultParams[0]); }
public function testMapperPassWithTwoTaggedLoaders() { $serviceIds = array('test_loader_1' => array(), 'test_loader_2' => array()); $this->builder->expects($this->once())->method('hasDefinition')->with('twig')->will($this->returnValue(true)); $this->builder->expects($this->once())->method('findTaggedServiceIds')->with('twig.loader')->will($this->returnValue($serviceIds)); $this->builder->expects($this->once())->method('getDefinition')->with('twig.loader.chain')->will($this->returnValue($this->chainLoader)); $this->builder->expects($this->once())->method('setAlias')->with('twig.loader', 'twig.loader.chain'); $this->pass->process($this->builder); $calls = $this->chainLoader->getMethodCalls(); $this->assertCount(2, $calls); $this->assertEquals('addLoader', $calls[0][0]); }
private function commandBusContainsMiddlewares($expectedMiddlewareIds) { $actualMiddlewareIds = []; foreach ($this->mainBusDefinition->getMethodCalls() as $methodCall) { list($method, $arguments) = $methodCall; $this->assertSame('appendMiddleware', $method); $this->assertCount(1, $arguments); $referencedService = $arguments[0]; $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $referencedService); $actualMiddlewareIds[] = (string) $referencedService; } $this->assertEquals($expectedMiddlewareIds, $actualMiddlewareIds); }
public function testProcess() { $container = new ContainerBuilder(); $simpleFactory = new Definition(); $simpleProcessor = new Definition('Test\\SimpleProcessor'); $simpleProcessor->addTag('processor'); $abstractProcessor = new Definition(); $abstractProcessor->setAbstract(true); $abstractProcessor->addTag('processor'); $lazyProcessor = new Definition('Test\\LazyProcessor'); $lazyProcessor->setLazy(true); $lazyProcessor->addTag('processor'); $withArgumentsProcessor = new Definition('Test\\WithArgumentsProcessor', ['test']); $withArgumentsProcessor->addTag('processor'); $container->addDefinitions(['simple_factory' => $simpleFactory, 'simple_processor' => $simpleProcessor, 'abstract_processor' => $abstractProcessor, 'lazy_processor' => $lazyProcessor, 'with_arguments_processor' => $withArgumentsProcessor]); $compilerPass = new CleanUpProcessorsCompilerPass('simple_factory', 'processor'); $compilerPass->process($container); $this->assertFalse($container->hasDefinition('simple_processor')); $this->assertTrue($container->hasDefinition('abstract_processor')); $this->assertTrue($container->hasDefinition('lazy_processor')); $this->assertTrue($container->hasDefinition('with_arguments_processor')); $methodCalls = $simpleFactory->getMethodCalls(); $this->assertCount(1, $methodCalls); $this->assertEquals(['addProcessor', ['simple_processor', 'Test\\SimpleProcessor']], $methodCalls[0]); }
/** * @param Definition $definition * @param string $methodName * @param array $arguments */ private function callMethodFirst(Definition $definition, $methodName, array $arguments) { $newMethodCall = array($methodName, $arguments); $currentMethodCalls = $definition->getMethodCalls(); array_unshift($currentMethodCalls, $newMethodCall); $definition->setMethodCalls($currentMethodCalls); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testProcess() { $container = new ContainerBuilder(); $processorBag = new Definition('Test\\ProcessorBag'); $processor1 = new Definition('Test\\Processor1'); $processor1->addTag('processor', ['action' => 'action1', 'group' => 'group1', 'priority' => 123]); $processor1->addTag('processor', ['action' => 'action2', 'group' => 'group2', 'test_attr' => 'test']); $processor1->addTag('processor', ['action' => 'action3', 'group' => 'group3', 'test_attr' => 'test1&test2']); $processor2 = new Definition('Test\\Processor2'); $processor2->addTag('processor', ['action' => 'action1']); $processor3 = new Definition('Test\\Processor3'); $processor3->addTag('processor'); $applicableChecker1 = new Definition('Test\\ApplicableChecker1'); $applicableChecker1->addTag('applicable_checker'); $applicableChecker2 = new Definition('Test\\ApplicableChecker1'); $applicableChecker2->addTag('applicable_checker', ['priority' => 123]); $container->addDefinitions(['processor_bag' => $processorBag, 'processor1' => $processor1, 'processor2' => $processor2, 'processor3' => $processor3, 'applicable_checker1' => $applicableChecker1, 'applicable_checker2' => $applicableChecker2]); $compilerPass = new LoadProcessorsCompilerPass('processor_bag', 'processor', 'applicable_checker'); $compilerPass->process($container); $methodCalls = $processorBag->getMethodCalls(); $this->assertCount(7, $methodCalls); $this->assertEquals(['addProcessor', ['processor1', [], 'action1', 'group1', 123]], $methodCalls[0]); $this->assertEquals(['addProcessor', ['processor1', ['test_attr' => 'test'], 'action2', 'group2', 0]], $methodCalls[1]); $this->assertEquals(['addProcessor', ['processor1', ['test_attr' => ['test1', 'test2']], 'action3', 'group3', 0]], $methodCalls[2]); $this->assertEquals(['addProcessor', ['processor2', [], 'action1', null, 0]], $methodCalls[3]); $this->assertEquals(['addProcessor', ['processor3', [], null, null, 0]], $methodCalls[4]); $this->assertEquals(['addApplicableChecker', [new Reference('applicable_checker1'), 0]], $methodCalls[5]); $this->assertEquals(['addApplicableChecker', [new Reference('applicable_checker2'), 123]], $methodCalls[6]); }
public function testAddMethodCallToMonolog() { $definition = new Definition(); $container = $this->getContainerBuilder(['monolog.logger_prototype' => $definition], ['riemann.integration.monolog' => true]); $pass = new MonologIntegrationPass(); $pass->process($container); self::assertCount(1, $definition->getMethodCalls()); }
/** * @param Definition $definition * * @return string|null */ private function getLocaleDriver(Definition $definition) { foreach ($definition->getMethodCalls() as $methodCall) { if ($methodCall[0] === 'setDriver') { return $methodCall[1][0]; } } }
public function testRegisterCommandHandlers() { $definition = new Definition('stdClass', ['', []]); $container = new ContainerBuilder(); $container->addDefinitions(['seven_service_bus.self_binding' => $definition, 'foo' => $this->getTaggedService('service_binding', ['topic' => 'foo_topic', 'version' => '0', 'method' => 'foo_method']), 'bar' => $this->getTaggedService('service_binding', ['topic' => 'bar_topic']), 'baz' => $this->getTaggedService('service_binding', ['version' => '0', 'method' => 'baz_method'])]); $this->process($container); $calls = $definition->getMethodCalls(); $this->assertEquals([['on', ['foo_topic', '0', 'foo', 'foo_method']], ['on', ['bar_topic', null, 'bar', null]]], $calls); }
protected function assertDefinitionContainsMethodCall(Definition $serviceDefinition, $expectedMethod, $expectedFirstArgument) { foreach ($serviceDefinition->getMethodCalls() as $methodCall) { if ($expectedMethod == $methodCall[0] && $expectedFirstArgument == $methodCall[1][0]) { return; } } $this->fail(sprintf('Failed assert that service (Class: %s) has method %s been called with first argument %s', $serviceDefinition->getClass(), $expectedMethod, $expectedFirstArgument)); }
/** * @param Definition $serviceDefinition * @param string $methodName * * @return array */ protected function getMethodCallsByName(Definition $serviceDefinition, $methodName) { $ret = []; foreach ($serviceDefinition->getMethodCalls() as $methodCall) { list($name, $args) = $methodCall; if ($name === $methodName) { $ret[] = $args; } } return $ret; }
/** * @param string $registry * @param Definition $definition * * @return Definition */ private function createLazyDefinition($registry, Definition $definition) { $lazy = new Definition(LazyServiceRegistry::class, [new Reference('service_container'), new Reference($registry)]); foreach ($definition->getMethodCalls() as $methodCall) { if ($methodCall[0] === 'offsetSet') { $methodCall[1][1] = (string) $methodCall[1][1]; $lazy->addMethodCall('setLazy', $methodCall[1]); } } return $lazy; }
/** * Merge Template into Service Definition * * @access private * @param \Symfony\Component\DependencyInjection\Definition $service * @param \Symfony\Component\DependencyInjection\Definition $template * @return \Symfony\Component\DependencyInjection\Definition */ private function mergeTemplateIntoDefinition(Definition $service, Definition $template) { $serviceMethodCalls = $service->getMethodCalls(); foreach ($template->getMethodCalls() as $methodCall) { list($method, $arguments) = $methodCall; if (!isset($serviceMethodCalls[$method])) { $service->addMethodCall($method, $arguments); } } return $service; }
public function testProcess() { $container = new ContainerBuilder(); $chainResolver = new Definition(); $resolver1 = new Definition(); $resolver2 = new Definition(); $resolver3 = new Definition(); $resolver4 = new Definition(); $resolver1->addTag(RoutingOptionsResolverPass::RESOLVER_TAG_NAME, ['priority' => -100]); $resolver2->addTag(RoutingOptionsResolverPass::RESOLVER_TAG_NAME, ['priority' => 100]); $resolver3->addTag(RoutingOptionsResolverPass::RESOLVER_TAG_NAME); $resolver4->addTag(RoutingOptionsResolverPass::RESOLVER_TAG_NAME, ['priority' => -100]); $container->addDefinitions([RoutingOptionsResolverPass::CHAIN_RESOLVER_SERVICE => $chainResolver, 'resolver1' => $resolver1, 'resolver2' => $resolver2, 'resolver3' => $resolver3, 'resolver4' => $resolver4]); $this->compilerPass->process($container); $this->assertEquals([['addResolver', [new Reference('resolver2')]], ['addResolver', [new Reference('resolver3')]], ['addResolver', [new Reference('resolver1')]], ['addResolver', [new Reference('resolver4')]]], $chainResolver->getMethodCalls()); }
/** * @param Definition $definition * @param string $name * @param mixed $value */ public function mergeMethodCall(Definition $definition, $name, $value) { $methodCalls = $definition->getMethodCalls(); foreach ($methodCalls as &$calls) { foreach ($calls as &$call) { if (is_string($call)) { if ($call !== $name) { continue 2; } continue 1; } $call = array(array_merge($call[0], $value)); } } $definition->setMethodCalls($methodCalls); }
public function testProcess() { $container = new ContainerBuilder(); $resolver = new Definition(); $provider1 = new Definition(); $provider2 = new Definition(); $provider3 = new Definition(); $provider4 = new Definition(); $provider1->addTag(EntityAliasProviderPass::PROVIDER_TAG_NAME, ['priority' => 100]); $provider2->addTag(EntityAliasProviderPass::PROVIDER_TAG_NAME, ['priority' => -100]); $provider3->addTag(EntityAliasProviderPass::PROVIDER_TAG_NAME); $provider4->addTag(EntityAliasProviderPass::PROVIDER_TAG_NAME, ['priority' => 100]); $container->addDefinitions([EntityAliasProviderPass::RESOLVER_SERVICE => $resolver, 'provider1' => $provider1, 'provider2' => $provider2, 'provider3' => $provider3, 'provider4' => $provider4]); $this->compilerPass->process($container); $this->assertEquals([['addProvider', [new Reference('provider2')]], ['addProvider', [new Reference('provider3')]], ['addProvider', [new Reference('provider1')]], ['addProvider', [new Reference('provider4')]]], $resolver->getMethodCalls()); }
public function testProcess() { $container = new ContainerBuilder(); $compiler = $this->getCompilerMock('chain_service', 'tag_name'); $chainProvider = new Definition(); $provider1 = new Definition(); $provider2 = new Definition(); $provider3 = new Definition(); $provider4 = new Definition(); $provider1->addTag(self::TAG_NAME, ['priority' => 100]); $provider2->addTag(self::TAG_NAME, ['priority' => -100]); $provider3->addTag(self::TAG_NAME); $provider4->addTag(self::TAG_NAME, ['priority' => 100]); $container->addDefinitions([self::SERVICE_ID => $chainProvider, 'provider1' => $provider1, 'provider2' => $provider2, 'provider3' => $provider3, 'provider4' => $provider4]); $compiler->process($container); $this->assertEquals([['addProvider', [new Reference('provider2')]], ['addProvider', [new Reference('provider3')]], ['addProvider', [new Reference('provider1')]], ['addProvider', [new Reference('provider4')]]], $chainProvider->getMethodCalls()); }
public function test_handlers_added_to_bus() { $container = new ContainerBuilder(); $container->setDefinition('rp.command_bus', $bus = new Definition('CommandBus')); $handler1 = new Definition('\\AppBundle\\Worksheet\\CommandHandler\\ReleaseResponseLockCommandHandler'); $handler1->addTag('command_bus.handler'); $handler2 = new Definition('\\AppBundle\\Worksheet\\CommandHandler\\ReleaseResponseLockCommandHandler'); $handler2->addTag('command_bus.handler'); $container->setDefinition('handler_1', $handler1); $container->setDefinition('handler_2', $handler2); $compiler = new AddCommandHandlersPass(); $compiler->process($container); $calls = $bus->getMethodCalls(); $this->assertCount(2, $calls); $this->assertEquals('addHandler', $calls[0][0]); $this->assertEquals('handler_1', $calls[0][1][0]); $this->assertEquals('addHandler', $calls[1][0]); $this->assertEquals('handler_2', $calls[1][1][0]); }
public function testProcess() { $dispatcherDef = new Definition(); $slotDef = new Definition(); $signalIdentifier = 'FooSignal'; $slotDef->addTag('ezpublish.api.slot', array('signal' => $signalIdentifier)); $containerBuilder = new ContainerBuilder(); $slotId = 'acme.foo_slot'; $containerBuilder->addDefinitions(array($slotId => $slotDef, 'ezpublish.signalslot.signal_dispatcher' => $dispatcherDef)); $pass = new SignalSlotPass(); $pass->process($containerBuilder); $this->assertTrue($dispatcherDef->hasMethodCall('attach')); $calls = $dispatcherDef->getMethodCalls(); list($method, $arguments) = $calls[0]; $this->assertSame('attach', $method); list($signal, $serviceId) = $arguments; $this->assertSame($signalIdentifier, $signal); $this->assertEquals($slotId, new Reference($serviceId)); }
private function injectClassResolverInPlaceHolder(array $attributes, Definition $serviceInWhichInjectDef, $serviceIdInWhichInjectDef) { list(, $group) = $this->computeClassResolverId($attributes); $classResolverId = $this->getClassResolverIdWithGroup('kassko_class_resolver.chain', $group); $index = isset($attributes['index']) ? $attributes['index'] : 0; if (!isset($attributes['method'])) { $serviceInWhichInjectDef->replaceArgument($index, new Reference($classResolverId)); } else { $method = $attributes['method']; $calls = $serviceInWhichInjectDef->getMethodCalls(); foreach ($calls as &$call) { if ($method === $call[0]) { if (!isset($call[1][$index])) { throw new OutOfBoundsException(sprintf('The index attribute "%s" for tag "%s"' . ' and service "%s" is not in the range [0, %d].', $index, $classesToRegisterTag, $serviceIdInWhichInjectDef, count($calls) - 1)); } $call[1][$index] = new Reference($classResolverId); } } unset($call); //Precaution. $serviceInWhichInjectDef->setMethodCalls($calls); } }
/** * Asserts that the given definition contains exactly one call to the method * and that it uses the specified parameters. * * @param Definition $definition * @param string $methodName * @param array $params */ private function assertDefinitionMethodCallOnce(Definition $definition, $methodName, array $params) { $calls = $definition->getMethodCalls(); $called = false; foreach ($calls as $call) { if ($call[0] !== $methodName) { continue; } if ($called) { $this->fail("Method '" . $methodName . "' is expected to be called only once, but it was called multiple times."); } $called = true; $this->assertEquals($params, $call[1], "Expected parameters to method '" . $methodName . "' did not match the actual parameters."); } if (!$called) { $this->fail("Method '" . $methodName . "' is expected to be called once, but it was never called."); } }
/** * 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->isShared()) { $service->setAttribute('shared', 'false'); } if (!$definition->isPublic()) { $service->setAttribute('public', 'false'); } if ($definition->isSynthetic()) { $service->setAttribute('synthetic', '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) { $autowiringType = $this->document->createElement('autowiring-type'); $autowiringType->appendChild($this->document->createTextNode($autowiringTypeValue)); $service->appendChild($autowiringType); } if ($callable = $definition->getConfigurator()) { $configurator = $this->document->createElement('configurator'); if (is_array($callable) && $callable[0] instanceof Definition) { $this->addService($callable[0], null, $configurator); $configurator->setAttribute('method', $callable[1]); } elseif (is_array($callable)) { $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]); $configurator->setAttribute('method', $callable[1]); } else { $configurator->setAttribute('function', $callable); } $service->appendChild($configurator); } $parent->appendChild($service); }
/** * Assertion for the DI Container, check if the given definition contains a method call with the given parameters. * * @param \Symfony\Component\DependencyInjection\Definition $definition * @param string $methodName * @param array $params */ protected function assertDICDefinitionMethodCallOnce($definition, $methodName, array $params = null) { $calls = $definition->getMethodCalls(); $called = false; foreach ($calls as $call) { if ($call[0] == $methodName) { if ($called) { $this->fail("Method '" . $methodName . "' is expected to be called only once, a second call was registered though."); } else { $called = true; if ($params !== null) { $this->assertEquals($params, $call[1], "Expected parameters to methods '" . $methodName . "' do not match the actual parameters."); } } } } if (!$called) { $this->fail("Method '" . $methodName . "' is expected to be called once, definition does not contain a call though."); } }
private function assertDICDefinitionMethodCallCount(Definition $definition, $methodName, array $params = array(), $nbCalls = 1) { $calls = $definition->getMethodCalls(); $called = 0; foreach ($calls as $call) { if ($call[0] == $methodName) { if ($called > $nbCalls) { break; } if (isset($params[$called])) { $this->assertEquals($params[$called], $call[1], "Expected parameters to methods '" . $methodName . "' do not match the actual parameters."); } $called++; } } $this->assertEquals($nbCalls, $called, sprintf('The method "%s" should be called %d times', $methodName, $nbCalls)); }
/** * 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; }
/** * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container * @param \Symfony\Component\DependencyInjection\Definition $definition * * @return void */ public function fixTemplates(ContainerBuilder $container, Definition $definition) { $definedTemplates = $container->getParameter('sonata.admin.configuration.templates'); $methods = array(); $pos = 0; foreach ($definition->getMethodCalls() as $method) { if ($method[0] == 'setTemplates') { $definedTemplates = array_merge($definedTemplates, $method[1][0]); continue; } if ($method[0] == 'setTemplate') { $definedTemplates[$method[1][0]] = $method[1][1]; continue; } $methods[$pos] = $method; $pos++; } $definition->setMethodCalls($methods); // make sure the default templates are defined $definedTemplates = array_merge(array('user_block' => 'SonataAdminBundle:Core:user_block.html.twig', 'add_block' => 'SonataAdminBundle:Core:add_block.html.twig', 'layout' => 'SonataAdminBundle::standard_layout.html.twig', 'ajax' => 'SonataAdminBundle::ajax_layout.html.twig', 'dashboard' => 'SonataAdminBundle:Core:dashboard.html.twig', 'list' => 'SonataAdminBundle:CRUD:list.html.twig', 'filter' => 'SonataAdminBundle:Form:filter_admin_fields.html.twig', 'show' => 'SonataAdminBundle:CRUD:show.html.twig', 'show_compare' => 'SonataAdminBundle:CRUD:show_compare.html.twig', 'edit' => 'SonataAdminBundle:CRUD:edit.html.twig', 'history' => 'SonataAdminBundle:CRUD:history.html.twig', 'history_revision_timestamp' => 'SonataAdminBundle:CRUD:history_revision_timestamp.html.twig', 'acl' => 'SonataAdminBundle:CRUD:acl.html.twig', 'action' => 'SonataAdminBundle:CRUD:action.html.twig', 'short_object_description' => 'SonataAdminBundle:Helper:short-object-description.html.twig', 'preview' => 'SonataAdminBundle:CRUD:preview.html.twig', 'list_block' => 'SonataAdminBundle:Block:block_admin_list.html.twig', 'delete' => 'SonataAdminBundle:CRUD:delete.html.twig', 'batch' => 'SonataAdminBundle:CRUD:list__batch.html.twig', 'select' => 'SonataAdminBundle:CRUD:list__select.html.twig', 'batch_confirmation' => 'SonataAdminBundle:CRUD:batch_confirmation.html.twig', 'inner_list_row' => 'SonataAdminBundle:CRUD:list_inner_row.html.twig', 'base_list_field' => 'SonataAdminBundle:CRUD:base_list_field.html.twig', 'pager_links' => 'SonataAdminBundle:Pager:links.html.twig', 'pager_results' => 'SonataAdminBundle:Pager:results.html.twig', 'tab_menu_template' => 'SonataAdminBundle:Core:tab_menu_template.html.twig'), $definedTemplates); $definition->addMethodCall('setTemplates', array($definedTemplates)); }
/** * Adds a service * * @param string $id * @param Definition $definition * * @return string */ private function addService($id, $definition) { $code = " {$id}:\n"; if ($definition->getClass()) { $code .= sprintf(" class: %s\n", $definition->getClass()); } 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", $definition->getFile()); } if ($definition->isSynthetic()) { $code .= sprintf(" synthetic: true\n"); } if ($definition->isSynchronized()) { $code .= sprintf(" synchronized: true\n"); } if ($definition->getFactoryClass()) { $code .= sprintf(" factory_class: %s\n", $definition->getFactoryClass()); } if ($definition->isLazy()) { $code .= sprintf(" lazy: true\n"); } if ($definition->getFactoryMethod()) { $code .= sprintf(" factory_method: %s\n", $definition->getFactoryMethod()); } if ($definition->getFactoryService()) { $code .= sprintf(" factory_service: %s\n", $definition->getFactoryService()); } 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 (ContainerInterface::SCOPE_CONTAINER !== ($scope = $definition->getScope())) { $code .= sprintf(" scope: %s\n", $scope); } if ($callable = $definition->getConfigurator()) { if (is_array($callable)) { if ($callable[0] instanceof Reference) { $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]); } else { $callable = array($callable[0], $callable[1]); } } $code .= sprintf(" configurator: %s\n", $this->dumper->dump($callable, 0)); } return $code; }
/** * 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; }
/** * 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 ($definition->getClass()) { $service->setAttribute('class', $definition->getClass()); } if ($definition->getFactoryMethod()) { $service->setAttribute('factory-method', $definition->getFactoryMethod()); } if ($definition->getFactoryService()) { $service->setAttribute('factory-service', $definition->getFactoryService()); } if (ContainerInterface::SCOPE_CONTAINER !== ($scope = $definition->getScope())) { $service->setAttribute('scope', $scope); } if (!$definition->isPublic()) { $service->setAttribute('public', 'false'); } 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->getConfigurator()) { $configurator = $this->document->createElement('configurator'); if (is_array($callable)) { $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]); $configurator->setAttribute('method', $callable[1]); } else { $configurator->setAttribute('function', $callable); } $service->appendChild($configurator); } $parent->appendChild($service); }
/** * 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; }