|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\PropertyInfo; |
13 | 13 |
|
| 14 | +use Doctrine\Common\Cache\Cache; |
| 15 | + |
14 | 16 | /** |
15 | 17 | * Default {@see PropertyInfoExtractorInterface} implementation. |
16 | 18 | * |
@@ -38,18 +40,30 @@ class PropertyInfoExtractor implements PropertyInfoExtractorInterface |
38 | 40 | */ |
39 | 41 | private $accessExtractors; |
40 | 42 |
|
| 43 | + /** |
| 44 | + * @var Cache |
| 45 | + */ |
| 46 | + private $cache; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var array |
| 50 | + */ |
| 51 | + private $arrayCache = array(); |
| 52 | + |
41 | 53 | /** |
42 | 54 | * @param PropertyListExtractorInterface[] $listExtractors |
43 | 55 | * @param PropertyTypeExtractorInterface[] $typeExtractors |
44 | 56 | * @param PropertyDescriptionExtractorInterface[] $descriptionExtractors |
45 | 57 | * @param PropertyAccessExtractorInterface[] $accessExtractors |
| 58 | + * @param Cache|null $cache |
46 | 59 | */ |
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) |
48 | 61 | { |
49 | 62 | $this->listExtractors = $listExtractors; |
50 | 63 | $this->typeExtractors = $typeExtractors; |
51 | 64 | $this->descriptionExtractors = $descriptionExtractors; |
52 | 65 | $this->accessExtractors = $accessExtractors; |
| 66 | + $this->cache = $cache; |
53 | 67 | } |
54 | 68 |
|
55 | 69 | /** |
@@ -111,11 +125,27 @@ public function isWritable($class, $property, array $context = array()) |
111 | 125 | */ |
112 | 126 | private function extract(array $extractors, $method, array $arguments) |
113 | 127 | { |
| 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 | + |
114 | 138 | foreach ($extractors as $extractor) { |
115 | 139 | $value = call_user_func_array(array($extractor, $method), $arguments); |
116 | 140 | if (null !== $value) { |
117 | | - return $value; |
| 141 | + break; |
118 | 142 | } |
119 | 143 | } |
| 144 | + |
| 145 | + if ($this->cache) { |
| 146 | + $this->cache->save($key, $value); |
| 147 | + } |
| 148 | + |
| 149 | + return $this->arrayCache[$key] = $value; |
120 | 150 | } |
121 | 151 | } |
0 commit comments