1 | <?php |
||
20 | class TemplateCompiler |
||
21 | { |
||
22 | |||
23 | const SHOULD_GENERATE_VIEWHELPER_INVOCATION = '##should_gen_viewhelper##'; |
||
24 | const MODE_NORMAL = 'normal'; |
||
25 | const MODE_WARMUP = 'warmup'; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $syntaxTreeInstanceCache = []; |
||
31 | |||
32 | /** |
||
33 | * @var NodeConverter |
||
34 | */ |
||
35 | protected $nodeConverter; |
||
36 | |||
37 | /** |
||
38 | * @var RenderingContextInterface |
||
39 | */ |
||
40 | protected $renderingContext; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $mode = self::MODE_NORMAL; |
||
46 | |||
47 | /** |
||
48 | * @var ParsedTemplateInterface |
||
49 | */ |
||
50 | protected $currentlyProcessingState; |
||
51 | |||
52 | /** |
||
53 | * Constructor |
||
54 | */ |
||
55 | public function __construct() |
||
59 | |||
60 | /** |
||
61 | * Instruct the TemplateCompiler to enter warmup mode, assigning |
||
62 | * additional context allowing cache-related implementations to |
||
63 | * subsequently check the mode. |
||
64 | * |
||
65 | * Cannot be reversed once done - should only be used from within |
||
66 | * FluidCacheWarmerInterface implementations! |
||
67 | */ |
||
68 | public function enterWarmupMode() |
||
72 | |||
73 | /** |
||
74 | * Returns TRUE only if the TemplateCompiler is in warmup mode. |
||
75 | */ |
||
76 | public function isWarmupMode() |
||
80 | |||
81 | /** |
||
82 | * @return ParsedTemplateInterface|NULL |
||
83 | */ |
||
84 | public function getCurrentlyProcessingState() |
||
88 | |||
89 | /** |
||
90 | * @param RenderingContextInterface $renderingContext |
||
91 | * @return void |
||
92 | */ |
||
93 | public function setRenderingContext(RenderingContextInterface $renderingContext) |
||
97 | |||
98 | /** |
||
99 | * @return RenderingContextInterface |
||
100 | */ |
||
101 | public function getRenderingContext() |
||
105 | |||
106 | /** |
||
107 | * @param NodeConverter $nodeConverter |
||
108 | * @return void |
||
109 | */ |
||
110 | public function setNodeConverter(NodeConverter $nodeConverter) |
||
114 | |||
115 | /** |
||
116 | * @return NodeConverter |
||
117 | */ |
||
118 | public function getNodeConverter() |
||
122 | |||
123 | /** |
||
124 | * @return void |
||
125 | */ |
||
126 | public function disable() |
||
130 | |||
131 | /** |
||
132 | * @return boolean |
||
133 | */ |
||
134 | public function isDisabled() |
||
138 | |||
139 | /** |
||
140 | * @param string $identifier |
||
141 | * @return boolean |
||
142 | */ |
||
143 | public function has($identifier) |
||
155 | |||
156 | /** |
||
157 | * @param string $identifier |
||
158 | * @return ParsedTemplateInterface |
||
159 | */ |
||
160 | public function get($identifier) |
||
174 | |||
175 | /** |
||
176 | * Resets the currently processing state |
||
177 | * |
||
178 | * @return void |
||
179 | */ |
||
180 | public function reset() |
||
184 | |||
185 | /** |
||
186 | * @param string $identifier |
||
187 | * @param ParsingState $parsingState |
||
188 | * @return void |
||
189 | */ |
||
190 | public function store($identifier, ParsingState $parsingState) |
||
247 | |||
248 | /** |
||
249 | * @param RootNode|string $storedLayoutNameArgument |
||
250 | * @return string |
||
251 | */ |
||
252 | protected function generateCodeForLayoutName($storedLayoutNameArgument) |
||
261 | |||
262 | /** |
||
263 | * @param ParsingState $parsingState |
||
264 | * @return string |
||
265 | */ |
||
266 | protected function generateSectionCodeFromParsingState(ParsingState $parsingState) |
||
281 | |||
282 | /** |
||
283 | * Replaces special characters by underscores |
||
284 | * @see http://www.php.net/manual/en/language.variables.basics.php |
||
285 | * |
||
286 | * @param string $identifier |
||
287 | * @return string the sanitized identifier |
||
288 | */ |
||
289 | protected function sanitizeIdentifier($identifier) |
||
293 | |||
294 | /** |
||
295 | * @param array $converted |
||
296 | * @param string $expectedFunctionName |
||
297 | * @param string $comment |
||
298 | * @return string |
||
299 | */ |
||
300 | protected function generateCodeForSection(array $converted, $expectedFunctionName, $comment) |
||
315 | |||
316 | /** |
||
317 | * Returns a unique variable name by appending a global index to the given prefix |
||
318 | * |
||
319 | * @param string $prefix |
||
320 | * @return string |
||
321 | */ |
||
322 | public function variableName($prefix) |
||
326 | |||
327 | /** |
||
328 | * @param NodeInterface $node |
||
329 | * @return string |
||
330 | */ |
||
331 | public function wrapChildNodesInClosure(NodeInterface $node) |
||
341 | |||
342 | /** |
||
343 | * Wraps one ViewHelper argument evaluation in a closure that can be |
||
344 | * rendered by passing a rendering context. |
||
345 | * |
||
346 | * @param ViewHelperNode $node |
||
347 | * @param string $argumentName |
||
348 | * @return string |
||
349 | */ |
||
350 | public function wrapViewHelperNodeArgumentEvaluationInClosure(ViewHelperNode $node, $argumentName) |
||
369 | } |
||
370 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: