Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[DependencyInjection] renamed constructor to factory method (like in …
…Spring)
  • Loading branch information
fabpot committed Jul 5, 2010
1 parent a9ad743 commit 4bbf2ae
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Components/DependencyInjection/Builder.php
Expand Up @@ -333,8 +333,8 @@ protected function createService(Definition $definition, $id)

$arguments = $this->resolveServices(self::resolveValue($definition->getArguments(), $this->getParameterBag()->all()));

if (null !== $definition->getConstructor()) {
$service = call_user_func_array(array(self::resolveValue($definition->getClass(), $this->getParameterBag()->all()), $definition->getConstructor()), $arguments);
if (null !== $definition->getFactoryMethod()) {
$service = call_user_func_array(array(self::resolveValue($definition->getClass(), $this->getParameterBag()->all()), $definition->getFactoryMethod()), $arguments);
} else {
$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);
}
Expand Down
24 changes: 12 additions & 12 deletions src/Symfony/Components/DependencyInjection/Definition.php
Expand Up @@ -22,7 +22,7 @@ class Definition
{
protected $class;
protected $file;
protected $constructor;
protected $factoryMethod;
protected $shared;
protected $arguments;
protected $calls;
Expand All @@ -45,27 +45,27 @@ public function __construct($class, array $arguments = array())
}

/**
* Sets the constructor method.
* Sets the factory method able to create an instance of this class.
*
* @param string $method The method name
*
* @return Definition The current instance
*/
public function setConstructor($method)
public function setFactoryMethod($method)
{
$this->constructor = $method;
$this->factoryMethod = $method;

return $this;
}

/**
* Gets the constructor method.
* Gets the factory method.
*
* @return Definition The constructor method name
* @return Definition The factory method name
*/
public function getConstructor()
public function getFactoryMethod()
{
return $this->constructor;
return $this->factoryMethod;
}

/**
Expand All @@ -83,7 +83,7 @@ public function setClass($class)
}

/**
* Sets the constructor method.
* Sets the service class.
*
* @return string The service class
*/
Expand All @@ -93,7 +93,7 @@ public function getClass()
}

/**
* Sets the constructor arguments to pass to the service constructor.
* Sets the arguments to pass to the service constructor/factory method.
*
* @param array $arguments An array of arguments
*
Expand All @@ -107,7 +107,7 @@ public function setArguments(array $arguments)
}

/**
* Adds a constructor argument to pass to the service constructor.
* Adds an argument to pass to the service constructor/factory method.
*
* @param mixed $argument An argument
*
Expand All @@ -121,7 +121,7 @@ public function addArgument($argument)
}

/**
* Gets the constructor arguments to pass to the service constructor.
* Gets the arguments to pass to the service constructor/factory method.
*
* @return array The array of arguments
*/
Expand Down
Expand Up @@ -92,8 +92,8 @@ protected function addServiceInstance($id, $definition)
$arguments[] = $this->dumpValue($value);
}

if (null !== $definition->getConstructor()) {
$code = sprintf(" \$instance = call_user_func(array(%s, '%s')%s);\n", $class, $definition->getConstructor(), $arguments ? ', '.implode(', ', $arguments) : '');
if (null !== $definition->getFactoryMethod()) {
$code = sprintf(" \$instance = call_user_func(array(%s, '%s')%s);\n", $class, $definition->getFactoryMethod(), $arguments ? ', '.implode(', ', $arguments) : '');
} elseif ($class != "'".str_replace('\\', '\\\\', $definition->getClass())."'") {
$code = sprintf(" \$class = %s;\n \$instance = new \$class(%s);\n", $class, implode(', ', $arguments));
} else {
Expand Down
Expand Up @@ -50,7 +50,7 @@ protected function addService($id, $definition)
$code = sprintf(" <service id=\"%s\" class=\"%s\"%s%s>\n",
$id,
$definition->getClass(),
$definition->getConstructor() ? sprintf(' constructor="%s"', $definition->getConstructor()) : '',
$definition->getFactoryMethod() ? sprintf(' factory-method="%s"', $definition->getFactoryMethod()) : '',
!$definition->isShared() ? ' shared="false"' : ''
);

Expand Down
Expand Up @@ -62,8 +62,8 @@ protected function addService($id, $definition)
$code .= sprintf(" file: %s\n", $definition->getFile());
}

if ($definition->getConstructor()) {
$code .= sprintf(" constructor: %s\n", $definition->getConstructor());
if ($definition->getFactoryMethod()) {
$code .= sprintf(" factory_method: %s\n", $definition->getFactoryMethod());
}

if ($definition->getArguments()) {
Expand Down
Expand Up @@ -134,9 +134,9 @@ protected function parseDefinition(BuilderConfiguration $configuration, $id, $se

$definition = new Definition((string) $service['class']);

foreach (array('shared', 'constructor') as $key) {
foreach (array('shared', 'factory-method') as $key) {
if (isset($service[$key])) {
$method = 'set'.ucfirst($key);
$method = 'set'.str_replace('-', '', $key);
$definition->$method((string) $service->getAttributeAsPhp($key));
}
}
Expand Down
Expand Up @@ -137,8 +137,8 @@ protected function parseDefinition(BuilderConfiguration $configuration, $id, $se
$definition->setShared($service['shared']);
}

if (isset($service['constructor'])) {
$definition->setConstructor($service['constructor']);
if (isset($service['factory_method'])) {
$definition->setFactoryMethod($service['factory_method']);
}

if (isset($service['file'])) {
Expand Down
Expand Up @@ -89,7 +89,7 @@
<xsd:attribute name="id" type="xsd:string" />
<xsd:attribute name="class" type="xsd:string" />
<xsd:attribute name="shared" type="boolean" />
<xsd:attribute name="constructor" type="xsd:string" />
<xsd:attribute name="factory-method" type="xsd:string" />
<xsd:attribute name="alias" type="xsd:string" />
</xsd:complexType>

Expand Down
Expand Up @@ -232,14 +232,14 @@ public function testCreateServiceArguments()
/**
* @covers Symfony\Components\DependencyInjection\Builder::createService
*/
public function testCreateServiceConstructor()
public function testCreateServiceFactoryMethod()
{
$builder = new Builder();
$builder->register('bar', 'stdClass');
$builder->register('foo1', 'FooClass')->setConstructor('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
$builder->register('foo1', 'FooClass')->setFactoryMethod('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
$builder->setParameter('value', 'bar');
$this->assertTrue($builder->get('foo1')->called, '->createService() calls the constructor to create the service instance');
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar')), $builder->get('foo1')->arguments, '->createService() passes the arguments to the constructor');
$this->assertTrue($builder->get('foo1')->called, '->createService() calls the factory method to create the service instance');
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar')), $builder->get('foo1')->arguments, '->createService() passes the arguments to the factory method');
}

/**
Expand Down
Expand Up @@ -27,14 +27,14 @@ public function testConstructor()
}

/**
* @covers Symfony\Components\DependencyInjection\Definition::setConstructor
* @covers Symfony\Components\DependencyInjection\Definition::getConstructor
* @covers Symfony\Components\DependencyInjection\Definition::setFactoryMethod
* @covers Symfony\Components\DependencyInjection\Definition::getFactoryMethod
*/
public function testSetGetConstructor()
{
$def = new Definition('stdClass');
$this->assertEquals(spl_object_hash($def), spl_object_hash($def->setConstructor('foo')), '->setConstructor() implements a fluent interface');
$this->assertEquals('foo', $def->getConstructor(), '->getConstructor() returns the constructor name');
$this->assertEquals(spl_object_hash($def), spl_object_hash($def->setFactoryMethod('foo')), '->setFactoryMethod() implements a fluent interface');
$this->assertEquals('foo', $def->getFactoryMethod(), '->getFactoryMethod() returns the factory method name');
}

/**
Expand Down
Expand Up @@ -12,7 +12,7 @@
register('foo', 'FooClass')->
addAnnotation('foo', array('foo' => 'foo'))->
addAnnotation('foo', array('bar' => 'bar'))->
setConstructor('getInstance')->
setFactoryMethod('getInstance')->
setArguments(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, new Reference('service_container')))->
setFile(realpath(__DIR__.'/../includes/foo.php'))->
setShared(false)->
Expand All @@ -28,7 +28,7 @@
;
$container->
register('foo.baz', '%baz_class%')->
setConstructor('getInstance')->
setFactoryMethod('getInstance')->
setConfigurator(array('%baz_class%', 'configureStatic1'))
;
$container->register('foo_bar', '%foo_class%');
Expand Down
Expand Up @@ -8,7 +8,7 @@
<service id="baz" class="BazClass" />
<service id="shared" class="FooClass" shared="true" />
<service id="non_shared" class="FooClass" shared="false" />
<service id="constructor" class="FooClass" constructor="getInstance" />
<service id="constructor" class="FooClass" factory-method="getInstance" />
<service id="file" class="FooClass">
<file>%path%/foo.php</file>
</service>
Expand Down
Expand Up @@ -9,7 +9,7 @@
<parameter key="foo">bar</parameter>
</parameters>
<services>
<service id="foo" class="FooClass" constructor="getInstance" shared="false">
<service id="foo" class="FooClass" factory-method="getInstance" shared="false">
<annotation name="foo" foo="foo" />
<annotation name="foo" bar="bar" />
<file>%path%foo.php</file>
Expand All @@ -33,7 +33,7 @@
<argument>%foo_bar%</argument>
<configurator service="foo.baz" method="configure" />
</service>
<service id="foo.baz" class="%baz_class%" constructor="getInstance">
<service id="foo.baz" class="%baz_class%" factory-method="getInstance">
<configurator class="%baz_class%" method="configureStatic1" />
</service>
<service id="foo_bar" class="%foo_class%">
Expand Down
Expand Up @@ -3,7 +3,7 @@ services:
baz: { class: BazClass }
shared: { class: FooClass, shared: true }
non_shared: { class: FooClass, shared: false }
constructor: { class: FooClass, constructor: getInstance }
constructor: { class: FooClass, factory_method: getInstance }
file: { class: FooClass, file: %path%/foo.php }
arguments: { class: FooClass, arguments: [foo, @foo, [true, false]] }
configurator1: { class: FooClass, configurator: sc_configure }
Expand Down
Expand Up @@ -10,7 +10,7 @@ services:
- { name: foo, foo: foo }
- { name: foo, bar: bar }
file: %path%foo.php
constructor: getInstance
factory_method: getInstance
arguments: [foo, '@foo.baz', { '%foo%': 'foo is %foo%', bar: '%foo%' }, true, '@service_container']
calls:
- [setBar, [bar]]
Expand All @@ -24,7 +24,7 @@ services:
configurator: ['@foo.baz', configure]
foo.baz:
class: %baz_class%
constructor: getInstance
factory_method: getInstance
configurator: ['%baz_class%', configureStatic1]
foo_bar:
class: %foo_class%
Expand Down
Expand Up @@ -119,7 +119,7 @@ public function testLoadServices()
$this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
$this->assertTrue($services['shared']->isShared(), '->load() parses the shared attribute');
$this->assertFalse($services['non_shared']->isShared(), '->load() parses the shared attribute');
$this->assertEquals('getInstance', $services['constructor']->getConstructor(), '->load() parses the constructor attribute');
$this->assertEquals('getInstance', $services['constructor']->getFactoryMethod(), '->load() parses the factory-method attribute');
$this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
$this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
$this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
Expand Down
Expand Up @@ -86,7 +86,7 @@ public function testLoadServices()
$this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
$this->assertTrue($services['shared']->isShared(), '->load() parses the shared attribute');
$this->assertFalse($services['non_shared']->isShared(), '->load() parses the shared attribute');
$this->assertEquals('getInstance', $services['constructor']->getConstructor(), '->load() parses the constructor attribute');
$this->assertEquals('getInstance', $services['constructor']->getFactoryMethod(), '->load() parses the factory_method attribute');
$this->assertEquals('%path%/foo.php', $services['file']->getFile(), '->load() parses the file tag');
$this->assertEquals(array('foo', new Reference('foo'), array(true, false)), $services['arguments']->getArguments(), '->load() parses the argument tags');
$this->assertEquals('sc_configure', $services['configurator1']->getConfigurator(), '->load() parses the configurator tag');
Expand Down

0 comments on commit 4bbf2ae

Please sign in to comment.