1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hiapi\Core\Endpoint; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use hiapi\endpoints\Module\InOutControl\VO\Collection; |
7
|
|
|
use hiapi\middlewares\CallableHandler; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use hiapi\Core\Endpoint\Middleware\ReduceHandler; |
10
|
|
|
use hiapi\Core\Endpoint\Middleware\RepeatHandler; |
11
|
|
|
use hiapi\endpoints\BuilderFactoryInterface; |
12
|
|
|
use hiqdev\yii\compat\yii; |
13
|
|
|
use League\Tactician\Middleware; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class BuilderFactory |
17
|
|
|
* |
18
|
|
|
* @author Dmytro Naumenko <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class BuilderFactory implements BuilderFactoryInterface |
21
|
|
|
{ |
22
|
|
|
public function endpoint(string $className): EndpointBuilder |
23
|
|
|
{ |
24
|
|
|
return (new EndpointBuilder()) |
25
|
|
|
->name($this->nameFromClassName($className)) |
26
|
|
|
->definedBy($className); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private function nameFromClassName(string $className): string |
30
|
|
|
{ |
31
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
32
|
|
|
return (new ReflectionClass($className))->getShortName(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function call(string $className) |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
|
|
'__class' => CallableHandler::class, |
39
|
|
|
'__construct()' => [yii::referenceTo($className)], |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Middleware|callable|string $handler |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function repeat($handler) |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
'__class' => RepeatHandler::class, |
51
|
|
|
'__construct()' => [$this->prepareHandler($handler)], |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function prepareHandler($handler) |
56
|
|
|
{ |
57
|
|
|
return is_string($handler) ? yii::referenceTo($handler) : $handler; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Middleware|callable|string $handler |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function reduce($handler) |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
'__class' => ReduceHandler::class, |
68
|
|
|
'__construct()' => [$this->prepareHandler($handler)], |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function many(string $className): Collection |
73
|
|
|
{ |
74
|
|
|
return Collection::of($className); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// TODO: move to internal project |
78
|
|
|
public function checkSelf(): Closure |
79
|
|
|
{ |
80
|
|
|
return static function ($command, $next) { |
81
|
|
|
return $next($command); |
82
|
|
|
}; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function input(array $inputOptions): array |
86
|
|
|
{ |
87
|
|
|
// TODO: Take Tafid's Pact PR |
88
|
|
|
return $inputOptions; |
89
|
|
|
} |
90
|
|
|
public function output(array $inputOptions): array |
91
|
|
|
{ |
92
|
|
|
// TODO: Take Tafid's Pact PR |
93
|
|
|
return $inputOptions; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|