Skip to content

[DependencyInjection] Autowire public typed properties #34769

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

Merged
merged 1 commit into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.1.0
-----

* added support to autowire public typed properties in php 7.4

5.0.0
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\TypedReference;

/**
* Looks for definitions with autowiring enabled and registers their corresponding "@required" properties.
*
* @author Sebastien Morel (Plopix) <morel.seb@gmail.com>
* @author Nicolas Grekas <p@tchwork.com>
*/
class AutowireRequiredPropertiesPass extends AbstractRecursivePass
{
/**
* {@inheritdoc}
*/
protected function processValue($value, bool $isRoot = false)
{
if (\PHP_VERSION_ID < 70400) {
return $value;
}
$value = parent::processValue($value, $isRoot);

if (!$value instanceof Definition || !$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
return $value;
}
if (!$reflectionClass = $this->container->getReflectionClass($value->getClass(), false)) {
return $value;
}

$properties = $value->getProperties();
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
if (false === $doc = $reflectionProperty->getDocComment()) {
continue;
}
if (false === stripos($doc, '@required') || !preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@required(?:\s|\*/$)#i', $doc)) {
continue;
}
if (\array_key_exists($name = $reflectionProperty->getName(), $properties)) {
continue;
}

$type = $reflectionProperty->getType()->getName();
$value->setProperty($name, new TypedReference($type, $type, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $name));
}

return $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function __construct()
new ResolveFactoryClassPass(),
new ResolveNamedArgumentsPass(),
new AutowireRequiredMethodsPass(),
new AutowireRequiredPropertiesPass(),
new ResolveBindingsPass(),
new ServiceLocatorTagPass(),
new CheckDefinitionValidityPass(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredPropertiesPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

require_once __DIR__.'/../Fixtures/includes/autowiring_classes.php';

if (\PHP_VERSION_ID >= 70400) {
require_once __DIR__.'/../Fixtures/includes/autowiring_classes_74.php';
}

/**
* @requires PHP 7.4
*/
class AutowireRequiredPropertiesPassTest extends TestCase
{
public function testInjection()
{
$container = new ContainerBuilder();
$container->register(Bar::class);
$container->register(A::class);
$container->register(B::class);
$container->register(PropertiesInjection::class)->setAutowired(true);

(new ResolveClassPass())->process($container);
(new AutowireRequiredPropertiesPass())->process($container);

$properties = $container->getDefinition(PropertiesInjection::class)->getProperties();

$this->assertArrayHasKey('plop', $properties);
$this->assertEquals(Bar::class, (string) $properties['plop']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Compiler;

class PropertiesInjection
{
/**
* @required
*/
public Bar $plop;

public function __construct(A $a)
{
}
}