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

Skip to content

[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

Merged
merged 1 commit into from
May 9, 2018

Conversation

magnetik
Copy link
Contributor

@magnetik magnetik commented Apr 27, 2018

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:

AppBundle\:
  resource: '../../src/AppBundle/*'
  exclude: '../../src/AppBundle/{Entity,Payload,Repository}'

in YAML after:

AppBundle\:
  resource: '../../src/AppBundle/*'
  exclude:
    - '../../src/AppBundle/{Entity,Payload,Repository}'
    - '../../src/AppBundle/Event/*Event.php'

In XML before:

<prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Migrations,Tests}" />

in XML after:

<prototype namespace="App\" resource="../src/*">
  <exclude>../src/{Entity,Migrations,Tests}</exclude>
  <exclude>../src/Yolo</exclude>
</prototype>

In PHP before:

$di->load(Prototype::class.'\\', '../Prototype')
        ->autoconfigure()
        ->exclude('../Prototype/{OtherDir,BadClasses}')

In PHP after:

    $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.

@carsonbot carsonbot added Status: Needs Review DependencyInjection DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature Deprecation labels Apr 27, 2018
@magnetik magnetik force-pushed the exclude-array branch 2 times, most recently from a879aad to 029b553 Compare April 27, 2018 13:59
@linaori
Copy link
Contributor

linaori commented Apr 27, 2018

Ah nice, this will make life a lot easier for complex exclusion rules in existing codebases!

Copy link
Member

@nicolas-grekas nicolas-grekas left a 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'));
Copy link
Member

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?

Copy link
Member

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(
Copy link
Member

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
Copy link
Member

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')) {
Copy link
Member

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

Copy link
Contributor Author

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?

Copy link
Member

Choose a reason for hiding this comment

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

I would say yes

@TomasVotruba
Copy link
Contributor

This is awesome, great job!

@magnetik magnetik force-pushed the exclude-array branch 2 times, most recently from 3a287e2 to 246640f Compare May 2, 2018 12:14
@magnetik
Copy link
Contributor Author

magnetik commented May 2, 2018

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.');
Copy link
Member

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 ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

@nicolas-grekas nicolas-grekas left a 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).

@nicolas-grekas nicolas-grekas changed the title [DI][DX] Allow exclude to be an array of patterns (taking #24428 over) [DI][DX] Allow exclude to be an array of patterns May 4, 2018
Copy link
Contributor

@ogizanagi ogizanagi left a 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 .

Copy link
Member

@chalasr chalasr left a 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
Copy link
Member

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)

Copy link
Contributor Author

@magnetik magnetik May 9, 2018

Choose a reason for hiding this comment

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

done (both occurrences)

@fabpot
Copy link
Member

fabpot commented May 9, 2018

Thank you @magnetik.

@fabpot fabpot merged commit 3ae3a03 into symfony:master May 9, 2018
fabpot added a commit that referenced this pull request May 9, 2018
…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)
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.1 Nov 1, 2018
This was referenced Nov 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DependencyInjection DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature Status: Reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants