1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Zenify |
7
|
|
|
* Copyright (c) 2014 Tomas Votruba (http://tomasvotruba.cz) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Zenify\DoctrineMigrations\DI; |
11
|
|
|
|
12
|
|
|
use Arachne\EventDispatcher\DI\EventDispatcherExtension; |
13
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand; |
14
|
|
|
use Nette\DI\Compiler; |
15
|
|
|
use Nette\DI\CompilerExtension; |
16
|
|
|
use Symfony\Component\Console\Application; |
17
|
|
|
use Zenify\DoctrineMigrations\CodeStyle\CodeStyle; |
18
|
|
|
use Zenify\DoctrineMigrations\Configuration\Configuration; |
19
|
|
|
use Zenify\DoctrineMigrations\EventSubscriber\ChangeCodingStandardEventSubscriber; |
20
|
|
|
use Zenify\DoctrineMigrations\EventSubscriber\RegisterMigrationsEventSubscriber; |
21
|
|
|
use Zenify\DoctrineMigrations\EventSubscriber\SetConsoleOutputEventSubscriber; |
22
|
|
|
use Zenify\DoctrineMigrations\Exception\DI\MissingExtensionException; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
final class MigrationsExtension extends CompilerExtension |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string[] |
30
|
|
|
*/ |
31
|
|
|
private $defaults = [ |
32
|
|
|
'table' => 'doctrine_migrations', |
33
|
|
|
'column' => 'version', |
34
|
|
|
'directory' => '%appDir%/../migrations', |
35
|
|
|
'namespace' => 'Migrations', |
36
|
|
|
'codingStandard' => CodeStyle::INDENTATION_TABS, |
37
|
|
|
'versionsOrganization' => NULL, |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string[] |
42
|
|
|
*/ |
43
|
|
|
private $subscribers = [ |
44
|
|
|
ChangeCodingStandardEventSubscriber::class, |
45
|
|
|
RegisterMigrationsEventSubscriber::class, |
46
|
|
|
SetConsoleOutputEventSubscriber::class, |
47
|
6 |
|
]; |
48
|
|
|
|
49
|
6 |
|
|
50
|
|
|
public function loadConfiguration() |
51
|
5 |
|
{ |
52
|
|
|
$this->ensureEventDispatcherExtensionIsRegistered(); |
53
|
5 |
|
|
54
|
|
|
$containerBuilder = $this->getContainerBuilder(); |
55
|
5 |
|
|
56
|
|
|
Compiler::loadDefinitions( |
57
|
|
|
$containerBuilder, |
58
|
5 |
|
$this->loadFromFile(__DIR__ . '/../config/services.neon')['services'] |
59
|
5 |
|
); |
60
|
5 |
|
|
61
|
5 |
|
foreach ($this->subscribers as $key => $subscriber) { |
62
|
|
|
$containerBuilder->addDefinition($this->prefix('listener' . $key)) |
63
|
|
|
->setClass($subscriber) |
64
|
5 |
|
->addTag(EventDispatcherExtension::TAG_SUBSCRIBER); |
65
|
|
|
} |
66
|
5 |
|
|
67
|
5 |
|
$config = $this->getValidatedConfig(); |
68
|
5 |
|
|
69
|
|
|
$containerBuilder->addDefinition($this->prefix('codeStyle')) |
70
|
5 |
|
->setClass(CodeStyle::class) |
71
|
5 |
|
->setArguments([$config['codingStandard']]); |
72
|
|
|
|
73
|
|
|
$this->addConfigurationDefinition($config); |
74
|
4 |
|
} |
75
|
|
|
|
76
|
4 |
|
|
77
|
4 |
|
public function beforeCompile() |
78
|
|
|
{ |
79
|
4 |
|
$containerBuilder = $this->getContainerBuilder(); |
80
|
4 |
|
$containerBuilder->prepareClassList(); |
81
|
4 |
|
|
82
|
|
|
$this->setConfigurationToCommands(); |
83
|
|
|
$this->loadCommandsToApplication(); |
84
|
5 |
|
} |
85
|
|
|
|
86
|
5 |
|
|
87
|
5 |
|
private function addConfigurationDefinition(array $config) |
88
|
|
|
{ |
89
|
5 |
|
$containerBuilder = $this->getContainerBuilder(); |
90
|
5 |
|
$configurationDefinition = $containerBuilder->addDefinition($this->prefix('configuration')); |
91
|
5 |
|
$configurationDefinition |
92
|
5 |
|
->setClass(Configuration::class) |
93
|
5 |
|
->addSetup('setMigrationsTableName', [$config['table']]) |
94
|
|
|
->addSetup('setMigrationsColumnName', [$config['column']]) |
95
|
5 |
|
->addSetup('setMigrationsDirectory', [$config['directory']]) |
96
|
|
|
->addSetup('setMigrationsNamespace', [$config['namespace']]); |
97
|
|
|
|
98
|
5 |
|
if ($config['versionsOrganization'] === Configuration::VERSIONS_ORGANIZATION_BY_YEAR) { |
99
|
|
|
$configurationDefinition->addSetup('setMigrationsAreOrganizedByYear'); |
100
|
|
|
|
101
|
5 |
|
} elseif ($config['versionsOrganization'] === Configuration::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) { |
102
|
|
|
$configurationDefinition->addSetup('setMigrationsAreOrganizedByYearAndMonth'); |
103
|
|
|
} |
104
|
4 |
|
} |
105
|
|
|
|
106
|
4 |
|
|
107
|
4 |
View Code Duplication |
private function setConfigurationToCommands() |
|
|
|
|
108
|
|
|
{ |
109
|
4 |
|
$containerBuilder = $this->getContainerBuilder(); |
110
|
4 |
|
$configurationDefinition = $containerBuilder->getDefinition($containerBuilder->getByType(Configuration::class)); |
111
|
|
|
|
112
|
4 |
|
foreach ($containerBuilder->findByType(AbstractCommand::class) as $commandDefinition) { |
113
|
|
|
$commandDefinition->addSetup('setMigrationConfiguration', ['@' . $configurationDefinition->getClass()]); |
114
|
|
|
} |
115
|
4 |
|
} |
116
|
|
|
|
117
|
4 |
|
|
118
|
4 |
View Code Duplication |
private function loadCommandsToApplication() |
|
|
|
|
119
|
4 |
|
{ |
120
|
4 |
|
$containerBuilder = $this->getContainerBuilder(); |
121
|
|
|
$applicationDefinition = $containerBuilder->getDefinition($containerBuilder->getByType(Application::class)); |
122
|
4 |
|
foreach ($containerBuilder->findByType(AbstractCommand::class) as $name => $commandDefinition) { |
123
|
|
|
$applicationDefinition->addSetup('add', ['@' . $name]); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
5 |
|
private function getValidatedConfig() : array |
129
|
|
|
{ |
130
|
5 |
|
$configuration = $this->getConfig($this->defaults); |
131
|
5 |
|
$this->validateConfig($configuration); |
|
|
|
|
132
|
5 |
|
$configuration['directory'] = $this->getContainerBuilder()->expand($configuration['directory']); |
|
|
|
|
133
|
|
|
|
134
|
5 |
|
return $configuration; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
6 |
|
private function ensureEventDispatcherExtensionIsRegistered() |
139
|
|
|
{ |
140
|
6 |
|
if ( ! $this->compiler->getExtensions(EventDispatcherExtension::class)) { |
141
|
1 |
|
throw new MissingExtensionException( |
142
|
1 |
|
sprintf('Please register required extension "%s" to your config.', EventDispatcherExtension::class) |
143
|
|
|
); |
144
|
|
|
} |
145
|
5 |
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.