Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit 6528e2a

Browse files
committed
Implemented Twig Cache Tags, finally :)
1 parent 8c2ba49 commit 6528e2a

27 files changed

+1077
-143
lines changed

Command/phpFastCacheCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
*
45
* This file is part of phpFastCache.
@@ -15,7 +16,6 @@
1516

1617
namespace phpFastCache\Bundle\Command;
1718

18-
use phpFastCache\Cache\ExtendedCacheItemPoolInterface;
1919
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
2020
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
2121
use Symfony\Component\Console\Input\InputArgument;

DataCollector/CacheCollector.php

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
<?php
22

3+
/**
4+
*
5+
* This file is part of phpFastCache.
6+
*
7+
* @license MIT License (MIT)
8+
*
9+
* For full copyright and license information, please see the docs/CREDITS.txt file.
10+
*
11+
* @author Georges.L (Geolim4) <contact@geolim4.com>
12+
* @author PastisD https://github.com/PastisD
13+
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
14+
*
15+
*/
16+
317
namespace phpFastCache\Bundle\DataCollector;
418

519
use phpFastCache\Api as phpFastCacheApi;
20+
use phpFastCache\Bundle\phpFastCacheBundle;
621
use phpFastCache\Bundle\Service\Cache;
7-
use phpFastCache\Cache\ExtendedCacheItemPoolInterface;
822
use phpFastCache\CacheManager;
923
use Symfony\Component\HttpFoundation\Request;
1024
use Symfony\Component\HttpFoundation\Response;
@@ -17,6 +31,11 @@ class CacheCollector extends DataCollector
1731
*/
1832
private $cache;
1933

34+
/**
35+
* @var array
36+
*/
37+
private $twig_cache_blocks = [];
38+
2039
/**
2140
* CacheCollector constructor.
2241
*
@@ -53,7 +72,9 @@ public function collect(Request $request, Response $response, \Exception $except
5372
}
5473

5574
$this->data = [
75+
'twigCacheBlocks' => $this->twig_cache_blocks,
5676
'apiVersion' => phpFastCacheApi::getVersion(),
77+
'bundleVersion' => phpFastCacheBundle::VERSION,
5778
'apiChangelog' => phpFastCacheApi::getChangelog(),
5879
'driverUsed' => $driverUsed,
5980
'instances' => $instances,
@@ -64,7 +85,11 @@ public function collect(Request $request, Response $response, \Exception $except
6485
'write' => (int) CacheManager::$WriteHits,
6586
],
6687
'coreConfig' => [
67-
'namespacePath' => CacheManager::getNamespacePath()
88+
'namespacePath' => CacheManager::getNamespacePath(),
89+
],
90+
'projectConfig' => [
91+
'twig_driver' => $this->cache->getConfig()['twig_driver'],
92+
'twig_block_debug' => $this->cache->getConfig()['twig_block_debug'],
6893
],
6994
];
7095
}
@@ -117,6 +142,14 @@ public function getCoreConfig()
117142
return $this->data[ 'coreConfig' ];
118143
}
119144

145+
/**
146+
* @return mixed
147+
*/
148+
public function getProjectConfig()
149+
{
150+
return $this->data[ 'projectConfig' ];
151+
}
152+
120153
/**
121154
* @return mixed
122155
*/
@@ -125,6 +158,14 @@ public function getApiVersion()
125158
return $this->data[ 'apiVersion' ];
126159
}
127160

161+
/**
162+
* @return mixed
163+
*/
164+
public function getBundleVersion()
165+
{
166+
return $this->data[ 'bundleVersion' ];
167+
}
168+
128169
/**
129170
* @return mixed
130171
*/
@@ -133,6 +174,31 @@ public function getApiChangelog()
133174
return $this->data[ 'apiChangelog' ];
134175
}
135176

177+
/**
178+
* @param string $blockName
179+
* @param array $cacheBlock
180+
* @return $this
181+
*/
182+
public function setTwigCacheBlock($blockName, array $cacheBlock)
183+
{
184+
if(isset($this->twig_cache_blocks[$blockName])){
185+
$this->twig_cache_blocks[$blockName] = array_merge($this->twig_cache_blocks[$blockName], $cacheBlock);
186+
}else{
187+
$this->twig_cache_blocks[$blockName] = $cacheBlock;
188+
}
189+
190+
191+
return $this;
192+
}
193+
194+
/**
195+
* @return array
196+
*/
197+
public function getTwigCacheBlocks()
198+
{
199+
return $this->data[ 'twigCacheBlocks' ];
200+
}
201+
136202
/**
137203
* @return string
138204
*/

DependencyInjection/Configuration.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
*
45
* This file is part of phpFastCache.
@@ -37,6 +38,12 @@ public function getConfigTreeBuilder()
3738

3839
$rootNode
3940
->children()
41+
->scalarNode('twig_driver')
42+
->isRequired()
43+
->end()
44+
->booleanNode('twig_block_debug')
45+
->defaultFalse()
46+
->end()
4047
->arrayNode('drivers')
4148
->useAttributeAsKey('name')
4249
->prototype('array')
@@ -45,7 +52,7 @@ public function getConfigTreeBuilder()
4552
->arrayNode('parameters')->isRequired()->prototype('variable')->end()
4653
->end()
4754
->end()
48-
->end() // drivers
55+
->end()
4956
->end();
5057

5158
return $treeBuilder;

DependencyInjection/phpFastCacheExtension.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
*
45
* This file is part of phpFastCache.
@@ -42,6 +43,14 @@ public function load(array $configs, ContainerBuilder $container)
4243
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
4344
$loader->load('services.yml');
4445

46+
/**
47+
* Includes services_dev.yml only
48+
* if we are in debug mode
49+
*/
50+
if(in_array($container->getParameter('kernel.environment'), ['dev', 'test'])){
51+
$loader->load('services_dev.yml');
52+
}
53+
4554
$configuration = new Configuration();
4655
$config = $this->processConfiguration($configuration, $configs);
4756

@@ -55,6 +64,5 @@ public function load(array $configs, ContainerBuilder $container)
5564
}
5665

5766
$container->setParameter('phpfastcache', $config);
58-
5967
}
6068
}

Docs/CREDITS.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
phpFastCache Bundle Copyright (c) 2016
2+
http://www.phpfastcache.com
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
22+
23+
phpFastCache Bundle Bundle Project Manager: Georges.L (Geolim4)
24+
25+
phpFastCache Bundle Contributors: https://github.com/PHPSocialNetwork/phpfastcache-bundle/graphs/contributors
26+
27+
Specials thanks:
28+
29+
- PastisD for helping us at the beginning of the project
30+
- Alexander (asm89) for providing the great Twig Extension slightly modified and integrated to the PhpFastCache bundle
31+
32+
33+
Software licenses:
34+
35+
PHP License, version 3.0:
36+
Pear (c) 2001-2004 PHP Group, http://pear.php.net
173 KB
Loading

