2015-03-24 13:45:14 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer ;
2017-06-02 15:38:10 +01:00
use Psr\Container\ContainerInterface ;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface ;
2015-03-24 13:45:14 +00:00
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface ;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface ;
use Symfony\Component\Translation\TranslatorInterface ;
/**
* Generates the catalogues for translations .
*
* @ author Xavier Leune < xavier . leune @ gmail . com >
*/
2017-06-02 15:38:10 +01:00
class TranslationsCacheWarmer implements CacheWarmerInterface , ServiceSubscriberInterface
2015-03-24 13:45:14 +00:00
{
2017-06-01 13:40:39 +01:00
private $container ;
2015-03-24 13:45:14 +00:00
private $translator ;
2017-06-01 13:40:39 +01:00
/**
* TranslationsCacheWarmer constructor .
*
2017-06-02 14:07:31 +01:00
* @ param ContainerInterface $container
2017-06-01 13:40:39 +01:00
*/
public function __construct ( $container )
2015-03-24 13:45:14 +00:00
{
2017-06-01 13:40:39 +01:00
// As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
if ( $container instanceof ContainerInterface ) {
$this -> container = $container ;
} elseif ( $container instanceof TranslatorInterface ) {
$this -> translator = $container ;
2017-06-02 14:07:31 +01:00
@ trigger_error ( sprintf ( 'Using a "%s" as first argument of %s is deprecated since version 3.4 and will be unsupported in version 4.0. Use a %s instead.' , TranslatorInterface :: class , __CLASS__ , ContainerInterface :: class ), E_USER_DEPRECATED );
2017-06-01 13:40:39 +01:00
} else {
2017-06-02 15:38:10 +01:00
throw new \InvalidArgumentException ( sprintf ( '%s only accepts instance of Psr\Container\ContainerInterface as first argument.' , __CLASS__ ));
2017-06-01 13:40:39 +01:00
}
2015-03-24 13:45:14 +00:00
}
/**
* { @ inheritdoc }
*/
public function warmUp ( $cacheDir )
{
2017-06-01 13:40:39 +01:00
if ( null === $this -> translator ) {
$this -> translator = $this -> container -> get ( 'translator' );
}
2015-03-24 13:45:14 +00:00
if ( $this -> translator instanceof WarmableInterface ) {
$this -> translator -> warmUp ( $cacheDir );
}
}
/**
* { @ inheritdoc }
*/
public function isOptional ()
{
return true ;
}
2017-06-02 15:38:10 +01:00
/**
* { @ inheritdoc }
*/
public static function getSubscribedServices ()
{
return array (
'translator' => TranslatorInterface :: class ,
);
}
2015-03-24 13:45:14 +00:00
}