Skip to content

Commit

Permalink
Merge branch '2.7' into 2.8
Browse files Browse the repository at this point in the history
* 2.7:
  Remove note about request service, which is not used anymore
  Remove information about request scpoe
  • Loading branch information
weaverryan committed Nov 30, 2015
2 parents d62fa0a + c36dc2a commit 4ad3bce
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 73 deletions.
63 changes: 0 additions & 63 deletions cookbook/console/console_command.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,69 +101,6 @@ service container. In other words, you have access to any configured service::
// ...
}

However, due to the :doc:`container scopes </cookbook/service_container/scopes>` this
code doesn't work for some services. For instance, if you try to get the ``request``
service or any other service related to it, you'll get the following error:

.. code-block:: text
You cannot create a service ("request") of an inactive scope ("request").
Consider the following example that uses the ``translator`` service to
translate some contents using a console command::

protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$translator = $this->getContainer()->get('translator');
if ($name) {
$output->writeln(
$translator->trans('Hello %name%!', array('%name%' => $name))
);
} else {
$output->writeln($translator->trans('Hello!'));
}
}

If you dig into the Translator component classes, you'll see that the ``request``
service is required to get the locale into which the contents are translated::

// vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
public function getLocale()
{
if (null === $this->locale && $this->container->isScopeActive('request')
&& $this->container->has('request')) {
$this->locale = $this->container->get('request')->getLocale();
}

return $this->locale;
}

Therefore, when using the ``translator`` service inside a command, you'll get the
previous *"You cannot create a service of an inactive scope"* error message.
The solution in this case is as easy as setting the locale value explicitly
before translating contents::

protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$locale = $input->getArgument('locale');

$translator = $this->getContainer()->get('translator');
$translator->setLocale($locale);

if ($name) {
$output->writeln(
$translator->trans('Hello %name%!', array('%name%' => $name))
);
} else {
$output->writeln($translator->trans('Hello!'));
}
}

However, for other services the solution might be more complex. For more details,
see :doc:`/cookbook/service_container/scopes`.

Invoking other Commands
-----------------------

Expand Down
10 changes: 0 additions & 10 deletions cookbook/templating/twig_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,6 @@ Now you must let the Service Container know about your newly created Twig Extens
->setPublic(false)
->addTag('twig.extension');
.. note::

Keep in mind that Twig Extensions are not lazily loaded. This means that
there's a higher chance that you'll get a
:class:`Symfony\\Component\\DependencyInjection\\Exception\\ServiceCircularReferenceException`
or a
:class:`Symfony\\Component\\DependencyInjection\\Exception\\ScopeWideningInjectionException`
if any services (or your Twig Extension in this case) are dependent on
the request service. For more information take a look at :doc:`/cookbook/service_container/scopes`.

Using the custom Extension
--------------------------

Expand Down

0 comments on commit 4ad3bce

Please sign in to comment.