1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saltwater\Salt; |
4
|
|
|
|
5
|
|
|
use Saltwater\Server as S; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Services encapsulate application logic |
9
|
|
|
* |
10
|
|
|
* @package Saltwater\Salt |
11
|
|
|
*/ |
12
|
|
|
class Service |
13
|
|
|
{ |
14
|
|
|
/** @var Context */ |
15
|
|
|
protected $context = null; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
protected $module = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Context $context |
22
|
|
|
* @param string $module |
23
|
|
|
* |
24
|
|
|
* @return void |
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
public function __construct($context = null, $module = null) |
27
|
|
|
{ |
28
|
|
|
$this->setContext($context); |
|
|
|
|
29
|
|
|
|
30
|
|
|
if (is_null($module) && !empty($context->module)) { |
31
|
|
|
$module = $context->module->getName(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->setModule($module); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Context $context |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function setContext($context) |
43
|
|
|
{ |
44
|
|
|
$this->context = $context; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $module |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function setModule($module) |
53
|
|
|
{ |
54
|
|
|
$this->module = $module; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Ensure that a method can be called within this service |
59
|
|
|
* |
60
|
|
|
* @param string $method |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function isCallable($method) |
65
|
|
|
{ |
66
|
|
|
return method_exists($this, $method); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Prepare the calling of a method |
71
|
|
|
* |
72
|
|
|
* @param object $call |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function prepareCall($call) |
77
|
|
|
{ |
78
|
|
|
return $this->isCallable($call->function); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Attempt to execute a call on this service |
83
|
|
|
* |
84
|
|
|
* @param object $call |
85
|
|
|
* @param mixed $data |
86
|
|
|
* |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
|
public function call($call, $data = null) |
90
|
|
|
{ |
91
|
|
|
if (!$this->isCallable($call->function)) { |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->executeCall($call, $call->function, $data); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Execute a call |
100
|
|
|
* |
101
|
|
|
* @param object $call |
102
|
|
|
* @param string $method |
103
|
|
|
* @param mixed $data |
104
|
|
|
* |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
|
|
protected function executeCall($call, $method, $data) |
108
|
|
|
{ |
109
|
|
|
$reflect = new \ReflectionMethod($this, $method); |
110
|
|
|
|
111
|
|
|
// Check whether we need to inject parameters |
112
|
|
|
if ($reflect->getNumberOfParameters()) { |
113
|
|
|
return call_user_func_array( |
114
|
|
|
array($this, $method), |
115
|
|
|
$this->getMethodArgs($reflect, $call->path, $data) |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// No parameter assembly necessary |
120
|
|
|
return call_user_func(array($this, $method)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Assemble injected method parameters |
125
|
|
|
* |
126
|
|
|
* Note: $path and $data are reserved parameters |
127
|
|
|
* |
128
|
|
|
* @param \ReflectionMethod $reflect |
129
|
|
|
* @param string $path |
130
|
|
|
* @param mixed $data |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
private function getMethodArgs($reflect, $path, $data) |
135
|
|
|
{ |
136
|
|
|
$args = array(); |
137
|
|
|
foreach ($reflect->getParameters() as $parameter) { |
138
|
|
|
$name = $parameter->getName(); |
139
|
|
|
|
140
|
|
|
if ($name == 'path') { |
141
|
|
|
$args[] = $path; |
142
|
|
|
continue; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($name == 'data') { |
146
|
|
|
$args[] = $data; |
147
|
|
|
continue; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$args[] = S::$n->provider($name, $this->module); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $args; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.