-
Notifications
You must be signed in to change notification settings - Fork 0
/
z.php
551 lines (499 loc) · 14.2 KB
/
z.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?php
/**
* Main class of the framework. Takes care of bootstraping, routing and models loading.
* User: radu.mogos
* Date: 30.11.2011
* Time: 00:36:02
*/
namespace
{
final class Z
{
/**
* The current running ZeroG Instance
* @var <\Sys\ZeroG>
*/
private static $_instance = NULL;
/**
* Holds an array of various singleton classes
* @var <array>
*/
private static $_singletons = array();
/**
* Request class
* @var <Sys\Request>
*/
private static $_request;
/**
* The current context ZeroG is running in. Eg: cms/index runs in cms_index
* context, so the cms_index tag will be processed from the layout xml file.
* Basically this is just a shortcut to the specific tag in the layout that
* should be processed
* @var <string>
*/
private static $_context = '';
/**
* Reference to the Localization/Translation class
* @var <\Sys\L10n\Locale>
*/
private static $_locale = NULL;
/**
* A shortcut to the Profiler class
* @var <\Sys\Profiler>
*/
private static $_profiler = NULL;
/**
* A shortcut to the Configuration class
* @var <App\Config\System>
*/
private static $_config = NULL;
/**
* The internal registry holding array of mixed types
*
* @var <array>
*/
private static $_registry = array();
/**
* Current running module name
*
* @var <string>
*/
private static $_module = '';
/**
* An array of the current available db connections
*
* @var <array>
*/
private static $_connections = array();
/**
* Current running controller name
* @var <mixed>
*/
private static $_controller = null;
/**
* Current running action name
* @var <string>
*/
private static $_action = '';
/**
* Current website view
* @var <Core\Model\Website\View>
*/
private static $_website_view = null;
/**
* Start up ZeroG, load the Localization class, store the REQUEST
* parameters and configure URL rewrites
*
* @param <string> $code Website or Website_View code
* @param <string> $type Possible values: Website or Website_View
*/
final public static function run($code = '', $type = '')
{
if (self::$_instance !== NULL)
return;
// generate the singleton
self::$_instance = self::getInstance();
// start global profiler timer
self::getProfiler()->start('timer/global');
// load config data + execute router
self::$_config = self::getSingleton('Sys\\Config');
// set current website
self::initializeWebsite($code, $type);
// load config data for current website/group or view
self::$_config->loadDatabaseData();
// call the framework start event
// it's called after the config is loaded because the observers are
// defined in the xml config files or in the config cache
self::dispatchEvent('zerog_start');
// get request parameters
self::getRequest();
if (self::getRequest()->getParams() === NULL)
throw new \Sys\Exception('No map matched the current route');
// load locale settings and labels
self::$_locale = new Sys\L10n\Locale(
self::getRequest()->getParam('locale', self::getConfig('config/global/default/general/locale')),
self::getConfig('config/global/default/general/timezone')
);
// run the application
self::bootstrap();
// call the framework stop event
self::dispatchEvent('zerog_stop');
// end global profiler timer
self::getProfiler()->stop('timer/global');
}
/**
* Bootstraps the application, meaning it creates the current controller and then calls the specified action
*
* @static
* @return <void>
*/
final public static function bootstrap()
{
// current frontend router
$router = self::getRequest()->getParam('router');
// current module (extension)
$module = self::getRequest()->getParam('module');
// current controller called in our extension
$controller = self::getRequest()->getParam('controller');
// current action called in our controller
$action = self::getRequest()->getParam('action');
// register also the module, so we can attach it to the modules config class
self::register('current_module', self::getConfig()->getModule($module));
// current context, used to fetch the current layout xml tag
self::$_context = $router.'_'.$controller.'_'.$action;
// store the current module, controller, and action
self::$_module = $module;
// store the current controller
//self::$_controller = $controller;
// store the current action
$action .= 'Action';
self::$_action = $action;
// get the controller classname (+path) from the currenly loaded extension
$controllerClassName = self::getConfig()->getModule($module)->getControllerClass($controller);
//if (!class_exists($controllerClassName))
// throw new Sys\Exception("The controller %s class you are trying to initialize does not exist", $controllerClassName);
// instantiate the extension's controller class
try
{
if (class_exists($controllerClassName))
$class = new $controllerClassName;
}
catch (LogicException $e)
{
die('TODO: Fa un redirect la pagina 404');
//$this->redirect('page/view/error');
}
self::$_controller = $class;
// send all the GET/POST requests to the controller's action for
// further processing
//if (!method_exists($class, $action))
// throw new Sys\Exception("The called action %s does not exist on the current controller", $action);
$class->dispatch();
// and this is all there is to it. our app should be running now...
}
/**
* Load website data based on code and type
*
* @param <string> $code Website or Website_View code
* @param <string> $type String for 'website' or 'website_view'
*/
public static function initializeWebsite($code, $type)
{
if ($type == 'website_view' && $code != '')
{
$website_view = \Z::getModel('core/website/view')->loadByCode($code);
self::$_website_view = $website_view;
//$website_group = $website_view->getWebsiteGroup();
//$website = $website_view->getWebsite();
}
else if ($type == 'website')
{
if ($code != '')
{
$website = \Z::getModel('core/website')->loadByCode($code);
}
else
{
$website = \Z::getModel('core/website')->loadDefault();
}
$website_group = \Z::getModel('core/website/group')->load($website->getDefaultWebsiteGroupId());
$website_view = \Z::getModel('core/website/view')->load($website_group->getDefaultWebsiteViewId());
$website_view->setWebsite($website)->setWebsiteGroup($website_group);
self::$_website_view = $website_view;
}
else
{
throw new \Sys\Exception("Please specify a valid website code and type to display!");
}
if ($website_view->getIsActive() == 0)
{
throw new \Sys\Exception("The currently selected website_view is not active");
}
}
/**
* Get current loaded website view
*
* @return <Core\Model\Website\View>
*/
public static function getWebsiteView()
{
return self::$_website_view;
}
/**
* Dispatch observer callbacks defined in xml files
*
* @param <string> $eventName
* @param <mixed> $eventParams
*/
public static function dispatchEvent($eventName, $eventParams = null)
{
// There can be more than 1 observer for an event
$observersForThisEvent = self::$_config->getEventObserver($eventName);
if ($observersForThisEvent !== FALSE)
{
$eventData = new \Sys\Model();
$eventData->setData($eventParams);
foreach ($observersForThisEvent as $event)
{
$class = self::getSingleton($event['class']);
$action = $event['method'];
$class->$action($eventData);
}
}
// check if we have an observer and then execute it
// TO BE IMPLEMENTED
}
/**
* Returns the request instance
*/
public static function getRequest()
{
if (self::$_request == null)
self::$_request = new \Sys\Request();
return self::$_request;
}
/**
* Context parameters are read from app/config/routes.xml
*
* @return <array>
*/
/*public static function getContextParams()
{
return self::$_config->getRouter()->getContextParams();
}*/
/**
* Return the cached context name
*
* @return <string>
*/
public static function getContext()
{
return self::$_context;
}
/**
* Return the profiler
*
* @return <\Sys\Profiler>
*/
public static function getProfiler()
{
if (!array_key_exists('Sys\\Profiler', self::$_singletons))
{
self::$_singletons['Sys\\Profiler'] = new \Sys\Profiler();
self::$_profiler = self::$_singletons['Sys\\Profiler'];
}
return self::$_profiler;
}
/**
* Return the current ZeroG instance
* @return <\Sys\ZeroG> ZeroG instance
*/
final public static function getInstance()
{
if (self::$_instance === NULL)
{
self::$_instance = new Z();
//self::init();
}
return self::$_instance;
}
/**
* Return a singleton of a certain class
*
* @param <type> $class Which class to return
* @param <type> $classParams The parameters sent to the class, in case of initialization
* @return class
*/
public static function getSingleton($class, $classParams = '')
{
//static $_singletons = array();
$identifier = $class;
// if it's a model we created in an extension, the identifier and
// class are different, so we need to load the proper class
if (strpos($class, '/') !== FALSE)
$class = self::$_config->getModelClass($identifier);
if (!array_key_exists($identifier, self::$_singletons)) {
//$serializedFile = 'var/cache/serialized/'.md5($class).'.ser';
self::getProfiler()->start($class);
//if (!file_exists($serializedFile))
//{
self::$_singletons[$identifier] = new $class($classParams);
// file_put_contents($serializedFile, serialize(self::$_singletons[$class]));
//}
//else
// self::$_singletons[$class] = unserialize(file_get_contents($serializedFile));
self::getProfiler()->stop($class);
}
return self::$_singletons[$identifier];
}
/**
* Get the current controller object
* @return <mixed>
*/
public static function getController()
{
return self::$_controller;
}
/**
* Get the current action name
* @return <string>
*/
public static function getAction()
{
return self::$_action;
}
/**
* Returns the database connection specified by $connectionName in app/etc/local.xml
*
* @param <string> $connectionName Connection name to use
*/
public static function getDatabaseConnection($connectionName = 'default_setup')
{
// Store an array of connections, depending if it's a write/read adapter for example, or
// if we have more than one database connections.
// The default connection is specified in the "default_setup" adapter
if (!array_key_exists($connectionName, self::$_connections))
{
self::$_connections[$connectionName] = new \Sys\Database\Pdo($connectionName);
}
return self::$_connections[$connectionName];
}
/**
* Store a value in the registry
*
* @param <string> $name
* @param <mixed> $value
*/
public static function register($name, $value)
{
self::$_registry[$name] = $value;
}
/**
* Return a value from the registry
*
* @param <string> $name
* @return <mixed>
*/
public static function registry($name)
{
if (isset(self::$_registry[$name]))
return self::$_registry[$name];
return FALSE;
}
/**
* Resets the registry records
*/
public static function resetRegistry()
{
self::$_registry = array();
}
/**
* Prevent cloning
*/
final private function __clone() {}
// --- shortcut helpers ---
// eg: L is a shortcut to the locale, for translations, currency, etc
/**
* Returns a current helper class (if it does not exist, it creates it
* as a singleton)
*
* @param <string> $name
* @return <mixed>
*/
public static function getHelper($name)
{
if (substr($name, 0, 4) == "Sys\\")
return self::getSingleton($name);
return self::getSingleton(self::$_config->getHelperClass($name),
self::$_config->getHelper($name)->getModule());
}
/**
* Returns a model class by identifier
*
* @param <string> $name
* @return <mixed>
*/
public static function getModel($name)
{
//return self::getSingleton(self::$_config->getModelClass($name));
$class = self::$_config->getModelClass($name);
self::getProfiler()->start($class);
$instance = new $class;
self::getProfiler()->stop($class);
return $instance;
}
/**
* Add a block to the current layout (if the layout was not rendered yet)
*
* @param <string> $name
* @param <string> $type
* @return \Sys\Layout\Block
*/
public static function getBlock($name, $type)
{
$class = self::$_config->getBlockClass($type);
self::getProfiler()->start($class);
$instance = new $class($name, $type);
self::getProfiler()->stop($class);
return $instance;
}
/**
* Returns the current controller's layout object
*
* @return <Sys\Layout>
*/
public static function getLayout()
{
return self::$_controller->getLayout();
}
/**
* Returns a resource model
*
* @param <string> $name
* @return <mixed>
*/
public static function getResource($name)
{
$resourceIdentifier = 'resource_'.$name;
if (!array_key_exists($resourceIdentifier, self::$_singletons))
{
//$class = self::$_config->getResourceClass($name);
//self::$_singletons[$resourceIdentifier] = new $class;
self::$_singletons[$resourceIdentifier] = self::$_config->getResource($name);
}
return self::$_singletons[$resourceIdentifier];
}
/**
* Return the value of a config variable
*
* @param <string> $variable
* @return <mixed>
*/
public static function getConfig($variable = NULL)
{
return self::$_config->getConfig($variable);
}
/**
* Returns a reference to the current locale
*
* @see L()
*
* @return <\Sys\L10n\Locale>
*/
public static function getLocale()
{
return self::$_locale;
}
/**
* Return a translated label, based on the current locale
*
* @param <string> $label The label to translate, in english
* @param <string> $module The module the label belongs to
* @return <string> The translated label
*/
public static function __($label, $module)
{
return self::$_locale->__($label, $module);
}
}
}