Skip to content

[DependencyInjection] Add autowiring capabilities #15613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix @fabpot and @stof comments.
  • Loading branch information
dunglas committed Oct 2, 2015
commit 3b7f5538643a5b25e8e5c4060a50826fc34d59f9
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,19 @@ public function process(ContainerBuilder $container)
*/
private function completeDefinition($id, Definition $definition)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this method can be simplified if we use PropertyInfo component ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. What do you have in mind?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it might be useful for guessing types

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's useful here because the PHP reflection is enough. We only need typehint information.

{
if (!($reflectionClass = $this->getReflectionClass($id, $definition))) {
if (!$reflectionClass = $this->getReflectionClass($id, $definition)) {
return;
}

$this->container->addClassResource($reflectionClass);

if (!($constructor = $reflectionClass->getConstructor())) {
if (!$constructor = $reflectionClass->getConstructor()) {
return;
}

$arguments = $definition->getArguments();
foreach ($constructor->getParameters() as $index => $parameter) {
if (!($typeHint = $parameter->getClass())) {
if (!$typeHint = $parameter->getClass()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the case of non-existent classes still need to be handled

continue;
}

Expand Down Expand Up @@ -243,6 +244,10 @@ private function getReflectionClass($id, Definition $definition)

$class = $this->container->getParameterBag()->resolveValue($class);

return $this->reflectionClasses[$id] = new \ReflectionClass($class);
try {
return $this->reflectionClasses[$id] = new \ReflectionClass($class);
} catch (\ReflectionException $reflectionException) {
// return null
}
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ public function getConfigurator()
*
* @return Definition The current instance
*/
public function setTypes(array $types)
public function setAutowiringTypes(array $types)
{
$this->autowiringTypes = array();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,20 @@ private function parseDefinition($id, $service, $file)
}

if (isset($service['autowiring_types'])) {
if (!is_array($service['autowiring_types'])) {
throw new InvalidArgumentException(sprintf('Parameter "autowiring_types" must be 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));
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));
}

$definition->addAutowiringType($autowiringType);
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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public function testTypes()
$def = new Definition('stdClass');

$this->assertEquals(array(), $def->getAutowiringTypes());
$this->assertSame($def, $def->setTypes(array('Foo')));
$this->assertSame($def, $def->setAutowiringTypes(array('Foo')));
$this->assertEquals(array('Foo'), $def->getAutowiringTypes());
$this->assertSame($def, $def->addAutowiringType('Bar'));
$this->assertTrue($def->hasAutowiringType('Bar'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ services:
foo_service:
class: FooClass
# types is not an array
autowiring_types: string
autowiring_types: 1
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ services:
foo_service:
class: FooClass
autowiring_types: [ Foo, Bar ]

baz_service:
class: Baz
autowiring_types: Foo
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ public function testTypes()
$loader->load('services22.yml');

$this->assertEquals(array('Foo', 'Bar'), $container->getDefinition('foo_service')->getAutowiringTypes());
$this->assertEquals(array('Foo'), $container->getDefinition('baz_service')->getAutowiringTypes());
}

public function testAutowire()
Expand Down
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