-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFramework.php
executable file
·526 lines (466 loc) · 12.5 KB
/
Framework.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
<?php
umask(0002);
if (!defined('PROFILE') )
{
$profile = file_get_contents(WEBEDIT_HOME . DIRECTORY_SEPARATOR . 'profile');
if ( $profile === false || $profile == '' )
{
throw new Exception('Profile not defined. Please define a profile in file ./profile.');
}
define('PROFILE', trim($profile) );
}
if (!defined('FRAMEWORK_HOME'))
{
define('FRAMEWORK_HOME', WEBEDIT_HOME . DIRECTORY_SEPARATOR . 'framework');
}
if (!defined('AG_CACHE_DIR'))
{
define('AG_CACHE_DIR', WEBEDIT_HOME . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . PROFILE);
}
define('CHANGE_LOG_DIR', WEBEDIT_HOME . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . PROFILE);
if (!is_dir(CHANGE_LOG_DIR)) @mkdir(CHANGE_LOG_DIR, 0777, true);
define('CHANGE_BUILD_DIR', WEBEDIT_HOME . DIRECTORY_SEPARATOR . 'build' . DIRECTORY_SEPARATOR . PROFILE);
class Framework
{
/**
* @var integer
*/
static $logLevel = null;
/**
* @var boolean
*/
private static $debugEnabled, $infoEnabled, $warnEnabled, $errorEnabled, $fatalEnabled;
/**
* @var float[]
*/
private static $benchSteps = array();
/**
* @var integer
*/
private static $benchStepsIndex = -1;
/**
* @return string
*/
public static function getVersion()
{
return FRAMEWORK_VERSION;
}
/**
* @see project config and DEVELOPMENT_MODE constant
* @return boolean
*/
public static function inDevelopmentMode()
{
return AG_DEVELOPMENT_MODE;
}
/**
* @return boolean
*/
public static function isSiteEnabled()
{
$siteDisabledFlag = f_util_FileUtils::buildWebeditPath("site_is_disabled");
return !file_exists($siteDisabledFlag);
}
/**
* @param string $message
* @param integer $priority elementof {Logger::DEBUG, ...}
* @param string $loggerGroup
*/
public static function log($message, $priority, $loggerGroup = "webapp")
{
if (self::getLogLevel() <= $priority)
{
LoggingService::getInstance()->log($message, $loggerGroup, $priority);
}
}
/**
* @param string $message
* @param string $loggerGroup
*/
public static function debug($message, $loggerGroup = "webapp")
{
self::log("[DEBUG]\t".$message, Logger::DEBUG, $loggerGroup);
}
/**
* @param string $message
* @param string $loggerGroup
*/
public static function info($message, $loggerGroup = "webapp")
{
self::log("[INFO]\t".$message, Logger::INFO, $loggerGroup);
}
/**
* @param string $message
* @param string $loggerGroup
*/
public static function warn($message, $loggerGroup = "webapp")
{
self::log("[WARN]\t".$message, Logger::WARN, $loggerGroup);
}
/**
* @param string $message
* @param string $loggerGroup
*/
public static function error($message, $loggerGroup = "webapp")
{
self::log("[ERROR]\t".$message, Logger::ERROR, $loggerGroup);
}
/**
* @param Exception $e
* @param string $loggerGroup
*/
public static function exception($e, $loggerGroup = "webapp")
{
self::log("[EXCEPTION]\t".get_class($e).": ".$e->getMessage()."\n".$e->getTraceAsString(), Logger::ERROR, $loggerGroup);
}
/**
* @param string $message
* @param string $loggerGroup
*/
public static function fatal($message, $loggerGroup = "webapp")
{
self::log("[FATAL]\t".$message, Logger::FATAL, $loggerGroup);
}
/**
* @param string $message
*/
public static function deprecated($message)
{
if (self::inDevelopmentMode()) {trigger_error($message, E_USER_DEPRECATED);}
}
/**
* @return integer
*/
private static function getLogLevel()
{
if (self::$logLevel === null)
{
self::$logLevel = constant('Logger::'.AG_LOGGING_LEVEL);
if (self::$logLevel == false) {self::$logLevel = Logger::WARN;}
}
return self::$logLevel;
}
/**
* @param integer $priority elementof {Logger::DEBUG, ...}
* @return boolean true if log of priority $priority is enabled
*/
private static function isLogEnabled($priority)
{
return self::getLogLevel() <= $priority;
}
/**
* @return boolean true if debug log is enabled
*/
public static function isDebugEnabled()
{
if (null === self::$debugEnabled)
{
self::$debugEnabled = self::isLogEnabled(Logger::DEBUG);
}
return self::$debugEnabled;
}
/**
* @return boolean true if info log is enabled
*/
public static function isInfoEnabled()
{
if (null === self::$infoEnabled)
{
self::$infoEnabled = self::isLogEnabled(Logger::INFO);
}
return self::$infoEnabled;
}
/**
* @return boolean true if warn log is enabled
*/
public static function isWarnEnabled()
{
if (null === self::$warnEnabled)
{
self::$warnEnabled = self::isLogEnabled(Logger::WARN);
}
return self::$warnEnabled;
}
/**
* @return boolean true if error log is enabled
*/
public static function isErrorEnabled()
{
if (null === self::$errorEnabled)
{
self::$errorEnabled = self::isLogEnabled(Logger::ERROR);
}
return self::$errorEnabled;
}
/**
* @return boolean true if fatal log is enabled
*/
public static function isFatalEnabled()
{
if (null === self::$fatalEnabled)
{
self::$fatalEnabled = self::isLogEnabled(Logger::FATAL);
}
return self::$fatalEnabled;
}
/**
* To benchmark a piece of code, use:
* Framework::startBench(); // begin bench process
* $brands = $this->getBrandsList();
* Framework::bench("getBrandList"); // debug time between this call and last call
* foreach ($brands as $brand)
* {
* $label = $brand->getLabel();
* $index[strtolower($label[0])][$label] = $brand;
* }
* Framework::endBench("getBrandList processing"); // debug time between this call and last call and end bench process
*/
public static function startBench()
{
self::$benchSteps[] = microtime(true);
self::$benchStepsIndex++;
}
/**
* @see Framework::startBench($msg)
* @param string $msg
*/
public static function bench($msg)
{
if (self::isDebugEnabled())
{
$newStep = microtime(true);
self::debug('|BENCH|' . ($newStep-self::$benchSteps[self::$benchStepsIndex]) . '|' . $msg);
self::$benchSteps[self::$benchStepsIndex] = $newStep;
}
}
/**
* @see Framework::startBench($msg)
* @param string $msg
*/
public static function endBench($msg = null)
{
if (!is_null($msg))
{
self::bench($msg);
}
array_pop(self::$benchSteps);
self::$benchStepsIndex--;
}
/**
* @return string
*/
public static function getBaseUrl()
{
$webSite = website_WebsiteModuleService::getInstance()->getCurrentWebsite();
if ($webSite->isContextLangAvailable())
{
return $webSite->getProtocol() . '://'. $webSite->getDomain();
}
return $webSite->getProtocol() . '://'. $webSite->getVoDomain();
}
/**
* @return string
*/
public static function getUIBaseUrl()
{
return self::getUIProtocol() . '://'.self::getUIDefaultHost();
}
/**
* @return string
*/
public static function getUIProtocol()
{
return DEFAULT_UI_PROTOCOL;
}
/**
* @return string
*/
public static function getUIDefaultHost()
{
if (!defined('AG_WEBAPP_HOST'))
{
$middlewareIni = Framework::getConfiguration('general');
if ( isset($middlewareIni['server-fqdn']) )
{
define('AG_WEBAPP_HOST', $middlewareIni['server-fqdn']);
}
else
{
define('AG_WEBAPP_HOST', $_SERVER['HTTP_HOST']);
}
}
return AG_WEBAPP_HOST;
}
/**
* @return string
*/
public static function getDefaultSenderHost()
{
if (!defined('DEFAULT_SENDER_HOST'))
{
define('DEFAULT_SENDER_HOST', self::getUIDefaultHost());
}
return DEFAULT_SENDER_HOST;
}
/**
* @return string
*/
public static function getDefaultNoReplySender()
{
if (!defined('NOREPLY_DEFAULT_EMAIL'))
{
define('NOREPLY_DEFAULT_EMAIL', 'noreply@' . self::getDefaultSenderHost());
}
return NOREPLY_DEFAULT_EMAIL;
}
/**
* Return true if the $path configuration exist
* @param string $path
*/
public static function hasConfiguration($path)
{
return change_ConfigurationService::getInstance()->hasConfiguration($path);
}
/**
* Return an array with part of configuration of Framework
* or throw a Exception if the $path configuration does not exist
*
* @param string $path
* @param boolean $strict
* @throws Exception if the $path configuration does not exist
* @return string|false if the path was not founded and strict value if false
*/
public static function getConfiguration($path, $strict = true)
{
return change_ConfigurationService::getInstance()->getConfiguration($path, $strict);
}
/**
* Return an array with part of configuration of Framework
* or null if the $path configuration does not exist
*
* @param string $path
* @param mixed $defaultValue
* @return mixed|null
*/
public static function getConfigurationValue($path, $defaultValue = null)
{
return change_ConfigurationService::getInstance()->getConfigurationValue($path, $defaultValue);
}
//DEPRECATED
/**
* @deprecated use change_ConfigurationService::getAllConfiguration()
*/
public static function getAllConfiguration()
{
return change_ConfigurationService::getInstance()->getAllConfiguration();
}
/**
* @deprecated
*/
public static function addPackageConfiguration($packageName, $infos)
{
$cs = change_ConfigurationService::getInstance();
$config = $cs->getAllConfiguration();
if ($config != null && isset($config['packageversion']))
{
$cs->addVolatileProjectConfigurationNamedEntry('packageversion/' . $packageName, $infos);
}
else
{
throw new Exception('Framework configuration not loaded');
}
}
/**
* @deprecated use change_ConfigurationService::loadConfiguration()
*/
public static function loadConfiguration($env = '', $onlyConfig = false)
{
change_ConfigurationService::getInstance()->loadConfiguration($env, $onlyConfig);
}
/**
* @deprecated use change_ConfigurationService::loadConfiguration()
*/
public static function reloadConfiguration($env = '')
{
change_ConfigurationService::getInstance()->loadConfiguration($env);
}
/**
* @deprecated
*/
public static function getCompanyName()
{
return AG_WEBAPP_NAME;
}
/**
* @deprecated (will be removed in 4.0)
*/
public final static function parseComponentType($componentType,$whatwewant=null,$noException=false)
{
$matches = array();
if (preg_match('#^(.*)/([\w_\-]+)$#', $componentType, $matches))
{
$res = array();
$res['component'] = $matches[2];
$res['package'] = $matches[1];
$tt = explode("_", $res['package']);
if ($tt[0] == "framework")
{
$res['package_type'] = "framework";
$res['package_name'] = "framework";
}
else
{
$res['package_type'] = $tt[0];
$res['package_name'] = $tt[1];
}
if ($whatwewant===null)
{
return $res;
}
else
{
return $res[$whatwewant];
}
}
if ($noException) return null;
$e = new ClassException("invalid-component-type");
$e->setAttribute('componentType', $componentType);
throw $e;
}
}
// Load configuration
require_once(FRAMEWORK_HOME . '/service/ConfigurationService.class.php');
change_ConfigurationService::getInstance()->loadConfiguration(PROFILE);
require_once(FRAMEWORK_HOME . '/loader/ResourceResolver.class.php');
require_once(FRAMEWORK_HOME . '/loader/ClassResolver.class.php');
require_once(FRAMEWORK_HOME . '/loader/Resolver.class.php');
require_once(FRAMEWORK_HOME . '/loader/ResourceLoader.class.php');
require_once(FRAMEWORK_HOME . '/loader/ClassLoader.class.php');
require_once(FRAMEWORK_HOME . '/loader/Loader.class.php');
if (spl_autoload_register(array(ClassLoader::getInstance(), "autoload")) === false)
{
throw new Exception("Could not register Change framework autoload function");
}
define('AG_WEBAPP_DIR', PROJECT_OVERRIDE);
ini_set('include_path', FRAMEWORK_HOME . '/libs/pear' . PATH_SEPARATOR . PEAR_DIR);
ini_set('arg_separator.output', '&');
ini_set('magic_quotes_runtime', 0);
require_once(FRAMEWORK_HOME.'/libs/mvc/Context.class.php');
require_once(FRAMEWORK_HOME.'/libs/mvc/Controller.class.php');
require_once(FRAMEWORK_HOME.'/libs/mvc/Request.class.php');
require_once(FRAMEWORK_HOME.'/libs/mvc/Storage.class.php');
require_once(FRAMEWORK_HOME.'/libs/mvc/User.class.php');
require_once(FRAMEWORK_HOME.'/libs/mvc/Action.class.php');
require_once(FRAMEWORK_HOME.'/libs/mvc/View.class.php');
// Load modules informations
require_once(FRAMEWORK_HOME."/service/Injection.php");
require_once(FRAMEWORK_HOME."/service/BaseService.class.php");
require_once(FRAMEWORK_HOME."/service/ModuleService.class.php");
$ms = ModuleService::getInstance();
$ms->loadCacheFile();
require_once(FRAMEWORK_HOME . '/service/LoggingService.class.php');
LoggingService::getInstance()->registerErrorHandler();
// Set the locale.
$localResult = setlocale(LC_ALL, 'en_US.UTF-8');
// Set GMT TimeZone
date_default_timezone_set('GMT');