Skip to content

[DI][DX] Allow exclude to be an array of patterns #24428

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

[DI][DX] Allow exclude to be an array of patterns #24428

wants to merge 1 commit into from

Conversation

apetitpa
Copy link

@apetitpa apetitpa commented Oct 4, 2017

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #23956
License MIT
Doc PR not provided yet

Before:

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

After:

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

@carsonbot carsonbot added Status: Needs Review DependencyInjection DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature labels Oct 4, 2017
$loader->registerClasses(
new Definition(),
'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\',
'Prototype/*', array(
Copy link
Contributor

Choose a reason for hiding this comment

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

FileLoader::registerClasses docblock must be updated, right?

* @param string $exclude A globed path of files to exclude

Copy link
Author

Choose a reason for hiding this comment

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

You're absolutely right, I completely missed this one. My bad

Copy link
Author

Choose a reason for hiding this comment

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

Docblock updated

* @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|array $exclude A globed path of files to exclude or an array of globed paths of files to exclude
Copy link
Contributor

Choose a reason for hiding this comment

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

should it be string[] instad of array ?

if (null === $excludePrefix) {
$excludePrefix = $resource->getPrefix();
if ($excludePatterns) {
$excludePatterns = (array) $parameterBag->unescapeValue($parameterBag->resolveValue($excludePatterns));
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather do the cast in registerClasses and add the array $excludePatterns typehint in this method

Copy link
Author

Choose a reason for hiding this comment

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

It's done

@nicolas-grekas
Copy link
Member

This might be enough for Yaml, but this doesn't benefit Xml nor the PHP configurators yet, do we want to make this available for these formats also?

@nicolas-grekas nicolas-grekas added this to the 4.1 milestone Oct 8, 2017
@nicolas-grekas
Copy link
Member

Moving to 4.1.

@apetitpa
Copy link
Author

apetitpa commented Oct 8, 2017

@nicolas-grekas I'm sorry I could not finish this earlier, I'll rebase and edit base branch on github

@nicolas-grekas
Copy link
Member

@apetitpa let's continue this one?

@apetitpa
Copy link
Author

@nicolas-grekas I'm back on it ! Would you mind giving me a hint on what's left to do please ?

@apetitpa apetitpa changed the base branch from 3.4 to master December 30, 2017 11:39
@nicolas-grekas
Copy link
Member

I think we should make this work somehow for other config formats (xml and php-dsl)

@apetitpa
Copy link
Author

apetitpa commented Jan 9, 2018

@nicolas-grekas Thanks for your help I have rebased on master and started to work something out, so this means I should make this piece of xml configuration work, right ?

<prototype namespace="AppBundle\" resource="../../src/AppBundle/*">
    <exclude>../../src/AppBundle/{Entity,Repository}</exclude>
</prototype>

About the php based configuration it should already work since you can now pass an array as the third argument of registerClasses() or am I missing something ?

$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', array('../../src/AppBundle/{Entity,Repository}'));

@nicolas-grekas
Copy link
Member

I was more specifically referring to the Configurator in the Loader namespace. Look for a call to this method there, you should find something to adjust.

@nicolas-grekas
Copy link
Member

ping @apetitpa

@nicolas-grekas
Copy link
Member

@apetitpa do you think you can finish this PR before the feature freeze by the end of the month?

@apetitpa
Copy link
Author

@nicolas-grekas I'll do my best but I'm very short in time these days. I'll give it a try probably on next saturday evening, is it too late ?

Copy link
Member

@stof stof left a comment

Choose a reason for hiding this comment

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

Whne updating the XML loader, don't forget to update the XSD too.

* @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[] $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.

right type should actually be string|string[]|null (the phpdoc should be string|null in 3.4)

foreach ($this->glob($excludePattern, true, $resource) as $path => $info) {
if (null === $excludePrefix) {
$excludePrefix = $resource->getPrefix();
if ($excludePatterns) {
Copy link
Member

Choose a reason for hiding this comment

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

you could remove this diff, handling the empty array like any other array (the string|null type was needing a dedicated handling of null before)

@nicolas-grekas nicolas-grekas modified the milestones: 4.1, next Apr 20, 2018
@magnetik
Copy link
Contributor

It's now too late for 4.1. If you miss time to finish @apetitpa I can take it over if it's good for you.

@magnetik
Copy link
Contributor

I've created a pull request (just above) to finish it :)

magnetik added a commit to magnetik/symfony that referenced this pull request Apr 27, 2018
magnetik added a commit to magnetik/symfony that referenced this pull request Apr 27, 2018
@nicolas-grekas
Copy link
Member

Closing in favor of #27075.
Thank you @apetitpa for the inspiration!

@apetitpa
Copy link
Author

I'm sorry I couldn't make it guys and thank you @magnetik for taking over.

@apetitpa apetitpa deleted the exclude-array branch April 30, 2018 07:50
magnetik added a commit to magnetik/symfony that referenced this pull request May 2, 2018
magnetik added a commit to magnetik/symfony that referenced this pull request May 2, 2018
magnetik added a commit to magnetik/symfony that referenced this pull request May 2, 2018
magnetik added a commit to magnetik/symfony that referenced this pull request May 9, 2018
magnetik added a commit to magnetik/symfony that referenced this pull request 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
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: Needs Work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants