1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the MIT license. For more information, see |
18
|
|
|
* <https://github.com/baleen/migrations>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Baleen\Cli\Provider; |
22
|
|
|
|
23
|
|
|
use Baleen\Cli\BaseCommand; |
24
|
|
|
use Baleen\Cli\CommandBus\Config\InitHandler; |
25
|
|
|
use Baleen\Cli\CommandBus\Config\InitMessage; |
26
|
|
|
use Baleen\Cli\CommandBus\Config\StatusHandler; |
27
|
|
|
use Baleen\Cli\CommandBus\Config\StatusMessage; |
28
|
|
|
use Baleen\Cli\CommandBus\Factory\DefaultFactory; |
29
|
|
|
use Baleen\Cli\CommandBus\Factory\MessageFactoryInterface; |
30
|
|
|
use Baleen\Cli\CommandBus\Repository\CreateHandler; |
31
|
|
|
use Baleen\Cli\CommandBus\Repository\CreateMessage; |
32
|
|
|
use Baleen\Cli\CommandBus\Repository\LatestHandler as RepositoryLatestHandler; |
33
|
|
|
use Baleen\Cli\CommandBus\Repository\LatestMessage as RepositoryLatestCommand; |
34
|
|
|
use Baleen\Cli\CommandBus\Repository\ListHandler; |
35
|
|
|
use Baleen\Cli\CommandBus\Repository\ListMessage; |
36
|
|
|
use Baleen\Cli\CommandBus\Storage\LatestHandler as StorageLatestHandler; |
37
|
|
|
use Baleen\Cli\CommandBus\Storage\LatestMessage as StorageLatestCommand; |
38
|
|
|
use Baleen\Cli\CommandBus\Timeline\ExecuteHandler; |
39
|
|
|
use Baleen\Cli\CommandBus\Timeline\ExecuteMessage; |
40
|
|
|
use Baleen\Cli\CommandBus\Timeline\MigrateHandler; |
41
|
|
|
use Baleen\Cli\CommandBus\Timeline\MigrateMessage; |
42
|
|
|
use Baleen\Cli\Exception\CliException; |
43
|
|
|
use League\Container\Container; |
44
|
|
|
use League\Container\ContainerInterface; |
45
|
|
|
use League\Container\ServiceProvider; |
46
|
|
|
use League\Tactician\Setup\QuickStart; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Class CommandsProvider. |
50
|
|
|
* |
51
|
|
|
* @author Gabriel Somoza <[email protected]> |
52
|
|
|
*/ |
53
|
|
|
class CommandsProvider extends ServiceProvider |
54
|
|
|
{ |
55
|
|
|
protected $provides = [ |
56
|
|
|
Services::COMMANDS, |
57
|
|
|
Services::COMMAND_BUS, |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** @var array */ |
61
|
|
|
protected $commands = [ |
62
|
|
|
Services::CMD_CONFIG_INIT => [ |
63
|
|
|
'message' => InitMessage::class, |
64
|
|
|
'handler' => InitHandler::class, |
65
|
|
|
], |
66
|
|
|
Services::CMD_CONFIG_STATUS => [ |
67
|
|
|
'message' => StatusMessage::class, |
68
|
|
|
'handler' => StatusHandler::class, |
69
|
|
|
], |
70
|
|
|
Services::CMD_REPOSITORY_CREATE => [ |
71
|
|
|
'message' => CreateMessage::class, |
72
|
|
|
'handler' => CreateHandler::class, |
73
|
|
|
], |
74
|
|
|
Services::CMD_REPOSITORY_LATEST => [ |
75
|
|
|
'message' => RepositoryLatestCommand::class, |
76
|
|
|
'handler' => RepositoryLatestHandler::class, |
77
|
|
|
], |
78
|
|
|
Services::CMD_REPOSITORY_LIST => [ |
79
|
|
|
'message' => ListMessage::class, |
80
|
|
|
'handler' => ListHandler::class, |
81
|
|
|
], |
82
|
|
|
Services::CMD_STORAGE_LATEST => [ |
83
|
|
|
'message' => StorageLatestCommand::class, |
84
|
|
|
'handler' => StorageLatestHandler::class, |
85
|
|
|
], |
86
|
|
|
Services::CMD_TIMELINE_EXECUTE => [ |
87
|
|
|
'message' => ExecuteMessage::class, |
88
|
|
|
'handler' => ExecuteHandler::class, |
89
|
|
|
], |
90
|
|
|
Services::CMD_TIMELINE_MIGRATE => [ |
91
|
|
|
'message' => MigrateMessage::class, |
92
|
|
|
'handler' => MigrateHandler::class, |
93
|
|
|
], |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
*/ |
99
|
|
|
public function __construct() |
100
|
|
|
{ |
101
|
|
|
$this->provides = array_merge($this->provides, array_keys($this->commands)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Use the register method to register items with the container via the |
106
|
|
|
* protected $this->container property or the `getContainer` method |
107
|
|
|
* from the ContainerAwareTrait. |
108
|
|
|
*/ |
109
|
|
|
public function register() |
110
|
|
|
{ |
111
|
|
|
$container = $this->getContainer(); |
112
|
|
|
|
113
|
|
|
$commands = $this->commands; |
114
|
|
|
|
115
|
|
|
// add all message classes to the container |
116
|
|
|
foreach ($commands as $alias => $config) { |
117
|
|
|
$container->add($alias, function (Container $container, $config) { |
118
|
|
|
/** @var MessageFactoryInterface $factory */ |
119
|
|
|
$factory = !empty($config['factory']) ? $config['factory'] : DefaultFactory::class; |
120
|
|
|
$factory = $container->get($factory); |
121
|
|
|
if (!$factory instanceof MessageFactoryInterface) { |
122
|
|
|
throw new CliException(sprintf( |
123
|
|
|
'Expected factory to be an instance of "%s". Got "%s" instead.', |
124
|
|
|
MessageFactoryInterface::class, |
125
|
|
|
is_object($factory) ? get_class($factory) : gettype($factory) |
126
|
|
|
)); |
127
|
|
|
} |
128
|
|
|
return $factory->create($config['message']); |
129
|
|
|
})->withArguments([ContainerInterface::class, $config]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// setup the command bus to know which handler to use for each message class |
133
|
|
|
$container->singleton(Services::COMMAND_BUS, function () use ($commands) { |
134
|
|
|
$map = []; |
135
|
|
|
foreach ($commands as $alias => $config) { |
136
|
|
|
$message = $config['message']; |
137
|
|
|
$handler = $config['handler']; |
138
|
|
|
$map[$message] = new $handler(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return QuickStart::create($map); |
142
|
|
|
}); |
143
|
|
|
|
144
|
|
|
// create a service (that's just an array) that has a list of all the commands for the app |
145
|
|
|
$container->add(Services::COMMANDS, function (ContainerInterface $container) use ($commands) { |
146
|
|
|
$commandList = []; |
147
|
|
|
foreach ($commands as $alias => $config) { |
148
|
|
|
$commandList[] = new BaseCommand($container, $alias, $config['message']); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $commandList; |
152
|
|
|
})->withArgument(ContainerInterface::class); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|