1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saltwater\App\Provider; |
4
|
|
|
|
5
|
|
|
use Saltwater\Bus\Water\Chain; |
6
|
|
|
use Saltwater\Salt\Provider; |
7
|
|
|
use Saltwater\Server as S; |
8
|
|
|
use Saltwater\Utils as U; |
9
|
|
|
use Saltwater\Salt\Service; |
10
|
|
|
use Saltwater\Salt\Context; |
11
|
|
|
|
12
|
|
|
class ServiceChain extends Provider |
13
|
|
|
{ |
14
|
|
|
private $chain; |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->chain = new Chain(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function resolve($input, $result = null) |
22
|
|
|
{ |
23
|
|
|
$length = count($this->chain); |
24
|
|
|
|
25
|
|
|
$service = new Service; |
26
|
|
|
|
27
|
|
|
for ($i = 0; $i < $length; ++$i) { |
28
|
|
|
$result = $this->chain( |
29
|
|
|
$service, $this->chain[$i], $input, $result, ($i == $length - 1) |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $result; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Service $service |
38
|
|
|
* @param object $item |
39
|
|
|
* @param mixed $input |
40
|
|
|
* @param mixed $result |
41
|
|
|
* @param bool $last |
42
|
|
|
* |
43
|
|
|
* @return mixed|null |
44
|
|
|
*/ |
45
|
|
|
private function chain(&$service, &$item, $input, $result, $last) |
46
|
|
|
{ |
47
|
|
|
$item->context->pushData($result); |
48
|
|
|
|
49
|
|
|
$service->setContext($item->context); |
50
|
|
|
|
51
|
|
|
if (!$service->prepareCall($item)) { |
52
|
|
|
$service = S::$n->service->get($item->service, $item->context); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $service->call($item, $last ? $input : null); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Context $context |
60
|
|
|
* @param string $cmd |
61
|
|
|
* @param array $path |
62
|
|
|
*/ |
63
|
|
|
protected function explode($context, $cmd, $path) |
64
|
|
|
{ |
65
|
|
|
$root = array_shift($path); |
66
|
|
|
|
67
|
|
|
// This is for simple commands upon an established service |
68
|
|
|
if (empty($path) && !empty($this->chain)) { |
69
|
|
|
$this->push($context, $cmd, $root); |
70
|
|
|
|
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($c = S::$n->context->get($root, $context)) { |
75
|
|
|
$context = $c; |
76
|
|
|
|
77
|
|
|
$root = array_shift($path); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->explodePush($path, $context, $cmd, $root); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Context $context |
85
|
|
|
* @param string $cmd |
86
|
|
|
*/ |
87
|
|
|
private function explodePush($path, $context, $cmd, $root) |
88
|
|
|
{ |
89
|
|
|
$next = array_shift($path); |
90
|
|
|
|
91
|
|
|
// Either push a call on the last service or a new one into the chain |
92
|
|
|
if (empty($path)) { |
93
|
|
|
$this->push($context, $cmd, $root, $next); |
94
|
|
|
} else { |
95
|
|
|
$this->push($context, 'get', $root, $next); |
96
|
|
|
|
97
|
|
|
// We have leftovers! |
98
|
|
|
$this->explode($context, $cmd, $path); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Context $context |
104
|
|
|
* @param string $cmd |
105
|
|
|
* @param string $service |
106
|
|
|
* @param string $path |
107
|
|
|
*/ |
108
|
|
|
protected function push($context, $cmd, $service, $path = null) |
109
|
|
|
{ |
110
|
|
|
$method = $service; |
111
|
|
|
|
112
|
|
|
if (!empty($path) && !is_numeric($path)) { |
113
|
|
|
$method = $path; |
114
|
|
|
|
115
|
|
|
$path = null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->chain[] = (object) array( |
119
|
|
|
'context' => $context, |
120
|
|
|
'http' => $cmd, |
121
|
|
|
'service' => $service, |
122
|
|
|
'method' => $method, |
123
|
|
|
'function' => $cmd . U::dashedToCamelCase($method), |
124
|
|
|
'path' => $path |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|