|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Basis\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Basis\Application; |
|
6
|
|
|
use Basis\Config; |
|
7
|
|
|
use Basis\Event; |
|
|
|
|
|
|
8
|
|
|
use Basis\Filesystem; |
|
9
|
|
|
use Basis\Runner; |
|
10
|
|
|
use Exception; |
|
11
|
|
|
|
|
12
|
|
|
class Api |
|
13
|
|
|
{ |
|
14
|
|
|
public function index(Config $config, Runner $runner, Event $event) |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
if (!array_key_exists('rpc', $_REQUEST)) { |
|
17
|
|
|
return [ |
|
18
|
|
|
'success' => false, |
|
19
|
|
|
'message' => 'No rpc defined', |
|
20
|
|
|
]; |
|
21
|
|
|
} |
|
22
|
|
|
$data = json_decode($_REQUEST['rpc']); |
|
23
|
|
|
|
|
24
|
|
|
if (!$data) { |
|
25
|
|
|
return [ |
|
26
|
|
|
'success' => false, |
|
27
|
|
|
'message' => 'Invalid rpc format', |
|
28
|
|
|
]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$request = is_array($data) ? $data : [$data]; |
|
32
|
|
|
|
|
33
|
|
|
$response = []; |
|
34
|
|
|
foreach ($request as $rpc) { |
|
35
|
|
|
$result = $this->process($runner, $rpc); |
|
36
|
|
|
if (property_exists($rpc, 'tid')) { |
|
37
|
|
|
$result['tid'] = $rpc->tid; |
|
38
|
|
|
} |
|
39
|
|
|
$response[] = $result; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
try { |
|
43
|
|
|
$event->fireChanges(); |
|
44
|
|
|
} catch (Exception $e) { |
|
45
|
|
|
return ['success' => false, 'message' => $e->getMessage()]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return is_array($data) ? $response : $response[0]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function process($runner, $rpc) |
|
52
|
|
|
{ |
|
53
|
|
|
if (!property_exists($rpc, 'job')) { |
|
54
|
|
|
return [ |
|
55
|
|
|
'success' => false, |
|
56
|
|
|
'message' => 'Invalid rpc format: no job', |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (!property_exists($rpc, 'params')) { |
|
61
|
|
|
return [ |
|
62
|
|
|
'success' => false, |
|
63
|
|
|
'message' => 'Invalid rpc format: no params', |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
try { |
|
68
|
|
|
$params = is_object($rpc->params) ? get_object_vars($rpc->params) : []; |
|
69
|
|
|
$data = $runner->dispatch($rpc->job, $params); |
|
70
|
|
|
$data = $this->removeSystemObjects($data); |
|
71
|
|
|
|
|
72
|
|
|
return [ |
|
73
|
|
|
'success' => true, |
|
74
|
|
|
'data' => $data, |
|
75
|
|
|
]; |
|
76
|
|
|
} catch (Exception $e) { |
|
77
|
|
|
return [ |
|
78
|
|
|
'success' => false, |
|
79
|
|
|
'message' => $e->getMessage(), |
|
80
|
|
|
'trace' => explode(PHP_EOL, $e->getTraceAsString()), |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function removeSystemObjects($data) |
|
86
|
|
|
{ |
|
87
|
|
|
if (!$data) { |
|
88
|
|
|
return []; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (is_object($data)) { |
|
92
|
|
|
$data = get_object_vars($data); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
foreach ($data as $k => $v) { |
|
96
|
|
|
if (is_array($v) || is_object($v)) { |
|
97
|
|
|
if ($v instanceof Application) { |
|
98
|
|
|
unset($data[$k]); |
|
99
|
|
|
} else { |
|
100
|
|
|
$data[$k] = $this->removeSystemObjects($v); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $data; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: