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

Commit

Permalink
Implemented Twig Cache Tags, finally :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Geolim4 committed Dec 3, 2016
1 parent c8fd93f commit a9ab00c
Show file tree
Hide file tree
Showing 26 changed files with 1,005 additions and 33 deletions.
1 change: 1 addition & 0 deletions Command/phpFastCacheCommand.php
@@ -1,4 +1,5 @@
<?php

/**
*
* This file is part of phpFastCache.
Expand Down
70 changes: 68 additions & 2 deletions DataCollector/CacheCollector.php
@@ -1,10 +1,24 @@
<?php

/**
*
* This file is part of phpFastCache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt file.
*
* @author Georges.L (Geolim4) <contact@geolim4.com>
* @author PastisD https://github.com/PastisD
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
*
*/

namespace phpFastCache\Bundle\DataCollector;

use phpFastCache\Api as phpFastCacheApi;
use phpFastCache\Bundle\phpFastCacheBundle;
use phpFastCache\Bundle\Service\Cache;
use phpFastCache\Cache\ExtendedCacheItemPoolInterface;
use phpFastCache\CacheManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -17,6 +31,11 @@ class CacheCollector extends DataCollector
*/
private $cache;

/**
* @var array
*/
private $twig_cache_blocks = [];

/**
* CacheCollector constructor.
*
Expand Down Expand Up @@ -53,7 +72,9 @@ public function collect(Request $request, Response $response, \Exception $except
}

$this->data = [
'twigCacheBlocks' => $this->twig_cache_blocks,
'apiVersion' => phpFastCacheApi::getVersion(),
'bundleVersion' => phpFastCacheBundle::VERSION,
'apiChangelog' => phpFastCacheApi::getChangelog(),
'driverUsed' => $driverUsed,
'instances' => $instances,
Expand All @@ -64,7 +85,11 @@ public function collect(Request $request, Response $response, \Exception $except
'write' => (int) CacheManager::$WriteHits,
],
'coreConfig' => [
'namespacePath' => CacheManager::getNamespacePath()
'namespacePath' => CacheManager::getNamespacePath(),
],
'projectConfig' => [
'twig_driver' => $this->cache->getConfig()['twig_driver'],
'twig_block_debug' => $this->cache->getConfig()['twig_block_debug'],
],
];
}
Expand Down Expand Up @@ -117,6 +142,14 @@ public function getCoreConfig()
return $this->data[ 'coreConfig' ];
}

/**
* @return mixed
*/
public function getProjectConfig()
{
return $this->data[ 'projectConfig' ];
}

/**
* @return mixed
*/
Expand All @@ -125,6 +158,14 @@ public function getApiVersion()
return $this->data[ 'apiVersion' ];
}

/**
* @return mixed
*/
public function getBundleVersion()
{
return $this->data[ 'bundleVersion' ];
}

/**
* @return mixed
*/
Expand All @@ -133,6 +174,31 @@ public function getApiChangelog()
return $this->data[ 'apiChangelog' ];
}

/**
* @param string $blockName
* @param array $cacheBlock
* @return $this
*/
public function setTwigCacheBlock($blockName, array $cacheBlock)
{
if(isset($this->twig_cache_blocks[$blockName])){
$this->twig_cache_blocks[$blockName] = array_merge($this->twig_cache_blocks[$blockName], $cacheBlock);
}else{
$this->twig_cache_blocks[$blockName] = $cacheBlock;
}


return $this;
}

/**
* @return array
*/
public function getTwigCacheBlocks()
{
return $this->data[ 'twigCacheBlocks' ];
}

/**
* @return string
*/
Expand Down
9 changes: 8 additions & 1 deletion DependencyInjection/Configuration.php
@@ -1,4 +1,5 @@
<?php

/**
*
* This file is part of phpFastCache.
Expand Down Expand Up @@ -37,6 +38,12 @@ public function getConfigTreeBuilder()

$rootNode
->children()
->scalarNode('twig_driver')
->isRequired()
->end()
->booleanNode('twig_block_debug')
->defaultFalse()
->end()
->arrayNode('drivers')
->useAttributeAsKey('name')
->prototype('array')
Expand All @@ -45,7 +52,7 @@ public function getConfigTreeBuilder()
->arrayNode('parameters')->isRequired()->prototype('variable')->end()
->end()
->end()
->end() // drivers
->end()
->end();

return $treeBuilder;
Expand Down
10 changes: 9 additions & 1 deletion DependencyInjection/phpFastCacheExtension.php
@@ -1,4 +1,5 @@
<?php

/**
*
* This file is part of phpFastCache.
Expand Down Expand Up @@ -42,6 +43,14 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

/**
* Includes services_dev.yml only
* if we are in debug mode
*/
if(in_array($container->getParameter('kernel.environment'), ['dev', 'test'])){
$loader->load('services_dev.yml');
}

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

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

$container->setParameter('phpfastcache', $config);

}
}
36 changes: 36 additions & 0 deletions Docs/CREDITS.txt
@@ -0,0 +1,36 @@
phpFastCache Bundle Copyright (c) 2016
http://www.phpfastcache.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


phpFastCache Bundle Bundle Project Manager: Georges.L (Geolim4)

