-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[DI] Added _instanceof example #8239
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -22,6 +22,8 @@ which means it's always safe to upgrade across minor versions. | |
All of the new features are **optional**: they are not enabled by default, so you | ||
need to actually change your configuration files to use them. | ||
|
||
.. _`service-33-default_definition`: | ||
|
||
The new Default services.yml File | ||
--------------------------------- | ||
|
||
|
@@ -417,7 +419,7 @@ In general, the new best practice is to use normal constructor dependency inject | |
4) Auto-tagging with autoconfigure | ||
---------------------------------- | ||
|
||
The last big change is the ``autoconfigure`` key, which is set to ``true`` under | ||
The fourth big change is the ``autoconfigure`` key, which is set to ``true`` under | ||
``_defaults``. Thanks to this, the container will auto-tag services registered in | ||
this file. For example, suppose you want to create an event subscriber. First, you | ||
create the class:: | ||
|
@@ -474,6 +476,69 @@ Many autoconfigured tags have an optional priority. If you need to specify a pri | |
(or any other optional tag attribute), no problem! Just :ref:`manually configure your service <services-manually-wire-args>` | ||
and add the tag. Your tag will take precedence over the one added by auto-configuration. | ||
|
||
5) Auto-configure with _instanceof | ||
---------------------------------- | ||
|
||
And the final big change is ``_instanceof``. It acts as a default definition | ||
template (see `service-33-default_definition`_), but only for services whose | ||
class matches a defined one. | ||
This can be very useful when many services share some tag that cannot be | ||
inherited from an abstract definition: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/services.yml | ||
services: | ||
# ... | ||
|
||
_instanceof: | ||
class: AppBundle\Domain\LoaderInterface | ||
public: true | ||
tags: ['app.domain_loader'] | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/services.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<!-- ... --> | ||
|
||
<instanceof class="AppBundle\Domain\LoaderInterface" public="true"> | ||
<tag name="app.domain_loader" /> | ||
</prototype> | ||
</services> | ||
</container> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/services.php | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
$domainLoaderDefinition = new Definition(); | ||
|
||
$domainLoaderDefinition->addTag('app.domain_loader'); | ||
|
||
// To use as default template | ||
$definition = new Definition(); | ||
|
||
$definition | ||
->setAutowired(true) | ||
->setAutoconfigured(true) | ||
->setPublic(false) | ||
->setInstanceofConditionals( | ||
'AppBundle\Domain\LoaderInterface' => $domainLoaderDefinition, | ||
) | ||
; | ||
|
||
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/{Entity,Repository}'); | ||
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. we don't have this in the YAML or XML example |
||
|
||
What about Performance | ||
---------------------- | ||
|
||
|
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.
Looks like it's
id
instead ofclass
based of my research in the codebase with 3.3