-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#4228] Move synthetic services to its own recipe
- Loading branch information
Showing
4 changed files
with
52 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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 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 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 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