phpFastCache Bundle Contributors: https://github.com/PHPSocialNetwork/phpfastcache-bundle/graphs/contributors

Specials thanks:

- PastisD for helping us at the beginning of the project
- Alexander (asm89) for providing the great Twig Extension slightly modified and integrated to the PhpFastCache bundle


Software licenses:

PHP License, version 3.0:
Pear (c) 2001-2004 PHP Group, http://pear.php.net
Binary file added Docs/Example/Screenshoots/profiler.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion Docs/Example/app/config/phpfastcache-config.yml
@@ -1,5 +1,7 @@
# PhpFastCache configuration
php_fast_cache:
twig_driver: "filecache" # This option must be a valid declared driver, in our example: "filecache" or "memcachecache" or "apccache" etc...
twig_block_debug: false # This option will wrap CACHE/ENDCACHE blocks with block debug as HTML comment
drivers:
filecache:
type: Files
Expand Down Expand Up @@ -54,4 +56,4 @@ php_fast_cache:
parameters: []
devnullcache:
type: Devnull
parameters: []
parameters: []
19 changes: 18 additions & 1 deletion README.md
Expand Up @@ -14,6 +14,8 @@ composer require phpfastcache/phpfastcache-bundle
```yml
# PhpFastCache configuration
php_fast_cache:
twig_driver: "filecache" # This option must be a valid declared driver, in our example: "filecache"
twig_block_debug: false # This option will wrap CACHE/ENDCACHE blocks with block debug as HTML comment
drivers:
filecache:
type: Files
Expand All @@ -32,6 +34,7 @@ $bundles[] = new phpFastCache\Bundle\phpFastCacheBundle();

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

Caching data in your controller:
```php
public function indexAction(Request $request)
{
Expand All @@ -50,7 +53,21 @@ public function indexAction(Request $request)
]);
}
```

Or in your template:
```twig
<div>
{#
* 'myrandom6' Is your cache key identifier, must be unique
* 300 Is the time to live (TTL) before the cache expires and get regenerated
#}
{% cache 'myrandom6' 300 %}
<textarea>
<!-- Some heavy stuff like Doctrine Lazy Entities -->
{% for i in 1..1000 %}{{ random() }}{% endfor %}
</textarea>
{% endcache %}
</div>
```
#### :boom: phpFastCache Bundle support
Found an issue or had an idea ? Come here [here](https://github.com/PHPSocialNetwork/phpfastcache-bundle/issues) and let us know !

40 changes: 24 additions & 16 deletions Resources/config/services.yml
@@ -1,28 +1,36 @@
parameters:
php_fast_cache.cache.class: phpFastCache\Bundle\Service\Cache
php_fast_cache.data_collector.class: phpFastCache\Bundle\DataCollector\CacheCollector
php_fast_cache.human_readable.class: phpFastCache\Bundle\Twig\HumanReadableExtension
php_fast_cache.human_readable.class: phpFastCache\Bundle\Twig\HumanReadableExtension\Extension
php_fast_cache.twig_cache.class: phpFastCache\Bundle\Twig\CacheExtension\Extension
php_fast_cache.twig_cache_stategy.class: phpFastCache\Bundle\Twig\CacheExtension\CacheStrategy\LifetimeCacheStrategy
php_fast_cache.twig_cache_provider.class: phpFastCache\Bundle\Twig\CacheExtension\CacheProvider\PsrCacheAdapter

services:
phpfastcache:
class: "%php_fast_cache.cache.class%"
arguments:
- "%phpfastcache%"
- "@?debug.stopwatch"

phpfastcache.request_collector:
class: "%php_fast_cache.data_collector.class%"
arguments:
- "@phpfastcache"
public: false
tags:
-
name: data_collector
template: '@phpFastCache/data_collector/template.html.twig'
id: 'phpfastcache'
priority: 300

phpfastcache.human_readable_size:
class: "%php_fast_cache.human_readable.class%"
tags:
- { name: twig.extension }
- { name: twig.extension }
phpfastcache.twig_cache_driver_provider:
class: "%php_fast_cache.cache.class%"
factory: [ "@phpfastcache", "getTwigCacheInstance" ]
phpfastcache.twig_cache_provider:
class: "%php_fast_cache.twig_cache_provider.class%"
arguments:
- "@phpfastcache.twig_cache_driver_provider"
phpfastcache.twig_cache_stategy:
class: "%php_fast_cache.twig_cache_stategy.class%"
arguments:
- "@phpfastcache.twig_cache_provider"
- "@?phpfastcache.request_collector"
- "%phpfastcache%"
twig.extension.cache:
class: "%php_fast_cache.twig_cache.class%"
arguments:
- "@phpfastcache.twig_cache_stategy"
tags:
- { name: twig.extension }
15 changes: 15 additions & 0 deletions Resources/config/services_dev.yml
@@ -0,0 +1,15 @@
parameters:
php_fast_cache.data_collector.class: phpFastCache\Bundle\DataCollector\CacheCollector

services:
phpfastcache.request_collector:
class: "%php_fast_cache.data_collector.class%"
arguments:
- "@phpfastcache"
public: false
tags:
-
name: data_collector
template: '@phpFastCache/data_collector/template.html.twig'
id: 'phpfastcache'
priority: 300

0 comments on commit a9ab00c

Please sign in to comment.