-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathApplication.php
More file actions
226 lines (191 loc) · 8.1 KB
/
Copy pathApplication.php
File metadata and controls
226 lines (191 loc) · 8.1 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
declare(strict_types=1);
namespace PHPCensor\Console;
use Exception;
use Monolog\Handler\HandlerInterface;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Phinx\Config\Config as PhinxConfig;
use Phinx\Console\Command\Create;
use Phinx\Console\Command\Migrate;
use Phinx\Console\Command\Rollback;
use Phinx\Console\Command\Status;
use PHPCensor\BuildFactory;
use PHPCensor\Command\AddSecretCommand;
use PHPCensor\Command\CheckLocalizationsCommand;
use PHPCensor\Command\CreateAdminCommand;
use PHPCensor\Command\CreateBuildCommand;
use PHPCensor\Command\InstallCommand;
use PHPCensor\Command\RemoveOldBuildsCommand;
use PHPCensor\Command\RebuildQueueCommand;
use PHPCensor\Command\WorkerCommand;
use PHPCensor\Common\Application\ConfigurationInterface;
use PHPCensor\DatabaseManager;
use PHPCensor\Common\Exception\InvalidArgumentException;
use PHPCensor\Logging\AnsiFormatter;
use PHPCensor\Logging\Handler;
use PHPCensor\Service\BuildService;
use PHPCensor\Store\BuildStore;
use PHPCensor\Store\ProjectStore;
use PHPCensor\Store\SecretStore;
use PHPCensor\Store\UserStore;
use PHPCensor\StoreRegistry;
use Symfony\Component\Console\Application as BaseApplication;
/**
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class Application extends BaseApplication
{
private const LOGO = <<<'LOGO'
____ __ ______ ______
/ __ \/ / / / __ \ / ____/__ ____ _________ _____
/ /_/ / /_/ / /_/ / / / / _ \/ __ \/ ___/ __ \/ ___/
/ ____/ __ / ____/ / /___/ __/ / / (__ ) /_/ / /
/_/ /_/ /_/_/ \____/\___/_/ /_/____/\____/_/
LOGO;
private ConfigurationInterface $configuration;
private DatabaseManager $databaseManager;
private StoreRegistry $storeRegistry;
/**
* @throws Exception
*/
protected function initLogger(ConfigurationInterface $applicationConfig): Logger
{
$rotate = (bool)$applicationConfig->get('php-censor.log.rotate', false);
$maxFiles = (int)$applicationConfig->get('php-censor.log.max_files', 0);
/** @var HandlerInterface[] $loggerHandlers */
$loggerHandlers = [];
if ($rotate) {
$loggerHandlers[] = new RotatingFileHandler(RUNTIME_DIR . 'console.log', $maxFiles, Logger::DEBUG);
} else {
$loggerHandlers[] = new StreamHandler(RUNTIME_DIR . 'console.log', Logger::DEBUG);
}
$loggerHandlers[0]->setFormatter(new AnsiFormatter());
$logger = new Logger('php-censor', $loggerHandlers);
Handler::register($logger);
return $logger;
}
public function __construct(
ConfigurationInterface $configuration,
DatabaseManager $databaseManager,
StoreRegistry $storeRegistry,
string $name = 'PHP Censor',
string $version = 'UNKNOWN'
) {
$version = \trim(\file_get_contents(ROOT_DIR . 'VERSION.md'));
$version = !empty($version) ? $version : '0.0.0 (UNKNOWN)';
parent::__construct($name, $version);
$this->configuration = $configuration;
$this->databaseManager = $databaseManager;
$this->storeRegistry = $storeRegistry;
$oldDatabaseSettings = $this->configuration->get('b8.database', []);
$databaseSettings = $this->configuration->get('php-censor.database', []);
if ($oldDatabaseSettings && !$databaseSettings) {
throw new InvalidArgumentException(
'Missing database settings in application config "config.yml" (Section: "php-censor.database")'
);
}
$phinxSettings = [];
if ($databaseSettings) {
$phinxSettings = [
'paths' => [
'migrations' => ROOT_DIR . 'src/Migrations',
],
'environments' => [
'default_migration_table' => 'migrations',
'default_database' => 'php-censor',
'php-censor' => [
'adapter' => $databaseSettings['type'],
'host' => $databaseSettings['servers']['write'][0]['host'],
'name' => $databaseSettings['name'],
'user' => $databaseSettings['username'],
'pass' => $databaseSettings['password'],
],
],
];
}
if (!empty($databaseSettings['port'])) {
$phinxSettings['environments']['php-censor']['port'] =
(int)$databaseSettings['port'];
}
if (!empty($databaseSettings['servers']['write'][0]['port'])) {
$phinxSettings['environments']['php-censor']['port'] =
(int)$databaseSettings['servers']['write'][0]['port'];
}
if (!empty($databaseSettings['type'])
&& $databaseSettings['type'] === 'pgsql'
) {
if (!\array_key_exists('pgsql-sslmode', $databaseSettings['servers']['write'][0])) {
$databaseSettings['servers']['write'][0]['pgsql-sslmode'] = 'prefer';
}
$phinxSettings['environments']['php-censor']['host'] .=
';sslmode=' . $databaseSettings['servers']['write'][0]['pgsql-sslmode'];
}
$phinxConfig = new PhinxConfig($phinxSettings);
$this->add(
(new Create())
->setConfig($phinxConfig)
->setName('php-censor-migrations:create')
);
$this->add(
(new Migrate())
->setConfig($phinxConfig)
->setName('php-censor-migrations:migrate')
);
$this->add(
(new Rollback())
->setConfig($phinxConfig)
->setName('php-censor-migrations:rollback')
);
$this->add(
(new Status())
->setConfig($phinxConfig)
->setName('php-censor-migrations:status')
);
/** @var UserStore $userStore */
$userStore = $this->storeRegistry->get('User');
/** @var ProjectStore $projectStore */
$projectStore = $this->storeRegistry->get('Project');
/** @var BuildStore $buildStore */
$buildStore = $this->storeRegistry->get('Build');
/** @var SecretStore $secretStore */
$secretStore = $this->storeRegistry->get('Secret');
$buildFactory = new BuildFactory(
$this->configuration,
$this->storeRegistry
);
$buildService = new BuildService(
$this->configuration,
$this->storeRegistry,
$buildFactory,
$buildStore,
$projectStore
);
$logger = $this->initLogger($this->configuration);
$this->add(new InstallCommand($this->configuration, $this->databaseManager, $this->storeRegistry, $logger));
$this->add(new CreateAdminCommand($this->configuration, $this->databaseManager, $this->storeRegistry, $logger, $userStore));
$this->add(new CreateBuildCommand($this->configuration, $this->databaseManager, $this->storeRegistry, $logger, $projectStore, $buildService));
$this->add(new RemoveOldBuildsCommand($this->configuration, $this->databaseManager, $this->storeRegistry, $logger, $projectStore, $buildService));
$this->add(new WorkerCommand($this->configuration, $this->databaseManager, $this->storeRegistry, $logger, $buildService, $buildFactory));
$this->add(new RebuildQueueCommand($this->configuration, $this->databaseManager, $this->storeRegistry, $logger));
$this->add(new CheckLocalizationsCommand($this->configuration, $this->databaseManager, $this->storeRegistry, $logger));
$this->add(new AddSecretCommand($this->configuration, $this->databaseManager, $this->storeRegistry, $logger, $secretStore));
}
public function getHelp(): string
{
return self::LOGO . parent::getHelp();
}
/**
* Returns the long version of the application.
*
* @return string The long application version
*/
public function getLongVersion(): string
{
return \sprintf('<info>%s</info> v%s', $this->getName(), $this->getVersion());
}
}