1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hiapi\Core\Endpoint; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
7
|
|
|
use hiapi\Core\Endpoint\Middleware\CheckPermissionsMiddleware; |
8
|
|
|
use hiapi\Core\Endpoint\Middleware\TypeCheckMiddleware; |
9
|
|
|
use hiapi\exceptions\ConfigurationException; |
10
|
|
|
use hiapi\middlewares\CallableHandler; |
11
|
|
|
use hiapi\middlewares\ValidateCommandMiddleware; |
12
|
|
|
use hiqdev\yii\compat\yii; |
13
|
|
|
use League\Tactician\CommandBus; |
14
|
|
|
use League\Tactician\Middleware; |
15
|
|
|
use Psr\Container\ContainerInterface; |
16
|
|
|
use yii\base\Model; |
17
|
|
|
use yii\web\User; |
18
|
|
|
|
19
|
|
|
class EndpointProcessor |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Endpoint |
23
|
|
|
*/ |
24
|
|
|
private $endpoint; |
25
|
|
|
/** |
26
|
|
|
* @var ContainerInterface |
27
|
|
|
*/ |
28
|
|
|
private $di; |
29
|
|
|
|
30
|
|
|
public function __construct(ContainerInterface $di) |
31
|
|
|
{ |
32
|
|
|
$this->di = $di; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $command |
37
|
|
|
* @param Endpoint $endpoint |
38
|
|
|
* @return Model|ArrayCollection |
39
|
|
|
*/ |
40
|
|
|
public function __invoke($command, Endpoint $endpoint) |
41
|
|
|
{ |
42
|
|
|
$this->endpoint = $endpoint; |
43
|
|
|
|
44
|
|
|
$middlewares = []; |
45
|
|
|
$middlewares[] = new TypeCheckMiddleware($endpoint); |
46
|
|
|
$middlewares[] = new CheckPermissionsMiddleware($endpoint, $this->di->get(User::class)); |
47
|
|
|
$middlewares[] = new ValidateCommandMiddleware(); |
48
|
|
|
$middlewares = array_merge($middlewares, $this->endpointMiddlewares()); |
49
|
|
|
|
50
|
|
|
$result = (new CommandBus($middlewares))->handle($command); |
|
|
|
|
51
|
|
|
|
52
|
|
|
return $result; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Produces array of middlewares. |
57
|
|
|
* |
58
|
|
|
* TODO: Maybe, the Endpoint must prepare the middlewares array itself? |
59
|
|
|
* |
60
|
|
|
* @return Middleware[] |
61
|
|
|
*/ |
62
|
|
|
private function endpointMiddlewares(): array |
63
|
|
|
{ |
64
|
|
|
return array_map(function ($item): Middleware { |
65
|
|
|
if (is_string($item)) { |
66
|
|
|
$item = $this->di->get($item); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (is_array($item)) { |
70
|
|
|
$item = yii::createObject($item); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($item instanceof Middleware) { |
74
|
|
|
return $item; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (is_object($item) && is_callable($item)) { |
78
|
|
|
$item = Closure::fromCallable($item); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($item instanceof \Closure) { |
82
|
|
|
return new CallableHandler($item); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
throw new ConfigurationException(sprintf('Do not know how to instantiate %s', (string)$item)); |
86
|
|
|
}, $this->endpoint->middlewares); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: