Skip to content

Commit d68db38

Browse files
committed
BC - change cache service
1 parent 9ef6136 commit d68db38

File tree

7 files changed

+33
-105
lines changed

7 files changed

+33
-105
lines changed

Cache/LocalFile.php

Lines changed: 0 additions & 74 deletions
This file was deleted.

DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function getConfigTreeBuilder()
3030
$rootNode
3131
->children()
3232
->scalarNode('enabled')->defaultValue(true)->end()
33-
->scalarNode('cache')->defaultValue(null)->end()
33+
->scalarNode('cache_adapter')->defaultValue(null)->end()
3434
->end()
3535
->children()
3636
->arrayNode('modifiers')

DependencyInjection/PhpMobTwigModifyExtension.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ public function load(array $configs, ContainerBuilder $container)
4343
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
4444
$loader->load('services.xml');
4545

46-
$definition = $container->getDefinition('phpmob_twig_modify.modifier');
47-
$definition->replaceArgument(0, new Reference($config['cache'] ?: 'phpmob_twig_modify.cache.local_file'));
46+
if ($config['cache_adapter']) {
47+
$definition = $container->getDefinition('phpmob_twig_modify.modifier');
48+
$definition->replaceArgument(0, new Reference($config['cache_adapter']));
49+
}
4850

4951
foreach ($config['modifiers'] as $key => $options) {
5052
$definition->addMethodCall('addType', [$key, $options]);

Modifier/Modify.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace PhpMob\TwigModifyBundle\Modifier;
1313

14-
use Doctrine\Common\Cache\Cache;
14+
use Symfony\Component\Cache\Adapter\AdapterInterface;
1515

1616
/**
1717
* @author Ishmael Doss <[email protected]>
@@ -26,10 +26,10 @@ class Modify
2626
static private $types = [];
2727

2828
/**
29-
* @param Cache $cache
29+
* @param AdapterInterface $cache
3030
* @param array $types
3131
*/
32-
public function __construct(Cache $cache = null, array $types = [])
32+
public function __construct(AdapterInterface $cache = null, array $types = [])
3333
{
3434
self::$cache = $cache;
3535
self::$types = $types;
@@ -46,9 +46,10 @@ public static function addType($type, $minifier)
4646

4747
/**
4848
* @param $content
49-
* @param string $type
49+
* @param $type
50+
* @return mixed
5051
*
51-
* @return string
52+
* @throws \Psr\Cache\InvalidArgumentException
5253
*/
5354
public static function modify($content, $type)
5455
{
@@ -64,16 +65,16 @@ public static function modify($content, $type)
6465

6566
$cacheId = md5($content);
6667

67-
if (self::$cache && self::$cache->contains($cacheId)) {
68-
return self::$cache->fetch($cacheId);
68+
if (self::$cache && self::$cache->hasItem($cacheId)) {
69+
return self::$cache->getItem($cacheId)->get();
6970
}
7071

7172
$content = call_user_func_array(
7273
[$modifier['class'], $modifier['method']],
7374
[$content, $modifier['options']]
7475
);
7576

76-
self::$cache && self::$cache->save($cacheId, $content);
77+
self::$cache && self::$cache->save(self::$cache->getItem($cacheId)->set($content));
7778

7879
return $content;
7980
}

README.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ recommended.
1010

1111
```yaml
1212
"require": {
13-
"phpmob/twig-modify-bundle": "~1.0"
13+
"phpmob/twig-modify-bundle": "~2.0"
1414
}
1515
```
1616

@@ -90,23 +90,17 @@ A wrapped modifier for `\voku\helper\AntiXSS`, Thanks [voku/anti-xss](https://gi
9090
```
9191

9292
## Cache
93-
TwigModifyBundle use local cache folder by default, however you can use any cache that implemented `\Doctrine\Common\Cache\Cache` interface and then change the confiuration for your cache service:
93+
TwigModifyBundle use local cache folder by default, however you can use any cache that implemented `\Symfony\Component\Cache\Adapter\AdapterInterface` interface and then change the confiuration for your cache service:
9494

9595
```yaml
9696
phpmob_twig_modify:
97-
cache: "my_cache_service_id"
97+
cache_adapter: "my_cache_adapter_service_id"
9898
```
9999
100-
You can also use doctrine cache.
100+
You can also use symfony [framework-bundle cache adapter](https://symfony.com/doc/current/reference/configuration/framework.html#reference-templating-cache).
101101
```yaml
102-
103-
doctrine_cache:
104-
providers:
105-
modifier_cache:
106-
type: file_system
107-
108102
phpmob_twig_modify:
109-
cache: "doctrine_cache.providers.modifier_cache"
103+
cache_adapter: "cache.app"
110104
```
111105
112106
Don't fogot enable `DoctrineCacheBundle` in your `AppKernel.php` - See https://symfony.com/doc/current/bundles/DoctrineCacheBundle/index.html
@@ -139,8 +133,8 @@ Configuration reference:
139133

140134
```yaml
141135
phpmob_twig_modify:
142-
# cache serivce implemented `\Doctrine\Common\Cache\Cache` interface.
143-
cache: "my_cache_service_id"
136+
# cache adapter serivce implemented `\Symfony\Component\Cache\Adapter\AdapterInterface` interface.
137+
cache_adapter: "my_cache_service_id"
144138
# application wide toggle enable/disable all modifiers
145139
enabled: true
146140
# custom/override modifiers (same key of modifier will override other previous defined)

Resources/config/services.xml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
8-
<service id="phpmob_twig_modify.cache.local_file" class="PhpMob\TwigModifyBundle\Cache\LocalFile" public="false">
9-
<argument>%kernel.cache_dir%/modify</argument>
10-
</service>
11-
128
<service id="phpmob_twig_modify.modifier" class="PhpMob\TwigModifyBundle\Modifier\Modify">
13-
<argument/>
9+
<argument type="service">
10+
<service id="phpmob_twig_modify.cache" class="\Symfony\Component\Cache\Adapter\FilesystemAdapter">
11+
<argument /> <!-- namespace -->
12+
<argument>0</argument> <!-- default lifetime -->
13+
<argument>%kernel.cache_dir%/pools</argument>
14+
</service>
15+
</argument>
1416
<argument type="collection">
1517
<argument key="jsmin" type="collection">
1618
<argument key="class">\PhpMob\TwigModifyBundle\Modifier\JSMin</argument>

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
],
2323
"require": {
2424
"php": "^5.6|^7.0",
25-
"doctrine/cache": "^1.4.2",
2625
"ezyang/htmlpurifier": "^4.9",
2726
"tedivm/jshrink": "^1.2",
2827
"tubalmartin/cssmin": "^4.1",
29-
"voku/anti-xss": "^4.0"
28+
"voku/anti-xss": "^4.0",
29+
"symfony/cache": "^3.1",
30+
"symfony/config": "^3.1",
31+
"symfony/dependency-injection": "^3.1",
32+
"symfony/http-kernel": "^3.1"
3033
},
3134
"require-dev": {
3235
"phpunit/phpunit": "^6.4.3"

0 commit comments

Comments
 (0)