diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ec74423..d898e28 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -51,6 +51,13 @@ public function getConfigTreeBuilder() ->isRequired() ->info('Service that implements Handlebars\Cache.') ->end() + ->arrayNode('redis') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('client_service')->defaultValue('snc_redis.default')->end() + ->scalarNode('key_prefix')->defaultValue('smartive-handlebars:')->end() + ->end() + ->end() ->end() ->end() ->arrayNode('twig') diff --git a/DependencyInjection/SmartiveHandlebarsExtension.php b/DependencyInjection/SmartiveHandlebarsExtension.php index 5a7aad2..a06f2f2 100644 --- a/DependencyInjection/SmartiveHandlebarsExtension.php +++ b/DependencyInjection/SmartiveHandlebarsExtension.php @@ -38,7 +38,7 @@ public function load(array $configs, ContainerBuilder $container) $this->loadService($serviceName, $config[$serviceName], $container, $loader); if ('cache' === $serviceName) { - $this->loadCache($container, $config, $loader); + $this->loadCache($container, $config); } } } @@ -95,16 +95,15 @@ private function getParameterName($serviceName, $configName) /** * Loads the caching service * - * @param array $config Configuration values - * @param ContainerBuilder $container Container instance - * @param XmlFileLoader $loader Loader instance + * @param ContainerBuilder $container Container instance + * @param array $config Configuration values * * @return void */ - private function loadCache(ContainerBuilder $container, array $config, XmlFileLoader $loader) + private function loadCache(ContainerBuilder $container, array $config) { if (empty($config['cache']['service'])) { - return; + throw new InvalidConfigurationException('You need to specify a cache service in order to enable caching.'); } try { @@ -113,6 +112,23 @@ private function loadCache(ContainerBuilder $container, array $config, XmlFileLo throw new InvalidConfigurationException('Caching can only be configured if templating is enabled.'); } + $this->prepareRedisCache($container, $config['cache']); + $templatingService->addMethodCall('setCache', [new Reference($config['cache']['service'])]); } + + /** + * Prepares the Redis cache + * + * @param ContainerBuilder $container Container instance + * @param array $cacheConfig Cache configuration values + * + * @return void + */ + private function prepareRedisCache(ContainerBuilder $container, array $cacheConfig) + { + $redisCacheService = $container->findDefinition('smartive_handlebars.cache.redis'); + $redisCacheService->replaceArgument(0, new Reference($cacheConfig['redis']['client_service'])); + $redisCacheService->replaceArgument(2, $cacheConfig['redis']['key_prefix']); + } } diff --git a/README.md b/README.md index 857c187..08cbaf5 100644 --- a/README.md +++ b/README.md @@ -126,9 +126,38 @@ smartive_handlebars: ``` There are several caching services / strategies available per default: -- `smartive_handlebars.cache.disk`: Uses [Handlebars\Cache\Disk](https://github.com/XaminProject/handlebars.php/blob/master/src/Handlebars/Cache/Disk.php) to read/write file cache in Symfony's cache directory -- `smartive_handlebars.cache.apc`: Uses [Handlebars\Cache\APC](https://github.com/XaminProject/handlebars.php/blob/master/src/Handlebars/Cache/APC.php) to read/write cache objects using [APC](http://php.net/manual/en/book.apc.php) -- `smartive_handlebars.cache.redis`: Uses the [RedisBundle](https://github.com/snc/SncRedisBundle) to read/write cache objects using a [Redis Server](http://redis.io/). The Redis client (has to be of type `\Predis\Client` or `\Redis`) being used can be specified using the `smartive_handlebars.cache.redis.client` parameter which defaults to `snc_redis.default`. + +### Disk + +Service ID: `smartive_handlebars.cache.disk` + +Uses [Handlebars\Cache\Disk](https://github.com/XaminProject/handlebars.php/blob/master/src/Handlebars/Cache/Disk.php) to read/write file cache in Symfony's cache directory + +### APC + +Service ID: `smartive_handlebars.cache.apc` + +Uses [Handlebars\Cache\APC](https://github.com/XaminProject/handlebars.php/blob/master/src/Handlebars/Cache/APC.php) to read/write cache objects using [APC](http://php.net/manual/en/book.apc.php) + +### Redis + +Service ID: `smartive_handlebars.cache.redis` + +Uses [PhpRedis](https://github.com/phpredis/phpredis) or [Predis](https://github.com/nrk/predis) to read/write cache objects using a [Redis Server](http://redis.io/). +This bundle integrates with [RedisBundle](https://github.com/snc/SncRedisBundle) in order to make configuring your Redis implementation even easier. +The default Redis client being used is `snc_redis.default` (see [RedisBundle documentation](https://github.com/snc/SncRedisBundle/blob/master/Resources/doc/index.md#usage)). + +The default configuration can be overridden looks as follows: + +``` +smartive_handlebars: + cache: + redis: + client_service: 'snc_redis.default' + key_prefix: 'smartive-handlebars:' +``` + +### Custom You can also define your own caching services by adding a class which implements [Handlebars\Cache](https://github.com/XaminProject/handlebars.php/blob/master/src/Handlebars/Cache.php). To use your custom cache service you have to register it in your service configuration: diff --git a/Resources/config/cache.xml b/Resources/config/cache.xml index aba8bd7..9cc10ad 100644 --- a/Resources/config/cache.xml +++ b/Resources/config/cache.xml @@ -2,10 +2,6 @@ - - snc_redis.default - smartive-handlebars: - @@ -13,10 +9,10 @@ %kernel.cache_dir%/smartive_handlebars - service(parameter('smartive_handlebars.cache.redis.client')) + - %smartive_handlebars.cache.redis.key_prefix% + smartive-handlebars: diff --git a/Tests/Functional/Fixtures/app/config/config_test_cache_redis.yml b/Tests/Functional/Fixtures/app/config/config_test_cache_redis.yml index 7541130..1a43c94 100644 --- a/Tests/Functional/Fixtures/app/config/config_test_cache_redis.yml +++ b/Tests/Functional/Fixtures/app/config/config_test_cache_redis.yml @@ -12,6 +12,9 @@ smartive_handlebars: cache: enabled: true service: 'smartive_handlebars.cache.redis' + redis: + client_service: 'snc_redis.default' + key_prefix: 'smartive-handlebars:' templating: template_directories: - %kernel.root_dir%/Resources/hbs_views