1
|
|
|
<?php |
2
|
|
|
namespace T4web\Migrations\Service; |
3
|
|
|
|
4
|
|
|
use ArrayIterator; |
5
|
|
|
use GlobIterator; |
6
|
|
|
use FilesystemIterator; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
use ReflectionProperty; |
9
|
|
|
use T4web\Migrations\Config; |
10
|
|
|
use T4web\Migrations\MigrationVersion\Table; |
11
|
|
|
use T4web\Migrations\Migration\MigrationInterface; |
12
|
|
|
|
13
|
|
|
class VersionResolver |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Config |
17
|
|
|
*/ |
18
|
|
|
private $config; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Table |
22
|
|
|
*/ |
23
|
|
|
private $versionTable; |
24
|
|
|
|
25
|
|
|
public function __construct(Config $config, $versionTable) |
26
|
|
|
{ |
27
|
|
|
$this->config = $config; |
28
|
|
|
$this->versionTable = $versionTable; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getAll($all = false) |
32
|
|
|
{ |
33
|
|
|
$classes = new ArrayIterator(); |
34
|
|
|
|
35
|
|
|
$iterator = new GlobIterator( |
36
|
|
|
sprintf('%s/Version_*.php', $this->config->getDir()), |
37
|
|
|
FilesystemIterator::KEY_AS_FILENAME |
38
|
|
|
); |
39
|
|
|
foreach ($iterator as $item) { |
40
|
|
|
/** @var $item \SplFileInfo */ |
41
|
|
|
if (preg_match('/(Version_(\d+))\.php/', $item->getFilename(), $matches)) { |
42
|
|
|
$applied = $this->versionTable->applied($matches[2]); |
43
|
|
|
if ($all || !$applied) { |
44
|
|
|
$className = $this->config->getNamespace() . '\\' . $matches[1]; |
45
|
|
|
|
46
|
|
|
if (!class_exists($className)) { /** @noinspection PhpIncludeInspection */ |
47
|
|
|
require_once $this->config->getDir() . '/' . $item->getFilename(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (class_exists($className)) { |
51
|
|
|
$reflectionClass = new ReflectionClass($className); |
52
|
|
|
$reflectionDescription = new ReflectionProperty($className, 'description'); |
53
|
|
|
|
54
|
|
|
if ($reflectionClass->implementsInterface(MigrationInterface::class)) { |
55
|
|
|
$classes->append( |
56
|
|
|
[ |
57
|
|
|
'version' => $matches[2], |
58
|
|
|
'class' => $className, |
59
|
|
|
'description' => $reflectionDescription->getValue(), |
60
|
|
|
'applied' => $applied, |
61
|
|
|
] |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$classes->uasort( |
70
|
|
View Code Duplication |
function ($a, $b) { |
|
|
|
|
71
|
|
|
if ($a['version'] == $b['version']) { |
72
|
|
|
return 0; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return ($a['version'] < $b['version']) ? -1 : 1; |
76
|
|
|
} |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
return $classes; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.