From 421e7773c4625ecb90fa8b634e1cd0a62cf7adb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rouven=20We=C3=9Fling?= Date: Sat, 2 Nov 2013 13:45:44 +0100 Subject: [PATCH 1/2] [DependencyInjection] added support for inlining Configurators --- .../Compiler/InlineServiceDefinitionsPass.php | 5 ++ .../DependencyInjection/Dumper/PhpDumper.php | 14 +++++- .../Tests/Fixtures/containers/container9.php | 9 ++++ .../Tests/Fixtures/graphviz/services9.dot | 3 ++ .../Tests/Fixtures/php/services9.php | 46 ++++++++++++++++++- .../Tests/Fixtures/php/services9_compiled.php | 27 ++++++++++- .../Tests/Fixtures/xml/services9.xml | 8 ++++ .../Tests/Fixtures/yaml/services9.yml | 8 ++++ 8 files changed, 114 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPass.php b/src/Symfony/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPass.php index 2868cca63fa8..944a95ebfacf 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPass.php @@ -62,6 +62,11 @@ public function process(ContainerBuilder $container) $definition->setProperties( $this->inlineArguments($container, $definition->getProperties()) ); + + $configurator = $this->inlineArguments($container, array($definition->getConfigurator())); + $definition->setConfigurator( + $configurator[0] + ); } } diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index d5debb6c6e19..70c3caa6a982 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -159,6 +159,7 @@ private function addServiceLocalTempVariables($cId, $definition) $this->getServiceCallsFromArguments($iDefinition->getArguments(), $calls, $behavior); $this->getServiceCallsFromArguments($iDefinition->getMethodCalls(), $calls, $behavior); $this->getServiceCallsFromArguments($iDefinition->getProperties(), $calls, $behavior); + $this->getServiceCallsFromArguments(array($iDefinition->getConfigurator()), $calls, $behavior); } $code = ''; @@ -482,7 +483,15 @@ private function addServiceConfigurator($id, $definition, $variableName = 'insta if (is_array($callable)) { if ($callable[0] instanceof Reference) { - return sprintf(" %s->%s(\$%s);\n", $this->getServiceCall((string) $callable[0]), $callable[1], $variableName); + return sprintf(" %s->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName); + } + + if ($callable[0] instanceof Definition) { + if ($this->definitionVariables->contains($callable[0])) { + return sprintf(" %s->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName); + } elseif (version_compare(PHP_VERSION, '5.4.0') >= 0) { + return sprintf(" (%s)->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName); + } } return sprintf(" call_user_func(array(%s, '%s'), \$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName); @@ -1070,7 +1079,8 @@ private function getInlinedDefinitions(Definition $definition) $definitions = array_merge( $this->getDefinitionsFromArguments($definition->getArguments()), $this->getDefinitionsFromArguments($definition->getMethodCalls()), - $this->getDefinitionsFromArguments($definition->getProperties()) + $this->getDefinitionsFromArguments($definition->getProperties()), + $this->getDefinitionsFromArguments(array($definition->getConfigurator())) ); $this->inlinedDefinitions->offsetSet($definition, $definitions); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php index b5e9b073082f..8a13d232d4ad 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php @@ -83,5 +83,14 @@ ->register('depends_on_request', 'stdClass') ->addMethodCall('setRequest', array(new Reference('request', ContainerInterface::NULL_ON_INVALID_REFERENCE, false))) ; +$container + ->register('configurator_service', 'ConfClass') + ->setPublic(false) + ->addMethodCall('setFoo', array(new Reference('baz'))) +; +$container + ->register('configured_service', 'stdClass') + ->setConfigurator(array(new Reference('configurator_service'), 'configureStdClass')) +; return $container; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot index cb006641d0b1..6ac9eb31d0ee 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot @@ -14,6 +14,8 @@ digraph sc { node_baz [label="baz\nBaz\n", shape=record, fillcolor="#eeeeee", style="filled"]; node_request [label="request\nRequest\n", shape=record, fillcolor="#eeeeee", style="filled"]; node_depends_on_request [label="depends_on_request\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"]; + node_configurator_service [label="configurator_service\nConfClass\n", shape=record, fillcolor="#eeeeee", style="filled"]; + node_configured_service [label="configured_service\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"]; node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=record, fillcolor="#9999ff", style="filled"]; node_foo2 [label="foo2\n\n", shape=record, fillcolor="#ff9999", style="filled"]; node_foo3 [label="foo3\n\n", shape=record, fillcolor="#ff9999", style="filled"]; @@ -31,4 +33,5 @@ digraph sc { node_inlined -> node_baz [label="setBaz()" style="dashed"]; node_baz -> node_foo_with_inline [label="setFoo()" style="dashed"]; node_depends_on_request -> node_request [label="setRequest()" style="dashed"]; + node_configurator_service -> node_baz [label="setFoo()" style="dashed"]; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php index 522df356fa5f..b474526c422b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php @@ -25,6 +25,8 @@ public function __construct() $this->methodMap = array( 'bar' => 'getBarService', 'baz' => 'getBazService', + 'configurator_service' => 'getConfiguratorServiceService', + 'configured_service' => 'getConfiguredServiceService', 'depends_on_request' => 'getDependsOnRequestService', 'factory_service' => 'getFactoryServiceService', 'foo' => 'getFooService', @@ -51,9 +53,11 @@ public function __construct() */ protected function getBarService() { - $this->services['bar'] = $instance = new \FooClass('foo', $this->get('foo.baz'), $this->getParameter('foo_bar')); + $a = $this->get('foo.baz'); + + $this->services['bar'] = $instance = new \FooClass('foo', $a, $this->getParameter('foo_bar')); - $this->get('foo.baz')->configure($instance); + $a->configure($instance); return $instance; } @@ -75,6 +79,23 @@ protected function getBazService() return $instance; } + /** + * Gets the 'configured_service' service. + * + * This service is shared. + * This method always returns the same instance of the service. + * + * @return stdClass A stdClass instance. + */ + protected function getConfiguredServiceService() + { + $this->services['configured_service'] = $instance = new \stdClass(); + + $this->get('configurator_service')->configureStdClass($instance); + + return $instance; + } + /** * Gets the 'depends_on_request' service. * @@ -224,6 +245,27 @@ protected function synchronizeRequestService() } } + /** + * Gets the 'configurator_service' service. + * + * This service is shared. + * This method always returns the same instance of the service. + * + * This service is private. + * If you want to be able to request this service from the container directly, + * make it public, otherwise you might end up with broken code. + * + * @return ConfClass A ConfClass instance. + */ + protected function getConfiguratorServiceService() + { + $this->services['configurator_service'] = $instance = new \ConfClass(); + + $instance->setFoo($this->get('baz')); + + return $instance; + } + /** * Gets the 'inlined' service. * diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index 4505e51d0ac3..bc23014c609b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -34,6 +34,7 @@ public function __construct() $this->methodMap = array( 'bar' => 'getBarService', 'baz' => 'getBazService', + 'configured_service' => 'getConfiguredServiceService', 'depends_on_request' => 'getDependsOnRequestService', 'factory_service' => 'getFactoryServiceService', 'foo' => 'getFooService', @@ -59,9 +60,11 @@ public function __construct() */ protected function getBarService() { - $this->services['bar'] = $instance = new \FooClass('foo', $this->get('foo.baz'), $this->getParameter('foo_bar')); + $a = $this->get('foo.baz'); + + $this->services['bar'] = $instance = new \FooClass('foo', $a, $this->getParameter('foo_bar')); - $this->get('foo.baz')->configure($instance); + $a->configure($instance); return $instance; } @@ -83,6 +86,26 @@ protected function getBazService() return $instance; } + /** + * Gets the 'configured_service' service. + * + * This service is shared. + * This method always returns the same instance of the service. + * + * @return stdClass A stdClass instance. + */ + protected function getConfiguredServiceService() + { + $a = new \ConfClass(); + $a->setFoo($this->get('baz')); + + $this->services['configured_service'] = $instance = new \stdClass(); + + $a->configureStdClass($instance); + + return $instance; + } + /** * Gets the 'depends_on_request' service. * diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml index 60fcfce59a97..cc94d68c2cdc 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml @@ -76,6 +76,14 @@ + + + + + + + + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml index c3180f730277..5ce8aef38fbd 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml @@ -70,5 +70,13 @@ services: calls: - [setRequest, ['@?request']] + configurator_service: + class: ConfClass + calls: + - [setFoo, ['@baz']] + + configured_service: + class: stdClass + configurator: ['@configurator_service', configureStdClass] alias_for_foo: @foo alias_for_alias: @foo From d8ddb1f978817ba13e84c0c51d54b4023a3f63b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rouven=20We=C3=9Fling?= Date: Sat, 2 Nov 2013 15:35:21 +0100 Subject: [PATCH 2/2] [DependencyInjection] Remove some calls to call_user_func in generated containers. --- .../DependencyInjection/Dumper/PhpDumper.php | 16 ++++++++++++++-- .../Tests/Fixtures/php/services9.php | 2 +- .../Tests/Fixtures/php/services9_compiled.php | 6 +++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 70c3caa6a982..0a17780d20b2 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -494,7 +494,13 @@ private function addServiceConfigurator($id, $definition, $variableName = 'insta } } - return sprintf(" call_user_func(array(%s, '%s'), \$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName); + $class = $this->dumpValue($callable[0]); + // If the class is a string we can optimize call_user_func away + if (strpos($class, "'") === 0) { + return sprintf(" %s::%s(\$%s);\n", substr($class, 1, -1), $callable[1], $variableName); + } + + return sprintf(" call_user_func(array(%s, '%s'), \$%s);\n", $class, $callable[1], $variableName); } return sprintf(" %s(\$%s);\n", $callable, $variableName); @@ -698,7 +704,13 @@ private function addNewInstance($id, Definition $definition, $return, $instantia if (null !== $definition->getFactoryMethod()) { if (null !== $definition->getFactoryClass()) { - return sprintf(" $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($definition->getFactoryClass()), $definition->getFactoryMethod(), $arguments ? ', '.implode(', ', $arguments) : ''); + $class = $this->dumpValue($definition->getFactoryClass()); + + // If the class is a string we can optimize call_user_func away + if (strpos($class, "'") === 0) { + return sprintf(" $return{$instantiation}%s::%s(%s);\n", substr($class, 1, -1), $definition->getFactoryMethod(), $arguments ? implode(', ', $arguments) : ''); + } + return sprintf(" $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $class, $definition->getFactoryMethod(), $arguments ? ', '.implode(', ', $arguments) : ''); } if (null !== $definition->getFactoryService()) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php index b474526c422b..4be046ad7ea4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php @@ -138,7 +138,7 @@ protected function getFooService() { $a = $this->get('foo.baz'); - $this->services['foo'] = $instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo')), true, $this); + $this->services['foo'] = $instance = FooClass::getInstance('foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo')), true, $this); $instance->setBar($this->get('bar')); $instance->initialize(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index bc23014c609b..1d8a5569d813 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -148,7 +148,7 @@ protected function getFooService() { $a = $this->get('foo.baz'); - $this->services['foo'] = $instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $a, array('bar' => 'foo is bar', 'foobar' => 'bar'), true, $this); + $this->services['foo'] = $instance = FooClass::getInstance('foo', $a, array('bar' => 'foo is bar', 'foobar' => 'bar'), true, $this); $instance->setBar($this->get('bar')); $instance->initialize(); @@ -169,9 +169,9 @@ protected function getFooService() */ protected function getFoo_BazService() { - $this->services['foo.baz'] = $instance = call_user_func(array('BazClass', 'getInstance')); + $this->services['foo.baz'] = $instance = BazClass::getInstance(); - call_user_func(array('BazClass', 'configureStatic1'), $instance); + BazClass::configureStatic1($instance); return $instance; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy