Feature #7608 » fluid-shorthandsyntax_7608_v2.patch

Bastian Waidelich, 2012-11-26 17:44

View differences:

Classes/TYPO3/Fluid/Core/Parser/Configuration.php
25 25
	protected $interceptors = array();
26 26

  
27 27
	/**
28
	 * Set the opening symbol for inline ViewHelper and object accessor syntax
29
	 *
30
	 * @var string
31
	 */
32
	protected $openingShorthandSymbol = '{';
33

  
34
	/**
35
	 * Set the closing symbol for inline ViewHelper and object accessor syntax
36
	 *
37
	 * @var string
38
	 */
39
	protected $closingShorthandSymbol = '}';
40

  
41
	/**
28 42
	 * Adds an interceptor to apply to values coming from object accessors.
29 43
	 *
30 44
	 * @param \TYPO3\Fluid\Core\Parser\InterceptorInterface $interceptor
......
54 68
		return new \SplObjectStorage();
55 69
	}
56 70

  
71
	/**
72
	 * @param string $openingShorthandSymbol
73
	 */
74
	public function setOpeningShorthandSymbol($openingShorthandSymbol) {
75
		$this->openingShorthandSymbol = $openingShorthandSymbol;
76
	}
77

  
78
	/**
79
	 * @return string
80
	 */
81
	public function getOpeningShorthandSymbol() {
82
		return $this->openingShorthandSymbol;
83
	}
84

  
85
	/**
86
	 * @param string $closingShorthandSymbol
87
	 */
88
	public function setClosingShorthandSymbol($closingShorthandSymbol) {
89
		$this->closingShorthandSymbol = $closingShorthandSymbol;
90
	}
91

  
92
	/**
93
	 * @return string
94
	 */
95
	public function getClosingShorthandSymbol() {
96
		return $this->closingShorthandSymbol;
97
	}
98

  
57 99
}
58 100
?>
Classes/TYPO3/Fluid/Core/Parser/TemplateParser.php
112 112
	 */
113 113
	public static $SPLIT_PATTERN_SHORTHANDSYNTAX = '/
114 114
		(
115
			{                                # Start of shorthand syntax
115
			SHORTHAND_OPEN                   # Start of shorthand syntax
116 116
				(?:                          # Shorthand syntax is either composed of...
117 117
					[a-zA-Z0-9\->_:,.()]     # Various characters
118 118
					|"(?:\\\"|[^"])*"        # Double-quoted strings
......
120 120
					|(?R)                    # Other shorthand syntaxes inside, albeit not in a quoted string
121 121
					|\s+                     # Spaces
122 122
				)+
123
			}                                # End of shorthand syntax
123
			SHORTHAND_CLOSE                  # End of shorthand syntax
124 124
		)/x';
125 125

  
126 126
	/**
......
133 133
	 *
134 134
	 */
135 135
	public static $SCAN_PATTERN_SHORTHANDSYNTAX_OBJECTACCESSORS = '/
