Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ExpressionLanguage] Introduce a ParserCacheInterface with array/doct…
…rine implementations
- Loading branch information
1 parent
b1542f0
commit c8e6799
Showing
6 changed files
with
212 additions
and
6 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/Symfony/Bridge/Doctrine/ExpressionLanguage/DoctrineParserCache.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\ExpressionLanguage; | ||
|
||
use Doctrine\Common\Cache\Cache; | ||
use Symfony\Component\ExpressionLanguage\ParsedExpression; | ||
use Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface; | ||
|
||
/** | ||
* @author Adrien Brault <adrien.brault@gmail.com> | ||
*/ | ||
class DoctrineParserCache implements ParserCacheInterface | ||
{ | ||
/** | ||
* @var Cache | ||
*/ | ||
private $cache; | ||
|
||
public function __construct(Cache $cache) | ||
{ | ||
$this->cache = $cache; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function fetch($key) | ||
{ | ||
return $this->cache->fetch($key); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function save($key, ParsedExpression $expression) | ||
{ | ||
$this->cache->save($key, $expression); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Symfony/Bridge/Doctrine/Tests/ExpressionLanguage/DoctrineParserCacheTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\ExpressionLanguage; | ||
|
||
use Symfony\Bridge\Doctrine\ExpressionLanguage\DoctrineParserCache; | ||
|
||
class DoctrineParserCacheTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testFetch() | ||
{ | ||
$doctrineCacheMock = $this->getMock('Doctrine\Common\Cache\Cache'); | ||
$parserCache = new DoctrineParserCache($doctrineCacheMock); | ||
|
||
$doctrineCacheMock->expects($this->once()) | ||
->method('fetch') | ||
->will($this->returnValue('bar')); | ||
|
||
$result = $parserCache->fetch('foo'); | ||
|
||
$this->assertEquals('bar', $result); | ||
} | ||
|
||
public function testSave() | ||
{ | ||
$doctrineCacheMock = $this->getMock('Doctrine\Common\Cache\Cache'); | ||
$parserCache = new DoctrineParserCache($doctrineCacheMock); | ||
|
||
$expression = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ParsedExpression') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$doctrineCacheMock->expects($this->once()) | ||
->method('save') | ||
->with('foo', $expression); | ||
|
||
$parserCache->save('foo', $expression); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/Symfony/Component/ExpressionLanguage/ParserCache/ArrayParserCache.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\ExpressionLanguage\ParserCache; | ||
|
||
use Symfony\Component\ExpressionLanguage\ParsedExpression; | ||
|
||
/** | ||
* @author Adrien Brault <adrien.brault@gmail.com> | ||
*/ | ||
class ArrayParserCache implements ParserCacheInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $cache = array(); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function fetch($key) | ||
{ | ||
return isset($this->cache[$key]) ? $this->cache[$key] : null; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function save($key, ParsedExpression $expression) | ||
{ | ||
$this->cache[$key] = $expression; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/ExpressionLanguage/ParserCache/ParserCacheInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\ExpressionLanguage\ParserCache; | ||
|
||
use Symfony\Component\ExpressionLanguage\ParsedExpression; | ||
|
||
/** | ||
* @author Adrien Brault <adrien.brault@gmail.com> | ||
*/ | ||
interface ParserCacheInterface | ||
{ | ||
/** | ||
* @param string $key | ||
* @param ParsedExpression $data | ||
*/ | ||
public function save($key, ParsedExpression $expression); | ||
|
||
/** | ||
* @param string $key | ||
* @return ParsedExpression|null | ||
*/ | ||
public function fetch($key); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\ExpressionLanguage\Tests; | ||
|
||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
use Symfony\Component\ExpressionLanguage\ParsedExpression; | ||
|
||
class ExpressionLanguageTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testCachedParse() | ||
{ | ||
$cacheMock = $this->getMock('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface'); | ||
$savedParsedExpression = null; | ||
$expressionLanguage = new ExpressionLanguage($cacheMock); | ||
|
||
$cacheMock | ||
->expects($this->exactly(2)) | ||
->method('fetch') | ||
->with('1 + 1//') | ||
->will($this->returnCallback(function () use (&$savedParsedExpression) { | ||
return $savedParsedExpression; | ||
})) | ||
; | ||
$cacheMock | ||
->expects($this->exactly(1)) | ||
->method('save') | ||
->with('1 + 1//', $this->isInstanceOf('Symfony\Component\ExpressionLanguage\ParsedExpression')) | ||
->will($this->returnCallback(function ($key, $expression) use (&$savedParsedExpression) { | ||
$savedParsedExpression = $expression; | ||
})) | ||
; | ||
|
||
$parsedExpression = $expressionLanguage->parse('1 + 1', array()); | ||
$this->assertSame($savedParsedExpression, $parsedExpression); | ||
|
||
$parsedExpression = $expressionLanguage->parse('1 + 1', array()); | ||
$this->assertSame($savedParsedExpression, $parsedExpression); | ||
} | ||
} |