Skip to content
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

[Cookbook] [CLI] Getting Services from the Service Container example does not work #890

Closed
jonathaningram opened this issue Nov 27, 2011 · 7 comments
Labels
actionable Clear and specific issues ready for anyone to take them. bug Console DependencyInjection

Comments

@jonathaningram
Copy link
Contributor

I am troubleshooting the annoying You cannot create a service ("request") of an inactive scope ("request"). CLI error, and I checked the docs to see if they successfully used the translator in a command. I found the following example, but it appears to fail with the same error too, so I propose either a) fix the example so that it works (thus helping me too ;) ) or b) changing the example to retrieve a different service that does not depend on the request.

<?php

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!'));
    }
}

The doc URL is here

@jonathaningram
Copy link
Contributor Author

Follow up: I believe a decent solution to this is as follows:

<?php

protected function execute(InputInterface $input, OutputInterface $output)
{
    // Retrieve the locale from the user input (e.g. 'en_US')
    $locale = $input->getArgument('locale');

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

    $name = $input->getArgument('name');

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

I believe this goes hand in hand with the 2.1 BC break regarding the locale and how it is part of the request. In some respect the CLI command is a request, therefore the locale can come from the command line arguments (i.e. locale).

What do you think? Make a PR?

@weaverryan
Copy link
Member

Hi @jonathaningram !

Sorry for the slow response, but you've diagnosed the situation well. Unfortunately, I don't get the same error as you when I try it in 2.0. And I do like your second code-block (because it explicitly sets the locale, which is a great thing to show), but if there were an error, it would happen when retrieving the translator service, so the second code block would have the same error.

Do you remember more about this issue? Were you trying on 2.1 (the master branch) when you did this?

@jonathaningram
Copy link
Contributor Author

Hello @weaverryan - sorry my slow response!

I believe I was using master, yes.

I just checked my code, and you'll get this error at the following stack:

[Symfony\Component\DependencyInjection\Exception\InactiveScopeException]   
  You cannot create a service ("request") of an inactive scope ("request").  

Exception trace:
 () at /Users/jonathaningram/my_app/app/cache/dev/appDevDebugProjectContainer.php:3559
 appDevDebugProjectContainer->getRequestService() at /Users/jonathaningram/my_app/app/bootstrap.php.cache:194
 Symfony\Component\DependencyInjection\Container->get() at /Users/jonathaningram/my_app/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php:69
 Symfony\Bundle\FrameworkBundle\Translation\Translator->getLocale() at /Users/jonathaningram/my_app/vendor/symfony/src/Symfony/Component/Translation/Translator.php:121
 Symfony\Component\Translation\Translator->trans() at /Users/jonathaningram/my_app/src/...

In the stack trace, we get to calling the trans method. Which ends up going to getLocale which is in the FrameworkBundle:

<?php

/**
 * {@inheritdoc}
 */
public function getLocale()
{
    if (null === $this->locale && $this->container->has('request')) {
        $this->locale = $this->container->get('request')->getLocale();
    }

    return $this->locale;
}

Which tries to get request.

Note: I am on some flavour of master, not the same as when I filed the original issue. So unless there is some new check (i.e. after the master I'm on) when you do $container->get('translator'), not sure why the error is different. Is there a new check that ensures you cannot retrieve a service if you are not in that scope? I mean, the translator needs the request scope, so the new check ensures that you cannot even get the translator in the first place?

Hope that helps...

@weaverryan
Copy link
Member

Basically, the issue here is obviously that there's no request in the CLI. Usually, if you have a problem, you're trying to do something you shouldn't do in a command, since it makes sense that there is no CLI. But obviously, it does make sense to try to translate things.

I think we should add a note about the issue of the missing request service and the types of issues it may cause. The solution will be different each time, but we can highlight that. For example, in the case of the translator, you can simply call ->setLocale manually on the translator service before using it. In other cases, it's possible that you will need to manually create a request service, call enterScope('request') and then 'set('request', $request)`, but I've never personally run into a situation where I needed to got that far.

Point is, if we add a note about it or a small section, then I think we're properly helping people.

@weaverryan
Copy link
Member

Oh, and in this case, the FrameworkBundle Translator now does check for the active scope before trying to get the request, so it will simply return null. We should probably manually call setLocale to really make this an illustrative example.

@wouterj
Copy link
Member

wouterj commented Mar 23, 2013

In the note, we can link to cookbook/service_container/scopes.

Moreover, I think a sidebar will better fit this change.

weaverryan added a commit that referenced this issue Jun 7, 2014
… (javiereguiluz)

This PR was merged into the 2.3 branch.

Discussion
----------

Fixed the section about getting services from a command

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | yes
| Applies to    | 2.3+
| Fixed tickets | #890

Hopefully this PR fixes one of the oldest errors of the Symfony documentation.

Commits
-------

8993beb Linked the cookbook about DIC scopes
0f3c218 Fixed the section about getting services from a command
@weaverryan
Copy link
Member

Fixed finally! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actionable Clear and specific issues ready for anyone to take them. bug Console DependencyInjection
Projects
None yet
Development

No branches or pull requests

3 participants