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