|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\log\Logger; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Startup for async platforms (Swoole, Workerman, php-pm...) |
|
9
|
|
|
* Ubiquity\controllers$StartupAsync |
|
10
|
|
|
* This class is part of Ubiquity |
|
11
|
|
|
* |
|
12
|
|
|
* @author jcheron <[email protected]> |
|
13
|
|
|
* @version 1.0.0 |
|
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
|
|
|
Logger::warn ( 'Startup', $e->getTraceAsString (), 'runAction' ); |
|
40
|
|
|
if (self::$config ['debug']) { |
|
41
|
|
|
throw $e; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
if (($binaryCalls & self::FINALIZE) && $finalize) { |
|
45
|
|
|
$controller->finalize (); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} catch ( \Error $eC ) { |
|
50
|
|
|
Logger::warn ( 'Startup', $eC->getTraceAsString (), 'runAction' ); |
|
51
|
|
|
if (self::$config ['debug']) { |
|
52
|
|
|
throw $eC; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function getControllerInstance($controllerName): ?object { |
|
58
|
|
|
if (! isset ( self::$controllers [$controllerName] )) { |
|
59
|
|
|
return self::$controllers [$controllerName] = self::_getControllerInstance ( $controllerName ); |
|
60
|
|
|
} |
|
61
|
|
|
return self::$controllers [$controllerName]; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|