|
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,29 @@ 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
|
46 | 58 | */
|
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) |
48 | 60 | {
|
49 | 61 | $this->listExtractors = $listExtractors;
|
50 | 62 | $this->typeExtractors = $typeExtractors;
|
51 | 63 | $this->descriptionExtractors = $descriptionExtractors;
|
52 | 64 | $this->accessExtractors = $accessExtractors;
|
| 65 | + $this->cache = $cache; |
53 | 66 | }
|
54 | 67 |
|
55 | 68 | /**
|
@@ -111,11 +124,27 @@ public function isWritable($class, $property, array $context = array())
|
111 | 124 | */
|
112 | 125 | private function extract(array $extractors, $method, array $arguments)
|
113 | 126 | {
|
| 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 | + |
114 | 137 | foreach ($extractors as $extractor) {
|
115 | 138 | $value = call_user_func_array(array($extractor, $method), $arguments);
|
116 | 139 | if (null !== $value) {
|
117 |
| - return $value; |
| 140 | + break; |
118 | 141 | }
|
119 | 142 | }
|
| 143 | + |
| 144 | + if ($this->cache) { |
| 145 | + $this->cache->save($key, $value); |
| 146 | + } |
| 147 | + |
| 148 | + return $this->arrayCache[$key] = $value; |
120 | 149 | }
|
121 | 150 | }
|
0 commit comments