/
ServiceProvider.php
210 lines (182 loc) · 6.63 KB
/
ServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
namespace Concrete\Core\Console;
use Concrete\Core\Foundation\Service\Provider;
use Concrete\Core\Tools\Console\Doctrine\ConsoleRunner as DeprecatedConsoleRunner;
use Concrete\Core\Updater\Migrations\Configuration as MigrationsConfiguration;
use Doctrine\DBAL\Migrations\OutputWriter;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Output\ConsoleOutput;
class ServiceProvider extends Provider
{
/** @var \Concrete\Core\Console\Application */
protected $cli;
/** @var bool */
protected $installed;
/**
* Commands that are always available.
*
* @var string[]
*/
protected $commands = [
Command\InfoCommand::class,
Command\InstallCommand::class,
Command\InstallLanguageCommand::class,
Command\TranslatePackageCommand::class,
Command\GenerateIDESymbolsCommand::class,
Command\ConfigCommand::class,
Command\PackPackageCommand::class,
Command\ExecCommand::class,
Command\ServiceCommand::class,
Command\ResetCommand::class,
Command\PhpCodingStyleCommand::class,
];
/**
* Commands that only get added when concrete5 is installed.
*
* @var string[]
*/
protected $installedCommands = [
Command\CompareSchemaCommand::class,
Command\ClearCacheCommand::class,
Command\InstallPackageCommand::class,
Command\UninstallPackageCommand::class,
Command\UpdatePackageCommand::class,
Command\InstallThemeCommand::class,
Command\BlacklistClear::class,
Command\JobCommand::class,
Command\RefreshEntitiesCommand::class,
Command\GenerateSitemapCommand::class,
Command\FillThumbnailsTableCommand::class,
Command\RescanFilesCommand::class,
Command\UpdateCommand::class,
Command\SetDatabaseCharacterSetCommand::class,
Command\Express\ExportCommand::class,
Command\FixDatabaseForeignKeys::class,
Command\FixThemeValuesCommand::class,
];
/**
* The commands used for migration. These get an extra MigrationConfiguration object.
*
* @var string[]
*/
protected $migrationCommands = [
\Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand::class,
\Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand::class,
\Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand::class,
\Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand::class,
\Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand::class,
\Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand::class,
];
/**
* Registers the services provided by this provider.
*/
public function register()
{
/*
* @deprecated Use the final ConsoleRunner class directly
* @todo Remove in v9
*/
if (!class_exists(DeprecatedConsoleRunner::class)) {
class_alias(ConsoleRunner::class, DeprecatedConsoleRunner::class);
}
$this->app->extend(Application::class, function (Application $cli) {
$this->cli = $cli;
$this->setupDefaultCommands();
$this->setupDoctrineCommands();
return $cli;
});
}
public function setupDoctrineCommands()
{
if ($this->installed()) {
// Set the doctrine helperset to the CLI
$doctrineHelperSet = $this->app->call([ConsoleRunner::class, 'createHelperSet']);
if ($this->cli->getHelperSet()) {
foreach ($doctrineHelperSet as $key => $helper) {
$this->cli->getHelperSet()->set($helper, $key);
}
} else {
$this->cli->setHelperSet($doctrineHelperSet);
}
// Add Doctrine ConsoleRunner commands
ConsoleRunner::addCommands($this->cli);
// Add migration commands
$migrationsConfiguration = $this->getMigrationConfiguration();
foreach ($this->migrationCommands as $migrationsCommand) {
$command = $this->app->make($migrationsCommand);
$command->setMigrationConfiguration($migrationsConfiguration);
$this->add($command, true);
}
}
}
protected function setupDefaultCommands()
{
// Add commands that always work
$this->add($this->commands);
// Add commands that require install to work
$this->add($this->installedCommands, true);
}
/**
* Add a class to the CLI application.
*
* @param array|string|\Symfony\Component\Console\Command\Command $param
* @param bool $requireInstall
* @param callable|null $callback
*
* @return null|\Symfony\Component\Console\Command\Command
*/
private function add($param, $requireInstall = false, callable $callback = null)
{
// Handle array input
if (is_array($param)) {
foreach ($param as $item) {
$this->add($item, $requireInstall, $callback);
}
return null;
}
// If we're not installed, only register commands that are marked to handle that
if ($requireInstall && !$this->installed()) {
return null;
}
// Inflate the passed command
$command = is_string($param) ? $this->app->make($param) : $param;
// Make sure we have a command instance
if (!$command || !$command instanceof SymfonyCommand) {
throw new \InvalidArgumentException('Invalid command provided.');
}
// Handle callback function
if ($callback) {
$command = $callback($command);
if (!$command) {
return null;
}
}
// Add the cli command
return $this->cli->add($command);
}
/**
* Determine if the app is currently installed.
*
* @return bool
*/
private function installed()
{
if ($this->installed === null) {
$this->installed = $this->app->isInstalled();
}
return $this->installed;
}
/**
* @return \Concrete\Core\Updater\Migrations\Configuration|mixed
*/
private function getMigrationConfiguration()
{
$migrationsConfiguration = $this->app->make(MigrationsConfiguration::class);
$output = $this->app->make(ConsoleOutput::class);
$migrationsConfiguration->setOutputWriter(new OutputWriter(function ($message) use ($output) {
$output->writeln($message);
}));
return $migrationsConfiguration;
}
}