Skip to content

Commit

Permalink
Add parameter to specify the Redis client to use
Browse files Browse the repository at this point in the history
  • Loading branch information
mfeltscher committed Feb 4, 2016
1 parent e81eb65 commit 2dd9cee
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
7 changes: 1 addition & 6 deletions Cache/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
*/
class Redis implements Cache
{
/**
* Default key prefix
*/
const KEY_PREFIX_DEFAULT = 'smartive-handlebars:';

/**
* @var PredisClient|RedisClient
*/
Expand All @@ -40,7 +35,7 @@ class Redis implements Cache
* @param LoggerInterface $logger Logger instance
* @param string $keyPrefix A prefix to append
*/
public function __construct($redisClient, LoggerInterface $logger = null, $keyPrefix = self::KEY_PREFIX_DEFAULT)
public function __construct($redisClient, LoggerInterface $logger = null, $keyPrefix = '')
{
if (!$redisClient instanceof PredisClient && !$redisClient instanceof RedisClient) {
throw new \InvalidArgumentException('redisClient has to be of type \Predis\Client or \Redis');
Expand Down
14 changes: 13 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->scalarNode('cache')->info('Service that implements Handlebars\Cache.')->end()
->arrayNode('cache')
->canBeEnabled()
->beforeNormalization()
->ifString()
->then(function ($v) { return array('enabled' => true, 'service' => $v); })
->end()
->children()
->scalarNode('service')
->isRequired()
->info('Service that implements Handlebars\Cache.')
->end()
->end()
->end()
->arrayNode('twig')
->canBeDisabled()
->end()
Expand Down
13 changes: 7 additions & 6 deletions DependencyInjection/SmartiveHandlebarsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ public function load(array $configs, ContainerBuilder $container)
$serviceNames = [
'templating',
'twig',
'cache'
];

foreach ($serviceNames as $serviceName) {
if ($this->isConfigEnabled($container, $config[$serviceName])) {
$this->loadService($serviceName, $config[$serviceName], $container, $loader);

if ('cache' === $serviceName) {
$this->loadCache($container, $config, $loader);
}
}
}

$this->loadCache($container, $config, $loader);
}

/**
Expand Down Expand Up @@ -100,7 +103,7 @@ private function getParameterName($serviceName, $configName)
*/
private function loadCache(ContainerBuilder $container, array $config, XmlFileLoader $loader)
{
if (empty($config['cache'])) {
if (empty($config['cache']['service'])) {
return;
}

Expand All @@ -110,8 +113,6 @@ private function loadCache(ContainerBuilder $container, array $config, XmlFileLo
throw new InvalidConfigurationException('Caching can only be configured if templating is enabled.');
}

$loader->load('cache.xml');

$templatingService->addMethodCall('setCache', [new Reference($config['cache'])]);
$templatingService->addMethodCall('setCache', [new Reference($config['cache']['service'])]);
}
}
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ You can enable caching by setting `smartive_handlebars.cache` to a existing cach

```
smartive_handlebars:
cache: <service-id>
cache:
enabled: true
service: <service-id>
```

There are two caching services / strategies available per default:
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/). Per default, the `snc_redis.default` client is being used. To overwrite this, add your own service definition.
- `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`.

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:
Expand Down
8 changes: 7 additions & 1 deletion Resources/config/cache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="smartive_handlebars.cache.redis.client">snc_redis.default</parameter>
<parameter key="smartive_handlebars.cache.redis.key_prefix">smartive-handlebars:</parameter>
</parameters>

<services>
<service id="smartive_handlebars.cache.apc" class="Handlebars\Cache\APC" public="false" />
<service id="smartive_handlebars.cache.disk" class="Handlebars\Cache\Disk" public="false">
<argument>%kernel.cache_dir%/smartive_handlebars</argument>
</service>
<service id="smartive_handlebars.cache.redis" class="Smartive\HandlebarsBundle\Cache\Redis" public="false">
<argument type="service" id="snc_redis.default" on-invalid="null" />
<argument type="expression" on-invalid="null">service(parameter('smartive_handlebars.cache.redis.client'))</argument>
<argument type="service" id="logger" on-invalid="null" />
<tag name="monolog.logger" channel="smartive_handlebars" />
<argument>%smartive_handlebars.cache.redis.key_prefix%</argument>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ snc_redis:
dsn: redis://localhost

smartive_handlebars:
cache: 'smartive_handlebars.cache.redis'
cache:
enabled: true
service: 'smartive_handlebars.cache.redis'
templating:
template_directories:
- %kernel.root_dir%/Resources/hbs_views

0 comments on commit 2dd9cee

Please sign in to comment.