|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\log\Logger; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Startup for async platforms (Swoole, Workerman, Roadrunner, php-pm...) |
|
9
|
|
|
* Ubiquity\controllers$StartupAsync |
|
10
|
|
|
* This class is part of Ubiquity |
|
11
|
|
|
* |
|
12
|
|
|
* @author jcheron <[email protected]> |
|
13
|
|
|
* @version 1.0.2 |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
class 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
|
|
|
public static function runAction(array &$u, $initialize = true, $finalize = true): void { |
|
23
|
|
|
self::$controller = $ctrl = $u [0]; |
|
24
|
|
|
self::$action = $action = $u [1] ?? 'index'; |
|
25
|
|
|
self::$actionParams = \array_slice ( $u, 2 ); |
|
26
|
|
|
|
|
27
|
|
|
try { |
|
28
|
|
|
if (null !== $controller = self::getControllerInstance ( $ctrl )) { |
|
29
|
|
|
$binaryCalls = $controller->_binaryCalls ?? (self::IS_VALID + self::INITIALIZE + self::FINALIZE); |
|
30
|
|
|
if (($binaryCalls & self::IS_VALID) && ! $controller->isValid ( $action )) { |
|
31
|
|
|
$controller->onInvalidControl (); |
|
32
|
|
|
} else { |
|
33
|
|
|
if (($binaryCalls & self::INITIALIZE) && $initialize) { |
|
34
|
|
|
$controller->initialize (); |
|
35
|
|
|
} |
|
36
|
|
|
try { |
|
37
|
|
|
$controller->$action ( ...(self::$actionParams) ); |
|
38
|
|
|
} catch ( \Error $e ) { |
|
39
|
|
|
if (! \method_exists ( $controller, $action )) { |
|
40
|
|
|
static::onError ( 404, "This action does not exist on the controller " . $ctrl, $controller ); |
|
41
|
|
|
} else { |
|
42
|
|
|
Logger::warn ( 'Startup', $e->getTraceAsString (), 'runAction' ); |
|
43
|
|
|
if (self::$config ['debug']) { |
|
44
|
|
|
throw $e; |
|
45
|
|
|
} else { |
|
46
|
|
|
static::onError ( 500, $e->getMessage (), $controller ); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
if (($binaryCalls & self::FINALIZE) && $finalize) { |
|
51
|
|
|
$controller->finalize (); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} else { |
|
55
|
|
|
Logger::warn ( 'Startup', 'The controller `' . $ctrl . '` doesn\'t exists! <br/>', 'runAction' ); |
|
56
|
|
|
static::onError ( 404 ); |
|
57
|
|
|
} |
|
58
|
|
|
} catch ( \Error $eC ) { |
|
59
|
|
|
Logger::warn ( 'Startup', $eC->getTraceAsString (), 'runAction' ); |
|
60
|
|
|
if (self::$config ['debug']) { |
|
61
|
|
|
throw $eC; |
|
62
|
|
|
} else { |
|
63
|
|
|
static::onError ( 500, $eC->getMessage () ); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public static function getControllerInstance($controllerName): ?object { |
|
69
|
|
|
return self::$controllers [$controllerName] ??= self::_getControllerInstance ( $controllerName ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public static function warmupAction($controller, $action = 'index') { |
|
73
|
|
|
ob_start (); |
|
74
|
|
|
$ru = [ $controller,$action ]; |
|
75
|
|
|
static::runAction ( $ru, true, true ); |
|
76
|
|
|
ob_end_clean (); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|