o2system /
reactor
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System PHP Reactor package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | // ------------------------------------------------------------------------ |
||||
| 13 | |||||
| 14 | namespace O2System; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | /* |
||||
| 19 | * --------------------------------------------------------------- |
||||
| 20 | * ERROR REPORTING |
||||
| 21 | * --------------------------------------------------------------- |
||||
| 22 | * |
||||
| 23 | * Different environments will require different levels of error reporting. |
||||
| 24 | * By default development will show errors but testing and live will hide them. |
||||
| 25 | */ |
||||
| 26 | switch (strtoupper(ENVIRONMENT)) { |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 27 | case 'DEVELOPMENT': |
||||
| 28 | error_reporting(-1); |
||||
| 29 | ini_set('display_errors', 1); |
||||
| 30 | break; |
||||
| 31 | case 'TESTING': |
||||
| 32 | case 'PRODUCTION': |
||||
| 33 | ini_set('display_errors', 0); |
||||
| 34 | error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED); |
||||
| 35 | break; |
||||
| 36 | default: |
||||
| 37 | header('HTTP/1.1 503 Service Unavailable.', true, 503); |
||||
| 38 | echo 'The application environment is not set correctly.'; |
||||
| 39 | exit(1); // EXIT_ERROR |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | /* |
||||
| 43 | *--------------------------------------------------------------- |
||||
| 44 | * VENDOR PATH |
||||
| 45 | *--------------------------------------------------------------- |
||||
| 46 | * |
||||
| 47 | * RealPath to vendor folder. |
||||
| 48 | * |
||||
| 49 | * WITH TRAILING SLASH! |
||||
| 50 | */ |
||||
| 51 | if ( ! defined('PATH_VENDOR')) { |
||||
| 52 | define('PATH_VENDOR', PATH_ROOT . 'vendor' . DIRECTORY_SEPARATOR); |
||||
|
0 ignored issues
–
show
|
|||||
| 53 | } |
||||
| 54 | |||||
| 55 | /* |
||||
| 56 | *--------------------------------------------------------------- |
||||
| 57 | * FRAMEWORK PATH |
||||
| 58 | *--------------------------------------------------------------- |
||||
| 59 | * |
||||
| 60 | * RealPath to framework folder. |
||||
| 61 | * |
||||
| 62 | * WITH TRAILING SLASH! |
||||
| 63 | */ |
||||
| 64 | if ( ! defined('PATH_REACTOR')) { |
||||
| 65 | define('PATH_REACTOR', __DIR__ . DIRECTORY_SEPARATOR); |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | /* |
||||
| 69 | *--------------------------------------------------------------- |
||||
| 70 | * APP PATH |
||||
| 71 | *--------------------------------------------------------------- |
||||
| 72 | * |
||||
| 73 | * RealPath to application folder. |
||||
| 74 | * |
||||
| 75 | * WITH TRAILING SLASH! |
||||
| 76 | */ |
||||
| 77 | if ( ! defined('PATH_APP')) { |
||||
| 78 | define('PATH_APP', PATH_ROOT . DIR_APP . DIRECTORY_SEPARATOR); |
||||
|
0 ignored issues
–
show
|
|||||
| 79 | } |
||||
| 80 | |||||
| 81 | /* |
||||
| 82 | *--------------------------------------------------------------- |
||||
| 83 | * PUBLIC PATH |
||||
| 84 | *--------------------------------------------------------------- |
||||
| 85 | * |
||||
| 86 | * RealPath to public folder. |
||||
| 87 | * |
||||
| 88 | * WITH TRAILING SLASH! |
||||
| 89 | */ |
||||
| 90 | if ( ! defined('PATH_PUBLIC')) { |
||||
| 91 | define('PATH_PUBLIC', PATH_ROOT . DIR_PUBLIC . DIRECTORY_SEPARATOR); |
||||
|
0 ignored issues
–
show
|
|||||
| 92 | } |
||||
| 93 | |||||
| 94 | /* |
||||
| 95 | *--------------------------------------------------------------- |
||||
| 96 | * CACHE PATH |
||||
| 97 | *--------------------------------------------------------------- |
||||
| 98 | * |
||||
| 99 | * RealPath to writable caching folder. |
||||
| 100 | * |
||||
| 101 | * WITH TRAILING SLASH! |
||||
| 102 | */ |
||||
| 103 | if ( ! defined('PATH_CACHE')) { |
||||
| 104 | define('PATH_CACHE', PATH_ROOT . DIR_CACHE . DIRECTORY_SEPARATOR); |
||||
|
0 ignored issues
–
show
|
|||||
| 105 | } |
||||
| 106 | |||||
| 107 | /* |
||||
| 108 | *--------------------------------------------------------------- |
||||
| 109 | * STORAGE PATH |
||||
| 110 | *--------------------------------------------------------------- |
||||
| 111 | * |
||||
| 112 | * RealPath to writable storage folder. |
||||
| 113 | * |
||||
| 114 | * WITH TRAILING SLASH! |
||||
| 115 | */ |
||||
| 116 | if ( ! defined('PATH_STORAGE')) { |
||||
| 117 | define('PATH_STORAGE', PATH_ROOT . DIR_STORAGE . DIRECTORY_SEPARATOR); |
||||
|
0 ignored issues
–
show
|
|||||
| 118 | } |
||||
| 119 | |||||
| 120 | /* |
||||
| 121 | *--------------------------------------------------------------- |
||||
| 122 | * DATABASE PATH |
||||
| 123 | *--------------------------------------------------------------- |
||||
| 124 | * |
||||
| 125 | * RealPath to writable database folder. |
||||
| 126 | * |
||||
| 127 | * WITH TRAILING SLASH! |
||||
| 128 | */ |
||||
| 129 | if ( ! defined('PATH_DATABASE')) { |
||||
| 130 | define('PATH_DATABASE', PATH_ROOT . DIR_DATABASE . DIRECTORY_SEPARATOR); |
||||
|
0 ignored issues
–
show
|
|||||
| 131 | } |
||||
| 132 | |||||
| 133 | /* |
||||
| 134 | *--------------------------------------------------------------- |
||||
| 135 | * FRAMEWORK CONSTANTS |
||||
| 136 | *--------------------------------------------------------------- |
||||
| 137 | */ |
||||
| 138 | require __DIR__ . '/Config/Constants.php'; |
||||
| 139 | |||||
| 140 | /* |
||||
| 141 | *--------------------------------------------------------------- |
||||
| 142 | * FRAMEWORK HELPERS |
||||
| 143 | *--------------------------------------------------------------- |
||||
| 144 | */ |
||||
| 145 | require __DIR__ . '/Helpers/Reactor.php'; |
||||
| 146 | |||||
| 147 | /** |
||||
| 148 | * Class Reactor |
||||
| 149 | * |
||||
| 150 | * @package O2System |
||||
| 151 | */ |
||||
| 152 | class Reactor extends Kernel |
||||
| 153 | { |
||||
| 154 | /** |
||||
| 155 | * Reactor::$config |
||||
| 156 | * |
||||
| 157 | * Reactor Container Config |
||||
| 158 | * |
||||
| 159 | * @var Reactor\Containers\Config |
||||
| 160 | */ |
||||
| 161 | public $config; |
||||
| 162 | |||||
| 163 | /** |
||||
| 164 | * Reactor::$globals |
||||
| 165 | * |
||||
| 166 | * Reactor Container Globals |
||||
| 167 | * |
||||
| 168 | * @var Reactor\Containers\Globals |
||||
| 169 | */ |
||||
| 170 | public $globals; |
||||
| 171 | |||||
| 172 | /** |
||||
| 173 | * Reactor::$environment |
||||
| 174 | * |
||||
| 175 | * Reactor Container Environment |
||||
| 176 | * |
||||
| 177 | * @var Reactor\Containers\Environment |
||||
| 178 | */ |
||||
| 179 | public $environment; |
||||
| 180 | |||||
| 181 | /** |
||||
| 182 | * Reactor::$models |
||||
| 183 | * |
||||
| 184 | * Reactor Container Models |
||||
| 185 | * |
||||
| 186 | * @var Reactor\Containers\Models |
||||
| 187 | */ |
||||
| 188 | public $models; |
||||
| 189 | |||||
| 190 | // ------------------------------------------------------------------------ |
||||
| 191 | |||||
| 192 | /** |
||||
| 193 | * Reactor::__construct |
||||
| 194 | */ |
||||
| 195 | protected function __construct() |
||||
| 196 | { |
||||
| 197 | parent::__construct(); |
||||
| 198 | |||||
| 199 | if (profiler() !== false) { |
||||
| 200 | profiler()->watch('Starting O2System Reactor'); |
||||
| 201 | } |
||||
| 202 | |||||
| 203 | // Instantiate Config Container |
||||
| 204 | if (profiler() !== false) { |
||||
| 205 | profiler()->watch('Starting Config Container'); |
||||
| 206 | } |
||||
| 207 | |||||
| 208 | $this->config = new Reactor\Containers\Config(); |
||||
| 209 | |||||
| 210 | if (profiler() !== false) { |
||||
| 211 | profiler()->watch('Starting Reactor Services'); |
||||
| 212 | } |
||||
| 213 | |||||
| 214 | $services = [ |
||||
| 215 | 'Services\Loader' => 'loader', |
||||
| 216 | ]; |
||||
| 217 | |||||
| 218 | foreach ($services as $className => $classOffset) { |
||||
| 219 | $this->services->load($className, $classOffset); |
||||
| 220 | } |
||||
| 221 | |||||
| 222 | // Instantiate Config Container |
||||
| 223 | if (profiler() !== false) { |
||||
| 224 | profiler()->watch('Starting Config Container'); |
||||
| 225 | } |
||||
| 226 | |||||
| 227 | $this->config = new Reactor\Containers\Config(); |
||||
| 228 | |||||
| 229 | // Instantiate Globals Container |
||||
| 230 | if (profiler() !== false) { |
||||
| 231 | profiler()->watch('Starting Globals Container'); |
||||
| 232 | } |
||||
| 233 | $this->globals = new Reactor\Containers\Globals(); |
||||
| 234 | |||||
| 235 | // Instantiate Environment Container |
||||
| 236 | if (profiler() !== false) { |
||||
| 237 | profiler()->watch('Starting Environment Container'); |
||||
| 238 | } |
||||
| 239 | $this->environment = new Reactor\Containers\Environment(); |
||||
| 240 | |||||
| 241 | // Instantiate Models Container |
||||
| 242 | if (profiler() !== false) { |
||||
| 243 | profiler()->watch('Starting Models Container'); |
||||
| 244 | } |
||||
| 245 | |||||
| 246 | $this->models = new Reactor\Containers\Models(); |
||||
| 247 | |||||
| 248 | if (config()->loadFile('cache') === true) { |
||||
|
0 ignored issues
–
show
The method
loadFile() does not exist on O2System\Kernel\DataStructures\Config.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 249 | // Instantiate Cache Service |
||||
| 250 | if (profiler() !== false) { |
||||
| 251 | profiler()->watch('Starting Cache Service'); |
||||
| 252 | } |
||||
| 253 | |||||
| 254 | $this->services->add(new Reactor\Services\Cache(config('cache', true)), 'cache'); |
||||
|
0 ignored issues
–
show
It seems like
config('cache', true) can also be of type O2System\Reactor\Containers\Config; however, parameter $config of O2System\Reactor\Services\Cache::__construct() does only seem to accept O2System\Cache\DataStructures\Config, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 255 | } |
||||
| 256 | } |
||||
| 257 | |||||
| 258 | // ------------------------------------------------------------------------ |
||||
| 259 | |||||
| 260 | /** |
||||
| 261 | * Reactor::__reconstruct |
||||
| 262 | */ |
||||
| 263 | protected function __reconstruct() |
||||
| 264 | { |
||||
| 265 | if (is_cli()) { |
||||
| 266 | $this->cliHandler(); |
||||
| 267 | } else { |
||||
| 268 | $this->httpHandler(); |
||||
| 269 | } |
||||
| 270 | } |
||||
| 271 | |||||
| 272 | // ------------------------------------------------------------------------ |
||||
| 273 | |||||
| 274 | /** |
||||
| 275 | * Reactor::cliHandler |
||||
| 276 | * |
||||
| 277 | * @return void |
||||
| 278 | * @throws \ReflectionException |
||||
| 279 | */ |
||||
| 280 | private function cliHandler() |
||||
| 281 | { |
||||
| 282 | // Instantiate CLI Router Service |
||||
| 283 | $this->services->load(Kernel\Cli\Router::class); |
||||
| 284 | |||||
| 285 | if (profiler() !== false) { |
||||
| 286 | profiler()->watch('Parse Router Request'); |
||||
| 287 | } |
||||
| 288 | router()->handle(); |
||||
| 289 | |||||
| 290 | if ($commander = router()->getCommander()) { |
||||
| 291 | if ($commander instanceof Kernel\Cli\Router\DataStructures\Commander) { |
||||
| 292 | |||||
| 293 | // Autoload Model |
||||
| 294 | $modelClassName = str_replace('Commanders', 'Models', $commander->getName()); |
||||
| 295 | |||||
| 296 | if (class_exists($modelClassName)) { |
||||
| 297 | models()->load($modelClassName, 'commander'); |
||||
| 298 | } |
||||
| 299 | |||||
| 300 | if (profiler() !== false) { |
||||
| 301 | profiler()->watch('Instantiating Requested Commander: ' . $commander->getClass()); |
||||
| 302 | } |
||||
| 303 | $requestCommander = $commander->getInstance(); |
||||
| 304 | |||||
| 305 | if (profiler() !== false) { |
||||
| 306 | profiler()->watch('Execute Requested Commander: ' . $commander->getClass()); |
||||
| 307 | } |
||||
| 308 | $requestCommander->execute(); |
||||
| 309 | |||||
| 310 | exit(EXIT_SUCCESS); |
||||
|
0 ignored issues
–
show
|
|||||
| 311 | } |
||||
| 312 | } |
||||
| 313 | } |
||||
| 314 | |||||
| 315 | // ------------------------------------------------------------------------ |
||||
| 316 | |||||
| 317 | /** |
||||
| 318 | * Reactor::httpHandler |
||||
| 319 | * |
||||
| 320 | * @return void |
||||
| 321 | */ |
||||
| 322 | private function httpHandler() |
||||
| 323 | { |
||||
| 324 | // Instantiate Http Router Service |
||||
| 325 | $this->services->load(Reactor\Http\Router::class); |
||||
| 326 | |||||
| 327 | if (profiler() !== false) { |
||||
| 328 | profiler()->watch('Parse Router Request'); |
||||
| 329 | } |
||||
| 330 | router()->handle(); |
||||
| 331 | |||||
| 332 | // Instantiate Http Middleware Service |
||||
| 333 | $this->services->load('Http\Middleware', 'middleware'); |
||||
| 334 | |||||
| 335 | if (profiler() !== false) { |
||||
| 336 | profiler()->watch('Running Middleware Service: Pre Controller'); |
||||
| 337 | } |
||||
| 338 | middleware()->run(); |
||||
| 339 | |||||
| 340 | if($this->services->has('controller')) { |
||||
| 341 | $controller = $this->services->get('controller'); |
||||
| 342 | |||||
| 343 | // Autoload Model |
||||
| 344 | $modelClassName = str_replace('Controllers', 'Models', $controller->getName()); |
||||
| 345 | |||||
| 346 | if (class_exists($modelClassName)) { |
||||
| 347 | $this->models->register($modelClassName, 'controller'); |
||||
| 348 | } |
||||
| 349 | |||||
| 350 | if (profiler() !== false) { |
||||
| 351 | profiler()->watch('Instantiating Requested Controller: ' . $controller->getClass()); |
||||
| 352 | } |
||||
| 353 | $requestController = $controller->getInstance(); |
||||
| 354 | |||||
| 355 | if (method_exists($requestController, '__reconstruct')) { |
||||
| 356 | $requestController->__reconstruct(); |
||||
| 357 | } elseif (method_exists($requestController, 'initialize')) { |
||||
| 358 | $requestController->initialize(); |
||||
| 359 | } |
||||
| 360 | |||||
| 361 | $this->services->add($requestController, 'controller'); |
||||
| 362 | |||||
| 363 | if (profiler() !== false) { |
||||
| 364 | profiler()->watch('Calling Middleware Service: Post Controller'); |
||||
| 365 | } |
||||
| 366 | middleware()->run(); |
||||
| 367 | |||||
| 368 | $requestMethod = $controller->getRequestMethod(); |
||||
| 369 | $requestMethodArgs = $controller->getRequestMethodArgs(); |
||||
| 370 | |||||
| 371 | // Call the requested controller method |
||||
| 372 | if (profiler() !== false) { |
||||
| 373 | profiler()->watch('Execute Requested Controller Method'); |
||||
| 374 | } |
||||
| 375 | |||||
| 376 | $requestController->__call($requestMethod, $requestMethodArgs); |
||||
| 377 | } else { |
||||
| 378 | // Show Error (404) Page Not Found |
||||
| 379 | output()->sendError(404); |
||||
| 380 | } |
||||
| 381 | } |
||||
| 382 | } |
||||
| 383 |