136
		^{                                                      # Start of shorthand syntax
137
			                                                # A shorthand syntax is either...
138
			(?P<Object>[a-zA-Z0-9\-_.]*)                                     # ... an object accessor
136
		^SHORTHAND_OPEN                                                                  # Start of shorthand syntax
137
			                                                                             # A shorthand syntax is either...
138
			(?P<Object>[a-zA-Z0-9\-_.]*)                                                 # ... an object accessor
139 139
			\s*(?P<Delimiter>(?:->)?)\s*
140 140

  
141
			(?P<ViewHelper>                                 # ... a ViewHelper
142
				[a-zA-Z0-9]+                                # Namespace prefix of ViewHelper (as in $SCAN_PATTERN_TEMPLATE_VIEWHELPERTAG)
141
			(?P<ViewHelper>                                                              # ... a ViewHelper
142
				[a-zA-Z0-9]+                                                             # Namespace prefix of ViewHelper (as in $SCAN_PATTERN_TEMPLATE_VIEWHELPERTAG)
143 143
				:
144
				[a-zA-Z0-9\\.]+                             # Method Identifier (as in $SCAN_PATTERN_TEMPLATE_VIEWHELPERTAG)
145
				\(                                          # Opening parameter brackets of ViewHelper
146
					(?P<ViewHelperArguments>                # Start submatch for ViewHelper arguments. This is taken from $SCAN_PATTERN_SHORTHANDSYNTAX_ARRAYS
144
				[a-zA-Z0-9\\.]+                                                          # Method Identifier (as in $SCAN_PATTERN_TEMPLATE_VIEWHELPERTAG)
145
				\(                                                                       # Opening parameter brackets of ViewHelper
146
					(?P<ViewHelperArguments>                                             # Start submatch for ViewHelper arguments. This is taken from $SCAN_PATTERN_SHORTHANDSYNTAX_ARRAYS
147 147
						(?:
148
							\s*[a-zA-Z0-9\-_]+                  # The keys of the array
149
							\s*:\s*                             # Key|Value delimiter :
150
							(?:                                 # Possible value options:
151
								"(?:\\\"|[^"])*"                # Double qouoted string
152
								|\'(?:\\\\\'|[^\'])*\'          # Single quoted string
153
								|[a-zA-Z0-9\-_.]+               # variable identifiers
154
								|{(?P>ViewHelperArguments)}     # Another sub-array
155
							)                                   # END possible value options
156
							\s*,?                               # There might be a , to seperate different parts of the array
157
						)*                                  # The above cycle is repeated for all array elements
158
					)                                       # End ViewHelper Arguments submatch
159
				\)                                          # Closing parameter brackets of ViewHelper
148
							\s*[a-zA-Z0-9\-_]+                                           # The keys of the array
149
							\s*:\s*                                                      # Key|Value delimiter :
150
							(?:                                                          # Possible value options:
151
								"(?:\\\"|[^"])*"                                         # Double qouoted string
152
								|\'(?:\\\\\'|[^\'])*\'                                   # Single quoted string
153
								|[a-zA-Z0-9\-_.]+                                        # variable identifiers
154
								|SHORTHAND_OPEN(?P>ViewHelperArguments)SHORTHAND_CLOSE$  # Another sub-array
155
							)                                                            # END possible value options
156
							\s*,?                                                        # There might be a , to seperate different parts of the array
157
						)*                                                               # The above cycle is repeated for all array elements
158
					)                                                                    # End ViewHelper Arguments submatch
159
				\)                                                                       # Closing parameter brackets of ViewHelper
160 160
			)?
161
			(?P<AdditionalViewHelpers>                      # There can be more than one ViewHelper chained, by adding more -> and the ViewHelper (recursively)
161
			(?P<AdditionalViewHelpers>                                                   # There can be more than one ViewHelper chained, by adding more -> and the ViewHelper (recursively)
162 162
				(?:
163 163
					\s*->\s*
164 164
					(?P>ViewHelper)
165 165
				)*
166 166
			)
167
		}$/x';
167
		SHORTHAND_CLOSE$/x';
168 168

  
169 169
	/**
170 170
	 * THIS IS ALMOST THE SAME AS $SCAN_PATTERN_SHORTHANDSYNTAX_OBJECTACCESSORS
......
178 178
		\(                                          # Opening parameter brackets of ViewHelper
179 179
			(?P<ViewHelperArguments>                # Start submatch for ViewHelper arguments. This is taken from $SCAN_PATTERN_SHORTHANDSYNTAX_ARRAYS
180 180
				(?:
181
					\s*[a-zA-Z0-9\-_]+                  # The keys of the array
182
					\s*:\s*                             # Key|Value delimiter :
183
					(?:                                 # Possible value options:
184
						"(?:\\\"|[^"])*"                # Double qouoted string
185
						|\'(?:\\\\\'|[^\'])*\'          # Single quoted string
186
						|[a-zA-Z0-9\-_.]+               # variable identifiers
187
						|{(?P>ViewHelperArguments)}     # Another sub-array
188
					)                                   # END possible value options
189
					\s*,?                               # There might be a , to seperate different parts of the array
190
				)*                                  # The above cycle is repeated for all array elements
191
			)                                       # End ViewHelper Arguments submatch
192
		\)                                          # Closing parameter brackets of ViewHelper
181
					\s*[a-zA-Z0-9\-_]+                                          # The keys of the array
182
					\s*:\s*                                                     # Key|Value delimiter :
183
					(?:                                                         # Possible value options:
184
						"(?:\\\"|[^"])*"                                        # Double qouoted string
185
						|\'(?:\\\\\'|[^\'])*\'                                  # Single quoted string
186
						|[a-zA-Z0-9\-_.]+                                       # variable identifiers
187
						|SHORTHAND_OPEN(?P>ViewHelperArguments)SHORTHAND_CLOSE  # Another sub-array
188
					)                                                           # END possible value options
189
					\s*,?                                                       # There might be a , to seperate different parts of the array
190
				)*                                                              # The above cycle is repeated for all array elements
191
			)                                                                   # End ViewHelper Arguments submatch
192
		\)                                                                      # Closing parameter brackets of ViewHelper
193 193
		/x';
194 194

  
195 195
	/**
......
202 202
	 */
