Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitee82392

Browse files
committed
[DependencyInjection] Tweaked factories
1 parent6acf3e7 commitee82392

File tree

17 files changed

+203
-7
lines changed

17 files changed

+203
-7
lines changed

‎src/Symfony/Component/DependencyInjection/ContainerBuilder.php‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,19 @@ public function createService(Definition $definition, $id, $tryProxy = true)
941941

942942
$arguments =$this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())));
943943

944-
if (null !==$definition->getFactoryMethod()) {
944+
if (null !==$definition->getFactory()) {
945+
$factory =$definition->getFactory();
946+
947+
if (is_string($factory)) {
948+
$callable =$definition->getFactory();
949+
}elseif (is_array($factory)) {
950+
$callable =array($this->resolveServices($factory[0]),$factory[1]);
951+
}else {
952+
thrownewRuntimeException(sprintf('Cannot create service "%s" because of invalid factory',$id));
953+
}
954+
955+
$service =call_user_func_array($callable,$arguments);
956+
}elseif (null !==$definition->getFactoryMethod()) {
945957
if (null !==$definition->getFactoryClass()) {
946958
$factory =$parameterBag->resolveValue($definition->getFactoryClass());
947959
}elseif (null !==$definition->getFactoryService()) {

‎src/Symfony/Component/DependencyInjection/Definition.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public function __construct($class = null, array $arguments = array())
6666
*/
6767
publicfunctionsetFactory($factory)
6868
{
69+
if (is_string($factory) &&strpos($factory,'::') !==false) {
70+
$factory =explode('::',$factory,2);
71+
}
72+
6973
$this->factory =$factory;
7074

7175
return$this;

‎src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -730,13 +730,13 @@ private function addNewInstance($id, Definition $definition, $return, $instantia
730730
$class =$this->dumpValue($callable[0]);
731731
// If the class is a string we can optimize call_user_func away
732732
if (strpos($class,"'") ===0) {
733-
returnsprintf("$return{$instantiation}\%s::%s(%s);\n",substr($class,1, -1),$callable[1],$arguments ?implode(',',$arguments) :'');
733+
returnsprintf("$return{$instantiation}%s::%s(%s);\n",$this->dumpLiteralClass($class),$callable[1],$arguments ?implode(',',$arguments) :'');
734734
}
735735

736-
returnsprintf("$return{$instantiation}call_user_func(array(%s, '%s'),%s);\n",$this->dumpValue($callable[0]),$callable[1],$arguments ?','.implode(',',$arguments) :'');
736+
returnsprintf("$return{$instantiation}call_user_func(array(%s, '%s')%s);\n",$this->dumpValue($callable[0]),$callable[1],$arguments ?','.implode(',',$arguments) :'');
737737
}
738738

739-
returnsprintf("$return{$instantiation}%s(%s);\n",$callable,$arguments ?implode(',',$arguments) :'');
739+
returnsprintf("$return{$instantiation}\\%s(%s);\n",$callable,$arguments ?implode(',',$arguments) :'');
740740
}elseif (null !==$definition->getFactoryMethod()) {
741741
if (null !==$definition->getFactoryClass()) {
742742
$class =$this->dumpValue($definition->getFactoryClass());
@@ -1212,7 +1212,7 @@ private function hasReference($id, array $arguments, $deep = false, array $visit
12121212
/**
12131213
* Dumps values.
12141214
*
1215-
* @paramarray $value
1215+
* @parammixed $value
12161216
* @param bool $interpolate
12171217
*
12181218
* @return string
@@ -1249,6 +1249,30 @@ private function dumpValue($value, $interpolate = true)
12491249
thrownewRuntimeException('Cannot dump definitions which have a variable class name.');
12501250
}
12511251

1252+
if (null !==$value->getFactory()) {
1253+
$factory =$value->getFactory();
1254+
1255+
if (is_string($factory)) {
1256+
returnsprintf('\\%s(%s)',$factory,implode(',',$arguments));
1257+
}
1258+
1259+
if (is_array($factory)) {
1260+
if (is_string($factory[0])) {
1261+
returnsprintf('\\%s::%s(%s)',$factory[0],$factory[1],implode(',',$arguments));
1262+
}
1263+
1264+
if ($factory[0]instanceof Definition) {
1265+
returnsprintf("call_user_func(array(%s, '%s')%s)",$this->dumpValue($factory[0]),$factory[1],count($arguments) >0 ?','.implode(',',$arguments) :'');
1266+
}
1267+
1268+
if ($factory[0]instanceof Reference) {
1269+
returnsprintf('%s->%s(%s)',$this->dumpValue($factory[0]),$factory[1],implode(',',$arguments));
1270+
}
1271+
}
1272+
1273+
thrownewRuntimeException('Cannot dump definition because of invalid factory');
1274+
}
1275+
12521276
if (null !==$value->getFactoryMethod()) {
12531277
if (null !==$value->getFactoryClass()) {
12541278
returnsprintf("call_user_func(array(%s, '%s')%s)",$this->dumpValue($value->getFactoryClass()),$value->getFactoryMethod(),count($arguments) >0 ?','.implode(',',$arguments) :'');

‎src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private function parseDefinition($id, $service, $file)
196196

197197
if (isset($service['factory'])) {
198198
if (is_string($service['factory'])) {
199-
if (strpos($service['factory'],':')) {
199+
if (strpos($service['factory'],':') !==false &&strpos($service['factory'],'::') ===false) {
200200
$parts =explode(':',$service['factory']);
201201
$definition->setFactory(array($this->resolveServices('@'.$parts[0]),$parts[1]));
202202
}else {

‎src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,23 @@ public function testCreateServiceFactoryService()
341341
$this->assertInstanceOf('BazClass',$builder->get('baz_service'));
342342
}
343343

344+
/**
345+
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
346+
*/
347+
publicfunctiontestCreateServiceFactory()
348+
{
349+
$builder =newContainerBuilder();
350+
$builder->register('foo','Bar\FooClass')->setFactory('Bar\FooClass::getInstance');
351+
$builder->register('qux','Bar\FooClass')->setFactory(array('Bar\FooClass','getInstance'));
352+
$builder->register('bar','Bar\FooClass')->setFactory(array(newDefinition('Bar\FooClass'),'getInstance'));
353+
$builder->register('baz','Bar\FooClass')->setFactory(array(newReference('bar'),'getInstance'));
354+
355+
$this->assertTrue($builder->get('foo')->called,'->createService() calls the factory method to create the service instance');
356+
$this->assertTrue($builder->get('qux')->called,'->createService() calls the factory method to create the service instance');
357+
$this->assertTrue($builder->get('bar')->called,'->createService() uses anonymous service as factory');
358+
$this->assertTrue($builder->get('baz')->called,'->createService() uses another service as factory');
359+
}
360+
344361
/**
345362
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
346363
*/

‎src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ public function testConstructor()
3434
publicfunctiontestSetGetFactory()
3535
{
3636
$def =newDefinition('stdClass');
37+
3738
$this->assertSame($def,$def->setFactory('foo'),'->setFactory() implements a fluent interface');
3839
$this->assertEquals('foo',$def->getFactory(),'->getFactory() returns the factory');
40+
41+
$def->setFactory('Foo::bar');
42+
$this->assertEquals(array('Foo','bar'),$def->getFactory(),'->setFactory() converts string static method call to the array');
3943
}
4044

4145
publicfunctiontestSetGetFactoryClass()

‎src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ public function testAddService()
120120
}
121121
}
122122

123+
publicfunctiontestServicesWithAnonymousFactories()
124+
{
125+
$container =includeself::$fixturesPath.'/containers/container19.php';
126+
$dumper =newPhpDumper($container);
127+
128+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services19.php',$dumper->dump(),'->dump() dumps services with anonymous factories');
129+
}
130+
123131
/**
124132
* @expectedException \InvalidArgumentException
125133
* @expectedExceptionMessage Service id "bar$" cannot be converted to a valid PHP method name.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
useSymfony\Component\DependencyInjection\ContainerBuilder;
4+
useSymfony\Component\DependencyInjection\Definition;
5+
6+
require_once__DIR__.'/../includes/classes.php';
7+
8+
$container =newContainerBuilder();
9+
10+
$container
11+
->register('service_from_anonymous_factory','Bar\FooClass')
12+
->setFactory(array(newDefinition('Bar\FooClass'),'getInstance'))
13+
;
14+
15+
$anonymousServiceWithFactory =newDefinition('Bar\FooClass');
16+
$anonymousServiceWithFactory->setFactory('Bar\FooClass::getInstance');
17+
$container
18+
->register('service_with_method_call_and_factory','Bar\FooClass')
19+
->addMethodCall('setBar',array($anonymousServiceWithFactory))
20+
;
21+
22+
return$container;

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,9 @@
114114
->setProperty('foo','bar')
115115
->setFactory(array(newReference('new_factory'),'getInstance'))
116116
;
117+
$container
118+
->register('service_from_static_method','Bar\FooClass')
119+
->setFactory(array('Bar\FooClass','getInstance'))
120+
;
117121

118122
return$container;

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ digraph sc {
2121
node_decorator_service_with_name [label="decorator_service_with_name\nstdClass\n",shape=record,fillcolor="#eeeeee",style="filled"];
2222
node_new_factory [label="new_factory\nFactoryClass\n",shape=record,fillcolor="#eeeeee",style="filled"];
2323
node_new_factory_service [label="new_factory_service\nFooBarBaz\n",shape=record,fillcolor="#eeeeee",style="filled"];
24+
node_service_from_static_method [label="service_from_static_method\nBar\\FooClass\n",shape=record,fillcolor="#eeeeee",style="filled"];
2425
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n",shape=record,fillcolor="#9999ff",style="filled"];
2526
node_foo2 [label="foo2\n\n",shape=record,fillcolor="#ff9999",style="filled"];
2627
node_foo3 [label="foo3\n\n",shape=record,fillcolor="#ff9999",style="filled"];

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp