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

Getting the current locale in the service became a very hard task #5486

Closed
vladcosorg opened this issue Sep 11, 2012 · 6 comments
Closed

Getting the current locale in the service became a very hard task #5486

vladcosorg opened this issue Sep 11, 2012 · 6 comments

Comments

@vladcosorg
Copy link

I need to get current locale, before i've injected session, and got locale from there, now, after upgrade, I have to get it from request, so I do:

services:
    entity_locale_setter.listener:
        class: Giftshop\FrontBundle\Listener\EntityLocaleSetter
        arguments: [@request]
        tags:
            - { name: doctrine.event_listener, event: postLoad }

Obviously i get the scope widening error

ScopeWideningInjectionException: Scope Widening Injection detected: The definition "entity_locale_setter.listener" references the service "request" which belongs to a narrower scope. Generally, it is safer to either move "entity_locale_setter.listener" to scope "request" or alternatively rely on the provider pattern by injecting the container itself, and requesting the service "request" each time it is needed. In rare, special cases however that might not be necessary, then you can set the reference to strict=false to get rid of this error.

Ok, i chnage it to scope:request and i get

ScopeWideningInjectionException: Scope Widening Injection detected: The definition "doctrine.dbal.default_connection.event_manager" references the service "entity_locale_setter.listener" which belongs to a narrower scope. Generally, it is safer to either move "doctrine.dbal.default_connection.event_manager" to scope "request" or alternatively rely on the provider pattern by injecting the container itself, and requesting the service "entity_locale_setter.listener" each time it is needed. In rare, special cases however that might not be necessary, then you can set the reference to strict=false to get rid of this error.

Can someone tell me please, how to get the freaking locale? :D Why is it so hard, or I;m doing something wrong?

I'm injecting now the service container, but I don't like this dirty approach.

@stloyd
Copy link
Contributor

stloyd commented Sep 14, 2012

Hmmm, did you tried to turn off this strict param ?

services:
    entity_locale_setter.listener:
        class: Giftshop\FrontBundle\Listener\EntityLocaleSetter
        arguments: [@request=]
        tags:
            - { name: doctrine.event_listener, event: postLoad }

@antoinegomez
Copy link

If you only need the locale I suggest passing the %locale% parameter instead of the Request service.

@stof
Copy link
Member

stof commented Sep 14, 2012

@antoinegomez this will not give you the current locale but the default locale

@vladcosorg
Copy link
Author

@stloyd

Yes i did, no effect.

@lennerd
Copy link

lennerd commented Sep 15, 2012

I think you come into the dark side of the dependency injection. Relaying on the request means moving to a narrower scope, because the request is created while the application is running.

But as jedi ninjas, let's try to solve this.

I would try to use a second event listener listening to kernel.request. This event gets your entity_locale_setter.listener service and sets the request manually when the kernel.request event occurs.

# services.yml

services:
    entity_locale_setter.listener:
        class: Giftshop\FrontBundle\Listener\EntityLocaleSetter
        tags:
            - { name: doctrine.event_listener, event: postLoad }

    kernel_request.listener:
        class: Giftshop\FrontBundle\Listener\KernelRequestListener
        arguments: [@entity_locale_setter.listener]
        tags:
            - { name: kernel.event_listener, method: onKernelRequest }
<?php
// KernelRequestListener.php

namespace Giftshop\FrontBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class KernelRequestListener
{

    private $entityLocaleSetter;

    public function __construct(EntityLocaleSetter $entityLocaleSetter)
    {
        $this->entityLocaleSetter = $entityLocaleSetter;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        $this->entityLocaleSetter->setRequest($event->getRequest());
    }

}

@vladcosorg
Copy link
Author

@lennerd Forgot to thank you, your solution solved my problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants