DependencyInjection/HydraExtension.php
protected function loadCacheDriver(array $config, ContainerBuilder $container)
    {
        $cacheDriver = $config['metadata_cache_driver'];
        $cacheDriverService = 'hydra.metadata_cache';

        switch ($cacheDriver['type']) {
            case 'service':
                $container->setAlias($cacheDriverService, new Alias($cacheDriver['id'], false));

                return;
            case 'file':
                $cacheDir = $container->getParameterBag()->resolveValue($cacheDriver['file_cache_dir']);

                if (!is_dir($cacheDir) && false === @mkdir($cacheDir, 0777, true)) {
                    throw new \RuntimeException(sprintf('Could not create cache directory "%s".', $cacheDir));
                }

                $cacheDef = new Definition('%hydra.cache.file.class%', array($cacheDir, '.hydracache.php'));
                break;
            case 'memcache':
                $memcacheClass = !empty($cacheDriver['class']) ? $cacheDriver['class'] : '%hydra.cache.memcache.class%';
                $memcacheInstanceClass = !empty($cacheDriver['instance_class']) ? $cacheDriver['instance_class'] : '%hydra.cache.memcache_instance.class%';
                $memcacheHost = !empty($cacheDriver['host']) ? $cacheDriver['host'] : '%hydra.cache.memcache_host%';
                $memcachePort = !empty($cacheDriver['port']) || (isset($cacheDriver['port']) && $cacheDriver['port'] === 0)  ? $cacheDriver['port'] : '%hydra.cache.memcache_port%';
                $cacheDef = new Definition($memcacheClass);
                $memcacheInstance = new Definition($memcacheInstanceClass);
                $memcacheInstance->addMethodCall('connect', array(
                    $memcacheHost, $memcachePort
                ));
                $container->setDefinition(sprintf('hydra.%s_memcache_instance', $config['name']), $memcacheInstance);
                $cacheDef->addMethodCall('setMemcache', array(new Reference(sprintf('hydra.%s_memcache_instance', $config['name']))));
                break;
            case 'memcached':
                $memcachedClass = !empty($cacheDriver['class']) ? $cacheDriver['class'] : '%hydra.cache.memcached.class%';
                $memcachedInstanceClass = !empty($cacheDriver['instance_class']) ? $cacheDriver['instance_class'] : '%hydra.cache.memcached_instance.class%';
                $memcachedHost = !empty($cacheDriver['host']) ? $cacheDriver['host'] : '%hydra.cache.memcached_host%';
                $memcachedPort = !empty($cacheDriver['port']) ? $cacheDriver['port'] : '%hydra.cache.memcached_porthydra.%';
                $cacheDef = new Definition($memcachedClass);
                $memcachedInstance = new Definition($memcachedInstanceClass);
                $memcachedInstance->addMethodCall('addServer', array(
                    $memcachedHost, $memcachedPort
                ));
                $container->setDefinition(sprintf('hydra.%s_memcached_instance', $config['name']), $memcachedInstance);
                $cacheDef->addMethodCall('setMemcached', array(new Reference(sprintf('hydra.%s_memcached_instance', $config['name']))));
                break;
            case 'redis':
                $redisClass = !empty($cacheDriver['class']) ? $cacheDriver['class'] : '%hydra.cache.redis.class%';
                $redisInstanceClass = !empty($cacheDriver['instance_class']) ? $cacheDriver['instance_class'] : '%hydra.cache.redis_instance.class%';
                $redisHost = !empty($cacheDriver['host']) ? $cacheDriver['host'] : '%hydra.cache.redis_host%';
                $redisPort = !empty($cacheDriver['port']) ? $cacheDriver['port'] : '%hydra.cache.redis_port%';
                $cacheDef = new Definition($redisClass);
                $redisInstance = new Definition($redisInstanceClass);
                $redisInstance->addMethodCall('connect', array(
                    $redisHost, $redisPort
                ));
                $container->setDefinition(sprintf('hydra.%s_redis_instance', $config['name']), $redisInstance);
                $cacheDef->addMethodCall('setRedis', array(new Reference(sprintf('hydra.%s_redis_instance', $config['name']))));
                break;
            case 'apc':
            case 'array':
            case 'xcache':
            case 'wincache':
            case 'zenddata':
                $cacheDef = new Definition('%hydra.'.sprintf('cache.%s.class', $cacheDriver['type']).'%');
                break;
            default:
                throw new \InvalidArgumentException(sprintf('"%s" is an unrecognized Hydra cache driver.', $cacheDriver['type']));
        }

        $cacheDef->setPublic(false);
        // generate a unique namespace for the given application
        $namespace = 'sf2'.$this->getMappingResourceExtension().md5($container->getParameter('kernel.root_dir').$container->getParameter('kernel.environment'));
        $cacheDef->addMethodCall('setNamespace', array($namespace));

        $container->setDefinition($cacheDriverService, $cacheDef);
    }