203 203
	public static $SCAN_PATTERN_SHORTHANDSYNTAX_ARRAYS = '/^
204 204
		(?P<Recursion>                                  # Start the recursive part of the regular expression - describing the array syntax
205
			{                                           # Each array needs to start with {
205
			SHORTHAND_OPEN                              # Each array needs to start with opening shorthand symbol
206 206
				(?P<Array>                              # Start submatch
207 207
					(?:
208 208
						\s*[a-zA-Z0-9\-_]+              # The keys of the array
......
216 216
						\s*,?                           # There might be a , to seperate different parts of the array
217 217
					)*                                  # The above cycle is repeated for all array elements
218 218
				)                                       # End array submatch
219
			}                                           # Each array ends with }
219
			SHORTHAND_CLOSE                             # Each array ends with closing shorthand symbol
220 220
		)$/x';
221 221

  
222 222
	/**
......
225 225
	 *
226 226
	 */
227 227
	public static $SPLIT_PATTERN_SHORTHANDSYNTAX_ARRAY_PARTS = '/
228
		(?P<ArrayPart>                                             # Start submatch
229
			(?P<Key>[a-zA-Z0-9\-_]+)                               # The keys of the array
230
			\s*:\s*                                                   # Key|Value delimiter :
231
			(?:                                                       # Possible value options:
232
				(?P<QuotedString>                                     # Quoted string
228
		(?P<ArrayPart>                                                                         # Start submatch
229
			(?P<Key>[a-zA-Z0-9\-_]+)                                                           # The keys of the array
230
			\s*:\s*                                                                            # Key|Value delimiter :
231
			(?:                                                                                # Possible value options:
232
				(?P<QuotedString>                                                              # Quoted string
233 233
					(?:"(?:\\\"|[^"])*")
234 234
					|(?:\'(?:\\\\\'|[^\'])*\')
235 235
				)
236
				|(?P<VariableIdentifier>[a-zA-Z][a-zA-Z0-9\-_.]*)    # variable identifiers have to start with a letter
237
				|(?P<Number>[0-9.]+)                                  # Number
238
				|{\s*(?P<Subarray>(?:(?P>ArrayPart)\s*,?\s*)+)\s*}              # Another sub-array
239
			)                                                         # END possible value options
240
		)                                                          # End array part submatch
236
				|(?P<VariableIdentifier>[a-zA-Z][a-zA-Z0-9\-_.]*)                              # variable identifiers have to start with a letter
237
				|(?P<Number>[0-9.]+)                                                           # Number
238
				|SHORTHAND_OPEN\s*(?P<Subarray>(?:(?P>ArrayPart)\s*,?\s*)+)\s*SHORTHAND_CLOSE  # Another sub-array
239
			)                                                                                  # END possible value options
240
		)                                                                                      # End array part submatch
241 241
	/x';
242 242

  
243 243
	/**
......
308 308
	}
309 309

  
310 310
	/**
311
	 * @return \TYPO3\Fluid\Core\Parser\Configuration
312
	 */
313
	public function getConfiguration() {
314
		return $this->configuration;
315
	}
316

  
317
	/**
311 318
	 * Parses a given template string and returns a parsed template object.
312 319
	 *
313 320
	 * The resulting ParsedTemplate can then be rendered by calling evaluate() on it.
......
320 327
	 * @throws Exception
321 328
	 */
322 329
	public function parse($templateString) {
323
		if (!is_string($templateString)) throw new \TYPO3\Fluid\Core\Parser\Exception('Parse requires a template string as argument, ' . gettype($templateString) . ' given.', 1224237899);
330
		if (!is_string($templateString)) {
331
			throw new \TYPO3\Fluid\Core\Parser\Exception(sprintf('Parse requires a template string as argument, %s given.', gettype($templateString)), 1224237899);
332
		}
324 333

  
325 334
		$this->reset();
326 335

  
......
632 641

  
633 642
			// ViewHelpers
634 643
		$matches = array();
635
		if (strlen($viewHelperString) > 0 && preg_match_all(self::$SPLIT_PATTERN_SHORTHANDSYNTAX_VIEWHELPER, $viewHelperString, $matches, PREG_SET_ORDER) > 0) {
644
		if (strlen($viewHelperString) > 0 && preg_match_all($this->prepareTemplateRegularExpression(self::$SPLIT_PATTERN_SHORTHANDSYNTAX_VIEWHELPER), $viewHelperString, $matches, PREG_SET_ORDER) > 0) {
636 645
				// The last ViewHelper has to be added first for correct chaining.
637 646
			foreach (array_reverse($matches) as $singleMatch) {
638 647
				if (strlen($singleMatch['ViewHelperArguments']) > 0) {
......
772 781

  
773 782
	/**
774 783
	 * Takes a regular expression template and replaces "NAMESPACE" with the
775
	 * currently registered namespace identifiers. Returns a regular expression
776
	 * which is ready to use.
784
	 * currently registered namespace identifiers and "SHORTHAND_OPEN"/"SHORTHAND_CLOSE"
785
	 * with the configured symbols. @see \TYPO3\Fluid\Core\Parser\Configuration::setOpeningShorthandSymbol()
786
	 * Returns a regular expression which is ready to use.
777 787
	 *
778 788
	 * @param string $regularExpression Regular expression template
779 789
	 * @return string Regular expression ready to be used
780 790
	 */
781 791
	protected function prepareTemplateRegularExpression($regularExpression) {
782
		return str_replace('NAMESPACE', implode('|', array_keys($this->namespaces)), $regularExpression);
792
		$preparedRegularExpression = str_replace('NAMESPACE', implode('|', array_keys($this->namespaces)), $regularExpression);
793
		if ($this->configuration !== NULL) {
794
			$openingShorthandSymbol = preg_quote($this->configuration->getOpeningShorthandSymbol(), '#');
795
			$closingShorthandSymbol = preg_quote($this->configuration->getClosingShorthandSymbol(), '#');
796
		} else {
797
			$openingShorthandSymbol = '{';
798
			$closingShorthandSymbol = '}';
799
		}
800
		$preparedRegularExpression = str_replace('SHORTHAND_OPEN', $openingShorthandSymbol, $preparedRegularExpression);
801
		$preparedRegularExpression = str_replace('SHORTHAND_CLOSE', $closingShorthandSymbol, $preparedRegularExpression);
802
		return $preparedRegularExpression;
783 803
	}
784 804

  
785 805
	/**
......
796 816

  
797 817
		foreach ($sections as $section) {
798 818
			$matchedVariables = array();
799
			if (preg_match(self::$SCAN_PATTERN_SHORTHANDSYNTAX_OBJECTACCESSORS, $section, $matchedVariables) > 0) {
819
			if (preg_match($this->prepareTemplateRegularExpression(self::$SCAN_PATTERN_SHORTHANDSYNTAX_OBJECTACCESSORS), $section, $matchedVariables) > 0) {
800 820
				$this->objectAccessorHandler($state, $matchedVariables['Object'], $matchedVariables['Delimiter'], (isset($matchedVariables['ViewHelper'])?$matchedVariables['ViewHelper']:''), (isset($matchedVariables['AdditionalViewHelpers'])?$matchedVariables['AdditionalViewHelpers']:''));
801
			} elseif (preg_match(self::$SCAN_PATTERN_SHORTHANDSYNTAX_ARRAYS, $section, $matchedVariables) > 0) {
821
			} elseif (preg_match($this->prepareTemplateRegularExpression(self::$SCAN_PATTERN_SHORTHANDSYNTAX_ARRAYS), $section, $matchedVariables) > 0) {
802 822
				$this->arrayHandler($state, $matchedVariables['Array']);
803 823
			} else {
804 824
				$this->textHandler($state, $section);
......
836 856
	 */
837 857
	protected function recursiveArrayHandler($arrayText) {
838 858
		$matches = array();
839
		if (preg_match_all(self::$SPLIT_PATTERN_SHORTHANDSYNTAX_ARRAY_PARTS, $arrayText, $matches, PREG_SET_ORDER) > 0) {
859
		if (preg_match_all($this->prepareTemplateRegularExpression(self::$SPLIT_PATTERN_SHORTHANDSYNTAX_ARRAY_PARTS), $arrayText, $matches, PREG_SET_ORDER) > 0) {
840 860
			$arrayToBuild = array();
841 861
			foreach ($matches as $singleMatch) {
842 862
				$arrayKey = $singleMatch['Key'];
843
- 
(2-2/2)