Docs/Example/app/config/phpfastcache-config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# PhpFastCache configuration
22
php_fast_cache:
3+
twig_driver: "filecache" # This option must be a valid declared driver, in our example: "filecache" or "memcachecache" or "apccache" etc...
4+
twig_block_debug: false # This option will wrap CACHE/ENDCACHE blocks with block debug as HTML comment
35
drivers:
46
filecache:
57
type: Files
@@ -54,4 +56,4 @@ php_fast_cache:
5456
parameters: []
5557
devnullcache:
5658
type: Devnull
57-
parameters: []
59+
parameters: []

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ composer require phpfastcache/phpfastcache-bundle
1414
```yml
1515
# PhpFastCache configuration
1616
php_fast_cache:
17+
twig_driver: "filecache" # This option must be a valid declared driver, in our example: "filecache"
18+
twig_block_debug: false # This option will wrap CACHE/ENDCACHE blocks with block debug as HTML comment
1719
drivers:
1820
filecache:
1921
type: Files
@@ -32,6 +34,7 @@ $bundles[] = new phpFastCache\Bundle\phpFastCacheBundle();
3234

3335
#### :rocket: Step 4: Accelerate your app by making use of PhpFastCache service
3436

37+
Caching data in your controller:
3538
```php
3639
public function indexAction(Request $request)
3740
{
@@ -50,7 +53,21 @@ public function indexAction(Request $request)
5053
]);
5154
}
5255
```
53-
56+
Or in your template:
57+
```twig
58+
<div>
59+
{#
60+
* 'myrandom6' Is your cache key identifier, must be unique
61+
* 300 Is the time to live (TTL) before the cache expires and get regenerated
62+
#}
63+
{% cache 'myrandom6' 300 %}
64+
<textarea>
65+
<!-- Some heavy stuff like Doctrine Lazy Entities -->
66+
{% for i in 1..1000 %}{{ random() }}{% endfor %}
67+
</textarea>
68+
{% endcache %}
69+
</div>
70+
```
5471
#### :boom: phpFastCache Bundle support
5572
Found an issue or had an idea ? Come here [here](https://github.com/PHPSocialNetwork/phpfastcache-bundle/issues) and let us know !
5673

Resources/config/services.yml

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
parameters:
22
php_fast_cache.cache.class: phpFastCache\Bundle\Service\Cache
3-
php_fast_cache.data_collector.class: phpFastCache\Bundle\DataCollector\CacheCollector
4-
php_fast_cache.human_readable.class: phpFastCache\Bundle\Twig\HumanReadableExtension
3+
php_fast_cache.human_readable.class: phpFastCache\Bundle\Twig\HumanReadableExtension\Extension
4+
php_fast_cache.twig_cache.class: phpFastCache\Bundle\Twig\CacheExtension\Extension
5+
php_fast_cache.twig_cache_stategy.class: phpFastCache\Bundle\Twig\CacheExtension\CacheStrategy\LifetimeCacheStrategy
6+
php_fast_cache.twig_cache_provider.class: phpFastCache\Bundle\Twig\CacheExtension\CacheProvider\PsrCacheAdapter
57

68
services:
79
phpfastcache:
810
class: "%php_fast_cache.cache.class%"
911
arguments:
1012
- "%phpfastcache%"
1113
- "@?debug.stopwatch"
12-
13-
phpfastcache.request_collector:
14-
class: "%php_fast_cache.data_collector.class%"
15-
arguments:
16-
- "@phpfastcache"
17-
public: false
18-
tags:
19-
-
20-
name: data_collector
21-
template: '@phpFastCache/data_collector/template.html.twig'
22-
id: 'phpfastcache'
23-
priority: 300
24-
2514
phpfastcache.human_readable_size:
2615
class: "%php_fast_cache.human_readable.class%"
2716
tags:
28-
- { name: twig.extension }
17+
- { name: twig.extension }
18+
phpfastcache.twig_cache_driver_provider:
19+
class: "%php_fast_cache.cache.class%"
20+
factory: [ "@phpfastcache", "getTwigCacheInstance" ]
21+
phpfastcache.twig_cache_provider:
22+
class: "%php_fast_cache.twig_cache_provider.class%"
23+
arguments:
24+
- "@phpfastcache.twig_cache_driver_provider"
25+
phpfastcache.twig_cache_stategy:
26+
class: "%php_fast_cache.twig_cache_stategy.class%"
27+
arguments:
28+
- "@phpfastcache.twig_cache_provider"
29+
- "@?phpfastcache.request_collector"
30+
- "%phpfastcache%"
31+
twig.extension.cache:
32+
class: "%php_fast_cache.twig_cache.class%"
33+
arguments:
34+
- "@phpfastcache.twig_cache_stategy"
35+
tags:
36+
- { name: twig.extension }

Resources/config/services_dev.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
parameters:
2+
php_fast_cache.data_collector.class: phpFastCache\Bundle\DataCollector\CacheCollector
3+
4+
services:
5+
phpfastcache.request_collector:
6+
class: "%php_fast_cache.data_collector.class%"
7+
arguments:
8+
- "@phpfastcache"
9+
public: false
10+
tags:
11+
-
12+
name: data_collector
13+
template: '@phpFastCache/data_collector/template.html.twig'
14+
id: 'phpfastcache'
15+
priority: 300

0 commit comments

Comments
 (0)