Rev 34 | Rev 45 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 34 | Rev 35 | ||
---|---|---|---|
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 (self::$_instance === null) |
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::${$name}; |
81 | return $class::${$name}; |
82 | }
|
82 | }
|
83 | 83 | ||
84 | $method = 'get' . ucfirst($name); |
84 | $method = 'get' . ucfirst($name); |
85 | 85 | ||
86 | if (method_exists($this, $method)) |
86 | if (method_exists($this, $method)) |
87 | {
|
87 | {
|
88 | return $this->$method(); |
88 | return $this->$method(); |
89 | }
|
89 | }
|
90 | 90 | ||
91 | if (property_exists($this, "_$name")) |
91 | if (property_exists($this, "_$name")) |
92 | {
|
92 | {
|
93 | return $this->{"_$name"}; |
93 | return $this->{"_$name"}; |
94 | }
|
94 | }
|
95 | 95 | ||
96 | return $this->$name; |
96 | return $this->$name; |
97 | }
|
97 | }
|
98 | 98 | ||
99 | /**
|
99 | /**
|
100 | * Setter for properties
|
100 | * Setter for properties
|
101 | *
|
101 | *
|
102 | * @param string $name
|
102 | * @param string $name
|
103 | * @param mixed $value The new property value before assignment
|
103 | * @param mixed $value The new property value before assignment
|
104 | * @throws ModelPropertyException
|
104 | * @throws ModelPropertyException
|
105 | */
|
105 | */
|
106 | public function __set($name, $value) |
106 | public function __set($name, $value) |
107 | {
|
107 | {
|
108 | $method = 'set' . ucfirst($name); |
108 | $method = 'set' . ucfirst($name); |
109 | 109 | ||
110 | if (method_exists($this, $method)) |
110 | if (method_exists($this, $method)) |
111 | {
|
111 | {
|
112 | return $this->$method($value); |
112 | return $this->$method($value); |
113 | }
|
113 | }
|
114 | 114 | ||
115 | if (property_exists($this, "_$name")) |
115 | if (property_exists($this, "_$name")) |
116 | {
|
116 | {
|
117 | $this->{"_$name"} = $value; |
117 | $this->{"_$name"} = $value; |
118 | return $this->{"_$name"}; |
118 | return $this->{"_$name"}; |
119 | }
|
119 | }
|
120 | 120 | ||
121 | /* NOTE: Attempts to set other properties are _silently_ _ignored_ */
|
121 | /* NOTE: Attempts to set other properties are _silently_ _ignored_ */
|
122 | }
|
122 | }
|
123 | 123 | ||
124 | /**
|
124 | /**
|
125 | * Runs the application, setting up session management and
|
125 | * Runs the application, setting up session management and
|
126 | * constructing the controller indicated by the URI
|
126 | * constructing the controller indicated by the URI
|
127 | */
|
127 | */
|
128 | public function run() |
128 | public function run() |
129 | {
|
129 | {
|
130 | $this->startSession(); |
130 | $this->startSession(); |
131 | 131 | ||
132 | $controller = self::getParam('controller', $_REQUEST); |
132 | $controller = self::getParam('controller', $_REQUEST); |
133 | if (!$controller) |
133 | if (!$controller) |
134 | {
|
134 | {
|
135 | $controller = $this->_defaultController; |
135 | $controller = $this->_defaultController; |
136 | }
|
136 | }
|
137 | 137 | ||
138 | $controller = ucfirst($controller); |
138 | $controller = ucfirst($controller); |
139 | 139 | ||
140 | $controller = $controller . 'Controller'; |
140 | $controller = $controller . 'Controller'; |
141 | require_once "{$this->_controllerPath}/{$controller}.php"; |
141 | require_once "{$this->_controllerPath}/{$controller}.php"; |
142 | $this->_currentController = new $controller(); |
142 | $this->_currentController = new $controller(); |
143 | 143 | ||
144 | return $this; |
144 | return $this; |
145 | }
|
145 | }
|
146 | 146 | ||
147 | protected function startSession() |
147 | protected function startSession() |
148 | {
|
148 | {
|
149 | session_start(); |
149 | session_start(); |
150 | }
|
150 | }
|
151 | 151 | ||
152 | /**
|
152 | /**
|
153 | * Gets a request parameter
|
153 | * Gets a request parameter
|
154 | *
|
154 | *
|
155 | * @param string $key
|
155 | * @param string $key
|
156 | * Key to look up in the array
|
156 | * Key to look up in the array
|
157 | * @param array $array
|
157 | * @param array $array
|
158 | * Array where to look up <var>$key</var>.
|
158 | * Array where to look up <var>$key</var>.
|
159 | * The default is <code>$_GET</code>.
|
159 | * The default is <code>$_GET</code>.
|
160 | * @return mixed
|
160 | * @return mixed
|
161 | * <code>null</code> if there is no such <var>$key</var>
|
161 | * <code>null</code> if there is no such <var>$key</var>
|
162 | * in <var>$array</var>
|
162 | * in <var>$array</var>
|
163 | */
|
163 | */
|
164 | public static function getParam($key, array $array = null) |
164 | public static function getParam($key, array $array = null) |
165 | {
|
165 | {
|
166 | if ($array === null) |
166 | if ($array === null) |
167 | {
|
167 | {
|
168 | $array = $_GET; |
168 | $array = $_GET; |
169 | }
|
169 | }
|
170 | 170 | ||
171 | return isset($array[$key]) ? $array[$key] : null; |
171 | return isset($array[$key]) ? $array[$key] : null; |
172 | }
|
172 | }
|
173 | 173 | ||
174 | /**
|
174 | /**
|
175 | * Registers a database
|
175 | * Registers a database
|
176 | *
|
176 | *
|
177 | * @param string $key
|
177 | * @param string $key
|
178 | * @param Database $database
|
178 | * @param Database $database
|
179 | */
|
179 | */
|
180 | public function registerDatabase($key, Database $database) |
180 | public function registerDatabase($key, Database $database) |
181 | {
|
181 | {
|
182 | Registry::set($key, $database); |
182 | Registry::set($key, $database); |
183 | }
|
183 | }
|
184 | 184 | ||
185 | /**
|
185 | /**
|
186 | * Sets the default database
|
186 | * Sets the default database
|
187 | * @param key Registry key to refer to the {@link Database}
|
187 | * @param key Registry key to refer to the {@link Database}
|
188 | */
|
188 | */
|
189 | public function setDefaultDatabase($key) |
189 | public function setDefaultDatabase($key) |
190 | {
|
190 | {
|
191 | $this->_defaultDatabase = $key; |
191 | $this->_defaultDatabase = $key; |
192 | }
|
192 | }
|
193 | 193 | ||
194 | /**
|
194 | /**
|
195 | * Sets the current controller for this application
|
195 | * Sets the current controller for this application
|
196 | *
|
196 | *
|
197 | * @param Controller $controller
|
197 | * @param Controller $controller
|
198 | * @return Application
|
198 | * @return Application
|
199 | */
|
199 | */
|
200 | public function setCurrentController(Controller $controller) |
200 | public function setCurrentController(Controller $controller) |
201 | {
|
201 | {
|
202 | $this->_currentController = $controller; |
202 | $this->_currentController = $controller; |
203 | return $this; |
203 | return $this; |
204 | }
|
204 | }
|
205 | 205 | ||
206 | /**
|
206 | /**
|
207 | * Returns the current controller for this application
|
207 | * Returns the current controller for this application
|
208 | *
|
208 | *
|
209 | * @return Controller
|
209 | * @return Controller
|
210 | */
|
210 | */
|
211 | public function getCurrentController() |
211 | public function getCurrentController() |
212 | {
|
212 | {
|
213 | return $this->_currentController; |
213 | return $this->_currentController; |
214 | }
|
214 | }
|
215 | 215 | ||
216 | /**
|
216 | /**
|
217 | * Returns the default database for this application
|
217 | * Returns the default database for this application
|
218 | *
|
218 | *
|
219 | * @return Database
|
219 | * @return Database
|
220 | */
|
220 | */
|
221 | public function getDefaultDatabase() |
221 | public function getDefaultDatabase() |
222 | {
|
222 | {
|
223 | return Registry::get($this->_defaultDatabase); |
223 | return Registry::get($this->_defaultDatabase); |
224 | }
|
224 | }
|
225 | 225 | ||
226 | /**
|
226 | /**
|
227 | * Returns a relative URI reference for an action of the
|
227 | * Returns a relative URI-reference for an action of the
|
228 | * application
|
228 | * application
|
229 | *
|
229 | *
|
230 | * @param string[optional] $controller
|
230 | * @param string[optional] $controller
|
231 | * @param string[optional] $action
|
231 | * @param string[optional] $action
|
232 | * @param int[optional] $id
|
232 | * @param int[optional] $id
|
233 | */
|
233 | */
|
234 | public function getURL($controller = null, $action = null, $id = null) |
234 | public function getURL($controller = null, $action = null, $id = null) |
235 | {
|
235 | {
|
236 | /* Apache module */
|
236 | /* Apache module */
|
237 | $url = self::getParam('SCRIPT_URL', $_SERVER); |
237 | $url = self::getParam('SCRIPT_URL', $_SERVER); |
238 | if ($url === null) |
238 | if ($url === null) |
239 | {
|
239 | {
|
240 | /* FastCGI */
|
240 | /* FastCGI */
|
241 | $url = self::getParam('URL', $_SERVER); |
241 | $url = self::getParam('URL', $_SERVER); |
242 | if ($url === null) |
242 | if ($url === null) |
243 | {
|
243 | {
|
244 | /* Server/PHP too old, compute URI */
|
244 | /* Server/PHP too old, compute URI */
|
245 | $url = self::getParam('REQUEST_URI', $_SERVER); |
245 | $url = self::getParam('REQUEST_URI', $_SERVER); |
246 | if (preg_match('/^[^?]+/', $url, $matches) > 0) |
246 | if (preg_match('/^[^?]+/', $url, $matches) > 0) |
247 | {
|
247 | {
|
248 | $url = $matches[0]; |
248 | $url = $matches[0]; |
249 | }
|
249 | }
|
250 | else
|
250 | else
|
251 | {
|
251 | {
|
252 | /* Has .php in it, but at least it works */
|
252 | /* Has .php in it, but at least it works */
|
253 | $url = self::getParam('SCRIPT_NAME', $_SERVER); |
253 | $url = self::getParam('SCRIPT_NAME', $_SERVER); |
254 | if ($url === null) |
254 | if ($url === null) |
255 | {
|
255 | {
|
256 | throw new Exception( |
256 | throw new Exception( |
257 | 'None of $_SERVER["SCRIPT_URL"], $_SERVER["URL"],'
|
257 | 'None of $_SERVER["SCRIPT_URL"], $_SERVER["URL"],'
|
258 | . ' $_SERVER["REQUEST_URI"], or $_SERVER["SCRIPT_NAME"]' |
258 | . ' $_SERVER["REQUEST_URI"], or $_SERVER["SCRIPT_NAME"]' |
259 | . ' is available, cannot continue.'); |
259 | . ' is available, cannot continue.'); |
260 | }
|
260 | }
|
261 | }
|
261 | }
|
262 | }
|
262 | }
|
263 | }
|
263 | }
|
264 | 264 | ||
265 | $query = (($controller !== null) ? 'controller=' . $controller : '') |
265 | $query = (($controller !== null) ? 'controller=' . $controller : '') |
266 | . (($action !== null) ? '&action=' . $action : '') |
266 | . (($action !== null) ? '&action=' . $action : '') |
267 | . (($id !== null) ? '&id=' . $id : ''); |
267 | . (($id !== null) ? '&id=' . $id : ''); |
268 | 268 | ||
269 | return $url . ($query ? '?' . $query : ''); |
269 | return $url . ($query ? '?' . $query : ''); |
270 | }
|
270 | }
|
271 | 271 | ||
272 | /**
|
272 | /**
|
273 | * Performs a server-side redirect within the application
|
273 | * Performs a server-side redirect within the application
|
274 | */
|
274 | */
|
275 | public static function redirect($query = '') |
275 | public static function redirect($query = '') |
276 | {
|
276 | {
|
277 | $script_uri = self::getParam('SCRIPT_URI', $_SERVER); |
277 | $script_uri = self::getParam('SCRIPT_URI', $_SERVER); |
278 | if ($script_uri === null) |
278 | if ($script_uri === null) |
279 | {
|
279 | {
|
280 | /* Server/PHP too old, compute URI */
|
280 | /* Server/PHP too old, compute URI */
|
281 | if (preg_match('/^[^?]+/', |
281 | if (preg_match('/^[^?]+/', |
282 | self::getParam('REQUEST_URI', $_SERVER), $matches) > 0) |
282 | self::getParam('REQUEST_URI', $_SERVER), $matches) > 0) |
283 | {
|
283 | {
|
284 | $query_prefix = $matches[0]; |
284 | $query_prefix = $matches[0]; |
285 | }
|
285 | }
|
286 | else
|
286 | else
|
287 | {
|
287 | {
|
288 | /* Has .php in it, but at least it works */
|
288 | /* Has .php in it, but at least it works */
|
289 | $query_prefix = self::getParam('SCRIPT_NAME', $_SERVER); |
289 | $query_prefix = self::getParam('SCRIPT_NAME', $_SERVER); |
290 | }
|
290 | }
|
291 | 291 | ||
292 | /* TODO: Let user decide which ports map to which URI scheme */
|
292 | /* TODO: Let user decide which ports map to which URI scheme */
|
293 | $script_uri = (self::getParam('SERVER_PORT', $_SERVER) == 443 |
293 | $script_uri = (self::getParam('SERVER_PORT', $_SERVER) == 443 |
294 | ? 'https://'
|
294 | ? 'https://'
|
295 | : 'http://') |
295 | : 'http://') |
296 | . self::getParam('HTTP_HOST', $_SERVER) |
296 | . self::getParam('HTTP_HOST', $_SERVER) |
297 | . $query_prefix; |
297 | . $query_prefix; |
298 | }
|
298 | }
|
299 | 299 | ||
300 | header('Location: ' . $script_uri |
300 | header('Location: ' . $script_uri |
301 | . ($query ? (substr($query, 0, 1) === '?' ? '' : '?') . $query : '')); |
301 | . ($query ? (substr($query, 0, 1) === '?' ? '' : '?') . $query : '')); |
302 | }
|
302 | }
|
303 | }
|
303 | }
|
304 | 304 |