-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI][DX] Allow exclude to be an array of patterns #27075
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
Conversation
a879aad
to
029b553
Compare
Ah nice, this will make life a lot easier for complex exclusion rules in existing codebases! |
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.
looks great, thanks for taking over!
@@ -146,7 +146,16 @@ private function parseDefinitions(\DOMDocument $xml, $file, $defaults) | |||
foreach ($services as $service) { | |||
if (null !== $definition = $this->parseDefinition($service, $file, $defaults)) { | |||
if ('prototype' === $service->tagName) { | |||
$this->registerClasses($definition, (string) $service->getAttribute('namespace'), (string) $service->getAttribute('resource'), (string) $service->getAttribute('exclude')); | |||
$excludes = array_map(function ($node) { return $node->nodeValue; }, $this->getChildren($service, 'exclude')); |
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.
would array_column work here instead of array_map?
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.
nope. We need a property access, not an array access
if ($service->hasAttribute('exclude')) { | ||
$excludes[] = (string) $service->getAttribute('exclude'); | ||
} | ||
$this->registerClasses( |
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.
should be kept on one line - it would better fit our CS IMHO
$definition, | ||
(string) $service->getAttribute('namespace'), | ||
(string) $service->getAttribute('resource'), | ||
(array) $excludes |
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.
it's already an array, no need to cast I suppose
@@ -146,7 +146,16 @@ private function parseDefinitions(\DOMDocument $xml, $file, $defaults) | |||
foreach ($services as $service) { | |||
if (null !== $definition = $this->parseDefinition($service, $file, $defaults)) { | |||
if ('prototype' === $service->tagName) { | |||
$this->registerClasses($definition, (string) $service->getAttribute('namespace'), (string) $service->getAttribute('resource'), (string) $service->getAttribute('exclude')); | |||
$excludes = array_map(function ($node) { return $node->nodeValue; }, $this->getChildren($service, 'exclude')); | |||
if ($service->hasAttribute('exclude')) { |
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 would allow only tags XOR attribute (like done in similar cases in the loader AFAIK
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.
So in cas both are present, should I throw an exception?
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 would say yes
This is awesome, great job! |
3a287e2
to
246640f
Compare
I've updated the PR according to the changes. I'll do a PR for symfony doc too if needed. |
$excludes = array_column($this->getChildren($service, 'exclude'), 'nodeValue'); | ||
if ($service->hasAttribute('exclude')) { | ||
if (count($excludes) > 0) { | ||
throw new InvalidArgumentException('You cannot use both attribute "exclude" and <exclude> tags at the same time.'); |
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.
both the "exclude" attribute and ...
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.
done
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.
(would be OK on 4.1 on my side, the topic has been on the table for a few months already).
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.
Same, would be great to have this in 4.1. We already missed the opportunity to have it in 3.4/4.0.
Thanks for taking over @magnetik .
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.
👍 for 4.1
* @param Definition $prototype A definition to use as template | ||
* @param string $namespace The namespace prefix of classes in the scanned directory | ||
* @param string $resource The directory to look for classes, glob-patterns allowed | ||
* @param string|string[]|null $exclude A globed path of files to exclude or an array of globed paths of files to exclude |
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.
globbed? (not sure but I think so)
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.
done (both occurrences)
Thank you @magnetik. |
…netik) This PR was merged into the 4.2-dev branch. Discussion ---------- [DI][DX] Allow exclude to be an array of patterns | Q | A | ------------- | --- | Branch? | 4.2 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23956 | License | MIT This is basically continuing #24428. In YAML before: ```yaml AppBundle\: resource: '../../src/AppBundle/*' exclude: '../../src/AppBundle/{Entity,Payload,Repository}' ``` in YAML after: ```yaml AppBundle\: resource: '../../src/AppBundle/*' exclude: - '../../src/AppBundle/{Entity,Payload,Repository}' - '../../src/AppBundle/Event/*Event.php' ``` In XML before: ```xml <prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Migrations,Tests}" /> ``` in XML after: ```xml <prototype namespace="App\" resource="../src/*"> <exclude>../src/{Entity,Migrations,Tests}</exclude> <exclude>../src/Yolo</exclude> </prototype> ``` In PHP before: ```php $di->load(Prototype::class.'\\', '../Prototype') ->autoconfigure() ->exclude('../Prototype/{OtherDir,BadClasses}') ``` In PHP after: ```php $di->load(Prototype::class.'\\', '../Prototype') ->autoconfigure() ->exclude(['../Prototype/OtherDir', '../Prototype/BadClasses']) ``` Everything is backward compatible. Maybe a decision about handling both attribute exclude and element exclude in XML should be taken. Commits ------- 3ae3a03 [DI][DX] Allow exclude to be an array of patterns (taking #24428 over)
This is basically continuing #24428.
In YAML before:
in YAML after:
In XML before:
in XML after:
In PHP before:
In PHP after:
Everything is backward compatible.
Maybe a decision about handling both attribute exclude and element exclude in XML should be taken.