-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Split advanced container configuration article #5284
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
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
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
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
types | ||
parameters | ||
definitions | ||
synthetic_services | ||
compilation | ||
tags | ||
factories | ||
|
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
.. index:: | ||
single: DependencyInjection; Synthetic Services | ||
|
||
How to Inject Instances into the Container | ||
------------------------------------------ | ||
|
||
When using the container in your application, you sometimes need to inject an | ||
instance instead of configuring the container to create a new instance. | ||
|
||
For instance, if you're using the :doc:`HttpKernel </components/http_kernel/introduction>` | ||
component with the DependencyInjection component, then the ``kernel`` | ||
service is injected into the container from within the ``Kernel`` class:: | ||
|
||
// ... | ||
abstract class Kernel implements KernelInterface, TerminableInterface | ||
{ | ||
// ... | ||
protected function initializeContainer() | ||
{ | ||
// ... | ||
$this->container->set('kernel', $this); | ||
|
||
// ... | ||
} | ||
} | ||
|
||
The ``kernel`` service is called a synthetic service. This service has to be | ||
configured in the container, so the container knows the service does exist | ||
during compilation (otherwise, services depending on this ``kernel`` service | ||
will get a "service does not exists" error). | ||
|
||
In order to do so, you have to use | ||
:method:`Definition::setSynthetic() <Symfony\\Component\\DependencyInjection\\Definition::setSynthetic>`:: | ||
|
||
use Symfony\Component\DependencyInjectino\Definition; | ||
|
||
// synthetic services don't specify a class | ||
$kernelDefinition = new Definition(); | ||
$kernelDefinition->setSynthetic(true); | ||
|
||
$container->setDefinition('your_service', $kernelDefinition); | ||
|
||
Now, you can inject the instance in the container using | ||
:method:`Container::set() <Symfony\\Component\\DependencyInjection\\Container::set>`:: | ||
|
||
$yourService = new YourObject(); | ||
$container->set('your_service', $yourService); | ||
|
||
``$container->get('your_service')`` will now return the same instance as | ||
``$yourService``. |
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
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.
what about showing the other formats too, as done previously ? Otherwise, we are loosing some content
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.
This is something we have to think about. Currently, we only show PHP formats in this article. However, this article in general looks like it should recieve a face-lift. Let's do it in another PR :)