Function getCurrentOrDefaultLocale()
, defined in my service, may be invoked from a controller or through a command line script.
Accessing request
service from CLI throws an exception, and I'm using it to detect the CLI invocation. However catching an exception only for this purpose seems so bad to me.
Is there any reliable way to check if request is accessible in the currenct context (of execution, browser vs CLI)?
/**
* @return string
*/
protected function getCurrentOrDefaultLocale()
{
try {
$request = $this->container->get('request');
}
catch(InactiveScopeException $exception) {
return $this->container->getParameter('kernel.default_locale');
}
// With Symfony < 2.1.0 current locale is stored in the session
if(version_compare($this->sfVersion, '2.1.0', '<')) {
return $this->container->get('session')->getLocale();
}
// Symfony >= 2.1.0 current locale from the request
return $request->getLocale();
}