8000 feature #19741 [ExpressionLanguage] Making cache PSR6 compliant (Alex… · symfony/symfony@9fc7306 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9fc7306

Browse files
committed
feature #19741 [ExpressionLanguage] Making cache PSR6 compliant (Alexandre GESLIN)
This PR was merged into the 3.2-dev branch. Discussion ---------- [ExpressionLanguage] Making cache PSR6 compliant | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | [#7064](symfony/symfony-docs#7064) Adding Cache component compatible ParserCache in ExpressionLanguage component. I hope you will find it useful :) I would like to make tests also Commits ------- a7352ff [ExpressionLanguage] Making cache PSR6 compliant
2 parents 0ea0958 + a7352ff commit 9fc7306

File tree

11 files changed

+396
-22
lines changed

11 files changed

+396
-22
lines changed

UPGRADE-3.2.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ DependencyInjection
4040
* Calling `get()` on a `ContainerBuilder` instance before compiling the
4141
container is deprecated and will throw an exception in Symfony 4.0.
4242

43+
ExpressionLanguage
44+
-------------------
45+
46+
* Passing a `ParserCacheInterface` instance to the `ExpressionLanguage` has been
47+
deprecated and will not be supported in Symfony 4.0. You should use the
48+
`CacheItemPoolInterface` interface instead.
49+
4350
Form
4451
----
4552

UPGRADE-4.0.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ DependencyInjection
3333
* Requesting a private service with the `Container::get()` method is no longer
3434
supported.
3535

36+
ExpressionLanguage
37+
----------
38+
39+
* The ability to pass a `ParserCacheInterface` instance to the `ExpressionLanguage`
40+
class has been removed. You should use the `CacheItemPoolInterface` interface
41+
instead.
42+
3643
Form
3744
----
3845

src/Symfony/Bridge/Doctrine/ExpressionLanguage/DoctrineParserCache.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111

1212
namespace Symfony\Bridge\Doctrine\ExpressionLanguage;
1313

14+
@trigger_error('The '.__NAMESPACE__.'\DoctrineParserCache class is deprecated since version 3.2 and will be removed in 4.0. Use the Symfony\Component\Cache\Adapter\DoctrineAdapter class instead.', E_USER_DEPRECATED);
15+
1416
use Doctrine\Common\Cache\Cache;
1517
use Symfony\Component\ExpressionLanguage\ParsedExpression;
1618
use Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface;
1719

1820
/**
1921
* @author Adrien Brault <adrien.brault@gmail.com>
22+
*
23+
* @deprecated DoctrineParserCache class is deprecated since version 3.2 and will be removed in 4.0. Use the Symfony\Component\Cache\Adapter\DoctrineAdapter class instead.
2024
*/
2125
class DoctrineParserCache implements ParserCacheInterface
2226
{

src/Symfony/Bridge/Doctrine/Tests/ExpressionLanguage/DoctrineParserCacheTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Symfony\Bridge\Doctrine\ExpressionLanguage\DoctrineParserCache;
1515

16+
/**
17+
* @group legacy
18+
*/
1619
class DoctrineParserCacheTest extends \PHPUnit_Framework_TestCase
1720
{
1821
public function testFetch()

src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\Component\ExpressionLanguage;
1313

14-
use Symfony\Component\ExpressionLanguage\ParserCache\ArrayParserCache;
14+
use Psr\Cache\CacheItemPoolInterface;
15+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
16+
use Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheAdapter;
1517
use Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface;
1618

1719
/**
@@ -22,7 +24,7 @@
2224
class ExpressionLanguage
2325
{
2426
/**
25-
* @var ParserCacheInterface
27+
* @var CacheItemPoolInterface
2628
*/
2729
private $cache;
2830
private $lexer;
@@ -32,12 +34,21 @@ class ExpressionLanguage
3234
protected $functions = array();
3335

3436
/**
35-
* @param ParserCacheInterface $cache
37+
* @param CacheItemPoolInterface $cache
3638
* @param ExpressionFunctionProviderInterface[] $providers
3739
*/
38-
public function __construct(ParserCacheInterface $cache = null, array $providers = array())
40+
public function __construct($cache = null, array $providers = array())
3941
{
40-
$this->cache = $cache ?: new ArrayParserCache();
42+
if (null !== $cache) {
43+
if ($cache instanceof ParserCacheInterface) {
44+
@trigger_error(sprintf('Passing an instance of %s as constructor argument for %s is deprecated as of 3.2 and will be removed in 4.0. Pass an instance of %s instead.', ParserCacheInterface::class, self::class, CacheItemPoolInterface::class), E_USER_DEPRECATED);
45+
$cache = new ParserCacheAdapter($cache);
46+
} elseif (!$cache instanceof CacheItemPoolInterface) {
47+
throw new \InvalidArgumentException(sprintf('Cache argument has to implement %s.', CacheItemPoolInterface::class));
48+
}
49+
}
50+
51+
$this->cache = $cache ?: new ArrayAdapter();
4152
$this->registerFunctions();
4253
foreach ($providers as $provider) {
4354
$this->registerProvider($provider);
@@ -91,13 +102,14 @@ public function parse($expression, $names)
91102
$cacheKeyItems[] = is_int($nameKey) ? $name : $nameKey.':'.$name;
92103
}
93104

94-
$key = $expression.'//'.implode('|', $cacheKeyItems);
105+
$cacheItem = $this->cache->getItem(rawurlencode($expression.'//'.implode('|', $cacheKeyItems)));
95106

96-
if (null === $parsedExpression = $this->cache->fetch($key)) {
107+
if (null === $parsedExpression = $cacheItem->get()) {
97108
$nodes = $this->getParser()->parse($this->getLexer()->tokenize((string) $expression), $names);
98109
$parsedExpression = new ParsedExpression((string) $expression, $nodes);
99110

100-
$this->cache->save($key, $parsedExpression);
111+
$cacheItem->set($parsedExpression);
112+
$this->cache->save($cacheItem);
101113
}
102114

103115
return $parsedExpression;

src/Symfony/Component/ExpressionLanguage/ParserCache/ArrayParserCache.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
namespace Symfony\Component\ExpressionLanguage\ParserCache;
1313

14+
@trigger_error('The '.__NAMESPACE__.'\ArrayParserCache class is deprecated since version 3.2 and will be removed in 4.0. Use the Symfony\Component\Cache\Adapter\ArrayAdapter class instead.', E_USER_DEPRECATED);
15+
1416
use Symfony\Component\ExpressionLanguage\ParsedExpression;
1517

1618
/**
1719
* @author Adrien Brault <adrien.brault@gmail.com>
20+
*
21+
* @deprecated ArrayParserCache class is deprecated since version 3.2 and will be removed in 4.0. Use the Symfony\Component\Cache\Adapter\ArrayAdapter class instead.
1822
*/
1923
class ArrayParserCache implements ParserCacheInterface
2024
{
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\ExpressionLanguage\ParserCache;
13+
14+
use Psr\Cache\CacheItemInterface;
15+
use Psr\Cache\CacheItemPoolInterface;
16+
use Symfony\Component\Cache\CacheItem;
17+
18+
/**
19+
* @author Alexandre GESLIN <alexandre@gesl.in>
20+
*
21+
* @internal This class should be removed in Symfony 4.0.
22+
*/
23+
class ParserCacheAdapter implements CacheItemPoolInterface
24+
{
25+
private $pool;
26+
private $createCacheItem;
27+
28+
public function __construct(ParserCacheInterface $pool)
29+
{
30+
$this->pool = $pool;
31+
32+
$this->createCacheItem = \Closure::bind(
33+
function ($key, $value, $isHit) {
34+
$item = new CacheItem();
35+
$item->key = $key;
36+
$item->value = $value;
37+
$item->isHit = $isHit;
38+
39+
return $item;
40+
},
41+
null,
42+
CacheItem::class
43+
);
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function getItem($key)
50+
{
51+
$value = $this->pool->fetch($key);
52+
$f = $this->createCacheItem;
53+
54+
return $f($key, $value, null !== $value);
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function save(CacheItemInterface $item)
61+
{
62+
$this->pool->save($item->getKey(), $item->get());
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function getItems(array $keys = array())
69+
{
70+
throw new \BadMethodCallException('Not implemented');
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function hasItem($key)
77+
{
78+
throw new \BadMethodCallException('Not implemented');
79+
}
80+
81+
/**
82+
* {@inheritdoc}
83+
*/
84+
public function clear()
85+
{
86+
throw new \BadMethodCallException('Not implemented');
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public function deleteItem($key)
93+
{
94+
throw new \BadMethodCallException('Not implemented');
95+
}
96+
97+
/**
98+
* {@inheritdoc}
99+
*/
100+
public function deleteItems(array $keys)
101+
{
102+
throw new \BadMethodCallException('Not implemented');
103+
}
104+
105+
/**
106+
* {@inheritdoc}
107+
*/
108+
public function saveDeferred(CacheItemInterface $item)
109+
{
110+
throw new \BadMethodCallException('Not implemented');
111+
}
112+
113+
/**
114+
* {@inheritdoc}
115+
*/
116+
public function commit()
117+
{
118+
throw new \BadMethodCallException('Not implemented');
119+
}
120+
}

src/Symfony/Component/ExpressionLanguage/ParserCache/ParserCacheInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
namespace Symfony\Component\ExpressionLanguage\ParserCache;
1313

14+
@trigger_error('The '.__NAMESPACE__.'\ParserCacheInterface interface is deprecated since version 3.2 and will be removed in 4.0. Use Psr\Cache\CacheItemPoolInterface instead.', E_USER_DEPRECATED);
15+
1416
use Symfony\Component\ExpressionLanguage\ParsedExpression;
1517

1618
/**
1719
* @author Adrien Brault <adrien.brault@gmail.com>
20+
*
21+
* @deprecated since version 3.2, to be removed in 4.0. Use Psr\Cache\CacheItemPoolInterface instead.
1822
*/
1923
interface ParserCacheInterface
2024
{

0 commit comments

Comments
 (0)
0