Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22c5ede

Browse files
committed
[PropertInfo] Cache support
1 parent c3b60a9 commit 22c5ede

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/Symfony/Component/PropertyInfo/PropertyInfoExtractor.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\PropertyInfo;
1313

14+
use Doctrine\Common\Cache\Cache;
15+
1416
/**
1517
* Default {@see PropertyInfoExtractorInterface} implementation.
1618
*
@@ -38,18 +40,29 @@ class PropertyInfoExtractor implements PropertyInfoExtractorInterface
3840
*/
3941
private $accessExtractors;
4042

43+
/**
44+
* @var Cache
45+
*/
46+
private $cache;
47+
48+
/**
49+
* @var array
50+
*/
51+
private $arrayCache = array();
52+
4153
/**
4254
* @param PropertyListExtractorInterface[] $listExtractors
4355
* @param PropertyTypeExtractorInterface[] $typeExtractors
4456
* @param PropertyDescriptionExtractorInterface[] $descriptionExtractors
4557
* @param PropertyAccessExtractorInterface[] $accessExtractors
4658
*/
47-
public function __construct(array $listExtractors = array(), array $typeExtractors = array(), array $descriptionExtractors = array(), array $accessExtractors = array())
59+
public function __construct(array $listExtractors = array(), array $typeExtractors = array(), array $descriptionExtractors = array(), array $accessExtractors = array(), Cache $cache = null)
4860
{
4961
$this->listExtractors = $listExtractors;
5062
$this->typeExtractors = $typeExtractors;
5163
$this->descriptionExtractors = $descriptionExtractors;
5264
$this->accessExtractors = $accessExtractors;
65+
$this->cache = $cache;
5366
}
5467

5568
/**
@@ -111,11 +124,27 @@ public function isWritable($class, $property, array $context = array())
111124
*/
112125
private function extract(array $extractors, $method, array $arguments)
113126
{
127+
$key = $method . serialize($arguments);
128+
129+
if (isset($this->arrayCache[$key])) {
130+
return $this->arrayCache[$key];
131+
}
132+
133+
if ($this->cache && $value = $this->cache->fetch($key)) {
134+
return $this->arrayCache[$key] = $value;
135+
}
136+
114137
foreach ($extractors as $extractor) {
115138
$value = call_user_func_array(array($extractor, $method), $arguments);
116139
if (null !== $value) {
117-
return $value;
140+
break;
118141
}
119142
}
143+
144+
if ($this->cache) {
145+
$this->cache->save($key, $value);
146+
}
147+
148+
return $this->arrayCache[$key] = $value;
120149
}
121150
}

src/Symfony/Component/PropertyInfo/Tests/PropertyInfoExtractorTest.php

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

1212
namespace Symfony\Component\PropertyInfo\PropertyInfo\Tests;
1313

14+
use Doctrine\Common\Cache\ArrayCache;
1415
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
1516
use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyExtractor;
1617
use Symfony\Component\PropertyInfo\Tests\Fixtures\NullExtractor;
@@ -69,4 +70,13 @@ public function testGetProperties()
6970
{
7071
$this->assertEquals(array('a', 'b'), $this->propertyInfo->getProperties('Foo'));
7172
}
73+
74+
public function testCache()
75+
{
76+
$extractors = array(new NullExtractor(), new DummyExtractor());
77+
$this->propertyInfo = new PropertyInfoExtractor($extractors, $extractors, $extractors, $extractors, new ArrayCache());
78+
79+
$this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array()));
80+
$this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array()));
81+
}
7282
}

src/Symfony/Component/PropertyInfo/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
"require-dev": {
2929
"symfony/serializer": "~2.8|~3.0",
3030
"phpdocumentor/reflection": "^1.0.7",
31-
"doctrine/annotations": "~1.0"
31+
"doctrine/annotations": "~1.0",
32+
"doctrine/cache": "~1.0"
3233
},
3334
"conflict": {
3435
"phpdocumentor/reflection": "<1.0.7"
3536
},
3637
"suggest": {
38+
"doctrine/cache": "To cache results",
3739
"symfony/doctrine-bridge": "To use Doctrine metadata",
3840
"phpdocumentor/reflection": "To use the PHPDoc",
3941
"symfony/serializer": "To use Serializer metadata"

0 commit comments

Comments
 (0)