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

## Source for file configurable.php

Documentation is available at configurable.php

1. <?php
2. /**
3. * File containing the ezcMvcConfigurableDispatcher 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. * This class implements an example dispatcher that can be configured through
30. * ezcMvcDispatcherConfiguration.
31. *
32. * @package MvcTools
33. * @version //autogentag//
34. * @mainclass
35. */
36. class ezcMvcConfigurableDispatcher implements ezcMvcDispatcher
37. {
38. /**
39. * Contains the configuration that determines which request parser, router,
40. * view handler and response writer are used.
41. *
42. * @var ezcMvcDispatcherConfiguration
43. */
44. protected $configuration ;
46. /**
47. * Creates a new ezcMvcConfigurableDispatcher
48. *
49. * @param ezcMvcDispatcherConfiguration $configuration
50. */
51. public function __construct ( ezcMvcDispatcherConfiguration $configuration )
52. {
53. $this-> configuration = $configuration;
54. }
56. /**
57. * Creates the controller by using the routing information and request data.
58. *
59. * @param ezcMvcRoutingInformation $routingInformation
60. * @param ezcMvcRequest $request
61. * @return ezcMvcController
62. */
63. protected function createController ( ezcMvcRoutingInformation $routingInformation, ezcMvcRequest $request )
64. {
65. $controllerClass = $routingInformation->controllerClass;
66. $controller = new $controllerClass( $routingInformation->action, $request );
67. return $controller;
68. }
70. /**
71. * Checks whether the number of redirects does not exceed the limit, and
72. * increases the $redirects count.
73. *
74. * @throws ezcMvcInfiniteLoopException when the number of redirects exceeds
75. * the limit (25 by default).
76. * @param int $redirects
77. */
78. protected function checkRedirectLimit ( &$redirects )
79. {
80. $redirects++;
81. if ( $redirects >= 25 )
82. {
83. throw new ezcMvcInfiniteLoopException ( $redirects );
84. }
85. }
87. /**
88. * Uses the configuration to fetch the request parser
89. *
90. * @throws ezcMvcInvalidConfiguration when the returned object is of the wrong class
91. *
92. * @return ezcMvcRequestParser
93. */
94. protected function getRequestParser ()
95. {
96. // create the request parser
97. $requestParser = $this-> configuration -> createRequestParser ();
98. if ( ezcBase :: inDevMode () && !$requestParser instanceof ezcMvcRequestParser )
99. {
100. throw new ezcMvcInvalidConfiguration ( 'requestParser', $requestParser, 'instance of ezcMvcRequestParser' );
101. }
102. return $requestParser;
103. }
105. /**
106. * Uses the configuration to fetch the router
107. *
108. * @throws ezcMvcInvalidConfiguration when the returned object is of the wrong class
109. *
110. * @param ezcMvcRequest $request
111. * @return ezcMvcRouter
112. */
113. protected function getRouter ( ezcMvcRequest $request )
114. {
115. $router = $this-> configuration -> createRouter ( $request );
116. if ( ezcBase :: inDevMode () && !$router instanceof ezcMvcRouter )
117. {
118. throw new ezcMvcInvalidConfiguration ( 'router', $router, 'instance of ezcMvcRouter' );
119. }
120. return $router;
121. }
123. /**
124. * Uses the router (through createController()) to fetch the controller
125. *
126. * @throws ezcMvcInvalidConfiguration when the returned object is of the wrong class
127. *
128. * @param ezcMvcRoutingInformation $routingInformation
129. * @param ezcMvcRequest $request
130. * @return ezcMvcController
131. */
132. protected function getController ( ezcMvcRoutingInformation $routingInformation, ezcMvcRequest $request )
133. {
134. $controller = $this-> createController ( $routingInformation, $request );
135. if ( ezcBase :: inDevMode () && !$controller instanceof ezcMvcController )
136. {
137. throw new ezcMvcInvalidConfiguration ( 'controller', $controller, 'instance of ezcMvcController' );
138. }
139. $controller->setRouter( $routingInformation->router );
140. return $controller;
141. }
143. /**
144. * Uses the configuration to fetch the view handler
145. *
146. * @throws ezcMvcInvalidConfiguration when the returned object is of the wrong class
147. *
148. * @param ezcMvcRoutingInformation $routingInformation
149. * @param ezcMvcRequest $request
150. * @param ezcMvcResult $result
151. * @return ezcMvcView
152. */
153. protected function getView ( ezcMvcRoutingInformation $routingInformation, ezcMvcRequest $request, ezcMvcResult $result )
154. {
155. $view = $this-> configuration -> createView ( $routingInformation, $request, $result );
156. if ( ezcBase :: inDevMode () && !$view instanceof ezcMvcView )
157. {
158. throw new ezcMvcInvalidConfiguration ( 'view', $view, 'instance of ezcMvcView' );
159. }
160. return $view;
161. }
163. /**
164. * Uses the configuration to fetch a fatal redirect request object
165. *
166. * @throws ezcMvcInvalidConfiguration when the returned object is of the wrong class
167. *
168. * @param ezcMvcRequest $request
169. * @param ezcMvcResult $result
170. * @param Exception $e
171. * @return ezcMvcRequest
172. */
173. protected function getFatalRedirectRequest ( ezcMvcRequest $request, ezcMvcResult $result, Exception $e )
174. {
175. if ( $request->isFatal )
176. {
177. throw new ezcMvcFatalErrorLoopException ( $request );
178. }
179. $request = $this-> configuration -> createFatalRedirectRequest ( $request, new ezcMvcResult , $e );
180. $request->isFatal = true;
181. if ( ezcBase :: inDevMode () && !$request instanceof ezcMvcRequest )
182. {
183. throw new ezcMvcInvalidConfiguration ( 'request', $request, 'instance of ezcMvcRequest' );
184. }
185. return $request;
186. }
188. /**
189. * Uses the configuration to fetch the response writer
190. *
191. * @throws ezcMvcInvalidConfiguration when the returned object is of the wrong class
192. *
193. * @param ezcMvcRoutingInformation $routingInformation
194. * @param ezcMvcRequest $request
195. * @param ezcMvcResult $result
196. * @param ezcMvcResponse $response
197. * @return ezcMvcResponseWriter
198. */
199. protected function getResponseWriter ( ezcMvcRoutingInformation $routingInformation, ezcMvcRequest $request, ezcMvcResult $result, ezcMvcResponse $response )
200. {
201. $responseWriter = $this-> configuration -> createResponseWriter ( $routingInformation, $request, $result, $response );
202. if ( ezcBase :: inDevMode () && !$responseWriter instanceof ezcMvcResponseWriter )
203. {
204. throw new ezcMvcInvalidConfiguration ( 'responseWriter', $responseWriter, 'instance of ezcMvcResponseWriter' );
205. }
206. return $responseWriter;
207. }
209. /**
210. * Runs through the request, by using the configuration to obtain correct handlers.
211. */
212. public function run ()
213. {
214. // initialize infinite loop counter
215. $redirects = 0;
217. // create the request
218. $requestParser = $this-> getRequestParser ();
219. $request = $requestParser->createRequest();
221. // start of the request loop
222. do
223. {
224. // do the infinite loop check
225. $this-> checkRedirectLimit ( $redirects );
226. $continue = false;
228. // run pre-routing filters
229. $this-> configuration -> runPreRoutingFilters ( $request );
231. // create the router from the configuration
232. $router = $this-> getRouter ( $request );
234. // router creates routing information
235. try
236. {
237. $routingInformation = $router->getRoutingInformation();
238. }
239. catch ( ezcMvcRouteNotFoundException $e )
240. {
241. $request = $this-> getFatalRedirectRequest ( $request, new ezcMvcResult , $e );
242. $continue = true;
243. continue;
244. }
246. // run request filters
247. $filterResult = $this-> configuration -> runRequestFilters ( $routingInformation, $request );
249. if ( $filterResult instanceof ezcMvcInternalRedirect )
250. {
251. $request = $filterResult->request;
252. $continue = true;
253. continue;
254. }
256. // create the controller
257. $controller = $this-> getController ( $routingInformation, $request );
259. // run the controller
260. try
261. {
262. $result = $controller->createResult();
263. }
264. catch ( Exception $e )
265. {
266. $request = $this-> getFatalRedirectRequest ( $request, new ezcMvcResult , $e );
267. $continue = true;
268. continue;
269. }
271. if ( $result instanceof ezcMvcInternalRedirect )
272. {
273. $request = $result->request;
274. $continue = true;
275. continue;
276. }
277. if ( !$result instanceof ezcMvcResult )
278. {
279. throw new ezcMvcControllerException ( "The action '{$routingInformation->action}' of controller '{$routingInformation

->controllerClass}' did not return an ezcMvcResult object." );
- }
- $this-> configuration -> runResultFilters ( $routingInformation, $request, $result );
- if ( $result->status !== 0 )
- {
- $response = new ezcMvcResponse ;
- $response->status = $result->status;
- }
- else
- {
- // want the view manager to use my filters
- $view = $this-> getView ( $routingInformation, $request, $result );
- // create the response
- try
- {
- $response = $view->createResponse();
- }
- catch ( Exception $e )
- {
- $request = $this-> getFatalRedirectRequest ( $request, $result, $e );
- $continue = true;
- continue;
- }
- }
- $this-> configuration -> runResponseFilters ( $routingInformation, $request, $result, $response );
- // create the response writer
- $responseWriter = $this-> getResponseWriter ( $routingInformation, $request, $result, $response );
- // handle the response
- $responseWriter->handleResponse();
- }
- while ( $continue );
- }
- }
- ?>
Documentation generated by phpDocumentor 1.4.3