Initial commit: apps.supersam24.ru
This commit is contained in:
commit
e955e1da36
6566 files changed
+1271095
No files matched your search
+49
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\CacheProvider;
|
||||
|
||||
use Asm89\Twig\CacheExtension\CacheProviderInterface;
|
||||
use Doctrine\Common\Cache\Cache;
|
||||
|
||||
/**
|
||||
* Adapter class to use the cache classes provider by Doctrine.
|
||||
*
|
||||
* @author Alexander <iam.asm89@gmail.com>
|
||||
*/
|
||||
class DoctrineCacheAdapter implements CacheProviderInterface
|
||||
{
|
||||
private $cache;
|
||||
|
||||
/**
|
||||
* @param Cache $cache
|
||||
*/
|
||||
public function __construct(Cache $cache)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function fetch($key)
|
||||
{
|
||||
return $this->cache->fetch($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function save($key, $value, $lifetime = 0)
|
||||
{
|
||||
return $this->cache->save($key, $value, $lifetime);
|
||||
}
|
||||
}
|
||||
Vendored
+68
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\CacheProvider;
|
||||
|
||||
use Asm89\Twig\CacheExtension\CacheProviderInterface;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
|
||||
/**
|
||||
* Adapter class to make extension interoperable with every PSR-6 adapter.
|
||||
*
|
||||
* @see http://php-cache.readthedocs.io/
|
||||
*
|
||||
* @author Rvanlaak <rvanlaak@gmail.com>
|
||||
*/
|
||||
class PsrCacheAdapter implements CacheProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var CacheItemPoolInterface
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
/**
|
||||
* @param CacheItemPoolInterface $cache
|
||||
*/
|
||||
public function __construct(CacheItemPoolInterface $cache)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return mixed|false
|
||||
*/
|
||||
public function fetch($key)
|
||||
{
|
||||
// PSR-6 implementation returns null, CacheProviderInterface expects false
|
||||
$item = $this->cache->getItem($key);
|
||||
if ($item->isHit()) {
|
||||
return $item->get();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @param int|\DateInterval $lifetime
|
||||
* @return bool
|
||||
*/
|
||||
public function save($key, $value, $lifetime = 0)
|
||||
{
|
||||
$item = $this->cache->getItem($key);
|
||||
$item->set($value);
|
||||
$item->expiresAfter($lifetime);
|
||||
|
||||
return $this->cache->save($item);
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension;
|
||||
|
||||
/**
|
||||
* Cache provider interface.
|
||||
*
|
||||
* @author Alexander <iam.asm89@gmail.com>
|
||||
*/
|
||||
interface CacheProviderInterface
|
||||
{
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @return mixed False, if there was no value to be fetched. Null or a string otherwise.
|
||||
*/
|
||||
public function fetch($key);
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @param integer $lifetime
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function save($key, $value, $lifetime = 0);
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\CacheStrategy;
|
||||
|
||||
use Asm89\Twig\CacheExtension\CacheStrategyInterface;
|
||||
|
||||
/**
|
||||
* CacheStrategy which doesn't cache at all
|
||||
*
|
||||
* This strategy can be used in development mode, e.g. editing twig templates,
|
||||
* to prevent previously cached versions from being rendered.
|
||||
*
|
||||
* @see https://github.com/asm89/twig-cache-extension/pull/29
|
||||
*
|
||||
* @author Hagen Hübel <hhuebel@itinance.com>
|
||||
*
|
||||
* @package Asm89\Twig\CacheExtension\CacheStrategy
|
||||
*/
|
||||
class BlackholeCacheStrategy implements CacheStrategyInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function fetchBlock($key)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function generateKey($annotation, $value)
|
||||
{
|
||||
return microtime(true) . mt_rand();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function saveBlock($key, $block)
|
||||
{
|
||||
// fire and forget
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\CacheStrategy;
|
||||
|
||||
use Asm89\Twig\CacheExtension\CacheProviderInterface;
|
||||
use Asm89\Twig\CacheExtension\CacheStrategyInterface;
|
||||
use Asm89\Twig\CacheExtension\Exception\InvalidCacheKeyException;
|
||||
|
||||
/**
|
||||
* Strategy for generational caching.
|
||||
*
|
||||
* In theory the strategy only saves fragments to the cache with infinite
|
||||
* lifetime. The key of the strategy lies in the fact that the keys for blocks
|
||||
* will change as the value for which the key is generated changes.
|
||||
*
|
||||
* For example: entities containing a last update time, would include a
|
||||
* timestamp in the key.
|
||||
*
|
||||
* @see http://37signals.com/svn/posts/3113-how-key-based-cache-expiration-works
|
||||
*
|
||||
* @author Alexander <iam.asm89@gmail.com>
|
||||
*/
|
||||
class GenerationalCacheStrategy implements CacheStrategyInterface
|
||||
{
|
||||
private $keyGenerator;
|
||||
private $cache;
|
||||
private $lifetime;
|
||||
|
||||
/**
|
||||
* @param CacheProviderInterface $cache
|
||||
* @param KeyGeneratorInterface $keyGenerator
|
||||
* @param integer $lifetime
|
||||
*/
|
||||
public function __construct(CacheProviderInterface $cache, KeyGeneratorInterface $keyGenerator, $lifetime = 0)
|
||||
{
|
||||
$this->keyGenerator = $keyGenerator;
|
||||
$this->cache = $cache;
|
||||
$this->lifetime = $lifetime;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function fetchBlock($key)
|
||||
{
|
||||
return $this->cache->fetch($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function generateKey($annotation, $value)
|
||||
{
|
||||
$key = $this->keyGenerator->generateKey($value);
|
||||
|
||||
if (null === $key) {
|
||||
throw new InvalidCacheKeyException();
|
||||
}
|
||||
|
||||
return $annotation . '__GCS__' . $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function saveBlock($key, $block)
|
||||
{
|
||||
return $this->cache->save($key, $block, $this->lifetime);
|
||||
}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\CacheStrategy;
|
||||
|
||||
use Asm89\Twig\CacheExtension\CacheStrategyInterface;
|
||||
use Asm89\Twig\CacheExtension\Exception\NonExistingStrategyException;
|
||||
use Asm89\Twig\CacheExtension\Exception\NonExistingStrategyKeyException;
|
||||
|
||||
/**
|
||||
* Combines several configured cache strategies.
|
||||
*
|
||||
* Useful for combining for example generational cache strategy with a lifetime
|
||||
* cache strategy, but also useful when combining several generational cache
|
||||
* strategies which differ on cache lifetime (infinite, 1hr, 5m).
|
||||
*
|
||||
* @author Alexander <iam.asm89@gmail.com>
|
||||
*/
|
||||
class IndexedChainingCacheStrategy implements CacheStrategyInterface
|
||||
{
|
||||
/**
|
||||
* @var CacheStrategyInterface[]
|
||||
*/
|
||||
private $strategies;
|
||||
|
||||
/**
|
||||
* @param array $strategies
|
||||
*/
|
||||
public function __construct(array $strategies)
|
||||
{
|
||||
$this->strategies = $strategies;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function fetchBlock($key)
|
||||
{
|
||||
return $this->strategies[$key['strategyKey']]->fetchBlock($key['key']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function generateKey($annotation, $value)
|
||||
{
|
||||
if (!is_array($value) || null === $strategyKey = key($value)) {
|
||||
throw new NonExistingStrategyKeyException();
|
||||
}
|
||||
|
||||
if (!isset($this->strategies[$strategyKey])) {
|
||||
throw new NonExistingStrategyException($strategyKey);
|
||||
}
|
||||
|
||||
return array(
|
||||
'strategyKey' => $strategyKey,
|
||||
'key' => $this->strategies[$strategyKey]->generateKey($annotation, current($value)),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function saveBlock($key, $block)
|
||||
{
|
||||
return $this->strategies[$key['strategyKey']]->saveBlock($key['key'], $block);
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\CacheStrategy;
|
||||
|
||||
/**
|
||||
* Generates a key for a given value.
|
||||
*
|
||||
* @author Alexander <iam.asm89@gmail.com>
|
||||
*/
|
||||
interface KeyGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* Generate a cache key for a given value.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generateKey($value);
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\CacheStrategy;
|
||||
|
||||
use Asm89\Twig\CacheExtension\CacheProviderInterface;
|
||||
use Asm89\Twig\CacheExtension\CacheStrategyInterface;
|
||||
use Asm89\Twig\CacheExtension\Exception\InvalidCacheLifetimeException;
|
||||
|
||||
/**
|
||||
* Strategy for caching with a pre-defined lifetime.
|
||||
*
|
||||
* The value passed to the strategy is the lifetime of the cache block in
|
||||
* seconds.
|
||||
*
|
||||
* @author Alexander <iam.asm89@gmail.com>
|
||||
*/
|
||||
class LifetimeCacheStrategy implements CacheStrategyInterface
|
||||
{
|
||||
private $cache;
|
||||
|
||||
/**
|
||||
* @param CacheProviderInterface $cache
|
||||
*/
|
||||
public function __construct(CacheProviderInterface $cache)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function fetchBlock($key)
|
||||
{
|
||||
return $this->cache->fetch($key['key']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function generateKey($annotation, $value)
|
||||
{
|
||||
if (!is_numeric($value)) {
|
||||
throw new InvalidCacheLifetimeException($value);
|
||||
}
|
||||
|
||||
return array(
|
||||
'lifetime' => $value,
|
||||
'key' => '__LCS__' . $annotation,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function saveBlock($key, $block)
|
||||
{
|
||||
return $this->cache->save($key['key'], $block, $key['lifetime']);
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension;
|
||||
|
||||
/**
|
||||
* Cache strategy interface.
|
||||
*
|
||||
* @author Alexander <iam.asm89@gmail.com>
|
||||
*/
|
||||
interface CacheStrategyInterface
|
||||
{
|
||||
/**
|
||||
* Fetch the block for a given key.
|
||||
*
|
||||
* @param mixed $key
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetchBlock($key);
|
||||
|
||||
/**
|
||||
* Generate a key for the value.
|
||||
*
|
||||
* @param string $annotation
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function generateKey($annotation, $value);
|
||||
|
||||
/**
|
||||
* Save the contents of a rendered block.
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param string $block
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function saveBlock($key, $block);
|
||||
}
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\Exception;
|
||||
|
||||
/**
|
||||
* @todo Replace \RuntimeException with \InvalidArgumentException at version 2.0
|
||||
*/
|
||||
class BaseException extends \RuntimeException
|
||||
{
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\Exception;
|
||||
|
||||
class InvalidCacheKeyException extends BaseException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($message = 'Key generator did not return a key.', $code = 0, \Exception $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\Exception;
|
||||
|
||||
class InvalidCacheLifetimeException extends BaseException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($lifetime, $code = 0, \Exception $previous = null)
|
||||
{
|
||||
parent::__construct(sprintf('Value "%s" is not a valid lifetime.', gettype($lifetime)), $code, $previous);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\Exception;
|
||||
|
||||
class NonExistingStrategyException extends BaseException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($strategyKey, $code = 0, \Exception $previous = null)
|
||||
{
|
||||
parent::__construct(sprintf('No strategy configured with key "%s".', $strategyKey), $code, $previous);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\Exception;
|
||||
|
||||
class NonExistingStrategyKeyException extends BaseException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($message = 'No strategy key found in value.', $code = 0, \Exception $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension;
|
||||
|
||||
/**
|
||||
* Extension for caching template blocks with twig.
|
||||
*
|
||||
* @author Alexander <iam.asm89@gmail.com>
|
||||
*/
|
||||
class Extension extends \Twig_Extension
|
||||
{
|
||||
private $cacheStrategy;
|
||||
|
||||
/**
|
||||
* @param CacheStrategyInterface $cacheStrategy
|
||||
*/
|
||||
public function __construct(CacheStrategyInterface $cacheStrategy)
|
||||
{
|
||||
$this->cacheStrategy = $cacheStrategy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CacheStrategyInterface
|
||||
*/
|
||||
public function getCacheStrategy()
|
||||
{
|
||||
return $this->cacheStrategy;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
if (version_compare(\Twig_Environment::VERSION, '1.26.0', '>=')) {
|
||||
return __CLASS__;
|
||||
}
|
||||
return 'asm89_cache';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getTokenParsers()
|
||||
{
|
||||
return array(
|
||||
new TokenParser\Cache(),
|
||||
);
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\Node;
|
||||
|
||||
/**
|
||||
* Cache twig node.
|
||||
*
|
||||
* @author Alexander <iam.asm89@gmail.com>
|
||||
*/
|
||||
class CacheNode extends \Twig_Node
|
||||
{
|
||||
private static $cacheCount = 1;
|
||||
|
||||
/**
|
||||
* @param \Twig_Node_Expression $annotation
|
||||
* @param \Twig_Node_Expression $keyInfo
|
||||
* @param \Twig_NodeInterface $body
|
||||
* @param integer $lineno
|
||||
* @param string $tag
|
||||
*/
|
||||
public function __construct(\Twig_Node_Expression $annotation, \Twig_Node_Expression $keyInfo, \Twig_Node $body, $lineno, $tag = null)
|
||||
{
|
||||
parent::__construct(array('key_info' => $keyInfo, 'body' => $body, 'annotation' => $annotation), array(), $lineno, $tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function compile(\Twig_Compiler $compiler)
|
||||
{
|
||||
$i = self::$cacheCount++;
|
||||
|
||||
if (version_compare(\Twig_Environment::VERSION, '1.26.0', '>=')) {
|
||||
$extension = 'Asm89\Twig\CacheExtension\Extension';
|
||||
} else {
|
||||
$extension = 'asm89_cache';
|
||||
}
|
||||
|
||||
$compiler
|
||||
->addDebugInfo($this)
|
||||
->write("\$asm89CacheStrategy".$i." = \$this->env->getExtension('{$extension}')->getCacheStrategy();\n")
|
||||
->write("\$asm89Key".$i." = \$asm89CacheStrategy".$i."->generateKey(")
|
||||
->subcompile($this->getNode('annotation'))
|
||||
->raw(", ")
|
||||
->subcompile($this->getNode('key_info'))
|
||||
->write(");\n")
|
||||
->write("\$asm89CacheBody".$i." = \$asm89CacheStrategy".$i."->fetchBlock(\$asm89Key".$i.");\n")
|
||||
->write("if (\$asm89CacheBody".$i." === false) {\n")
|
||||
->indent()
|
||||
->write("ob_start();\n")
|
||||
->indent()
|
||||
->subcompile($this->getNode('body'))
|
||||
->outdent()
|
||||
->write("\n")
|
||||
->write("\$asm89CacheBody".$i." = ob_get_clean();\n")
|
||||
->write("\$asm89CacheStrategy".$i."->saveBlock(\$asm89Key".$i.", \$asm89CacheBody".$i.");\n")
|
||||
->outdent()
|
||||
->write("}\n")
|
||||
->write("echo \$asm89CacheBody".$i.";\n")
|
||||
;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of twig-cache-extension.
|
||||
*
|
||||
* (c) Alexander <iam.asm89@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Asm89\Twig\CacheExtension\TokenParser;
|
||||
|
||||
use Asm89\Twig\CacheExtension\Node\CacheNode;
|
||||
|
||||
/**
|
||||
* Parser for cache/endcache blocks.
|
||||
*
|
||||
* @author Alexander <iam.asm89@gmail.com>
|
||||
*/
|
||||
class Cache extends \Twig_TokenParser
|
||||
{
|
||||
/**
|
||||
* @param \Twig_Token $token
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function decideCacheEnd(\Twig_Token $token)
|
||||
{
|
||||
return $token->test('endcache');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getTag()
|
||||
{
|
||||
return 'cache';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function parse(\Twig_Token $token)
|
||||
{
|
||||
$lineno = $token->getLine();
|
||||
$stream = $this->parser->getStream();
|
||||
|
||||
$annotation = $this->parser->getExpressionParser()->parseExpression();
|
||||
|
||||
$key = $this->parser->getExpressionParser()->parseExpression();
|
||||
|
||||
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
|
||||
$body = $this->parser->subparse(array($this, 'decideCacheEnd'), true);
|
||||
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
|
||||
|
||||
return new CacheNode($annotation, $key, $body, $lineno, $this->getTag());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user