From 26c7813e8dbc2f66379479473baaa24f2cd28416 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sat, 23 May 2015 10:48:16 +0200 Subject: [PATCH 1/2] [#4228] Moved requiring files to definitions --- components/dependency_injection/advanced.rst | 41 ------------------- .../dependency_injection/definitions.rst | 12 ++++++ 2 files changed, 12 insertions(+), 41 deletions(-) diff --git a/components/dependency_injection/advanced.rst b/components/dependency_injection/advanced.rst index 82263ca6e66..d333a38de74 100644 --- a/components/dependency_injection/advanced.rst +++ b/components/dependency_injection/advanced.rst @@ -182,44 +182,3 @@ service by asking for the ``bar`` service like this:: foo: class: Example\Foo bar: "@foo" - - -Requiring Files ---------------- - -There might be use cases when you need to include another file just before -the service itself gets loaded. To do so, you can use the ``file`` directive. - -.. configuration-block:: - - .. code-block:: yaml - - services: - foo: - class: Example\Foo\Bar - file: "%kernel.root_dir%/src/path/to/file/foo.php" - - .. code-block:: xml - - - - - - - %kernel.root_dir%/src/path/to/file/foo.php - - - - - .. code-block:: php - - use Symfony\Component\DependencyInjection\Definition; - - $definition = new Definition('Example\Foo\Bar'); - $definition->setFile('%kernel.root_dir%/src/path/to/file/foo.php'); - $container->setDefinition('foo', $definition); - -Notice that Symfony will internally call the PHP statement ``require_once``, -which means that your file will be included only once per request. diff --git a/components/dependency_injection/definitions.rst b/components/dependency_injection/definitions.rst index c9d1e72cb3d..9c9574c7b9b 100644 --- a/components/dependency_injection/definitions.rst +++ b/components/dependency_injection/definitions.rst @@ -125,3 +125,15 @@ You can also replace any existing method calls with an array of new ones with:: the container is compiled. Once the container is compiled you cannot manipulate service definitions further. To learn more about compiling the container see :doc:`/components/dependency_injection/compilation`. + +Requiring Files +~~~~~~~~~~~~~~~ + +There might be use cases when you need to include another file just before +the service itself gets loaded. To do so, you can use the +:method:`Symfony\\Component\\DependencyInjection\\Definition::setFile` method:: + + $definition->setFile('/src/path/to/file/foo.php'); + +Notice that Symfony will internally call the PHP statement ``require_once``, +which means that your file will be included only once per request. From e3c26031b27addc04f84e6dddcadb5a032d71178 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sat, 23 May 2015 11:17:41 +0200 Subject: [PATCH 2/2] [#4228] Move synthetic services to its own recipe --- components/dependency_injection/advanced.rst | 55 ------------------- components/dependency_injection/index.rst | 1 + .../synthetic_services.rst | 50 +++++++++++++++++ components/map.rst.inc | 1 + 4 files changed, 52 insertions(+), 55 deletions(-) create mode 100644 components/dependency_injection/synthetic_services.rst diff --git a/components/dependency_injection/advanced.rst b/components/dependency_injection/advanced.rst index d333a38de74..0ea6b296a67 100644 --- a/components/dependency_injection/advanced.rst +++ b/components/dependency_injection/advanced.rst @@ -73,61 +73,6 @@ below) to access this service (via the alias). Services are by default public. -Synthetic Services ------------------- - -Synthetic services are services that are injected into the container instead -of being created by the container. - -For example, if you're using the :doc:`HttpKernel ` -component with the DependencyInjection component, then the ``request`` -service is injected in the -:method:`ContainerAwareHttpKernel::handle() ` -method when entering the request :doc:`scope `. -The class does not exist when there is no request, so it can't be included in -the container configuration. Also, the service should be different for every -subrequest in the application. - -To create a synthetic service, set ``synthetic`` to ``true``: - -.. configuration-block:: - - .. code-block:: yaml - - services: - request: - synthetic: true - - .. code-block:: xml - - - - - - - - - - .. code-block:: php - - use Symfony\Component\DependencyInjection\Definition; - - $container - ->setDefinition('request', new Definition()) - ->setSynthetic(true); - -As you see, only the ``synthetic`` option is set. All other options are only used -to configure how a service is created by the container. As the service isn't -created by the container, these options are omitted. - -Now, you can inject the class by using -:method:`Container::set `:: - - // ... - $container->set('request', new MyRequest(...)); - Aliasing -------- diff --git a/components/dependency_injection/index.rst b/components/dependency_injection/index.rst index 4261a0a7854..dfa2e1ef54b 100644 --- a/components/dependency_injection/index.rst +++ b/components/dependency_injection/index.rst @@ -8,6 +8,7 @@ types parameters definitions + synthetic_services compilation tags factories diff --git a/components/dependency_injection/synthetic_services.rst b/components/dependency_injection/synthetic_services.rst new file mode 100644 index 00000000000..cbe32a8c60a --- /dev/null +++ b/components/dependency_injection/synthetic_services.rst @@ -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 ` +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() `:: + + 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() `:: + + $yourService = new YourObject(); + $container->set('your_service', $yourService); + +``$container->get('your_service')`` will now return the same instance as +``$yourService``. diff --git a/components/map.rst.inc b/components/map.rst.inc index a2039699b4b..38bf2e07748 100644 --- a/components/map.rst.inc +++ b/components/map.rst.inc @@ -39,6 +39,7 @@ * :doc:`/components/dependency_injection/types` * :doc:`/components/dependency_injection/parameters` * :doc:`/components/dependency_injection/definitions` + * :doc:`/components/dependency_injection/synthetic_services` * :doc:`/components/dependency_injection/compilation` * :doc:`/components/dependency_injection/tags` * :doc:`/components/dependency_injection/factories`