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