Pluf Framework

Pluf Framework Git Source Tree

Root/src/Pluf/Dispatcher.php

1<?php
2/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3/*
4# ***** BEGIN LICENSE BLOCK *****
5# This file is part of Plume Framework, a simple PHP Application Framework.
6# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
7#
8# Plume Framework is free software; you can redistribute it and/or modify
9# it under the terms of the GNU Lesser General Public License as published by
10# the Free Software Foundation; either version 2.1 of the License, or
11# (at your option) any later version.
12#
13# Plume Framework is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU Lesser General Public License for more details.
17#
18# You should have received a copy of the GNU Lesser General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21#
22# ***** END LICENSE BLOCK ***** */
23
24class Pluf_Dispatcher
25{
26 /**
27 * The unique method to call.
28 *
29 * @param string Query string ('')
30 */
31 public static function dispatch($query='')
32 {
33 try {
34 $query = preg_replace('#^(/)+#', '/', '/'.$query);
35 $req = new Pluf_HTTP_Request($query);
36 $middleware = array();
37 foreach (Pluf::f('middleware_classes', array()) as $mw) {
38 $middleware[] = new $mw();
39 }
40 $skip = false;
41 foreach ($middleware as $mw) {
42 if (method_exists($mw, 'process_request')) {
43 $response = $mw->process_request($req);
44 if ($response !== false) {
45 // $response is a response
46 if (Pluf::f('pluf_runtime_header', false)) {
47 $response->headers['X-Perf-Runtime'] = sprintf('%.5f', (microtime(true) - $GLOBALS['_PX_starttime']));
48 }
49 $response->render($req->method != 'HEAD' and !defined('IN_UNIT_TESTS'));
50 $skip = true;
51 break;
52 }
53 }
54 }
55 if ($skip === false) {
56 $response = self::match($req);
57 if (!empty($req->response_vary_on)) {
58 $response->headers['Vary'] = $req->response_vary_on;
59 }
60 $middleware = array_reverse($middleware);
61 foreach ($middleware as $mw) {
62 if (method_exists($mw, 'process_response')) {
63 $response = $mw->process_response($req, $response);
64 }
65 }
66 if (Pluf::f('pluf_runtime_header', false)) {
67 $response->headers['X-Perf-Runtime'] = sprintf('%.5f', (microtime(true) - $GLOBALS['_PX_starttime']));
68 }
69 $response->render($req->method != 'HEAD' and !defined('IN_UNIT_TESTS'));
70 }
71 } catch (Exception $e) {
72 if (Pluf::f('debug', false) == true) {
73 $response = new Pluf_HTTP_Response_ServerErrorDebug($e);
74 } else {
75 $response = new Pluf_HTTP_Response_ServerError($e);
76 }
77 $response->render($req->method != 'HEAD' and !defined('IN_UNIT_TESTS'));
78 if (defined('IN_UNIT_TESTS')) {
79 throw $e;
80 }
81 }
82 /**
83 * [signal]
84 *
85 * Pluf_Dispatcher::postDispatch
86 *
87 * [sender]
88 *
89 * Pluf_Dispatcher
90 *
91 * [description]
92 *
93 * This signal is sent after the rendering of a request. This
94 * means you cannot affect the response but you can use this
95 * hook to do some cleaning.
96 *
97 * [parameters]
98 *
99 * array('request' => $request,
100 * 'response' => $response)
101 *
102 */
103 $params = array('request' => $req,
104 'response' => $response);
105 Pluf_Signal::send('Pluf_Dispatcher::postDispatch',
106 'Pluf_Dispatcher', $params);
107 return array($req, $response);
108 }
109
110 /**
111 * Match a query against the actions controllers.
112 *
113 * @see Pluf_HTTP_URL_reverse
114 *
115 * @param Pluf_HTTP_Request Request object
116 * @return Pluf_HTTP_Response Response object
117 */
118 public static function match($req, $firstpass=true)
119 {
120 try {
121 $views = $GLOBALS['_PX_views'];
122 $to_match = $req->query;
123 $n = count($views);
124 $i = 0;
125 while ($i<$n) {
126 $ctl = $views[$i];
127 if (preg_match($ctl['regex'], $to_match, $match)) {
128 if (!isset($ctl['sub'])) {
129 return self::send($req, $ctl, $match);
130 } else {
131 // Go in the subtree
132 $views = $ctl['sub'];
133 $i = 0;
134 $n = count($views);
135 $to_match = substr($to_match, strlen($match[0]));
136 continue;
137 }
138 }
139 $i++;
140 }
141 } catch (Pluf_HTTP_Error404 $e) {
142 // Need to add a 404 error handler
143 // something like Pluf::f('404_handler', 'class::method')
144 }
145 if ($firstpass and substr($req->query, -1) != '/') {
146 $req->query .= '/';
147 $res = self::match($req, false);
148 if ($res->status_code != 404) {
149 Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
150 $name = (isset($req->view[0]['name'])) ?
151 $req->view[0]['name'] :
152 $req->view[0]['model'].'::'.$req->view[0]['method'];
153 $url = Pluf_HTTP_URL_urlForView($name, array_slice($req->view[1], 1));
154 return new Pluf_HTTP_Response_Redirect($url, 301);
155 }
156 }
157 return new Pluf_HTTP_Response_NotFound($req);
158 }
159
160 /**
161 * Call the view found by self::match.
162 *
163 * The called view can throw an exception. This is fine and
164 * normal.
165 *
166 * @param Pluf_HTTP_Request Current request
167 * @param array The url definition matching the request
168 * @param array The match found by preg_match
169 * @return Pluf_HTTP_Response Response object
170 */
171 public static function send($req, $ctl, $match)
172 {
173 $req->view = array($ctl, $match);
174 $m = new $ctl['model']();
175 if (isset($m->{$ctl['method'].'_precond'})) {
176 // Here we have preconditions to respects. If the "answer"
177 // is true, then ok go ahead, if not then it a response so
178 // return it or an exception so let it go.
179 $preconds = $m->{$ctl['method'].'_precond'};
180 if (!is_array($preconds)) {
181 $preconds = array($preconds);
182 }
183 foreach ($preconds as $precond) {
184 if (!is_array($precond)) {
185 $res = call_user_func_array(
186 explode('::', $precond),
187 array(&$req)
188 );
189 } else {
190 $res = call_user_func_array(
191 explode('::', $precond[0]),
192 array_merge(array(&$req),
193 array_slice($precond, 1))
194 );
195 }
196 if ($res !== true) {
197 return $res;
198 }
199 }
200 }
201 if (!isset($ctl['params'])) {
202 return $m->$ctl['method']($req, $match);
203 } else {
204 return $m->$ctl['method']($req, $match, $ctl['params']);
205 }
206 }
207
208 /**
209 * Load the controllers.
210 *
211 * @param string File including the views.
212 * @return bool Success.
213 */
214 public static function loadControllers($file)
215 {
216 if (file_exists($file)) {
217 $GLOBALS['_PX_views'] = include $file;
218 return true;
219 }
220 return false;
221}
222}
223
224

Archive Download this file

Branches

Tags