Skip to content

Commit 3d7efb2

Browse files
committed
[PropertInfo] Cache support
1 parent c3b60a9 commit 3d7efb2

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/Symfony/Component/PropertyInfo/PropertyInfoExtractor.php

Lines changed: 32 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,30 @@ 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
58+
* @param Cache|null $cache
4659
*/
47-
public function __construct(array $listExtractors = array(), array $typeExtractors = array(), array $descriptionExtractors = array(), array $accessExtractors = array())
60+
public function __construct(array $listExtractors = array(), array $typeExtractors = array(), array $descriptionExtractors = array(), array $accessExtractors = array(), Cache $cache = null)
4861
{
4962
$this->listExtractors = $listExtractors;
5063
$this->typeExtractors = $typeExtractors;
5164
$this->descriptionExtractors = $descriptionExtractors;
5265
$this->accessExtractors = $accessExtractors;
66+
$this->cache = $cache;
5367
}
5468

5569
/**
@@ -111,11 +125,27 @@ public function isWritable($class, $property, array $context = array())
111125
*/
112126
private function extract(array $extractors, $method, array $arguments)
113127
{
128+
$key = $method.serialize($arguments);
129+
130+
if (isset($this->arrayCache[$key])) {
131+
return $this->arrayCache[$key];
132+
}
133+
134+
if ($this->cache && $value = $this->cache->fetch($key)) {
135+
return $this->arrayCache[$key] = $value;
136+
}
137+
114138
foreach ($extractors as $extractor) {
115139
$value = call_user_func_array(array($extractor, $method), $arguments);
116140
if (null !== $value) {
117-
return $value;
141+
break;
118142
}
119143
}
144+
145+
if ($this->cache) {
146+
$this->cache->save($key, $value);
147+
}
148+
149+
return $this->arrayCache[$key] = $value;
120150
}
121151
}

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy