Skip to content

Commit 6196890

Browse files
author
Bogdan Rancichi
committed
complete cache expressions
1 parent db875ca commit 6196890

10 files changed

+215
-11
lines changed

src/DependencyInjection/Compiler/CacheCompilerPass.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected function analyzeServicesTobeCached(ContainerBuilder $container)
4343
$proxyWarmup = $container->getDefinition('emag.cache.warmup');
4444
$cacheProxyFactory = new Reference('emag.cache.proxy.factory');
4545
$cacheServiceReference = new Reference($container->getParameter('emag.cache.service'));
46-
$expressionLanguage = $container->hasParameter('emag.cache.expression.language') ? new Reference($container->getParameter('emag.cache.expression.language')) : null;
46+
$expressionLanguage = $container->hasDefinition('emag.cache.expression.language') || $container->hasAlias('emag.cache.expression.language') ? new Reference('emag.cache.expression.language') : null;
4747

4848
foreach ($container->getDefinitions() as $serviceId => $definition) {
4949
if (!class_exists($definition->getClass()) || $this->isFromIgnoredNamespace($container, $definition->getClass())) {
@@ -101,4 +101,9 @@ private function isFromIgnoredNamespace(ContainerBuilder $container, $className)
101101
}
102102
return false;
103103
}
104+
105+
private function getExpressionLanguage()
106+
{
107+
108+
}
104109
}

src/DependencyInjection/EmagCacheExtension.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
88
use Symfony\Component\Config\FileLocator;
99
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
use Symfony\Component\DependencyInjection\Definition;
1011
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
1112
use Symfony\Component\DependencyInjection\Loader;
13+
use Symfony\Component\DependencyInjection\Reference;
1214
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1315
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1416

@@ -43,7 +45,7 @@ public function prepend(ContainerBuilder $container)
4345
}
4446

4547
$expressionLanguage = new \ReflectionClass($container->getDefinition($config['expression_language'])->getClass());
46-
if (!$expressionLanguage->isSubclassOf(ExpressionLanguage::class) || !($expressionLanguage instanceof ExpressionLanguage::class)) {
48+
if ($expressionLanguage->getName() !== ExpressionLanguage::class) {
4749
throw new CacheException(sprintf('You must provide a valid Expression Language service'));
4850
}
4951
}
@@ -59,9 +61,12 @@ public function load(array $configs, ContainerBuilder $container)
5961
$container->setParameter('emag.cache.service', $config['provider']);
6062
$container->setParameter('emag.cache.ignore.namespaces', $config['ignore_namespaces']);
6163
if (!$config['expression_language'] && class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
62-
$container->setParameter('emag.cache.expression.language', new ExpressionLanguage(new FilesystemAdapter('expr_cache')));
64+
$container->addDefinitions([
65+
'emag.cache.filesystem.adapter' => (new Definition(FilesystemAdapter::class))->addArgument('expr_cache'),
66+
'emag.cache.expression.language'=> (new Definition(ExpressionLanguage::class))->addArgument(new Reference('emag.cache.filesystem.adapter')),
67+
]);
6368
} elseif ($config['expression_language']) {
64-
$container->setParameter('emag.cache.expression.language', $config['expression_language']);
69+
$container->setAlias('emag.cache.expression.language', $config['expression_language']);
6570
}
6671

6772
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace CacheBundle\Tests;
4+
5+
use Doctrine\Common\Annotations\AnnotationReader;
6+
use Doctrine\Common\Annotations\AnnotationRegistry;
7+
use Emag\CacheBundle\Annotation\CacheExpression;
8+
use Emag\CacheBundle\Tests\Helpers\CacheableExpressionClass;
9+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
10+
use Symfony\Component\Config\Loader\LoaderInterface;
11+
use Symfony\Component\DependencyInjection\ContainerInterface;
12+
use Symfony\Component\HttpKernel\Kernel;
13+
14+
class CacheExpressionDefaultTest extends KernelTestCase
15+
{
16+
/**
17+
* @var ContainerInterface
18+
*/
19+
protected $container;
20+
21+
public function setUp()
22+
{
23+
parent::setUp();
24+
25+
static::$class = null;
26+
self::bootKernel(['environment' => 'test_expr_lang_default']);
27+
$this->container = self::$kernel->getContainer();
28+
}
29+
30+
protected static function getKernelClass()
31+
{
32+
return get_class(new class('test_expr_lang_default', []) extends Kernel
33+
{
34+
public function registerBundles()
35+
{
36+
return [
37+
new \Emag\CacheBundle\EmagCacheBundle()
38+
];
39+
}
40+
41+
public function registerContainerConfiguration(LoaderInterface $loader)
42+
{
43+
$loader->load(__DIR__ . '/config_default_expression.yml');
44+
}
45+
46+
public function __construct($environment, $debug)
47+
{
48+
parent::__construct($environment, $debug);
49+
50+
$loader = require __DIR__ . '/../vendor/autoload.php';
51+
52+
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
53+
$this->rootDir = __DIR__ . '/app/';
54+
}
55+
});
56+
}
57+
58+
public function testDefaultExpressionLanguage()
59+
{
60+
/** @var CacheableExpressionClass $object */
61+
$object = $this->container->get('cache.expr.test.service');
62+
$methodName = 'getIntenseResult';
63+
$objectReflectionClass = new \ReflectionClass($object);
64+
$annotationReader = $this->container->get('annotation_reader');
65+
/** @var CacheExpression $cacheExpressionAnnotation */
66+
$cacheExpressionAnnotation = $annotationReader->getMethodAnnotation(new \ReflectionMethod($objectReflectionClass->getParentClass()->getName(), $methodName), CacheExpression::class);
67+
$cacheExpressionAnnotation
68+
->setExpressionLanguage($this->container->get('emag.cache.expression.language'))
69+
->setContext($object)
70+
;
71+
72+
$result = $object->$methodName();
73+
$this->assertContains($object->buildCachePrefix(), $cacheExpressionAnnotation->getCache());
74+
$this->assertEquals(0, strpos($cacheExpressionAnnotation->getCache(), $object->buildCachePrefix()));
75+
$this->assertEquals($result, $object->$methodName());
76+
}
77+
78+
public function tearDown()
79+
{
80+
static::$class = null;
81+
}
82+
}

tests/CacheWrapperTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,21 @@ public function testCachePrefixExpressions()
155155
{
156156
/** @var CacheableClass $object */
157157
$object = $this->container->get('cache.testservice');
158+
$methodName = 'getCachePrefixFromExpression';
158159
$objectReflectionClass = new \ReflectionClass($object);
159-
$annotationReader = new AnnotationReader();
160+
$annotationReader = $this->container->get('annotation_reader');
160161
/** @var CacheExpression $cacheExpressionAnnotation */
161-
$cacheExpressionAnnotation = $annotationReader->getMethodAnnotation(new \ReflectionMethod($objectReflectionClass->getParentClass()->getName(), 'getCachePrefixFromExpression'), CacheExpression::class);
162-
$cacheExpressionAnnotation->setContext($object);
162+
$cacheExpressionAnnotation = $annotationReader->getMethodAnnotation(new \ReflectionMethod($objectReflectionClass->getParentClass()->getName(), $methodName), CacheExpression::class);
163+
$cacheExpressionAnnotation
164+
->setExpressionLanguage($this->container->get('emag.cache.expression.language'))
165+
->setContext($object)
166+
;
163167

168+
$result = $object->$methodName();
164169
$this->assertContains($object->calculateCachePrefix(), $cacheExpressionAnnotation->getCache());
165170
$this->assertEquals(0, strpos($cacheExpressionAnnotation->getCache(), $object->calculateCachePrefix()));
171+
sleep(1);
172+
$this->assertEquals($result, $object->$methodName());
166173
}
167174

168175
/**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Emag\CacheBundle\Tests\Helpers;
4+
5+
use Emag\CacheBundle\Annotation as eMAG;
6+
7+
class CacheableExpressionClass
8+
{
9+
/** @var string */
10+
private $prefix;
11+
12+
public function __construct($prefix)
13+
{
14+
$this->prefix = $prefix;
15+
}
16+
17+
/**
18+
* @eMAG\CacheExpression(cache="this.buildCachePrefix()")
19+
*
20+
* @return int
21+
*/
22+
public function getIntenseResult() : int
23+
{
24+
return rand();
25+
}
26+
27+
/**
28+
* @return string
29+
*/
30+
public function buildCachePrefix() : string
31+
{
32+
return sprintf('_expr[%s]', $this->prefix);
33+
}
34+
}

