1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\DefinitionParser; |
4
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\DefinitionParserInterface; |
6
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition; |
7
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationStep; |
8
|
|
|
use PhpParser\Error; |
9
|
|
|
use PhpParser\ParserFactory; |
10
|
|
|
|
11
|
|
|
class PHPDefinitionParser implements DefinitionParserInterface |
12
|
|
|
{ |
13
|
|
|
protected $mandatoryInterface = 'Kaliop\eZMigrationBundle\API\MigrationInterface'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Tells whether the given file can be handled by this handler, by checking e.g. the suffix |
17
|
|
|
* |
18
|
|
|
* @param string $migrationName typically a filename |
19
|
|
|
* @return bool |
20
|
|
|
*/ |
21
|
|
|
public function supports($migrationName) |
22
|
|
|
{ |
23
|
|
|
return pathinfo($migrationName, PATHINFO_EXTENSION) == 'php'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Parses a migration definition file, and returns the list of actions to take |
28
|
|
|
* |
29
|
|
|
* @param MigrationDefinition $definition |
30
|
|
|
* @return MigrationDefinition |
31
|
|
|
*/ |
32
|
|
|
public function parseMigrationDefinition(MigrationDefinition $definition) |
33
|
|
|
{ |
34
|
|
|
$status = MigrationDefinition::STATUS_PARSED; |
35
|
|
|
|
36
|
|
|
/// validate that php file is ok, contains a class with good interface |
37
|
|
|
$className = $this->getClassNameFromFile($definition->path); |
38
|
|
|
|
39
|
|
|
if ($className == '') { |
40
|
|
|
$status = MigrationDefinition::STATUS_INVALID; |
41
|
|
|
$message = 'The migration definition file should contain a valid class name. The class name is the part of the filename after the 1st underscore'; |
42
|
|
|
} else { |
43
|
|
|
|
44
|
|
|
// we use smart parsing instead before plain file inclusion, by usage of nikic/php-parser |
45
|
|
|
// this should help with broken php migrations |
46
|
|
|
|
47
|
|
|
$pf = new ParserFactory(); |
48
|
|
|
$parser = $pf->create(ParserFactory::PREFER_PHP7); |
49
|
|
|
try { |
50
|
|
|
$parser->parse(file_get_contents($definition->path)); |
51
|
|
|
|
52
|
|
|
include_once($definition->path); |
53
|
|
|
|
54
|
|
|
if (!class_exists($className)) { |
55
|
|
|
$status = MigrationDefinition::STATUS_INVALID; |
56
|
|
|
$message = "The migration definition file should contain a valid class '$className'"; |
57
|
|
|
} else { |
58
|
|
|
$interfaces = class_implements($className); |
59
|
|
|
if (!in_array($this->mandatoryInterface, $interfaces)) { |
60
|
|
|
$status = MigrationDefinition::STATUS_INVALID; |
61
|
|
|
$message = "The migration definition class '$className' should implement the interface '{$this->mandatoryInterface}'"; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} catch (Error $e) { |
65
|
|
|
$status = MigrationDefinition::STATUS_INVALID; |
66
|
|
|
$message = "The migration definition file '{$definition->path}' is not valid php'"; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
if ($status != MigrationDefinition::STATUS_PARSED) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
return new MigrationDefinition( |
73
|
|
|
$definition->name, |
74
|
|
|
$definition->path, |
75
|
|
|
$definition->rawDefinition, |
76
|
|
|
$status, |
77
|
|
|
array(), |
78
|
|
|
$message |
|
|
|
|
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return new MigrationDefinition( |
83
|
|
|
$definition->name, |
84
|
|
|
$definition->path, |
85
|
|
|
$definition->rawDefinition, |
86
|
|
|
MigrationDefinition::STATUS_PARSED, |
87
|
|
|
array( |
88
|
|
|
new MigrationStep('php', array('class' => $className), array('path' => $definition->path)) |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $fileName |
95
|
|
|
* @return string|null |
96
|
|
|
*/ |
97
|
|
|
protected function getClassNameFromFile($fileName) |
98
|
|
|
{ |
99
|
|
|
$parts = explode('_', pathinfo($fileName, PATHINFO_FILENAME), 2); |
100
|
|
|
return isset($parts[1]) ? $parts[1] : null; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.