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

Skip to content

[DependencyInjection] Add ReflectionHelper for native or static reflection of class #18578

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 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
"egulias/email-validator": "~1.2",
"symfony/polyfill-apcu": "~1.1",
"symfony/security-acl": "~2.8|~3.0",
"phpdocumentor/reflection-docblock": "^3.0"
"phpdocumentor/reflection-docblock": "^3.0",
"goaop/parser-reflection": "^1.0"
},
"conflict": {
"phpdocumentor/reflection-docblock": "<3.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Util\ReflectionHelper;

/**
* Guesses constructor arguments of services definitions and try to instantiate services if necessary.
Expand All @@ -29,6 +30,12 @@ class AutowirePass implements CompilerPassInterface
private $definedTypes = array();
private $types;
private $ambiguousServiceTypes = array();
private $reflectionHelper;

public function __construct(ReflectionHelper $reflectionHelper = null)
Copy link
Member

Choose a reason for hiding this comment

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

Instead of injecting an object, I suggest a string:
$reflectionClassClassname = \ReflectionClass::class
Could it be made to work this way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, because is neccessary to call __toString method for GoAOP.

{
$this->reflectionHelper = $reflectionHelper ?: new ReflectionHelper();
}

/**
* {@inheritdoc}
Expand Down Expand Up @@ -293,11 +300,7 @@ private function getReflectionClass($id, Definition $definition)

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

try {
return $this->reflectionClasses[$id] = new \ReflectionClass($class);
} catch (\ReflectionException $reflectionException) {
// return null
}
return $this->reflectionClasses[$id] = $this->reflectionHelper->getReflectionClass($class);
}

private function addServiceToAmbiguousType($id, $type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Util\ReflectionHelper;

/**
* @author Kévin Dunglas <[email protected]>
Expand Down Expand Up @@ -446,6 +447,24 @@ public function getCreateResourceTests()
['ClassChangedConstructorArgs', false],
);
}

public function testIgnoreServiceWithClassNotExisting()
{
$container = new ContainerBuilder();

$container->register('class_not_exist', __NAMESPACE__.'\OptionalServiceClass');

$barDefinition = $container->register('bar', __NAMESPACE__.'\Bar');
$barDefinition->setAutowired(true);

$pass = new AutowirePass();
$oldSetup = ReflectionHelper::preferStaticReflection(true);

$pass->process($container);
ReflectionHelper::preferStaticReflection($oldSetup);

$this->assertTrue($container->hasDefinition('bar'));
}
}

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

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 Symfony\Bug\NotExistClass;

class OptionalServiceClass extends NotExistClass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

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

namespace Symfony\Component\DependencyInjection\Util;

use Go\ParserReflection\ReflectionClass;

/**
* ReflectionHelper provides method for native or static reflection of class.
*
* @author Martin Hasoň <[email protected]>
*/
class ReflectionHelper
{
private static $staticReflection = false;
private static $reflections = array();
private $existsGoAopReflection;

public function __construct()
{
$this->existsGoAopReflection = class_exists(ReflectionClass::class);
}

public static function preferStaticReflection($use)
{
$oldSetup = self::$staticReflection;
self::$staticReflection = (bool) $use;

return $oldSetup;
}

/**
* Returns reflection instance for class.
*
* @param string $class Full name of the class
*
* @return \ReflectionClass|false
*/
public function getReflectionClass($class)
{
if (isset(self::$reflections[$class])) {
return self::$reflections[$class];
}

if (!self::$staticReflection || class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) {
return $this->getNativeReflection($class);
}

if ($this->existsGoAopReflection) {
return $this->getGoParserReflection($class);
}

return $this->getNativeReflection($class);
}

private function getNativeReflection($class)
{
try {
$reflector = self::$reflections[$class] = new \ReflectionClass($class);
} catch (\ReflectionException $e) {
$reflector = false;
}

return self::$reflections[$class] = $reflector;
}

private function getGoParserReflection($class)
{
try {
$reflectionClass = new ReflectionClass($class);
$reflectionClass->__toString(); // checks that reflection is complete and valid
Copy link
Member

Choose a reason for hiding this comment

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

I don't get this line: if it checks something, __toString isn't allowed to throw any exception, which means the check can't has any catchable side effect. Am I wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Personally, I don't like this trick with __toString, it will be better to call getParentClass() + getInterfaces() + getTraits() instead to verify all this stuff with unavailable parent classes.


$reflector = self::$reflections[$class] = $reflectionClass;
} catch (\InvalidArgumentException $e) {
$reflector = false;
}

return self::$reflections[$class] = $reflector;
}
}
6 changes: 4 additions & 2 deletions src/Symfony/Component/DependencyInjection/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
"require-dev": {
"symfony/yaml": "~2.8|~3.0",
"symfony/config": "~2.8|~3.0",
"symfony/expression-language": "~2.8|~3.0"
"symfony/expression-language": "~2.8|~3.0",
"goaop/parser-reflection": "^1.0"
},
"suggest": {
"symfony/yaml": "",
"symfony/config": "",
"symfony/proxy-manager-bridge": "Generate service proxies to lazy load them"
"symfony/proxy-manager-bridge": "Generate service proxies to lazy load them",
"goaop/parser-reflection": "Use static reflection for autowiring"
},
"autoload": {
"psr-4": { "Symfony\\Component\\DependencyInjection\\": "" },
Expand Down