8000 [DependencyInjection] Fix some edge cases with autowiring by dunglas · Pull Request #16464 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Fix some edge cases with autowiring #16464

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 2 commits into from
Nov 7, 2015

Conversation

dunglas
Copy link
Member
@dunglas dunglas commented Nov 4, 2015
Q A
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

Enhance the autowiring system:

  • Works with parent definitions and decorator
  • Always exclude parent definitions

It allows to autowire major services of the standard edition (tested with Swift Mailer, Monolog, Doctrine and Twig).

@dunglas dunglas changed the title Autowiring parent [DependencyInjection] Fix some edge cases with autowiring Nov 4, 2015
@@ -147,6 +148,11 @@ private function populateAvailableType($id, Definition $definition)
$this->types[$type] = $id;
}

// Cannot use reflection if the class isn't set
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be moved above the foreach-loop as it doesn't depend on the loop.
And improves performance a little.

Copy link
Member Author

Choose a reason for hiding this comment

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

It depends of the class of the definition. It cannot be moved outside of the loop.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean like this:

    private function populateAvailableType($id, Definition $definition)
    {
        // Never use abstract services
        if ($definition->isAbstract()) {
            return;
        }

        // Cannot use reflection if the class isn't set
        if (!$definition->getClass()) {
            return;
        }

        foreach ($definition->getAutowiringTypes() as $type) {
            $this->definedTypes[$type] = true;
            $this->types[$type] = $id;
        }

        if ($reflectionClass = $this->getReflectionClass($id, $definition)) {
            $this->extractInterfaces($id, $reflectionClass);
            $this->extractAncestors($id, $reflectionClass);
        }
    }

Check if a class is set, if not return before executing the loop.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've done that on purpose. Associating a service with a type using the autowiring_types key doesn't require reflection, so it doesn't require the class key to exist.

It allows to do things like associating types with a service definition where the class is associated in a compiler pass executed after the autowiring.

@dunglas dunglas force-pushed the autowiring-parent branch 2 times, most recently from ac456ee to 52f5936 Compare November 6, 2015 13:15
@dunglas
Copy link
Member Author
dunglas commented Nov 6, 2015

Rebased.

@fabpot
Copy link
Member
fabpot commented Nov 7, 2015

Thank you @dunglas.

@fabpot fabpot merged commit faefc60 into symfony:2.8 Nov 7, 2015
fabpot added a commit that referenced this pull request Nov 7, 2015
…ing (dunglas)

This PR was merged into the 2.8 branch.

Discussion
----------

[DependencyInjection] Fix some edge cases with autowiring

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Enhance the autowiring system:

- Works with parent definitions and decorator
- Always exclude parent definitions

It allows to autowire major services of the standard edition (tested with Swift Mailer, Monolog, Doctrine and Twig).

Commits
-------

faefc60 [DependencyInjection] Autowing: exclu
7A5C
de abstract definitons
71d502a [DependencyInjection] Autowiring: support parent/decorators
@fabpot fabpot mentioned this pull request Nov 16, 2015
@dunglas dunglas deleted the autowiring-parent branch December 5, 2015 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0