## Apache Zeta Components Manual :: File Source for regexp.php

## Source for file regexp.php

Documentation is available at regexp.php

1. <?php
2. /**
3. * File containing the ezcMvcRegexpRoute class
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
23. * @version //autogentag//
24. * @filesource
25. * @package MvcTools
26. */
28. /**
29. * Router class that uses regular expressions for matching routes.
30. *
31. * The routes are matched against the uri property of the request object.
32. *
33. * @package MvcTools
34. * @version //autogentag//
35. * @mainclass
36. */
37. class ezcMvcRegexpRoute implements ezcMvcRoute, ezcMvcReversibleRoute
38. {
39. /**
40. * This property contains the regular expression.
41. *
42. * @var string
43. */
44. protected $pattern ;
46. /**
47. * This is the name of the controller class that will be instantiated with the
48. * request variables obtained from the route, as well as the default values
49. * belonging to a route.
50. *
51. * @var string
52. */
53. protected $controllerClassName ;
55. /**
56. * Contains the action that the controller should execute.
57. *
58. * @var string
59. */
60. protected $action ;
62. /**
63. * The default values for the variables that are send to the controller. The route
64. * matchers can override those default values
65. *
66. * @var array(string)
67. */
68. protected $defaultValues ;
70. /**
71. * Constructs a new ezcMvcRegexpRoute with $pattern.
72. *
73. * When the route is matched (with the match() method), the route instantiates
74. * an object of the class $controllerClassName.
75. *
76. * @param string $pattern
77. * @param string $controllerClassName
78. * @param string $action
79. * @param array(string) $defaultValues
80. */
81. public function __construct ( $pattern, $controllerClassName, $action = null, array $defaultValues = array() )
82. {
83. $this-> pattern = $pattern;
84. $this-> controllerClassName = $controllerClassName;
85. $this-> action = $action;
86. $this-> defaultValues = $defaultValues;
87. }
89. /**
90. * Returns the request information that the matches() method will match the
91. * pattern against.
92. *
93. * @param ezcMvcRequest $request
94. * @return string
95. */
96. protected function getUriString ( ezcMvcRequest $request )
97. {
98. return $request->uri;
99. }
101. /**
102. * Evaluates the URI against this route.
103. *
104. * The method first runs the match. If the regular expression matches, it
105. * cleans up the variables to only include named parameters. it then
106. * creates an object containing routing information and returns it. If the
107. * route's pattern did not match it returns null.
108. *
109. * @param ezcMvcRequest $request
110. * @return null|ezcMvcRoutingInformation
111. */
112. public function matches ( ezcMvcRequest $request )
113. {
114. if ( $this-> pregMatch ( $request, $matches ) )
115. {
116. foreach ( $matches as $key => $match )
117. {
118. if ( is_numeric ( $key ) )
119. {
120. unset( $matches[$key] );
121. }
122. }
124. $request->variables = array_merge ( $this-> defaultValues , $request->variables, $matches );
126. return new ezcMvcRoutingInformation ( $this-> pattern , $this-> controllerClassName , $this-> action );
127. }
128. return null;
129. }
131. /**
132. * This method performs the actual regular expresion match against the
133. * $request's URI.
134. *
135. * @param ezcMvcRequest $request
136. * @param array(string) $matches
137. * @return bool
138. */
139. protected function pregMatch ( $request, &$matches )
140. {
141. return preg_match ( $this-> pattern , $this-> getUriString ( $request ), $matches );
142. }
144. /**
145. * Parses the pattern and adds the prefix.
146. *
147. * It's up to the developer to provide a meaningfull prefix. In this case,
148. * it needs to be a regular expression just like the pattern.
149. *
150. * @param mixed $prefix
151. * @throws ezcMvcRegexpRouteException if the prefix can not be prepended to
152. * the pattern.
153. */
154. public function prefix ( $prefix )
155. {
156. $pattern = $this-> pattern ;
157. // Find pattern delimiter
158. $patternDelim = $pattern[0];
159. // Obtain pattern modifiers
160. $patternModifier = substr ( strrchr ( $pattern, $patternDelim ), 1 );
161. // Find prefix delimiter
162. $prefixDelim = $prefix[0];
163. // Obtain prefix modifiers
164. $prefixModifier = substr ( strrchr ( $prefix, $prefixDelim ), 1 );
165. // If modifiers are not the same, throw exception
166. if ( $patternModifier !== $prefixModifier )
167. {
168. throw new ezcMvcRegexpRouteException ( "The pattern modifiers of the prefix '{$prefix}' and pattern '{$pattern}' do not match." );
169. }
170. // Reassemble the new pattern
171. $newPattern = $patternDelim;
172. $newPattern .= substr ( $prefix, 1, -1 - strlen ( $prefixModifier ) );
173. $newPattern .= substr ( $pattern, 1 + ( $pattern[1] == '^' ) );
175. $this-> pattern = $newPattern;
176. }
178. /**
179. * Generates an URL back out of a route, including possible arguments
180. *
181. * @param array $arguments
182. */
183. public function generateUrl ( array $arguments = null )
184. {
185. $url = $this->pattern;
186. // strip delimiters
187. $url = substr( $url, 1 );
188. $url = substr( $url, 0, -1 );
190. // strip ^
191. if ( substr( $url, 0, 1 ) == '^' )
192. {
193. $url = substr( $url, 1 );
194. }
196. // strip $
197. if ( substr ( $url, -1 ) == '$' )
198. {
199. $url = substr ( $url, 0, -1 );
200. }
202. // merge default values
203. if ( is_null ( $arguments ) )
204. {
205. $arguments = array( );
206. }
207. $arguments = array_merge ( $this-> defaultValues , $arguments );
209. $openPart = $openPartName = false;
210. $partName = $partOffset = $partLength = false;
211. $openParenthesis = 0;
213. for( $i = 0; isset( $url[$i] ); $i ++)
214. {
215. if ( $url[$i] == '(' )
216. {
217. $openParenthesis++;
218. }
219. elseif ( $url[$i] == ')' )
220. {
221. $openParenthesis--;
222. }
224. if ( substr ( $url, $i, 4 ) == '(?P<' )
225. {
226. $openPart = $openPartName = true;
227. $partOffset = $i;
228. $partLength = 4;
229. $i += 4;
230. }
232. if ( $openPart )
233. {
234. $partLength++;
236. if ( $url[$i] == ')' && $openParenthesis == 0 )
237. {
238. $url = str_replace (
239. substr ( $url, $partOffset, $partLength ),
240. $arguments[$partName],
241. $url
242. );
244. $i = $partOffset + strlen ( $arguments[$partName] );
246. $partName = '';
247. $openPart = false;
248. $partOffset = false;
249. $partLength = 0;
250. }
251. }
253. if ( $openPartName )
254. {
255. if ( $url[$i] == '>' )
256. {
257. $openPartName = false;
258. }
259. else
260. {
261. $partName.= $url[$i];
262. }
263. }
264. }
266. return $url;
267. }
268. }
269. ?>

Documentation generated by phpDocumentor 1.4.3