1 | <?php |
||
21 | class Indexable implements IndexableInterface |
||
22 | { |
||
23 | /** |
||
24 | * An array of raw configured callbacks for all types. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | private $callbacks = array(); |
||
29 | |||
30 | /** |
||
31 | * @var \Symfony\Component\DependencyInjection\ContainerInterface |
||
32 | */ |
||
33 | private $container; |
||
34 | |||
35 | /** |
||
36 | * An instance of ExpressionLanguage. |
||
37 | * |
||
38 | * @var ExpressionLanguage |
||
39 | */ |
||
40 | private $expressionLanguage; |
||
41 | |||
42 | /** |
||
43 | * An array of initialised callbacks. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | private $initialisedCallbacks = array(); |
||
48 | |||
49 | /** |
||
50 | * PropertyAccessor instance. |
||
51 | * |
||
52 | * @var PropertyAccessorInterface |
||
53 | */ |
||
54 | private $propertyAccessor; |
||
55 | |||
56 | /** |
||
57 | * @param array $callbacks |
||
58 | * @param ContainerInterface $container |
||
59 | */ |
||
60 | 21 | public function __construct(array $callbacks, ContainerInterface $container) |
|
61 | { |
||
62 | 21 | $this->callbacks = $callbacks; |
|
63 | 21 | $this->container = $container; |
|
64 | 21 | $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
|
65 | 21 | } |
|
66 | |||
67 | /** |
||
68 | * Return whether the object is indexable with respect to the callback. |
||
69 | * |
||
70 | * @param string $indexName |
||
71 | * @param string $typeName |
||
72 | * @param mixed $object |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | 17 | public function isObjectIndexable($indexName, $typeName, $object) |
|
95 | |||
96 | /** |
||
97 | * Builds and initialises a callback. |
||
98 | * |
||
99 | * @param string $type |
||
100 | * @param object $object |
||
101 | * |
||
102 | * @return mixed |
||
103 | */ |
||
104 | 17 | private function buildCallback($type, $object) |
|
126 | |||
127 | /** |
||
128 | * Processes a string expression into an Expression. |
||
129 | * |
||
130 | * @param string $type |
||
131 | * @param mixed $object |
||
132 | * @param string $callback |
||
133 | * |
||
134 | * @return Expression |
||
135 | */ |
||
136 | 8 | private function buildExpressionCallback($type, $object, $callback) |
|
137 | { |
||
138 | 8 | $expression = $this->getExpressionLanguage(); |
|
139 | 8 | if (!$expression) { |
|
140 | throw new \RuntimeException('Unable to process an expression without the ExpressionLanguage component.'); |
||
141 | } |
||
142 | |||
143 | try { |
||
144 | 8 | $callback = new Expression($callback); |
|
145 | 8 | $expression->compile($callback, array( |
|
146 | 8 | 'object', $this->getExpressionVar($object) |
|
147 | 8 | )); |
|
148 | |||
149 | 6 | return $callback; |
|
150 | 2 | } catch (SyntaxError $e) { |
|
151 | 2 | throw new \InvalidArgumentException(sprintf( |
|
152 | 2 | 'Callback for type "%s" is an invalid expression', |
|
153 | $type |
||
154 | 2 | ), $e->getCode(), $e); |
|
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Retreives a cached callback, or creates a new callback if one is not found. |
||
160 | * |
||
161 | * @param string $type |
||
162 | * @param object $object |
||
163 | * |
||
164 | * @return mixed |
||
165 | */ |
||
166 | 17 | private function getCallback($type, $object) |
|
167 | { |
||
168 | 17 | if (!array_key_exists($type, $this->initialisedCallbacks)) { |
|
169 | 17 | $this->initialisedCallbacks[$type] = $this->buildCallback($type, $object); |
|
170 | 12 | } |
|
171 | |||
172 | 12 | return $this->initialisedCallbacks[$type]; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Returns the ExpressionLanguage class if it is available. |
||
177 | * |
||
178 | * @return ExpressionLanguage|null |
||
179 | */ |
||
180 | 8 | private function getExpressionLanguage() |
|
181 | { |
||
182 | 8 | if (null === $this->expressionLanguage && class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) { |
|
183 | 8 | $this->expressionLanguage = new ExpressionLanguage(); |
|
184 | 8 | } |
|
185 | |||
186 | 8 | return $this->expressionLanguage; |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * Returns the variable name to be used to access the object when using the ExpressionLanguage |
||
191 | * component to parse and evaluate an expression. |
||
192 | * |
||
193 | * @param mixed $object |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | 8 | private function getExpressionVar($object = null) |
|
207 | |||
208 | /** |
||
209 | * Processes an array into a callback. Replaces the first element with a service if |
||
210 | * it begins with an @. |
||
211 | * |
||
212 | * @param string $type |
||
213 | * @param array $callback |
||
214 | * @return array |
||
215 | */ |
||
216 | 4 | private function processArrayToCallback($type, array $callback) |
|
240 | } |
||
241 |