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 Zenify\DoctrineMigrations\CodeStyle\CodeStyle; |
15
|
|
|
use Zenify\DoctrineMigrations\Configuration\Configuration; |
16
|
|
|
use Zenify\DoctrineMigrations\EventSubscriber\ChangeCodingStandardEventSubscriber; |
17
|
|
|
use Zenify\DoctrineMigrations\EventSubscriber\RegisterMigrationsEventSubscriber; |
18
|
|
|
use Zenify\DoctrineMigrations\EventSubscriber\SetConsoleOutputEventSubscriber; |
19
|
|
|
use Zenify\DoctrineMigrations\Exception\DI\MissingExtensionException; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
final class MigrationsExtension extends CompilerExtension |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string[] |
27
|
|
|
*/ |
28
|
|
|
private $defaults = [ |
29
|
|
|
'table' => 'doctrine_migrations', |
30
|
|
|
'column' => 'version', |
31
|
|
|
'directory' => '%appDir%/../migrations', |
32
|
|
|
'namespace' => 'Migrations', |
33
|
|
|
'codingStandard' => CodeStyle::INDENTATION_TABS, |
34
|
|
|
'versionsOrganization' => NULL, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string[] |
39
|
|
|
*/ |
40
|
|
|
private $subscribers = [ |
41
|
|
|
ChangeCodingStandardEventSubscriber::class, |
42
|
|
|
RegisterMigrationsEventSubscriber::class, |
43
|
|
|
SetConsoleOutputEventSubscriber::class, |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
|
47
|
6 |
|
public function loadConfiguration() |
48
|
|
|
{ |
49
|
6 |
|
$this->ensureEventDispatcherExtensionIsRegistered(); |
50
|
|
|
|
51
|
5 |
|
$containerBuilder = $this->getContainerBuilder(); |
52
|
|
|
|
53
|
5 |
|
$this->compiler->parseServices( |
|
|
|
|
54
|
|
|
$containerBuilder, |
55
|
5 |
|
$this->loadFromFile(__DIR__ . '/../config/services.neon') |
|
|
|
|
56
|
|
|
); |
57
|
|
|
|
58
|
5 |
|
foreach ($this->subscribers as $key => $subscriber) { |
59
|
5 |
|
$containerBuilder->addDefinition($this->prefix('listener' . $key)) |
60
|
5 |
|
->setClass($subscriber) |
61
|
5 |
|
->addTag(EventDispatcherExtension::TAG_SUBSCRIBER); |
62
|
|
|
} |
63
|
|
|
|
64
|
5 |
|
$config = $this->getValidatedConfig(); |
65
|
|
|
|
66
|
5 |
|
$containerBuilder->addDefinition($this->prefix('codeStyle')) |
67
|
5 |
|
->setClass(CodeStyle::class) |
68
|
5 |
|
->setArguments([$config['codingStandard']]); |
69
|
|
|
|
70
|
5 |
|
$this->addConfigurationDefinition($config); |
|
|
|
|
71
|
5 |
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
4 |
|
public function beforeCompile() |
78
|
|
|
{ |
79
|
4 |
|
$containerBuilder = $this->getContainerBuilder(); |
80
|
4 |
|
$containerBuilder->prepareClassList(); |
81
|
|
|
|
82
|
4 |
|
$this->setConfigurationToCommands(); |
83
|
4 |
|
$this->loadCommandsToApplication(); |
84
|
4 |
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
5 |
|
private function addConfigurationDefinition(array $config) |
88
|
|
|
{ |
89
|
5 |
|
$containerBuilder = $this->getContainerBuilder(); |
90
|
5 |
|
$configurationDefinition = $containerBuilder->addDefinition($this->prefix('configuration')); |
91
|
|
|
$configurationDefinition |
92
|
5 |
|
->setClass(Configuration::class) |
93
|
5 |
|
->addSetup('setMigrationsTableName', [$config['table']]) |
94
|
5 |
|
->addSetup('setMigrationsColumnName', [$config['column']]) |
95
|
5 |
|
->addSetup('setMigrationsDirectory', [$config['directory']]) |
96
|
5 |
|
->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
|
5 |
|
} |
105
|
|
|
|
106
|
|
|
|
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
|
4 |
|
$commandDefinition->addSetup('setMigrationConfiguration', ['@' . $configurationDefinition->getClass()]); |
114
|
|
|
} |
115
|
4 |
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
4 |
View Code Duplication |
private function loadCommandsToApplication() |
|
|
|
|
119
|
|
|
{ |
120
|
4 |
|
$containerBuilder = $this->getContainerBuilder(); |
121
|
4 |
|
$applicationDefinition = $containerBuilder->getDefinition($containerBuilder->getByType(Application::class)); |
122
|
4 |
|
foreach ($containerBuilder->findByType(AbstractCommand::class) as $name => $commandDefinition) { |
123
|
4 |
|
$applicationDefinition->addSetup('add', ['@' . $name]); |
124
|
|
|
} |
125
|
4 |
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
5 |
|
private function getValidatedConfig() |
132
|
|
|
{ |
133
|
5 |
|
$configuration = $this->getConfig($this->defaults); |
134
|
5 |
|
$this->validateConfig($configuration); |
|
|
|
|
135
|
5 |
|
$configuration['directory'] = $this->getContainerBuilder()->expand($configuration['directory']); |
|
|
|
|
136
|
|
|
|
137
|
5 |
|
return $configuration; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
6 |
|
private function ensureEventDispatcherExtensionIsRegistered() |
142
|
|
|
{ |
143
|
6 |
|
if ( ! $this->compiler->getExtensions(EventDispatcherExtension::class)) { |
144
|
1 |
|
throw new MissingExtensionException( |
145
|
1 |
|
sprintf('Please register required extension "%s" to your config.', EventDispatcherExtension::class) |
146
|
|
|
); |
147
|
|
|
} |
148
|
5 |
|
} |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
This method has been deprecated.