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
|
11 |
|
public function __construct(Configuration $configuration, CodeStyleInterface $codeStyle) |
33
|
|
|
{ |
34
|
11 |
|
$this->codeStyle = $codeStyle; |
35
|
11 |
|
$this->configuration = $configuration; |
36
|
11 |
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
9 |
|
public static function getSubscribedEvents() |
43
|
|
|
{ |
44
|
9 |
|
return [ConsoleEvents::TERMINATE => 'applyCodingStyle']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
10 |
|
public function applyCodingStyle(ConsoleTerminateEvent $event) |
49
|
|
|
{ |
50
|
10 |
|
$command = $event->getCommand(); |
51
|
10 |
|
if ( ! $this->isAllowedCommand($command->getName())) { |
52
|
6 |
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
$filename = $this->getCurrentMigrationFileName(); |
56
|
4 |
|
if (file_exists($filename)) { |
57
|
4 |
|
$this->codeStyle->applyForFile($filename); |
58
|
4 |
|
} |
59
|
4 |
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $name |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
10 |
|
private function isAllowedCommand($name) |
67
|
|
|
{ |
68
|
10 |
|
return in_array($name, ['migrations:generate', 'migrations:diff']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
4 |
|
private function getCurrentMigrationFileName() |
76
|
|
|
{ |
77
|
4 |
|
$version = $this->getCurrentVersionName(); |
78
|
|
|
|
79
|
4 |
|
$i = 0; |
80
|
4 |
|
while ( ! file_exists($this->getMigrationFileByVersion($version)) && $i <= 10) { |
81
|
|
|
$version--; |
82
|
|
|
$i++; |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
$path = $this->getMigrationFileByVersion($version); |
86
|
4 |
|
return $path; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
4 |
|
private function getCurrentVersionName() |
94
|
|
|
{ |
95
|
4 |
|
return date('YmdHis'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $version |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
4 |
|
private function getMigrationFileByVersion($version) |
104
|
|
|
{ |
105
|
4 |
|
$migrationDirectoryHelper = new MigrationDirectoryHelper($this->configuration); |
106
|
4 |
|
return $migrationDirectoryHelper->getMigrationDirectory() . '/Version' . $version . '.php'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|