Pluf Framework

Pluf Framework Commit Details


Date:2009-04-06 07:11:20 (15 years 8 months ago)
Author:Loic d'Anterroches
Branch:develop, master
Commit:a7df883a9b6b4e4bd529e6f8c03a811045d1996c
Parents: fc18955b62b883b690385f178509cb9c85286e12
Message:Added the modularization of the URLs.

Changes:

File differences

src/Pluf/Dispatcher.php
107107
108108
109109
110
111
110112
111113
112114
113115
114116
115
116
117
118
119
120117
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
118
119
120
121
122
123
124
125
126
155127
156
128
129
130
131
132
133
157134
158135
136
159137
160138
161139
......
177155
178156
179157
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
180206
181207
182208
/**
* 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)
{
// Order the controllers by priority
foreach ($GLOBALS['_PX_views'] as $key => $control) {
$priority[$key] = $control['priority'];
}
array_multisort($priority, SORT_ASC, $GLOBALS['_PX_views']);
try {
foreach ($GLOBALS['_PX_views'] as $key => $ctl) {
$match = array();
if (preg_match($ctl['regex'], $req->query, $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);
$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 {
return $m->$ctl['method']($req, $match, $ctl['params']);
// 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
}
/**
* 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.
src/Pluf/HTTP/URL.php
105105
106106
107107
108
109108
110109
111110
112111
113112
114
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
115141
116
142
117143
118
144
119145
120
121
146
147
148
149
150
151
152
153
154
155
156
157
158
122159
123160
124
125
126
127
128
129
130
131
161
132162
133163
134164
......
165195
166196
167197
168
169
170
171
172
198
199
173200
*/
function Pluf_HTTP_URL_reverse($view, $params=array())
{
$regex = null;
$model = '';
$method = '';
if (false !== strpos($view, '::')) {
list($model, $method) = split('::', $view);
}
foreach ($GLOBALS['_PX_views'] as $dview) {
$vdef = array($model, $method, $view);
$regbase = array('', array());
$regbase = Pluf_HTTP_URL_find($GLOBALS['_PX_views'], $vdef, $regbase);
if ($regbase === false) {
throw new Exception(sprintf('Error, the view: %s has not been found.', $view));
}
$url = '';
foreach ($regbase[1] as $regex) {
$url .= Pluf_HTTP_URL_buildReverseUrl($regex, $params);
}
if (!defined('IN_UNIT_TESTS')) {
$url = $regbase[0].$url;
}
return $url;
}
/**
* Go in the list of views to find the matching one.
*
* @param array Views
* @param array View definition array(model, method, name)
* @param array Regex of the view up to now and base
* @return mixed Regex of the view or false
*/
function Pluf_HTTP_URL_find($views, $vdef, $regbase)
{
foreach ($views as $dview) {
if (
(isset($dview['name']) && $dview['name'] == $view)
(isset($dview['name']) && $dview['name'] == $vdef[2])
or
($dview['model'] == $model && $dview['method'] == $method)
($dview['model'] == $vdef[0] && $dview['method'] == $vdef[1])
) {
$regex = $dview['regex'];
break;
$regbase[1][] = $dview['regex'];
if (!empty($dview['base'])) {
$regbase[0] = $dview['base'];
}
return $regbase;
}
if (isset($dview['sub'])) {
$regbase2 = $regbase;
$regbase2[1][] = $dview['regex'];
$res = Pluf_HTTP_URL_find($dview['sub'], $vdef, $regbase2);
if ($res) {
return $res;
}
}
}
if ($regex === null) {
throw new Exception(sprintf('Error, the view: %s has not been found.', $view));
}
$url = Pluf_HTTP_URL_buildReverseUrl($regex, $params);
if (isset($dview['base']) and !defined('IN_UNIT_TESTS')) {
$url = $dview['base'].$url;
}
return $url;
return false;
}
/**
'return $a;');
$url = preg_replace_callback($groups, $func, $url_regex);
}
$url = substr(substr($url, 2), 0, -2);
if (substr($url, -1) !== '$') {
return $url;
}
return substr($url, 0, -1);
preg_match('/^#\^?([^#\$]+)/', $url, $matches);
return $matches[1];
}
src/Pluf/Tests/Dispatch/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
<?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_Tests_Dispatch_Dispatcher extends UnitTestCase
{
protected $views = array();
function __construct()
{
parent::__construct('Test the dispatching.');
}
function setUp()
{
$this->views = $GLOBALS['_PX_views'];
}
function tearDown()
{
$GLOBALS['_PX_views'] = $this->views;
}
function hello()
{
return true;
}
function testSimple()
{
$GLOBALS['_PX_views'] = array(
array(
'regex' => '#^/hello/$#',
'base' => '',
'model' => 'Pluf_Tests_Dispatch_Dispatcher',
'method' => 'hello'
)
);
$req1 = (object) array('query' => '/hello/'); // match
$req2 = (object) array('query' => '/hello'); // match second pass
$req3 = (object) array('query' => '/hello/you/'); // no match
$this->assertIdentical(true, Pluf_Dispatcher::match($req1));
$this->assertIsA(Pluf_Dispatcher::match($req2),
'Pluf_HTTP_Response_Redirect');
$this->assertIsA(Pluf_Dispatcher::match($req3),
'Pluf_HTTP_Response_NotFound');
}
function testRecursif()
{
$GLOBALS['_PX_views'] = array(
array(
'regex' => '#^/hello/#',
'base' => '',
'sub' => array(
array(
'regex' => '#^world/$#',
'base' => '',
'model' => 'Pluf_Tests_Dispatch_Dispatcher',
'method' => 'hello'
)
)
));
$req1 = (object) array('query' => '/hello/world/'); // match
$req2 = (object) array('query' => '/hello/world'); // match second pass
$req3 = (object) array('query' => '/hello/you/'); // no match
$this->assertIdentical(true, Pluf_Dispatcher::match($req1));
$this->assertIsA(Pluf_Dispatcher::match($req2),
'Pluf_HTTP_Response_Redirect');
$this->assertIsA(Pluf_Dispatcher::match($req3),
'Pluf_HTTP_Response_NotFound');
Pluf::loadFunction('Pluf_HTTP_URL_reverse');
$this->assertEqual('/hello/world/',
Pluf_HTTP_URL_reverse('Pluf_Tests_Dispatch_Dispatcher::hello'));
}
}
src/Pluf/conf/pluf.test.php
3636
3737
3838
39
39
4040
4141
4242
$cfg['tmp_folder'] = '/tmp';
// The folder in which the templates of the application are located.
$cfg['templates_folder'] = array(dirname(__FILE__).'/../templates');
$cfg['template_folders'] = array(dirname(__FILE__).'/../templates');
$cfg['pluf_use_rowpermission'] = true;

Archive Download the corresponding diff file

Branches

Tags

Number of commits:
Page rendered in 0.06985s using 13 queries.