[TASK] Sync CMS Fluid with Flow Fluid 1.1 (part1)
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / Parser / Interceptor / Escape.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\Parser\Interceptor;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 /**
15 * An interceptor adding the escape viewhelper to the suitable places.
16 */
17 class Escape implements \TYPO3\CMS\Fluid\Core\Parser\InterceptorInterface {
18
19 /**
20 * Is the interceptor enabled right now?
21 *
22 * @var boolean
23 */
24 protected $interceptorEnabled = TRUE;
25
26 /**
27 * A stack of ViewHelperNodes which currently disable the interceptor.
28 * Needed to enable the interceptor again.
29 *
30 * @var array<\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\NodeInterface>
31 */
32 protected $viewHelperNodesWhichDisableTheInterceptor = array();
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
36 */
37 protected $objectManager;
38
39 /**
40 * Inject object manager
41 *
42 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
43 * @return void
44 */
45 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
46 $this->objectManager = $objectManager;
47 }
48
49 /**
50 * Adds a ViewHelper node using the Format\HtmlspecialcharsViewHelper to the given node.
51 * If "escapingInterceptorEnabled" in the ViewHelper is FALSE, will disable itself inside the ViewHelpers body.
52 *
53 * @param \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\NodeInterface $node
54 * @param integer $interceptorPosition One of the INTERCEPT_* constants for the current interception point
55 * @param \TYPO3\CMS\Fluid\Core\Parser\ParsingState $parsingState the current parsing state. Not needed in this interceptor.
56 * @return \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\NodeInterface
57 */
58 public function process(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\NodeInterface $node, $interceptorPosition, \TYPO3\CMS\Fluid\Core\Parser\ParsingState $parsingState) {
59 if ($interceptorPosition === \TYPO3\CMS\Fluid\Core\Parser\InterceptorInterface::INTERCEPT_OPENING_VIEWHELPER) {
60 if (!$node->getUninitializedViewHelper()->isEscapingInterceptorEnabled()) {
61 $this->interceptorEnabled = FALSE;
62 $this->viewHelperNodesWhichDisableTheInterceptor[] = $node;
63 }
64 } elseif ($interceptorPosition === \TYPO3\CMS\Fluid\Core\Parser\InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER) {
65 if (end($this->viewHelperNodesWhichDisableTheInterceptor) === $node) {
66 array_pop($this->viewHelperNodesWhichDisableTheInterceptor);
67 if (count($this->viewHelperNodesWhichDisableTheInterceptor) === 0) {
68 $this->interceptorEnabled = TRUE;
69 }
70 }
71 } elseif ($this->interceptorEnabled && $node instanceof \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ObjectAccessorNode) {
72 $escapeViewHelper = $this->objectManager->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\HtmlspecialcharsViewHelper');
73 $node = $this->objectManager->get(
74 'TYPO3\\CMS\\Fluid\\Core\\Parser\\SyntaxTree\\ViewHelperNode',
75 $escapeViewHelper,
76 array('value' => $node)
77 );
78 }
79 return $node;
80 }
81
82 /**
83 * This interceptor wants to hook into object accessor creation, and opening / closing ViewHelpers.
84 *
85 * @return array Array of INTERCEPT_* constants
86 */
87 public function getInterceptionPoints() {
88 return array(
89 \TYPO3\CMS\Fluid\Core\Parser\InterceptorInterface::INTERCEPT_OPENING_VIEWHELPER,
90 \TYPO3\CMS\Fluid\Core\Parser\InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER,
91 \TYPO3\CMS\Fluid\Core\Parser\InterceptorInterface::INTERCEPT_OBJECTACCESSOR
92 );
93 }
94 }
95
96 ?>