Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
StartupAsync.php
Go to the documentation of this file.
1<?php
2
4
6
16class StartupAsync extends Startup {
17 private const IS_VALID = 1;
18 private const INITIALIZE = 2;
19 private const FINALIZE = 4;
20 private static $controllers = [ ];
21
22 private static $routes=[];
23
31 public static function forward(string $url, bool $initialize = true, bool $finalize = true): void {
32 $methodUrl=$_SERVER['REQUEST_METHOD'].$url;
33 if(isset(self::$routes[$methodUrl])){
34 $m=self::$routes[$methodUrl];
35 $m($initialize,$finalize);
36 return ;
37 }
38 $u = self::parseUrl ( $url );
39 if (\is_array ( Router::getRoutes () ) && ($ru = Router::getRoute ( $url, true, self::$config ['debug'] ?? false)) !== false) {
40 if (\is_array ( $ru )) {
41 if (isset ( $ru ['controller'] )) {
42 (self::$routes[$methodUrl]=function($i,$f)use($ru){
43 static::runAction ( $ru, $i, $f );
44 })($initialize,$finalize);
45 } else {
46 (self::$routes[$methodUrl]=function() use($ru){
47 self::runCallable ( $ru );
48 })();
49 }
50 } else {
51 (self::$routes[$methodUrl]=function() use($ru){
52 echo $ru; // Displays route response from cache
53 })();
54
55 }
56 } else {
57 $ru =['controller'=>self::$ctrlNS . $u [0],'action'=> $u [1]??'index','params'=> \array_slice ( $u, 2 )];
58 (self::$routes[$methodUrl]=function($i,$f)use($ru){
59 static::runAction ( $ru, $i, $f );
60 })($initialize,$finalize);
61 }
62 }
63
64 public static function runAction(array &$u, bool $initialize = true, bool $finalize = true): void {
65 self::$controller = $ctrl = $u ['controller'];
66 self::$action = $action = $u ['action'];
67 self::$actionParams = $u['params']??[];
68
69 try {
70 if (null !== $controller = self::getControllerInstance ( $ctrl )) {
71 if($mainParams=$u['mainParams']??false){
72 static::setMainParams($controller,$mainParams);
73 }
74 $binaryCalls = $controller->_binaryCalls ?? (self::IS_VALID + self::INITIALIZE + self::FINALIZE);
75 if (($binaryCalls & self::IS_VALID) && ! $controller->isValid ( $action )) {
76 $controller->onInvalidControl ();
77 } else {
78 if (($binaryCalls & self::INITIALIZE) && $initialize) {
79 $controller->initialize ();
80 }
81 try {
82 $controller->$action ( ...(self::$actionParams) );
83 } catch ( \Error $e ) {
84 if (! \method_exists ( $controller, $action )) {
85 static::onError ( 404, "This action does not exist on the controller " . $ctrl, $controller );
86 } else {
87 static::logError($e->getCode(), $e->getMessage());
88 if (self::$config ['debug']) {
89 throw $e;
90 } else {
91 static::onError ( 500, $e->getMessage (), $controller );
92 }
93 }
94 }
95 if (($binaryCalls & self::FINALIZE) && $finalize) {
96 $controller->finalize ();
97 }
98 }
99 } else {
100 Logger::warn ( 'Startup', 'The controller `' . $ctrl . '` doesn\'t exist! <br/>', 'runAction' );
101 static::onError ( 404 );
102 }
103 } catch ( \Error $eC ) {
104 static::logError($eC->getCode(), $eC->getMessage());
105 if (self::$config ['debug']) {
106 throw $eC;
107 } else {
108 static::onError ( 500, $eC->getMessage () );
109 }
110 }
111 }
112
113 public static function getControllerInstance(string $controllerName): ?object {
114 return self::$controllers [$controllerName] ??= self::_getControllerInstance ( $controllerName );
115 }
116
117 public static function warmupAction(string $controller, string $action = 'index') {
118 \ob_start ();
119 $ru = [ 'controller'=>$controller,'action'=>$action ];
120 static::runAction ( $ru, true, true );
121 \ob_end_clean ();
122 }
123
124 public static function warmupForward(string $url, bool $initialize = true, bool $finalize = true): void {
125 \ob_start ();
126 static::forward( $url, $initialize, $finalize );
127 \ob_end_clean ();
128 }
129}
static getRoute($path, $cachedResponse=true, $debug=false)
Returns the route corresponding to a path.
Definition Router.php:125
static getRoutes()
Returns the array of loaded routes.
Definition Router.php:260
Startup for async platforms (Swoole, Workerman, Roadrunner, php-pm...) Ubiquity\controllers$StartupAs...
static getControllerInstance(string $controllerName)
static forward(string $url, bool $initialize=true, bool $finalize=true)
Forwards to url.
static runAction(array &$u, bool $initialize=true, bool $finalize=true)
Runs an action on a controller.
static warmupForward(string $url, bool $initialize=true, bool $finalize=true)
static warmupAction(string $controller, string $action='index')
Starts the framework.
Definition Startup.php:19
static runCallable(array &$u)
Runs a callback.
Definition Startup.php:208
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Definition Logger.php:14