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