-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
|
@@ -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 | ||
|
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 | ||
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. I don't get this line: if it checks something, 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. 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. Personally, I don't like this trick with |
||
|
||
$reflector = self::$reflections[$class] = $reflectionClass; | ||
} catch (\InvalidArgumentException $e) { | ||
$reflector = false; | ||
} | ||
|
||
return self::$reflections[$class] = $reflector; | ||
} | ||
} |
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.
Instead of injecting an object, I suggest a string:
$reflectionClassClassname = \ReflectionClass::class
Could it be made to work this way?
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.
No, because is neccessary to call
__toString
method for GoAOP.