forked from delakanda/wyf
/
Application.php
412 lines (374 loc) · 13 KB
/
Application.php
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/*
* WYF Framework
* Copyright (c) 2011 James Ekow Abaka Ainooson
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* A class which provides utilities for the running application. This class
* provides the final stage of the applications execution process. It is
* responsible for loading the controllers and their associated views. While
* controller methods are running, they can also use this class to alter some
* aspects of the view (like setting the title of the web page or adding a
* directive to load a javascript).
*
* @author James Ekow Abaka Ainooson <jainooson@gmail.com>
*/
class Application
{
/**
* Specifies that the current controller running was loaded from a
* Controller class.
* @var string
*/
const TYPE_MODULE = "type_module";
/**
* Specifies that the current controller running was loaded from a
* Model class.
* @var string
*/
const TYPE_MODEL = "type_model";
/**
* The notes that are currently displayed on the top of the rendered page.
* @var Array
*/
private static $notes = array();
/**
* Initial or default template used for laying out the pages. The template
* used can be chaged by altering the value of this variable during
* the execution of the controller.
*
* @var string
*/
public static $template;
/**
* The title of the page for this given request. Although the title of the page
* can be changed during the execution of your controller action by altering
* the value of this property, you should use the Application::setTitle() method
* for this purpose.
*
* @var string
* @see Application::setTitle
*/
private static $title;
/**
* The name of the application. The value of this variable is normally used
* during the rendering of the title of the application into the title
* tag of the HTML output. The default value from this variable is always
* read from the config.php file.
*
* @var string
*/
private static $siteName;
/**
* An array containing all the stylesheets which would be used for styling
* the current layout. Stylesheets can be added through the
* Application::addStylesheet method.
*
* @var array
* @see Application::addStylesheet.
*/
private static $stylesheets = array();
/**
* An array containing all the javascripts which would be loaded for the
* current request. Javascripts can be added through the
* Application::addJavascript method.
*
* @var array
*/
private static $javascripts = array();
/**
* A prefix to all the pages of the application. This property can be set if
* the application is running through a sub directory of the web server's
* root directory. This can be set through the config.php of the running
* application.
*
* @var string
*/
public static $prefix;
/**
* Contains the current menu tree for the menu loaded into the side bar.
* This variable is usually populated based on the roles currently attached
* to the user.
*
* @var array
*/
public static $menus = array();
/**
* The filesystem path to the loaction of the application's modules. By
* default, modules are store in the app/modules directory.
*
* @var string.
*/
public static $packagesPath;
/**
* Set to true when applicaiton is in CLI mode and false otherwise.
*
* @var boolean
*/
public static $cli = false;
/**
* Contains the output that is generated when the application is executed
* throuh the command line interface.
*
* @var string
*/
public static $cliOutput = "";
/**
* The current configuration map. Modifying this value during runtime has
* no effect.
* @var array
*/
public static $config;
/**
* An instance of the template engine which would be used to render
* the controller at the very last stage of the execution. You can use this
* instance to do any rendering you want during the execution of your
* controller. Using this prevents the loading of multiple instances of
* the template engine.
*
* @var Smarty
*/
public static $templateEngine;
/**
* The default controller route that should be loaded when the request is
* empty. You can think of this as your default or index page.
* @var string
*/
public static $defaultRoute = "dashboard";
/**
* The flag which indicates whether the side menu is visible or not.
* @var boolean
*/
private static $sideMenuHidden = false;
/**
* Adds a stylesheet to the list of stylesheets. This method adds
* the stylesheets at the bottom of the list.
*
* @param string $href A path to the stylesheet
* @param string $media The media of the stylesheet. Defaults to all.
* @param string $pathPrefix An optional prefix to add to the path.
*/
public static function addStylesheet($href, $pathPrefix = false, $media="all")
{
Application::$stylesheets[] = self::prepareStylesheetEntry($href, $pathPrefix, $media);
}
/**
* Adds a stylesheet to the list of stylesheets. This method adds the
* stylesheets to the top of the list.
*
* @param string $href A path to the stylesheet
* @param string $media The media of the stylesheet. Defaults to all.
* @param string $pathPrefix An optional prefix to add to the path.
*/
public static function preAddStylesheet($href, $pathPrefix = false, $media="all")
{
array_unshift(Application::$stylesheets, self::prepareStylesheetEntry($href, $pathPrefix, $media));
}
/**
* Format the stylesheet entry and make it a little bit more appropriate
* for rendering.
*
* @param string $href A path to the stylesheet
* @param string $media The media of the stylesheet. Defaults to all.
* @param string $pathPrefix An optional prefix to add to the path.
* @return Array. A structured array describing the array.
*/
private static function prepareStylesheetEntry($href, $pathPrefix, $media)
{
return array(
"href"=>($pathPrefix === false ? "app/themes/" . Application::$config['theme'] . "/" : $pathPrefix) . $href,"media"=>$media
);
}
/**
* This method returns a link to a resource within your WYF app. This method
* adds any prefixes the application requires.
*
* @param string $path
* @return string
*/
public static function getLink($path)
{
return Application::$prefix.$path;
}
/**
* A method to add a javascript to the list of javascripts.
*
* @param string $href A path to the javascript.
*/
public static function addJavascript($href)
{
Application::$javascripts[] = $href;
}
/**
* Sets the title of the page. This method appends the title set to
* the name of the site.
*
* @param string $title
*/
public static function setTitle($title="")
{
if($title=="")
{
Application::$title = Application::$siteName;
}
else
{
Application::$title = $title . " | ". Application::$siteName;
}
}
/**
* Outputs the application. This method is the final stage in the application
* lifecyle which calls all the template files and outputs the
* final application in HTML.
*/
public static function render()
{
$t = Application::$templateEngine;
if($_GET["q"]=="")
{
$_GET["q"]= Application::$defaultRoute;
}
$path = explode("/",$_GET["q"]);
Application::$template = "main.tpl";
$t->assign('prefix',Application::$prefix);
Application::setTitle();
$module = Controller::load($path);
if(Application::$cli)
{
ob_start();
}
if(Application::$template == "")
{
print $module->content;
}
else
{
$t->assign('content',$module->content);
$t->assign('module_name', $module->label);
$t->assign('module_description',$module->description);
$t->assign('side_menu_hidden', self::$sideMenuHidden);
foreach(array_keys(Application::$menus) as $key)
{
$t->assign($key, Menu::getContents($key));
}
$t->assign('stylesheets',Application::$stylesheets);
$t->assign('styles',$t->fetch('stylesheets.tpl'));
$t->assign('javascripts',Application::$javascripts);
$t->assign('scripts',$t->fetch('javascripts.tpl'));
$t->assign('title', Application::$title);
$t->assign('session', $_SESSION);
$t->assign('info',
array_merge((is_array($_SESSION['notes']) ? $_SESSION['notes'] : array()), self::$notes)
);
$t->display(Application::$template);
}
if(Application::$cli)
{
if(Application::$cliOutput=="")
{
print ob_get_clean();
}
else
{
file_put_contents(Application::$cliOutput, ob_get_clean());
}
}
}
/**
* Emits a header to redirect the page to a new location. This method
* checks to find out if the redirect parameter has been set in the url. If
* this parameter is set, it takes precedence over the parameter passed to
* the url.
*
* @param string $url The url to redirect to.
*/
public static function redirect($url, $notification = false)
{
if(isset($_GET["redirect"]))
{
header("Location: {$_GET["redirect"]}" . ($notification === false ? '' : "?notification={$notification}"));
}
else
{
header("Location: $url");
}
}
/**
* A utility method which generates camelized names for classes. It is called
* throughout the application to convert URLs and model paths.
*
* @param string $string
* @param string $delimiter
* @param string $baseDelimiter
*/
public static function camelize($string, $delimiter=".", $baseDelimiter = "")
{
if($baseDelimiter == "") $baseDelimiter = $delimiter;
$parts = explode($delimiter, $string);
$ret = "";
foreach($parts as $part)
{
$ret .= $delimiter == $baseDelimiter ? ucfirst(Application::camelize($part, "_", $baseDelimiter)) : ucfirst($part);
}
return $ret;
}
/**
* A utility method which converts a string to lowecase while converting
* all spaces to underscores.
*
* @param string $name The string to be converted/
* @return string A lowercase string with all spaces as underscores.
*/
public static function labelize($name)
{
return ucwords(str_replace("_", " ", $name));
}
/**
* Sets a flag in the template which prevents the rendering of the side menu.
* Please note that this method would only work if the template being used
* to render your app supports hiding side menus.
*/
public static function hideSideMenu()
{
self::$sideMenuHidden = true;
}
/**
* Unsets a flag in the template which prevents the rendering of the side menu.
* Please note that this method would only work if the template being used
* to render your app supports hiding side menus.
*/
public static function showSideMenu()
{
self::$sideMenuHidden = false;
}
/**
*
*/
public static function setSiteName($siteName)
{
self::$siteName = $siteName;
}
public static function getWyfHome($path = '')
{
return substr(__DIR__, strlen(getcwd()) + 1) . "/$path";
}
}