Rev 35 | Rev 52 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
27 | PointedEar | 1 | <?php |
2 | |||
29 | PointedEar | 3 | require_once __DIR__ . '/AbstractModel.php'; |
4 | require_once __DIR__ . '/Registry.php'; |
||
27 | PointedEar | 5 | |
6 | /** |
||
7 | * Basic application class |
||
8 | * |
||
9 | * @author Thomas Lahn |
||
10 | */ |
||
11 | class Application |
||
12 | { |
||
13 | /** |
||
14 | * Relative path to the controllers directory |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $_controllerPath = 'application/controllers'; |
||
45 | PointedEar | 18 | |
27 | PointedEar | 19 | /** |
20 | * Default controller of the application |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $_defaultController = 'Index'; |
||
45 | PointedEar | 24 | |
27 | PointedEar | 25 | /** |
26 | * Registry key for the default database of the application |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $_defaultDatabase; |
||
45 | PointedEar | 30 | |
27 | PointedEar | 31 | /** |
32 | * Currently active controller of this application |
||
33 | * @var Controller |
||
34 | */ |
||
35 | protected $_currentController; |
||
45 | PointedEar | 36 | |
27 | PointedEar | 37 | /** |
38 | * Singleton |
||
39 | * |
||
40 | * @var Application |
||
41 | */ |
||
42 | private static $_instance; |
||
45 | PointedEar | 43 | |
27 | PointedEar | 44 | protected function __construct() |
45 | { |
||
46 | /* Singleton pattern */ |
||
47 | } |
||
45 | PointedEar | 48 | |
27 | PointedEar | 49 | /** |
50 | * Gets a reference to the <code>Application</code> instance |
||
51 | * |
||
52 | * @param Application $instance |
||
53 | * The instance to be used as application. The default is a new |
||
54 | * application. This parameter is ignored if the application was |
||
55 | * already initialized. |
||
56 | * @return Application |
||
57 | */ |
||
58 | public static function getInstance(Application $instance = null) |
||
59 | { |
||
31 | PointedEar | 60 | if (self::$_instance === null) |
27 | PointedEar | 61 | { |
62 | self::$_instance = ($instance === null) ? new self() : $instance; |
||
63 | } |
||
45 | PointedEar | 64 | |
27 | PointedEar | 65 | return self::$_instance; |
66 | } |
||
45 | PointedEar | 67 | |
31 | PointedEar | 68 | /** |
69 | * Getter for properties |
||
70 | * |
||
71 | * @param string $name |
||
72 | * @throws ModelPropertyException |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function __get($name) |
||
76 | { |
||
77 | /* Support for Object-Relational Mappers */ |
||
78 | if (strpos($name, 'persistent') === 0) |
||
79 | { |
||
80 | $class = get_class($this); |
||
34 | PointedEar | 81 | return $class::${$name}; |
31 | PointedEar | 82 | } |
45 | PointedEar | 83 | |
31 | PointedEar | 84 | $method = 'get' . ucfirst($name); |
45 | PointedEar | 85 | |
31 | PointedEar | 86 | if (method_exists($this, $method)) |
87 | { |
||
88 | return $this->$method(); |
||
89 | } |
||
45 | PointedEar | 90 | |
31 | PointedEar | 91 | if (property_exists($this, "_$name")) |
92 | { |
||
93 | return $this->{"_$name"}; |
||
94 | } |
||
45 | PointedEar | 95 | |
31 | PointedEar | 96 | return $this->$name; |
97 | } |
||
45 | PointedEar | 98 | |
27 | PointedEar | 99 | /** |
31 | PointedEar | 100 | * Setter for properties |
101 | * |
||
102 | * @param string $name |
||
103 | * @param mixed $value The new property value before assignment |
||
104 | * @throws ModelPropertyException |
||
105 | */ |
||
106 | public function __set($name, $value) |
||
107 | { |
||
108 | $method = 'set' . ucfirst($name); |
||
45 | PointedEar | 109 | |
31 | PointedEar | 110 | if (method_exists($this, $method)) |
111 | { |
||
112 | return $this->$method($value); |
||
113 | } |
||
45 | PointedEar | 114 | |
31 | PointedEar | 115 | if (property_exists($this, "_$name")) |
116 | { |
||
117 | $this->{"_$name"} = $value; |
||
118 | return $this->{"_$name"}; |
||
119 | } |
||
45 | PointedEar | 120 | |
31 | PointedEar | 121 | /* NOTE: Attempts to set other properties are _silently_ _ignored_ */ |
122 | } |
||
45 | PointedEar | 123 | |
31 | PointedEar | 124 | /** |
27 | PointedEar | 125 | * Runs the application, setting up session management and |
126 | * constructing the controller indicated by the URI |
||
127 | */ |
||
128 | public function run() |
||
129 | { |
||
130 | $this->startSession(); |
||
45 | PointedEar | 131 | |
27 | PointedEar | 132 | $controller = self::getParam('controller', $_REQUEST); |
133 | if (!$controller) |
||
134 | { |
||
135 | $controller = $this->_defaultController; |
||
136 | } |
||
137 | |||
138 | $controller = ucfirst($controller); |
||
45 | PointedEar | 139 | |
27 | PointedEar | 140 | $controller = $controller . 'Controller'; |
141 | require_once "{$this->_controllerPath}/{$controller}.php"; |
||
142 | $this->_currentController = new $controller(); |
||
45 | PointedEar | 143 | |
27 | PointedEar | 144 | return $this; |
145 | } |
||
146 | |||
147 | protected function startSession() |
||
148 | { |
||
149 | session_start(); |
||
150 | } |
||
45 | PointedEar | 151 | |
27 | PointedEar | 152 | /** |
153 | * Gets a request parameter |
||
154 | * |
||
155 | * @param string $key |
||
156 | * Key to look up in the array |
||
157 | * @param array $array |
||
158 | * Array where to look up <var>$key</var>. |
||
159 | * The default is <code>$_GET</code>. |
||
160 | * @return mixed |
||
161 | * <code>null</code> if there is no such <var>$key</var> |
||
162 | * in <var>$array</var> |
||
163 | */ |
||
164 | public static function getParam($key, array $array = null) |
||
165 | { |
||
31 | PointedEar | 166 | if ($array === null) |
27 | PointedEar | 167 | { |
168 | $array = $_GET; |
||
169 | } |
||
45 | PointedEar | 170 | |
27 | PointedEar | 171 | return isset($array[$key]) ? $array[$key] : null; |
172 | } |
||
45 | PointedEar | 173 | |
27 | PointedEar | 174 | /** |
175 | * Registers a database |
||
176 | * |
||
177 | * @param string $key |
||
178 | * @param Database $database |
||
45 | PointedEar | 179 | * @return string Registry key |
180 | * @see Application::setDefaultDatabase() |
||
27 | PointedEar | 181 | */ |
45 | PointedEar | 182 | public function registerDatabase ($key, Database $database) |
27 | PointedEar | 183 | { |
184 | Registry::set($key, $database); |
||
45 | PointedEar | 185 | return $key; |
27 | PointedEar | 186 | } |
187 | |||
188 | /** |
||
189 | * Sets the default database |
||
45 | PointedEar | 190 | * @param string Registry key to refer to the {@link Database} |
27 | PointedEar | 191 | */ |
45 | PointedEar | 192 | public function setDefaultDatabase ($key) |
27 | PointedEar | 193 | { |
194 | $this->_defaultDatabase = $key; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Sets the current controller for this application |
||
199 | * |
||
200 | * @param Controller $controller |
||
201 | * @return Application |
||
202 | */ |
||
203 | public function setCurrentController(Controller $controller) |
||
204 | { |
||
205 | $this->_currentController = $controller; |
||
206 | return $this; |
||
207 | } |
||
45 | PointedEar | 208 | |
27 | PointedEar | 209 | /** |
210 | * Returns the current controller for this application |
||
211 | * |
||
212 | * @return Controller |
||
213 | */ |
||
214 | public function getCurrentController() |
||
215 | { |
||
216 | return $this->_currentController; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Returns the default database for this application |
||
221 | * |
||
222 | * @return Database |
||
223 | */ |
||
224 | public function getDefaultDatabase() |
||
225 | { |
||
226 | return Registry::get($this->_defaultDatabase); |
||
227 | } |
||
228 | |||
229 | /** |
||
35 | PointedEar | 230 | * Returns a relative URI-reference for an action of the |
27 | PointedEar | 231 | * application |
232 | * |
||
233 | * @param string[optional] $controller |
||
234 | * @param string[optional] $action |
||
235 | * @param int[optional] $id |
||
236 | */ |
||
237 | public function getURL($controller = null, $action = null, $id = null) |
||
238 | { |
||
239 | /* Apache module */ |
||
240 | $url = self::getParam('SCRIPT_URL', $_SERVER); |
||
241 | if ($url === null) |
||
242 | { |
||
243 | /* FastCGI */ |
||
244 | $url = self::getParam('URL', $_SERVER); |
||
245 | if ($url === null) |
||
246 | { |
||
31 | PointedEar | 247 | /* Server/PHP too old, compute URI */ |
248 | $url = self::getParam('REQUEST_URI', $_SERVER); |
||
249 | if (preg_match('/^[^?]+/', $url, $matches) > 0) |
||
250 | { |
||
251 | $url = $matches[0]; |
||
252 | } |
||
253 | else |
||
254 | { |
||
255 | /* Has .php in it, but at least it works */ |
||
256 | $url = self::getParam('SCRIPT_NAME', $_SERVER); |
||
257 | if ($url === null) |
||
258 | { |
||
259 | throw new Exception( |
||
260 | 'None of $_SERVER["SCRIPT_URL"], $_SERVER["URL"],' |
||
261 | . ' $_SERVER["REQUEST_URI"], or $_SERVER["SCRIPT_NAME"]' |
||
262 | . ' is available, cannot continue.'); |
||
263 | } |
||
264 | } |
||
27 | PointedEar | 265 | } |
266 | } |
||
45 | PointedEar | 267 | |
31 | PointedEar | 268 | $query = (($controller !== null) ? 'controller=' . $controller : '') |
269 | . (($action !== null) ? '&action=' . $action : '') |
||
270 | . (($id !== null) ? '&id=' . $id : ''); |
||
45 | PointedEar | 271 | |
27 | PointedEar | 272 | return $url . ($query ? '?' . $query : ''); |
273 | } |
||
45 | PointedEar | 274 | |
27 | PointedEar | 275 | /** |
276 | * Performs a server-side redirect within the application |
||
277 | */ |
||
278 | public static function redirect($query = '') |
||
279 | { |
||
280 | $script_uri = self::getParam('SCRIPT_URI', $_SERVER); |
||
31 | PointedEar | 281 | if ($script_uri === null) |
27 | PointedEar | 282 | { |
283 | /* Server/PHP too old, compute URI */ |
||
284 | if (preg_match('/^[^?]+/', |
||
285 | self::getParam('REQUEST_URI', $_SERVER), $matches) > 0) |
||
286 | { |
||
287 | $query_prefix = $matches[0]; |
||
288 | } |
||
289 | else |
||
290 | { |
||
291 | /* Has .php in it, but at least it works */ |
||
292 | $query_prefix = self::getParam('SCRIPT_NAME', $_SERVER); |
||
293 | } |
||
294 | |||
295 | /* TODO: Let user decide which ports map to which URI scheme */ |
||
296 | $script_uri = (self::getParam('SERVER_PORT', $_SERVER) == 443 |
||
297 | ? 'https://' |
||
298 | : 'http://') |
||
299 | . self::getParam('HTTP_HOST', $_SERVER) |
||
300 | . $query_prefix; |
||
301 | } |
||
302 | |||
303 | header('Location: ' . $script_uri |
||
304 | . ($query ? (substr($query, 0, 1) === '?' ? '' : '?') . $query : '')); |
||
305 | } |
||
306 | } |