|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Cubiche package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Cubiche |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Cubiche\Core\Bus\Command; |
|
12
|
|
|
|
|
13
|
|
|
use Cubiche\Core\Bus\Bus; |
|
14
|
|
|
use Cubiche\Core\Bus\Exception\NotFoundException; |
|
15
|
|
|
use Cubiche\Core\Bus\MessageInterface; |
|
16
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\CommandHandlerMiddleware; |
|
17
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Locator\InMemoryLocator; |
|
18
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerClass\HandlerClassResolver; |
|
19
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerMethodName\MethodWithShortObjectNameResolver; |
|
20
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\NameOfCommand\ChainResolver as NameOfCommandChainResolver; |
|
21
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\NameOfCommand\FromClassNameResolver; |
|
22
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\NameOfCommand\FromCommandNamedResolver; |
|
23
|
|
|
use Cubiche\Core\Bus\Middlewares\Locking\LockingMiddleware; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* CommandBus class. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class CommandBus extends Bus |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var CommandHandlerMiddleware |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $commandHandlerMiddleware; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return CommandBus |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function create() |
|
41
|
|
|
{ |
|
42
|
|
|
return new static([ |
|
43
|
|
|
250 => new LockingMiddleware(), |
|
44
|
|
|
100 => new CommandHandlerMiddleware(new HandlerClassResolver( |
|
45
|
|
|
new NameOfCommandChainResolver([ |
|
46
|
|
|
new FromCommandNamedResolver(), |
|
47
|
|
|
new FromClassNameResolver(), |
|
48
|
|
|
]), |
|
49
|
|
|
new MethodWithShortObjectNameResolver('Command'), |
|
50
|
|
|
new InMemoryLocator() |
|
51
|
|
|
)), |
|
52
|
|
|
]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function dispatch(MessageInterface $command) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!$command instanceof CommandInterface) { |
|
61
|
|
|
throw new \InvalidArgumentException( |
|
62
|
|
|
sprintf( |
|
63
|
|
|
'The object must be an instance of %s. Instance of %s given', |
|
64
|
|
|
CommandInterface::class, |
|
65
|
|
|
get_class($command) |
|
66
|
|
|
) |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->ensureCommandHandlerMiddleware(); |
|
71
|
|
|
|
|
72
|
|
|
parent::dispatch($command); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Ensure that exists an command handler middleware. |
|
77
|
|
|
* |
|
78
|
|
|
* @throws NotFoundException |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function ensureCommandHandlerMiddleware() |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->commandHandlerMiddleware !== null) { |
|
83
|
|
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
foreach ($this->middlewares as $priority => $collection) { |
|
87
|
|
|
foreach ($collection as $middleware) { |
|
88
|
|
|
if ($middleware instanceof CommandHandlerMiddleware) { |
|
89
|
|
|
$this->commandHandlerMiddleware = $middleware; |
|
90
|
|
|
|
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
throw NotFoundException::middlewareOfType(CommandHandlerMiddleware::class); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return CommandHandlerMiddleware |
|
101
|
|
|
*/ |
|
102
|
|
|
public function handlerMiddleware() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->ensureCommandHandlerMiddleware(); |
|
105
|
|
|
|
|
106
|
|
|
return $this->commandHandlerMiddleware; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $commandName |
|
111
|
|
|
* @param mixed $commandHandler |
|
112
|
|
|
*/ |
|
113
|
|
|
public function addHandler($commandName, $commandHandler) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->ensureCommandHandlerMiddleware(); |
|
116
|
|
|
|
|
117
|
|
|
$this->commandHandlerMiddleware->resolver()->addHandler($commandName, $commandHandler); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $commandName |
|
122
|
|
|
* |
|
123
|
|
|
* @return object |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getHandlerFor($commandName) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->ensureCommandHandlerMiddleware(); |
|
128
|
|
|
|
|
129
|
|
|
return $this->commandHandlerMiddleware->resolver()->getHandlerFor($commandName); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param string $commandName |
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getHandlerMethodFor($commandName) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->ensureCommandHandlerMiddleware(); |
|
140
|
|
|
|
|
141
|
|
|
return $this->commandHandlerMiddleware->resolver()->getHandlerMethodFor($commandName); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|