2016-03-04 17:43:16 +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\DependencyInjection\Compiler ;
2016-04-28 17:07:16 +01:00
use Symfony\Component\Cache\Adapter\RedisAdapter ;
2016-11-28 07:50:06 +00:00
use Symfony\Component\DependencyInjection\ChildDefinition ;
2016-03-04 17:43:16 +00:00
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface ;
use Symfony\Component\DependencyInjection\ContainerBuilder ;
2016-04-28 17:07:16 +01:00
use Symfony\Component\DependencyInjection\Definition ;
2016-03-14 09:01:08 +00:00
use Symfony\Component\DependencyInjection\Reference ;
2016-09-06 23:54:23 +01:00
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException ;
2016-03-04 17:43:16 +00:00
/**
* @ author Nicolas Grekas < p @ tchwork . com >
*/
class CachePoolPass implements CompilerPassInterface
{
/**
* { @ inheritdoc }
*/
public function process ( ContainerBuilder $container )
{
2016-11-24 10:24:08 +00:00
if ( $container -> hasParameter ( 'cache.prefix.seed' )) {
$seed = '.' . $container -> getParameterBag () -> resolveValue ( $container -> getParameter ( 'cache.prefix.seed' ));
} else {
$seed = '_' . $container -> getParameter ( 'kernel.root_dir' );
2016-04-14 11:03:03 +01:00
}
2016-12-08 15:18:22 +00:00
$seed .= '.' . $container -> getParameter ( 'kernel.name' ) . '.' . $container -> getParameter ( 'kernel.environment' );
2016-04-14 11:03:03 +01:00
2016-09-10 10:05:01 +01:00
$aliases = $container -> getAliases ();
2016-03-14 09:01:08 +00:00
$attributes = array (
2016-04-04 08:11:03 +01:00
'provider' ,
2016-03-14 09:01:08 +00:00
'namespace' ,
'default_lifetime' ,
);
2016-03-04 17:43:16 +00:00
foreach ( $container -> findTaggedServiceIds ( 'cache.pool' ) as $id => $tags ) {
2016-03-14 09:01:08 +00:00
$adapter = $pool = $container -> getDefinition ( $id );
2016-04-04 08:11:03 +01:00
if ( $pool -> isAbstract ()) {
continue ;
}
2016-12-07 19:11:50 +00:00
$isLazy = $pool -> isLazy ();
2016-11-28 07:50:06 +00:00
while ( $adapter instanceof ChildDefinition ) {
2016-03-14 09:01:08 +00:00
$adapter = $container -> findDefinition ( $adapter -> getParent ());
2016-12-07 19:11:50 +00:00
$isLazy = $isLazy || $adapter -> isLazy ();
2016-03-14 09:01:08 +00:00
if ( $t = $adapter -> getTag ( 'cache.pool' )) {
$tags [ 0 ] += $t [ 0 ];
}
2016-03-04 17:43:16 +00:00
}
2016-04-23 19:46:00 +01:00
if ( ! isset ( $tags [ 0 ][ 'namespace' ])) {
2016-11-24 10:24:08 +00:00
$tags [ 0 ][ 'namespace' ] = $this -> getNamespace ( $seed , $id );
2016-04-23 19:46:00 +01:00
}
2016-04-04 08:11:03 +01:00
if ( isset ( $tags [ 0 ][ 'clearer' ])) {
2016-09-10 10:05:01 +01:00
$clearer = strtolower ( $tags [ 0 ][ 'clearer' ]);
while ( isset ( $aliases [ $clearer ])) {
$clearer = ( string ) $aliases [ $clearer ];
}
2016-04-04 08:11:03 +01:00
} else {
$clearer = null ;
2016-03-11 08:48:42 +00:00
}
2016-04-04 08:11:03 +01:00
unset ( $tags [ 0 ][ 'clearer' ]);
2016-04-28 17:07:16 +01:00
if ( isset ( $tags [ 0 ][ 'provider' ])) {
$tags [ 0 ][ 'provider' ] = new Reference ( static :: getServiceProvider ( $container , $tags [ 0 ][ 'provider' ]));
2016-03-04 17:43:16 +00:00
}
2016-03-14 09:01:08 +00:00
$i = 0 ;
foreach ( $attributes as $attr ) {
if ( isset ( $tags [ 0 ][ $attr ])) {
$pool -> replaceArgument ( $i ++ , $tags [ 0 ][ $attr ]);
}
2016-04-04 08:11:03 +01:00
unset ( $tags [ 0 ][ $attr ]);
2016-03-04 17:43:16 +00:00
}
2016-03-14 09:01:08 +00:00
if ( ! empty ( $tags [ 0 ])) {
2016-09-06 23:54:23 +01:00
throw new InvalidArgumentException ( sprintf ( 'Invalid "cache.pool" tag for service "%s": accepted attributes are "clearer", "provider", "namespace" and "default_lifetime", found "%s".' , $id , implode ( '", "' , array_keys ( $tags [ 0 ]))));
2016-04-04 08:11:03 +01:00
}
2016-12-07 19:11:50 +00:00
$attr = array ();
2016-04-04 08:11:03 +01:00
if ( null !== $clearer ) {
2016-12-07 19:11:50 +00:00
$attr [ 'clearer' ] = $clearer ;
}
if ( ! $isLazy ) {
$pool -> setLazy ( true );
$attr [ 'unlazy' ] = true ;
}
if ( $attr ) {
$pool -> addTag ( 'cache.pool' , $attr );
2016-03-04 17:43:16 +00:00
}
}
}
2016-11-24 10:24:08 +00:00
private function getNamespace ( $seed , $id )
2016-03-04 17:43:16 +00:00
{
2016-11-24 10:24:08 +00:00
return substr ( str_replace ( '/' , '-' , base64_encode ( hash ( 'sha256' , $id . $seed , true ))), 0 , 10 );
2016-03-04 17:43:16 +00:00
}
2016-04-28 17:07:16 +01:00
/**
* @ internal
*/
public static function getServiceProvider ( ContainerBuilder $container , $name )
{
2016-12-13 11:21:30 +00:00
$container -> resolveEnvPlaceholders ( $name , null , $usedEnvs );
if ( 0 === strpos ( $name , 'redis://' ) || $usedEnvs ) {
2016-04-28 17:07:16 +01:00
$dsn = $name ;
if ( ! $container -> hasDefinition ( $name = md5 ( $dsn ))) {
$definition = new Definition ( \Redis :: class );
$definition -> setPublic ( false );
$definition -> setFactory ( array ( RedisAdapter :: class , 'createConnection' ));
$definition -> setArguments ( array ( $dsn ));
$container -> setDefinition ( $name , $definition );
}
}
return $name ;
}
2016-03-04 17:43:16 +00:00
}