-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[DI] Improve PSR4-based service discovery #23986
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,13 +51,6 @@ public function __construct(ContainerBuilder $container, FileLocatorInterface $l | |
*/ | ||
public function registerClasses(Definition $prototype, $namespace, $resource, $exclude = null) | ||
{ | ||
if ('\\' !== substr($namespace, -1)) { | ||
throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": %s.', $namespace)); | ||
} | ||
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) { | ||
throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: %s.', $namespace)); | ||
} | ||
|
||
$classes = $this->findClasses($namespace, $resource, $exclude); | ||
// prepare for deep cloning | ||
$prototype = serialize($prototype); | ||
|
@@ -123,15 +116,13 @@ private function findClasses($namespace, $pattern, $excludePattern) | |
if (!preg_match($extRegexp, $path, $m) || !$info->isReadable()) { | ||
continue; | ||
} | ||
$class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -strlen($m[0]))), '\\'); | ||
|
||
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $class)) { | ||
if (!$class = $this->getClassFromFile($path)) { | ||
// no class found in file | ||
continue; | ||
} | ||
// check to make sure the expected class exists | ||
if (!$r = $this->container->getReflectionClass($class)) { | ||
throw new InvalidArgumentException(sprintf('Expected to find class "%s" in file "%s" while importing services from resource "%s", but it was not found! Check the namespace prefix used with the resource.', $class, $path, $pattern)); | ||
} | ||
|
||
$r = $this->container->getReflectionClass($class); | ||
|
||
if (!$r->isInterface() && !$r->isTrait() && !$r->isAbstract()) { | ||
$classes[] = $class; | ||
|
@@ -149,4 +140,46 @@ private function findClasses($namespace, $pattern, $excludePattern) | |
|
||
return $classes; | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* | ||
* @return string|null | ||
* | ||
* @see http://jarretbyrne.com/2015/06/197/ | ||
*/ | ||
private function getClassFromFile($path) | ||
{ | ||
$contents = file_get_contents($path); | ||
$class = ''; | ||
$parsingNamespace = false; | ||
$parsingClass = false; | ||
|
||
foreach (token_get_all($contents) as $token) { | ||
if (is_array($token) && $token[0] === T_NAMESPACE) { | ||
$parsingNamespace = true; | ||
} | ||
|
||
if (is_array($token) && $token[0] === T_CLASS) { | ||
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. have you testing your code on a file containing anonymous classes ? |
||
$parsingClass = true; | ||
} | ||
|
||
if ($parsingNamespace) { | ||
if (is_array($token) && in_array($token[0], array(T_STRING, T_NS_SEPARATOR), true)) { | ||
$class .= $token[1]; | ||
} | ||
|
||
if ($token === ';') { | ||
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. what about code using the bracketed notation for namespaces ? |
||
$parsingNamespace = false; | ||
} | ||
} | ||
|
||
if ($parsingClass && is_array($token) && $token[0] === T_STRING) { | ||
$class .= '\\'.$token[1]; | ||
$parsingClass = false; | ||
} | ||
} | ||
|
||
return class_exists($class) ? $class : null; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...onent/DependencyInjection/Tests/Fixtures/Prototype/OtherDir/Component1/Dir1/NotAClass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
function foo() { | ||
return 'bar'; | ||
} |
7 changes: 7 additions & 0 deletions
7
...ponent/DependencyInjection/Tests/Fixtures/Prototype/OtherDir/Component1/Dir1/Service1.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\Component1\Dir1; | ||
|
||
class Service1 | ||
{ | ||
} |
8 changes: 8 additions & 0 deletions
8
...ponent/DependencyInjection/Tests/Fixtures/Prototype/OtherDir/Component1/Dir2/Service2.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\Component1\Dir2; | ||
|
||
class Service2 | ||
{ | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...ponent/DependencyInjection/Tests/Fixtures/Prototype/OtherDir/Component1/Dir3/Service3.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\Component1\Dir3; | ||
|
||
class Service3 | ||
{ | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...ponent/DependencyInjection/Tests/Fixtures/Prototype/OtherDir/Component2/Dir1/Service4.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\Component2\Dir1; | ||
|
||
class Service4 | ||
{ | ||
} |
8 changes: 8 additions & 0 deletions
8
...ponent/DependencyInjection/Tests/Fixtures/Prototype/OtherDir/Component2/Dir2/Service5.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\Component2\Dir2; | ||
|
||
class Service5 | ||
{ | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_prototype_glob.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
services: | ||
dir1: | ||
resource: ../Prototype/OtherDir/*/Dir1 | ||
tags: [foo] | ||
|
||
dir2: | ||
resource: ../Prototype/OtherDir/*/Dir2 | ||
tags: [bar] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think this may still fail if the parent class is missing. Can you confirm @nicolas-grekas ?
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.
not anymore
but anyway, this implementation is going to change completly (see my other comments)