blob: f05b285b5a6cae36207d769f59855d7a4bab9765 (
plain)
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
|
<?php
/**
* MINZ - Copyright 2011 Marien Fressinaud
* Sous licence AGPL3 <http://www.gnu.org/licenses/>
*/
/**
* Le Dispatcher s'occupe d'initialiser le Controller et d'executer l'action
* déterminée dans la Request
* C'est un singleton
*/
class Minz_Dispatcher {
/* singleton */
private static $instance = null;
private static $needsReset;
private static $registrations = array();
private $controller;
/**
* Récupère l'instance du Dispatcher
*/
public static function getInstance () {
if (self::$instance === null) {
self::$instance = new Minz_Dispatcher ();
}
return self::$instance;
}
/**
* Lance le controller indiqué dans Request
* Remplit le body de Response à partir de la Vue
* @exception Minz_Exception
*/
public function run () {
do {
self::$needsReset = false;
try {
$this->createController (Minz_Request::controllerName ());
$this->controller->init ();
$this->controller->firstAction ();
if (!self::$needsReset) {
$this->launchAction (
Minz_Request::actionName ()
. 'Action'
);
}
$this->controller->lastAction ();
if (!self::$needsReset) {
$this->controller->view ()->build ();
}
} catch (Minz_Exception $e) {
throw $e;
}
} while (self::$needsReset);
}
/**
* Informe le contrôleur qu'il doit recommancer car la requête a été modifiée
*/
public static function reset() {
self::$needsReset = true;
}
/**
* Instancie le Controller
* @param $base_name le nom du controller à instancier
* @exception ControllerNotExistException le controller n'existe pas
* @exception ControllerNotActionControllerException controller n'est
* > pas une instance de ActionController
*/
private function createController ($base_name) {
if (self::isRegistered($base_name)) {
self::loadController($base_name);
$controller_name = 'FreshExtension_' . $base_name . '_Controller';
} else {
$controller_name = 'FreshRSS_' . $base_name . '_Controller';
}
if (!class_exists ($controller_name)) {
throw new Minz_ControllerNotExistException (
$controller_name,
Minz_Exception::ERROR
);
}
$this->controller = new $controller_name ();
if (! ($this->controller instanceof Minz_ActionController)) {
throw new Minz_ControllerNotActionControllerException (
$controller_name,
Minz_Exception::ERROR
);
}
}
/**
* Lance l'action sur le controller du dispatcher
* @param $action_name le nom de l'action
* @exception ActionException si on ne peut pas exécuter l'action sur
* le controller
*/
private function launchAction ($action_name) {
if (!is_callable (array (
$this->controller,
$action_name
))) {
throw new Minz_ActionException (
get_class ($this->controller),
$action_name,
Minz_Exception::ERROR
);
}
call_user_func (array (
$this->controller,
$action_name
));
}
/**
* Register a controller file.
*
* @param $base_name the base name of the controller (i.e. ./?c=<base_name>)
* @param $base_path the base path where we should look into to find info.
*/
public static function registerController($base_name, $base_path) {
if (!self::isRegistered($base_name)) {
self::$registrations[$base_name] = $base_path;
}
}
/**
* Return if a controller is registered.
*
* @param $base_name the base name of the controller.
* @return true if the controller has been registered, false else.
*/
public static function isRegistered($base_name) {
return isset(self::$registrations[$base_name]);
}
/**
* Load a controller file (include).
*
* @param $base_name the base name of the controller.
*/
private static function loadController($base_name) {
$base_path = self::$registrations[$base_name];
$controller_filename = $base_path . '/Controllers/' . $base_name . 'Controller.php';
include_once $controller_filename;
}
private static function setViewPath($controller, $base_name) {
$base_path = self::$registrations[$base_name];
$controller->view()->setBasePathname($base_path);
}
}
|