srchub

srchub Git Source Tree


Root/pluf/src/Pluf/Dispatcher.php

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

Archive Download this file

Branches

Number of commits:
Page rendered in 0.10628s using 11 queries.