-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
Changes from 1 commit
9828da2
195f6c0
8d23c9d
27f4de8
5575ec1
edce23d
07d475e
afe009a
7ab5b08
a7ca3f2
57c494c
e642aa8
d00c7e3
55bb42c
7806487
585616d
c717c69
3b7f553
936a16a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,8 @@ | |
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class AutowiringPass implements CompilerPassInterface | ||
class AutowirePass implements CompilerPassInterface | ||
{ | ||
const AUTOWIRING = 'autowiring'; | ||
|
||
private $container; | ||
private $reflectionClasses = array(); | ||
private $definedTypes = array(); | ||
|
@@ -38,7 +36,7 @@ public function process(ContainerBuilder $container) | |
{ | ||
$this->container = $container; | ||
foreach ($container->getDefinitions() as $id => $definition) { | ||
if ($definition->hasTag(self::AUTOWIRING)) { | ||
if ($definition->isAutowired()) { | ||
$this->completeDefinition($id, $definition); | ||
} | ||
} | ||
|
@@ -64,6 +62,7 @@ private function completeDefinition($id, Definition $definition) | |
if (!($reflectionClass = $this->getReflectionClass($id, $definition))) { | ||
return; | ||
} | ||
$this->container->addClassResource($reflectionClass); | ||
|
||
if (!($constructor = $reflectionClass->getConstructor())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
return; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ class Definition | |
private $synchronized = false; | ||
private $lazy = false; | ||
private $decoratedService; | ||
private $autowired = false; | ||
private $autowiringTypes = array(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you might want change this prop. name with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
protected $arguments; | ||
|
@@ -838,6 +839,30 @@ public function setTypes(array $types) | |
return $this; | ||
} | ||
|
||
/** | ||
* Is the definition autowired? | ||
* | ||
* @return bool | ||
*/ | ||
public function isAutowired() | ||
{ | ||
return $this->autowired; | ||
} | ||
|
||
/** | ||
* Sets autowired. | ||
* | ||
* @param $autowired | ||
* | ||
* @return Definition The current instance | ||
*/ | ||
public function setAutowired($autowired) | ||
{ | ||
$this->autowired = $autowired; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Gets autowiring types that will default to this definition. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,6 +299,10 @@ private function parseDefinition($id, $service, $file) | |
$definition->setDecoratedService($service['decorates'], $renameId, $priority); | ||
} | ||
|
||
if (isset($service['autowire'])) { | ||
$definition->setAutowired($service['autowire']); | ||
} | ||
|
||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As having only one type is quite common, I would allow a single string to be passed here. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="bar" class="Bar" autowire="true" /> | ||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
services: | ||
bar_service: | ||
class: BarClass | ||
autowire: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra parenthesis are not needed.