Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d84e92c

Browse files
committed
feature #34769 [DependencyInjection] Autowire public typed properties (Plopix)
This PR was merged into the 5.1-dev branch. Discussion ---------- [DependencyInjection] Autowire public typed properties | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT ### Description This PR adds the Autowiring of **public typed properties** in PHP 7.4. It is only on "public" properties. It could let people think that services are better injected in "public". I don't know what to think about it, you? ### How about "private" properties - further thinking Even if I think that it would be awesome to be able to inject in "private" properties, we discussed it with @nicolas-grekas, and I agree Symfony should not break any standard logic. If the property is private then it is private the DI cannot touch it. But that could/would remove a lot of boilerplate, and if it is declarative, that might still be something to do. Maybe we could introduce a new annotation for injection in "private": `@requiredPrivated` ? Commits ------- cad7fbb [DI] Autowire public typed properties
2 parents 24ed8e1 + cad7fbb commit d84e92c

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
7+
* added support to autowire public typed properties in php 7.4
8+
49
5.0.0
510
-----
611

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\ContainerInterface;
15+
use Symfony\Component\DependencyInjection\Definition;
16+
use Symfony\Component\DependencyInjection\TypedReference;
17+
18+
/**
19+
* Looks for definitions with autowiring enabled and registers their corresponding "@required" properties.
20+
*
21+
* @author Sebastien Morel (Plopix) <[email protected]>
22+
* @author Nicolas Grekas <[email protected]>
23+
*/
24+
class AutowireRequiredPropertiesPass extends AbstractRecursivePass
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
protected function processValue($value, bool $isRoot = false)
30+
{
31+
if (\PHP_VERSION_ID < 70400) {
32+
return $value;
33+
}
34+
$value = parent::processValue($value, $isRoot);
35+
36+
if (!$value instanceof Definition || !$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
37+
return $value;
38+
}
39+
if (!$reflectionClass = $this->container->getReflectionClass($value->getClass(), false)) {
40+
return $value;
41+
}
42+
43+
$properties = $value->getProperties();
44+
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
45+
if (false === $doc = $reflectionProperty->getDocComment()) {
46+
continue;
47+
}
48+
if (false === stripos($doc, '@required') || !preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@required(?:\s|\*/$)#i', $doc)) {
49+
continue;
50+
}
51+
if (\array_key_exists($name = $reflectionProperty->getName(), $properties)) {
52+
continue;
53+
}
54+
55+
$type = $reflectionProperty->getType()->getName();
56+
$value->setProperty($name, new TypedReference($type, $type, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $name));
57+
}
58+
59+
return $value;
60+
}
61+
}

src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function __construct()
5757
new ResolveFactoryClassPass(),
5858
new ResolveNamedArgumentsPass(),
5959
new AutowireRequiredMethodsPass(),
60+
new AutowireRequiredPropertiesPass(),
6061
new ResolveBindingsPass(),
6162
new ServiceLocatorTagPass(),
6263
new CheckDefinitionValidityPass(),
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredPropertiesPass;
16+
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
19+
require_once __DIR__.'/../Fixtures/includes/autowiring_classes.php';
20+
21+
if (\PHP_VERSION_ID >= 70400) {
22+
require_once __DIR__.'/../Fixtures/includes/autowiring_classes_74.php';
23+
}
24+
25+
/**
26+
* @requires PHP 7.4
27+
*/
28+
class AutowireRequiredPropertiesPassTest extends TestCase
29+
{
30+
public function testInjection()
31+
{
32+
$container = new ContainerBuilder();
33+
$container->register(Bar::class);
34+
$container->register(A::class);
35+
$container->register(B::class);
36+
$container->register(PropertiesInjection::class)->setAutowired(true);
37+
38+
(new ResolveClassPass())->process($container);
39+
(new AutowireRequiredPropertiesPass())->process($container);
40+
41+
$properties = $container->getDefinition(PropertiesInjection::class)->getProperties();
42+
43+
$this->assertArrayHasKey('plop', $properties);
44+
$this->assertEquals(Bar::class, (string) $properties['plop']);
45+
}
46+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
4+
5+
class PropertiesInjection
6+
{
7+
/**
8+
* @required
9+
*/
10+
public Bar $plop;
11+
12+
public function __construct(A $a)
13+
{
14+
}
15+
}

0 commit comments

Comments
 (0)