protected function loadCacheDriver(array $config, ContainerBuilder $container)
    {
        $cacheDriver = $config['metadata_cache_driver'];
        $cacheDriverService = 'hydra.metadata_cache';

        switch ($cacheDriver['type']) {
            case 'service':
                $container->setAlias($cacheDriverService, new Alias($cacheDriver['id'], false));

                return;
            case 'file':
                $cacheDir = $container->getParameterBag()->resolveValue($cacheDriver['file_cache_dir']);

                if (!is_dir($cacheDir) && false === @mkdir($cacheDir, 0777, true)) {
                    throw new \RuntimeException(sprintf('Could not create cache directory "%s".', $cacheDir));
                }

                $cacheDef = new Definition('%hydra.cache.file.class%', array($cacheDir, '.hydracache.php'));
                break;
            case 'memcache':
                $memcacheClass = !empty($cacheDriver['class']) ? $cacheDriver['class'] : '%hydra.cache.memcache.class%';
                $memcacheInstanceClass = !empty($cacheDriver['instance_class']) ? $cacheDriver['instance_class'] : '%hydra.cache.memcache_instance.class%';
                $memcacheHost = !empty($cacheDriver['host']) ? $cacheDriver['host'] : '%hydra.cache.memcache_host%';
                $memcachePort = !empty($cacheDriver['port']) || (isset($cacheDriver['port']) && $cacheDriver['port'] === 0)  ? $cacheDriver['port'] : '%hydra.cache.memcache_port%';
                $cacheDef = new Definition($memcacheClass);
                $memcacheInstance = new Definition($memcacheInstanceClass);
                $memcacheInstance->addMethodCall('connect', array(
                    $memcacheHost, $memcachePort
                ));
                $container->setDefinition(sprintf('hydra.%s_memcache_instance', $config['name']), $memcacheInstance);
                $cacheDef->addMethodCall('setMemcache', array(new Reference(sprintf('hydra.%s_memcache_instance', $config['name']))));
                break;
            case 'memcached':
                $memcachedClass = !empty($cacheDriver['class']) ? $cacheDriver['class'] : '%hydra.cache.memcached.class%';
                $memcachedInstanceClass = !empty($cacheDriver['instance_class']) ? $cacheDriver['instance_class'] : '%hydra.cache.memcached_instance.class%';
                $memcachedHost = !empty($cacheDriver['host']) ? $cacheDriver['host'] : '%hydra.cache.memcached_host%';
                $memcachedPort = !empty($cacheDriver['port']) ? $cacheDriver['port'] : '%hydra.cache.memcached_porthydra.%';
                $cacheDef = new Definition($memcachedClass);
                $memcachedInstance = new Definition($memcachedInstanceClass);
                $memcachedInstance->addMethodCall('addServer', array(
                    $memcachedHost, $memcachedPort
                ));
                $container->setDefinition(sprintf('hydra.%s_memcached_instance', $config['name']), $memcachedInstance);
                $cacheDef->addMethodCall('setMemcached', array(new Reference(sprintf('hydra.%s_memcached_instance', $config['name']))));
                break;
            case 'redis':
                $redisClass = !empty($cacheDriver['class']) ? $cacheDriver['class'] : '%hydra.cache.redis.class%';
                $redisInstanceClass = !empty($cacheDriver['instance_class']) ? $cacheDriver['instance_class'] : '%hydra.cache.redis_instance.class%';
                $redisHost = !empty($cacheDriver['host']) ? $cacheDriver['host'] : '%hydra.cache.redis_host%';
                $redisPort = !empty($cacheDriver['port']) ? $cacheDriver['port'] : '%hydra.cache.redis_port%';
                $cacheDef = new Definition($redisClass);
                $redisInstance = new Definition($redisInstanceClass);
                $redisInstance->addMethodCall('connect', array(
                    $redisHost, $redisPort
                ));
                $container->setDefinition(sprintf('hydra.%s_redis_instance', $config['name']), $redisInstance);
                $cacheDef->addMethodCall('setRedis', array(new Reference(sprintf('hydra.%s_redis_instance', $config['name']))));
                break;
            case 'apc':
            case 'array':
            case 'xcache':
            case 'wincache':
            case 'zenddata':
                $cacheDef = new Definition('%hydra.'.sprintf('cache.%s.class', $cacheDriver['type']).'%');
                break;
            default:
                throw new \InvalidArgumentException(sprintf('"%s" is an unrecognized Hydra cache driver.', $cacheDriver['type']));
        }

        $cacheDef->setPublic(false);
        // generate a unique namespace for the given application
        $namespace = 'sf2'.$this->getMappingResourceExtension().md5($container->getParameter('kernel.root_dir').$container->getParameter('kernel.environment'));
        $cacheDef->addMethodCall('setNamespace', array($namespace));

        $container->setDefinition($cacheDriverService, $cacheDef);
    }