tests/IncorrectCachingServiceTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class IncorrectCachingServiceTest extends KernelTestCase
1010
{
1111
protected static function getKernelClass()
1212
{
13-
return get_class(new class('test_incorrect_service', []) extends Kernel
13+
return get_class(new class('test_incorrect_cache_service', []) extends Kernel
1414
{
1515
public function registerBundles()
1616
{
@@ -21,7 +21,7 @@ public function registerBundles()
2121

2222
public function registerContainerConfiguration(LoaderInterface $loader)
2323
{
24-
$loader->load(__DIR__ . '/config_incorrect_service.yml');
24+
$loader->load(__DIR__ . '/config_incorrect_cache_service.yml');
2525
}
2626

2727
public function __construct($environment, $debug)
@@ -42,6 +42,6 @@ public function __construct($environment, $debug)
4242
public function testIncorrectService()
4343
{
4444
static::$class = null;
45-
self::bootKernel(['environment' => 'test_incorrect_service']);
45+
self::bootKernel(['environment' => 'test_incorrect_cache_service']);
4646
}
4747
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Emag\CacheBundle\Tests;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6+
use Symfony\Component\Config\Loader\LoaderInterface;
7+
use Symfony\Component\HttpKernel\Kernel;
8+
9+
class IncorrectExpressionLanguageServiceTest extends KernelTestCase
10+
{
11+
protected static function getKernelClass()
12+
{
13+
return get_class(new class('test_incorrect_expr_lang_service', []) extends Kernel
14+
{
15+
public function registerBundles()
16+
{
17+
return [
18+
new \Emag\CacheBundle\EmagCacheBundle()
19+
];
20+
}
21+
22+
public function registerContainerConfiguration(LoaderInterface $loader)
23+
{
24+
$loader->load(__DIR__ . '/config_incorrect_expr_lang_service.yml');
25+
}
26+
27+
public function __construct($environment, $debug)
28+
{
29+
require __DIR__ . '/../vendor/autoload.php';
30+
31+
parent::__construct($environment, $debug);
32+
33+
$this->rootDir = __DIR__ . '/app/';
34+
}
35+
});
36+
}
37+
38+
/**
39+
* @expectedException \Emag\CacheBundle\Exception\CacheException
40+
* @expectedExceptionMessage You must provide a valid Expression Language service
41+
*/
42+
public function testIncorrectService()
43+
{
44+
static::$class = null;
45+
self::bootKernel(['environment' => 'test_incorrect_expr_lang_service']);
46+
}
47+
}

tests/config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
parameters:
2-
cache.service: cache.service
32
max.value: 20
43

54
monolog:
@@ -9,6 +8,7 @@ monolog:
98
level: info
109
emag_cache:
1110
provider: cache.service
11+
expression_language: expr.lang.service
1212

1313
services:
1414
cache_warmer:
@@ -23,3 +23,5 @@ services:
2323
class: Emag\CacheBundle\Tests\Helpers\ExtendedCacheableClass
2424
annotation_reader:
2525
class: Doctrine\Common\Annotations\AnnotationReader
26+
expr.lang.service:
27+
class: Symfony\Component\ExpressionLanguage\ExpressionLanguage
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
emag_cache:
2+
provider: cache.service
3+
4+
services:
5+
cache_warmer:
6+
class: Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate
7+
cache.service:
8+
class: Symfony\Component\Cache\Adapter\ArrayAdapter
9+
cache.expr.test.service:
10+
class: Emag\CacheBundle\Tests\Helpers\CacheableExpressionClass
11+
arguments: ["prefix_cache"]
12+
annotation_reader:
13+
class: Doctrine\Common\Annotations\AnnotationReader
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
emag_cache:
2+
provider: cache.service
3+
expression_language: expr.lang.fake
4+
5+
services:
6+
expr.lang.fake:
7+
class: Emag\CacheBundle\Tests\Helpers\CacheableClass
8+
cache.service:
9+
class: Symfony\Component\Cache\Adapter\ArrayAdapter

0 commit comments

Comments
 (0)