|
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\EventSubscriber; |
|
9
|
|
|
|
|
10
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Configuration; |
|
11
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Helper\MigrationDirectoryHelper; |
|
12
|
|
|
use Symfony\Component\Console\ConsoleEvents; |
|
13
|
|
|
use Symfony\Component\Console\Event\ConsoleTerminateEvent; |
|
14
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
15
|
|
|
use Zenify\DoctrineMigrations\Contract\CodeStyle\CodeStyleInterface; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
final class ChangeCodingStandardEventSubscriber implements EventSubscriberInterface |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Configuration |
|
23
|
|
|
*/ |
|
24
|
|
|
private $configuration; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var CodeStyleInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $codeStyle; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
6 |
|
public function __construct(Configuration $configuration, CodeStyleInterface $codeStyle) |
|
33
|
|
|
{ |
|
34
|
6 |
|
$this->codeStyle = $codeStyle; |
|
35
|
6 |
|
$this->configuration = $configuration; |
|
36
|
6 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
2 |
|
public static function getSubscribedEvents() |
|
43
|
|
|
{ |
|
44
|
2 |
|
return [ConsoleEvents::TERMINATE => 'applyCodingStyle']; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
6 |
|
public function applyCodingStyle(ConsoleTerminateEvent $event) |
|
49
|
|
|
{ |
|
50
|
6 |
|
$command = $event->getCommand(); |
|
51
|
6 |
|
if ( ! $this->isAllowedCommand($command->getName())) { |
|
52
|
4 |
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
$filename = $this->getCurrentMigrationFileName(); |
|
56
|
2 |
|
if (file_exists($filename)) { |
|
57
|
2 |
|
$this->codeStyle->applyForFile($filename); |
|
58
|
2 |
|
} |
|
59
|
2 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $name |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
6 |
|
private function isAllowedCommand($name) |
|
67
|
|
|
{ |
|
68
|
6 |
|
return in_array($name, ['migrations:generate', 'migrations:diff']); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
2 |
|
private function getCurrentMigrationFileName() |
|
76
|
|
|
{ |
|
77
|
2 |
|
$version = $this->getCurrentVersionName(); |
|
78
|
|
|
|
|
79
|
2 |
|
$i = 0; |
|
80
|
2 |
|
while ( ! file_exists($this->getMigrationFileByVersion($version)) && $i <= 10) { |
|
81
|
|
|
$version--; |
|
82
|
|
|
$i++; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
$path = $this->getMigrationFileByVersion($version); |
|
86
|
2 |
|
return $path; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
2 |
|
private function getCurrentVersionName() |
|
94
|
|
|
{ |
|
95
|
2 |
|
return date('YmdHis'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $version |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
2 |
|
private function getMigrationFileByVersion($version) |
|
104
|
|
|
{ |
|
105
|
2 |
|
$migrationDirectoryHelper = new MigrationDirectoryHelper($this->configuration); |
|
106
|
2 |
|
return $migrationDirectoryHelper->getMigrationDirectory() . '/Version' . $version . '.php'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|