protected function loadCacheDriver ( array $ config , ContainerBuilder $ container ) { $ cacheDriver = $ config [ 'metadata_cache_driver' ] ; $ cacheDriverService = 'hydra.metadata_cache' ; switch ( $ cacheDriver [ 'type' ] ) { case 'service' : $ container -> setAlias ( $ cacheDriverService , new Alias ( $ cacheDriver [ 'id' ] , false ) ) ; return ; case 'file' : $ cacheDir = $ container -> getParameterBag ( ) -> resolveValue ( $ cacheDriver [ 'file_cache_dir' ] ) ; if ( ! is_dir ( $ cacheDir ) && false === @ mkdir ( $ cacheDir , 0777 , true ) ) { throw new \ RuntimeException ( sprintf ( 'Could not create cache directory "%s".' , $ cacheDir ) ) ; } $ cacheDef = new Definition ( '%hydra.cache.file.class%' , array ( $ cacheDir , '.hydracache.php' ) ) ; break ; case 'memcache' : $ memcacheClass = ! empty ( $ cacheDriver [ 'class' ] ) ? $ cacheDriver [ 'class' ] : '%hydra.cache.memcache.class%' ; $ memcacheInstanceClass = ! empty ( $ cacheDriver [ 'instance_class' ] ) ? $ cacheDriver [ 'instance_class' ] : '%hydra.cache.memcache_instance.class%' ; $ memcacheHost = ! empty ( $ cacheDriver [ 'host' ] ) ? $ cacheDriver [ 'host' ] : '%hydra.cache.memcache_host%' ; $ memcachePort = ! empty ( $ cacheDriver [ 'port' ] ) || ( isset ( $ cacheDriver [ 'port' ] ) && $ cacheDriver [ 'port' ] === 0 ) ? $ cacheDriver [ 'port' ] : '%hydra.cache.memcache_port%' ; $ cacheDef = new Definition ( $ memcacheClass ) ; $ memcacheInstance = new Definition ( $ memcacheInstanceClass ) ; $ memcacheInstance -> addMethodCall ( 'connect' , array ( $ memcacheHost , $ memcachePort ) ) ; $ container -> setDefinition ( sprintf ( 'hydra.%s_memcache_instance' , $ config [ 'name' ] ) , $ memcacheInstance ) ; $ cacheDef -> addMethodCall ( 'setMemcache' , array ( new Reference ( sprintf ( 'hydra.%s_memcache_instance' , $ config [ 'name' ] ) ) ) ) ; break ; case 'memcached' : $ memcachedClass = ! empty ( $ cacheDriver [ 'class' ] ) ? $ cacheDriver [ 'class' ] : '%hydra.cache.memcached.class%' ; $ memcachedInstanceClass = ! empty ( $ cacheDriver [ 'instance_class' ] ) ? $ cacheDriver [ 'instance_class' ] : '%hydra.cache.memcached_instance.class%' ; $ memcachedHost = ! empty ( $ cacheDriver [ 'host' ] ) ? $ cacheDriver [ 'host' ] : '%hydra.cache.memcached_host%' ; $ memcachedPort = ! empty ( $ cacheDriver [ 'port' ] ) ? $ cacheDriver [ 'port' ] : '%hydra.cache.memcached_porthydra.%' ; $ cacheDef = new Definition ( $ memcachedClass ) ; $ memcachedInstance = new Definition ( $ memcachedInstanceClass ) ; $ memcachedInstance -> addMethodCall ( 'addServer' , array ( $ memcachedHost , $ memcachedPort ) ) ; $ container -> setDefinition ( sprintf ( 'hydra.%s_memcached_instance' , $ config [ 'name' ] ) , $ memcachedInstance ) ; $ cacheDef -> addMethodCall ( 'setMemcached' , array ( new Reference ( sprintf ( 'hydra.%s_memcached_instance' , $ config [ 'name' ] ) ) ) ) ; break ; case 'redis' : $ redisClass = ! empty ( $ cacheDriver [ 'class' ] ) ? $ cacheDriver [ 'class' ] : '%hydra.cache.redis.class%' ; $ redisInstanceClass = ! empty ( $ cacheDriver [ 'instance_class' ] ) ? $ cacheDriver [ 'instance_class' ] : '%hydra.cache.redis_instance.class%' ; $ redisHost = ! empty ( $ cacheDriver [ 'host' ] ) ? $ cacheDriver [ 'host' ] : '%hydra.cache.redis_host%' ; $ redisPort = ! empty ( $ cacheDriver [ 'port' ] ) ? $ cacheDriver [ 'port' ] : '%hydra.cache.redis_port%' ; $ cacheDef = new Definition ( $ redisClass ) ; $ redisInstance = new Definition ( $ redisInstanceClass ) ; $ redisInstance -> addMethodCall ( 'connect' , array ( $ redisHost , $ redisPort ) ) ; $ container -> setDefinition ( sprintf ( 'hydra.%s_redis_instance' , $ config [ 'name' ] ) , $ redisInstance ) ; $ cacheDef -> addMethodCall ( 'setRedis' , array ( new Reference ( sprintf ( 'hydra.%s_redis_instance' , $ config [ 'name' ] ) ) ) ) ; break ; case 'apc' : case 'array' : case 'xcache' : case 'wincache' : case 'zenddata' : $ cacheDef = new Definition ( '%hydra.' . sprintf ( 'cache.%s.class' , $ cacheDriver [ 'type' ] ) . '%' ) ; break ; default : throw new \ InvalidArgumentException ( sprintf ( '"%s" is an unrecognized Hydra cache driver.' , $ cacheDriver [ 'type' ] ) ) ; } $ cacheDef -> setPublic ( false ) ; // generate a unique namespace for the given application $ namespace = 'sf2' . $ this -> getMappingResourceExtension ( ) . md5 ( $ container -> getParameter ( 'kernel.root_dir' ) . $ container -> getParameter ( 'kernel.environment' ) ) ; $ cacheDef -> addMethodCall ( 'setNamespace' , array ( $ namespace ) ) ; $ container -> setDefinition ( $ cacheDriverService , $ cacheDef ) ; }
Loads a metadata cache driver

@param array            $config    An array of configuration settings
@param ContainerBuilder $container A ContainerBuilder instance

@throws \InvalidArgumentException In case of unknown driver type.
Loads a metadata cache driver