-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache expression #16
base: master
Are you sure you want to change the base?
Cache expression #16
Changes from all commits
619b728
e1b9786
837a498
0c8645c
fa5dba9
69c081f
1aeedbe
48aea7e
968484a
db875ca
6196890
ac8f56a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Emag\CacheBundle\Annotation; | ||
|
||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"METHOD"}) | ||
*/ | ||
class CacheExpression extends Cache | ||
{ | ||
/** | ||
* @var ExpressionLanguage | ||
*/ | ||
protected $expressionLanguage; | ||
|
||
/** | ||
* @var object | ||
*/ | ||
private $context; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $hasEvaluation = false; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getCache() : string | ||
{ | ||
if (!$this->hasEvaluation) { | ||
$this->cache = $this->expressionLanguage->evaluate($this->cache, ['this' => $this->context]); | ||
$this->hasEvaluation = true; | ||
} | ||
|
||
return $this->cache; | ||
} | ||
|
||
/** | ||
* @param object $context | ||
* | ||
* @return CacheExpression | ||
*/ | ||
public function setContext($context) : self | ||
{ | ||
$this->context = $context; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param ExpressionLanguage $language | ||
* | ||
* @return CacheExpression | ||
*/ | ||
public function setExpressionLanguage(ExpressionLanguage $language) : self | ||
{ | ||
$this->expressionLanguage = $language; | ||
|
||
return $this; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,14 @@ | |
|
||
use Emag\CacheBundle\Exception\CacheException; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Symfony\Component\Cache\Adapter\FilesystemAdapter; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; | ||
use Symfony\Component\DependencyInjection\Loader; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
|
||
/** | ||
|
@@ -31,6 +35,19 @@ public function prepend(ContainerBuilder $container) | |
if (!$provider->implementsInterface(CacheItemPoolInterface::class)) { | ||
throw new CacheException(sprintf('You\'ve referenced a service "%s" that can not be used for caching!', $config['provider'])); | ||
} | ||
|
||
if (!$config['expression_language']) { | ||
return; | ||
} | ||
|
||
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) { | ||
throw new CacheException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.'); | ||
} | ||
|
||
$expressionLanguage = new \ReflectionClass($container->getDefinition($config['expression_language'])->getClass()); | ||
if ($expressionLanguage->getName() !== ExpressionLanguage::class) { | ||
throw new CacheException(sprintf('You must provide a valid Expression Language service')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Too exact class match. You should be able to provide anything that extends expressionLanguage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course I will fix it. |
||
} | ||
} | ||
|
||
/** | ||
|
@@ -43,6 +60,14 @@ public function load(array $configs, ContainerBuilder $container) | |
|
||
$container->setParameter('emag.cache.service', $config['provider']); | ||
$container->setParameter('emag.cache.ignore.namespaces', $config['ignore_namespaces']); | ||
if (!$config['expression_language'] && class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) { | ||
$container->addDefinitions([ | ||
'emag.cache.filesystem.adapter' => (new Definition(FilesystemAdapter::class))->addArgument('expr_cache'), | ||
'emag.cache.expression.language'=> (new Definition(ExpressionLanguage::class))->addArgument(new Reference('emag.cache.filesystem.adapter')), | ||
]); | ||
} elseif ($config['expression_language']) { | ||
$container->setAlias('emag.cache.expression.language', $config['expression_language']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would reverse the if. it's cheaper... :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
} | ||
|
||
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | ||
$loader->load('services.yml'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,12 @@ | |
namespace Emag\CacheBundle\ProxyManager; | ||
|
||
use Emag\CacheBundle\Annotation\Cache; | ||
use Emag\CacheBundle\Annotation\CacheExpression; | ||
use Emag\CacheBundle\Exception\CacheException; | ||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use Doctrine\Common\Annotations\Reader; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
|
||
trait CacheableClassTrait | ||
{ | ||
|
@@ -23,6 +25,8 @@ trait CacheableClassTrait | |
*/ | ||
protected $readerForCacheMethod; | ||
|
||
protected $__expressionLanguage; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's with the __ ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid parameter collision. |
||
/** | ||
* @param CacheItemPoolInterface $cacheServiceForMethod | ||
*/ | ||
|
@@ -39,12 +43,23 @@ public function setReaderForCacheMethod(Reader $readerForCacheMethod) | |
$this->readerForCacheMethod = $readerForCacheMethod; | ||
} | ||
|
||
public function setExpressionLanguage(ExpressionLanguage $language = null) | ||
{ | ||
$this->__expressionLanguage = $language; | ||
} | ||
|
||
public function getCached(\ReflectionMethod $method, $params) | ||
{ | ||
$method->setAccessible(true); | ||
/** @var Cache $annotation */ | ||
$annotation = $this->readerForCacheMethod->getMethodAnnotation($method, Cache::class); | ||
|
||
if ($annotation instanceof CacheExpression) { | ||
$annotation | ||
->setContext($this) | ||
->setExpressionLanguage($this->__expressionLanguage) | ||
; | ||
} | ||
$cacheKey = $this->getCacheKey($method, $params, $annotation); | ||
|
||
$cacheItem = $this->cacheServiceForMethod->getItem($cacheKey); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace CacheBundle\Tests; | ||
|
||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use Doctrine\Common\Annotations\AnnotationRegistry; | ||
use Emag\CacheBundle\Annotation\CacheExpression; | ||
use Emag\CacheBundle\Tests\Helpers\CacheableExpressionClass; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\Config\Loader\LoaderInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\HttpKernel\Kernel; | ||
|
||
class CacheExpressionDefaultTest extends KernelTestCase | ||
{ | ||
/** | ||
* @var ContainerInterface | ||
*/ | ||
protected $container; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
static::$class = null; | ||
self::bootKernel(['environment' => 'test_expr_lang_default']); | ||
$this->container = self::$kernel->getContainer(); | ||
} | ||
|
||
protected static function getKernelClass() | ||
{ | ||
return get_class(new class('test_expr_lang_default', []) extends Kernel | ||
{ | ||
public function registerBundles() | ||
{ | ||
return [ | ||
new \Emag\CacheBundle\EmagCacheBundle() | ||
]; | ||
} | ||
|
||
public function registerContainerConfiguration(LoaderInterface $loader) | ||
{ | ||
$loader->load(__DIR__ . '/config_default_expression.yml'); | ||
} | ||
|
||
public function __construct($environment, $debug) | ||
{ | ||
parent::__construct($environment, $debug); | ||
|
||
$loader = require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
AnnotationRegistry::registerLoader(array($loader, 'loadClass')); | ||
$this->rootDir = __DIR__ . '/app/'; | ||
} | ||
}); | ||
} | ||
|
||
public function testDefaultExpressionLanguage() | ||
{ | ||
/** @var CacheableExpressionClass $object */ | ||
$object = $this->container->get('cache.expr.test.service'); | ||
$methodName = 'getIntenseResult'; | ||
$objectReflectionClass = new \ReflectionClass($object); | ||
$annotationReader = $this->container->get('annotation_reader'); | ||
/** @var CacheExpression $cacheExpressionAnnotation */ | ||
$cacheExpressionAnnotation = $annotationReader->getMethodAnnotation(new \ReflectionMethod($objectReflectionClass->getParentClass()->getName(), $methodName), CacheExpression::class); | ||
$cacheExpressionAnnotation | ||
->setExpressionLanguage($this->container->get('emag.cache.expression.language')) | ||
->setContext($object) | ||
; | ||
|
||
$result = $object->$methodName(); | ||
$this->assertContains($object->buildCachePrefix(), $cacheExpressionAnnotation->getCache()); | ||
$this->assertEquals(0, strpos($cacheExpressionAnnotation->getCache(), $object->buildCachePrefix())); | ||
$this->assertEquals($result, $object->$methodName()); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
static::$class = null; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expression-language can be put as suggested?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you put it as suggested you can't use
@CacheExpression
annotation I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can put it in both require-dev (so you can test it) and in suggests.
Who requires usage of expression should include the package explictly