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