1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\System;
9:
10: /**
11: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
12: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
13: * @copyright Copyright (c) 2019, Afterlogic Corp.
14: *
15: * @category Core
16: */
17: class Application
18: {
19: /**
20: * @type string
21: */
22: public const AUTH_TOKEN_KEY = 'AuthToken';
23:
24: /**
25: * @var \Aurora\System\Module\Manager
26: */
27: protected $oModuleManager;
28:
29: protected static $sBaseUrl = '';
30:
31: /**
32: * @return void
33: */
34: protected function __construct()
35: {
36: // \MailSo\Config::$FixIconvByMbstring = false;
37: \MailSo\Config::$SystemLogger = Api::SystemLogger();
38: \register_shutdown_function([$this, '__ApplicationShutdown']);
39: }
40:
41: public function __ApplicationShutdown()
42: {
43: $aStatistic = \MailSo\Base\Loader::Statistic();
44: if (\is_array($aStatistic)) {
45: if (isset($aStatistic['php']['memory_get_peak_usage'])) {
46: \Aurora\Api::Log('INFO[MEMORY]: Memory peak usage: '.$aStatistic['php']['memory_get_peak_usage']);
47: }
48:
49: if (isset($aStatistic['time'])) {
50: \Aurora\Api::Log('INFO[TIME]: Time delta: '.$aStatistic['time']);
51: }
52: }
53: }
54:
55: /**
56: * @return \Aurora\System\Application
57: */
58: public static function NewInstance()
59: {
60: return new self();
61: }
62:
63: /**
64: * @return \Aurora\System\Application
65: */
66: public static function SingletonInstance()
67: {
68: static $oInstance = null;
69: if (null === $oInstance) {
70: $oInstance = self::NewInstance();
71: }
72:
73: return $oInstance;
74: }
75:
76: public static function DebugMode($bDebug)
77: {
78: Api::$bDebug = $bDebug;
79: }
80:
81: public static function UseDbLogs()
82: {
83: Api::$bUseDbLog = true;
84: }
85:
86: public static function Start($sDefaultEntry = 'default')
87: {
88: if (!defined('AU_APP_START')) {
89: define('AU_APP_START', microtime(true));
90: }
91:
92: try {
93: Api::Init();
94: } catch (\Aurora\System\Exceptions\ApiException $oEx) {
95: \Aurora\System\Api::LogException($oEx);
96: }
97:
98: self::GetVersion();
99:
100: $mResult = self::SingletonInstance()->Route(
101: \strtolower(
102: Router::getItemByIndex(0, $sDefaultEntry)
103: )
104: );
105: if (\MailSo\Base\Http::SingletonInstance()->GetRequest('Format') !== 'Raw') {
106: echo $mResult;
107: }
108: }
109:
110: /**
111: * @return string
112: */
113: public static function GetVersion()
114: {
115: if (!defined('AU_APP_VERSION')) {
116: $sVersion = @\file_get_contents(AU_APP_ROOT_PATH.'VERSION');
117: \define('AU_APP_VERSION', $sVersion);
118: }
119: return AU_APP_VERSION;
120: }
121:
122: /**
123: * @return array
124: */
125: public static function GetPaths()
126: {
127: return Router::getItems();
128: }
129:
130: public function Route($sRoute)
131: {
132: return Api::GetModuleManager()->RunEntry($sRoute);
133: }
134:
135: public static function setBaseUrl($sBaseUrl = '')
136: {
137: self::$sBaseUrl = $sBaseUrl;
138: }
139:
140: public static function getBaseUrl()
141: {
142: if (empty(self::$sBaseUrl)) {
143: self::$sBaseUrl = \MailSo\Base\Http::SingletonInstance()->GetFullUrl();
144: }
145: return self::$sBaseUrl;
146: }
147: }
148: