1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MageTest\PhpSpec\MagentoExtension\Listener; |
4
|
|
|
|
5
|
|
|
use MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator\Xml\ConfigGenerator; |
6
|
|
|
use MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator\Xml\ModuleGenerator; |
7
|
|
|
use MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator\Xml\XmlGeneratorException; |
8
|
|
|
use MageTest\PhpSpec\MagentoExtension\Util\ClassDetector; |
9
|
|
|
use PhpSpec\Console\ConsoleIO as IO; |
10
|
|
|
use PhpSpec\Event\ExampleEvent; |
11
|
|
|
use PhpSpec\Event\SuiteEvent; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
13
|
|
|
use PhpSpec\Exception\Fracture\ClassNotFoundException as PhpSpecClassException; |
14
|
|
|
use Prophecy\Exception\Doubler\ClassNotFoundException as ProphecyClassException; |
15
|
|
|
|
16
|
|
|
class ModuleUpdateListener implements EventSubscriberInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $classNames = array(); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ModuleGenerator |
25
|
|
|
*/ |
26
|
|
|
private $moduleGenerator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ConfigGenerator |
30
|
|
|
*/ |
31
|
|
|
private $configGenerator; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var IO |
35
|
|
|
*/ |
36
|
|
|
private $io; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ClassDetector |
40
|
|
|
*/ |
41
|
|
|
private $detector; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param ModuleGenerator $moduleGenerator |
45
|
|
|
* @param ConfigGenerator $configGenerator |
46
|
|
|
* @param IO $io |
47
|
|
|
* @param ClassDetector $detector |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
ModuleGenerator $moduleGenerator, |
51
|
|
|
ConfigGenerator $configGenerator, |
52
|
|
|
IO $io, |
53
|
|
|
ClassDetector $detector |
54
|
|
|
) { |
55
|
|
|
$this->moduleGenerator = $moduleGenerator; |
56
|
|
|
$this->configGenerator = $configGenerator; |
57
|
|
|
$this->io = $io; |
58
|
|
|
$this->detector = $detector; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public static function getSubscribedEvents() |
65
|
|
|
{ |
66
|
|
|
return array( |
67
|
|
|
'afterExample' => array('getClassNameAfterExample', 10), |
68
|
|
|
'afterSuite' => array('createXmlAfterSuite', -20), |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param ExampleEvent $event |
74
|
|
|
*/ |
75
|
|
|
public function getClassNameAfterExample(ExampleEvent $event) |
76
|
|
|
{ |
77
|
|
|
$exception = $event->getException(); |
78
|
|
|
|
79
|
|
|
if ($this->exceptionIsNotUsable($exception)) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$className = $exception->getClassname(); |
84
|
|
|
|
85
|
|
|
if (strlen($className)) { |
86
|
|
|
$parts = explode('_', $className); |
87
|
|
|
if (!isset($parts[0]) || !isset($parts[1])) { |
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
$this->classNames[$className] = $parts[0] . '_' . $parts[1]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param SuiteEvent $event |
96
|
|
|
*/ |
97
|
|
|
public function createXmlAfterSuite(SuiteEvent $event) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
if (!$this->io->isCodeGenerationEnabled()) { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
foreach ($this->classNames as $className => $moduleName) { |
104
|
|
|
if (!$this->detector->classExists($className)) { |
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->moduleGenerator->generate(($moduleName)); |
109
|
|
|
|
110
|
|
|
$this->configGenerator->generateElement( |
111
|
|
|
$this->getClassType($className), |
112
|
|
|
$moduleName |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $className |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
private function getClassType($className) |
122
|
|
|
{ |
123
|
|
|
$parts = explode('_', $className); |
124
|
|
|
if (!isset($parts[2])) { |
125
|
|
|
throw new XmlGeneratorException('Could not determine an object type from ' . $className); |
126
|
|
|
} |
127
|
|
|
if ($this->partIsController($parts[count($parts)-1])) { |
128
|
|
|
return 'controller'; |
129
|
|
|
} |
130
|
|
|
return strtolower($parts[2]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $part |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
private function partIsController($part) |
138
|
|
|
{ |
139
|
|
|
$element = 'Controller'; |
140
|
|
|
return strlen($part) - strlen($element) === strrpos($part, $element); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param \Exception $exception |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
protected function exceptionIsNotUsable($exception) |
148
|
|
|
{ |
149
|
|
|
if (null === $exception) { |
150
|
|
|
return true; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (!($exception instanceof PhpSpecClassException || $exception instanceof ProphecyClassException)) { |
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.