|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Developer\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Magento\Framework\Code\Generator\ClassGenerator; |
|
7
|
|
|
use Magento\Framework\Filesystem\Directory\ReadFactory as DirectoryReadFactory; |
|
8
|
|
|
use Magento\Framework\Filesystem\Directory\WriteFactory as DirectoryWriteFactory; |
|
9
|
|
|
use Magento\Framework\Filesystem\Directory\WriteInterface; |
|
10
|
|
|
use Magento\Framework\Module\Dir as ModuleDir; |
|
11
|
|
|
use N98\Magento\Command\Developer\Console\Structure\ModuleNameStructure; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
use Zend\Code\Generator\FileGenerator; |
|
15
|
|
|
use Zend\Filter\Word\SeparatorToSeparator; |
|
16
|
|
|
|
|
17
|
|
|
abstract class AbstractGeneratorCommand extends AbstractConsoleCommand |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var WriteInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected static $currentModuleDirWriter = null; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param InputInterface $input |
|
26
|
|
|
* @param OutputInterface $output |
|
27
|
|
|
* |
|
28
|
|
|
* @return int|void |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
31
|
|
|
{ |
|
32
|
|
|
try { |
|
33
|
|
|
$this->catchedExecute($input, $output); |
|
34
|
|
|
} catch (Exception $e) { |
|
35
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param InputInterface $input |
|
41
|
|
|
* @param OutputInterface $output |
|
42
|
|
|
* |
|
43
|
|
|
* @return int|void |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function catchedExecute(InputInterface $input, OutputInterface $output) |
|
46
|
|
|
{ |
|
47
|
|
|
/* intentionally left blank, implement or implement execute() */ |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $type |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getCurrentModulePath($type = '') |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->get(ModuleDir::class)->getDir($this->getCurrentModuleName()->getFullModuleName(), $type); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $path |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getCurrentModuleFilePath($path) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->getCurrentModulePath() . DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getCurrentModuleNamespace() |
|
72
|
|
|
{ |
|
73
|
|
|
$moduleName = $this->getCurrentModuleName()->getFullModuleName(); |
|
74
|
|
|
|
|
75
|
|
|
return $this->getModuleNamespace($moduleName); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $moduleName |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getModuleNamespace($moduleName) |
|
84
|
|
|
{ |
|
85
|
|
|
list($vendorPrefix, $moduleNamespace) = explode('_', $moduleName); |
|
86
|
|
|
|
|
87
|
|
|
return $vendorPrefix . '\\' . $moduleNamespace; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return ModuleNameStructure |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getCurrentModuleName() |
|
94
|
|
|
{ |
|
95
|
|
|
try { |
|
96
|
|
|
$magerunInternal = $this->getScopeVariable('magerunInternal'); |
|
97
|
|
|
|
|
98
|
|
|
$currentModuleName = $magerunInternal->currentModule; |
|
99
|
|
|
|
|
100
|
|
|
if (empty($currentModuleName)) { |
|
101
|
|
|
throw new \InvalidArgumentException('No module defined'); |
|
102
|
|
|
} |
|
103
|
|
|
} catch (\InvalidArgumentException $e) { |
|
104
|
|
|
throw new \InvalidArgumentException('Module not defined. Please use "module <name>" command'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return new ModuleNameStructure($currentModuleName); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Returns an id generated by current module name |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getCurrentModuleId() |
|
116
|
|
|
{ |
|
117
|
|
|
return lcfirst(str_replace('_', '', $this->getCurrentModuleName())); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $name |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setCurrentModuleName($name) |
|
124
|
|
|
{ |
|
125
|
|
|
try { |
|
126
|
|
|
$magerunInternal = $this->getScopeVariable('magerunInternal'); |
|
127
|
|
|
} catch (\InvalidArgumentException $e) { |
|
128
|
|
|
$magerunInternal = new \stdClass(); |
|
129
|
|
|
} |
|
130
|
|
|
$magerunInternal->currentModule = $name; |
|
131
|
|
|
$this->setScopeVariable('magerunInternal', $magerunInternal); |
|
132
|
|
|
|
|
133
|
|
|
$this->reset(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param WriteInterface $currentModuleDirWriter |
|
138
|
|
|
*/ |
|
139
|
|
|
public function setCurrentModuleDirectoryWriter(WriteInterface $currentModuleDirWriter) |
|
140
|
|
|
{ |
|
141
|
|
|
self::$currentModuleDirWriter = $currentModuleDirWriter; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return WriteInterface |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getCurrentModuleDirectoryWriter() |
|
148
|
|
|
{ |
|
149
|
|
|
if (self::$currentModuleDirWriter === null) { |
|
150
|
|
|
$directoryWrite = $this->create(DirectoryWriteFactory::class); |
|
151
|
|
|
/** @var $directoryWrite DirectoryWriteFactory */ |
|
152
|
|
|
|
|
153
|
|
|
self::$currentModuleDirWriter = $directoryWrite->create($this->getCurrentModulePath()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return self::$currentModuleDirWriter; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return \Magento\Framework\Filesystem\Directory\Read |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getCurrentModuleDirectoryReader() |
|
163
|
|
|
{ |
|
164
|
|
|
$directoryRead = $this->get(DirectoryWriteFactory::class); |
|
165
|
|
|
|
|
166
|
|
|
/** @var $directoryRead DirectoryReadFactory */ |
|
167
|
|
|
return $directoryRead->create($this->getCurrentModulePath()); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param string $pathArgument |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getNormalizedPathByArgument($pathArgument) |
|
175
|
|
|
{ |
|
176
|
|
|
$namespaceFilterDot = $this->create( |
|
177
|
|
|
SeparatorToSeparator::class, |
|
178
|
|
|
['searchSeparator' => '.', 'replacementSeparator' => DIRECTORY_SEPARATOR] |
|
179
|
|
|
); |
|
180
|
|
|
$namespaceFilterBackslash = $this->create( |
|
181
|
|
|
SeparatorToSeparator::class, |
|
182
|
|
|
['searchSeparator' => '\\', 'replacementSeparator' => DIRECTORY_SEPARATOR] |
|
183
|
|
|
); |
|
184
|
|
|
$pathArgument = $namespaceFilterDot->filter($pathArgument); |
|
185
|
|
|
$pathArgument = $namespaceFilterBackslash->filter($pathArgument); |
|
186
|
|
|
|
|
187
|
|
|
$parts = explode(DIRECTORY_SEPARATOR, $pathArgument); |
|
188
|
|
|
|
|
189
|
|
|
return implode(DIRECTORY_SEPARATOR, array_map('ucfirst', $parts)); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param string $pathArgument |
|
194
|
|
|
* @return string |
|
195
|
|
|
*/ |
|
196
|
|
|
public function getNormalizedClassnameByArgument($pathArgument) |
|
197
|
|
|
{ |
|
198
|
|
|
$namespaceFilterDot = $this->create( |
|
199
|
|
|
SeparatorToSeparator::class, |
|
200
|
|
|
['searchSeparator' => '.', 'replacementSeparator' => '\\'] |
|
201
|
|
|
); |
|
202
|
|
|
$namespaceFilterBackslash = $this->create( |
|
203
|
|
|
SeparatorToSeparator::class, |
|
204
|
|
|
['searchSeparator' => '.', 'replacementSeparator' => '\\'] |
|
205
|
|
|
); |
|
206
|
|
|
|
|
207
|
|
|
$pathArgument = $namespaceFilterDot->filter($pathArgument); |
|
208
|
|
|
$pathArgument = $namespaceFilterBackslash->filter($pathArgument); |
|
209
|
|
|
|
|
210
|
|
|
$parts = explode('\\', $pathArgument); |
|
211
|
|
|
|
|
212
|
|
|
return implode('\\', array_map('ucfirst', $parts)); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param OutputInterface $output |
|
217
|
|
|
* @param ClassGenerator $classGenerator |
|
218
|
|
|
* @param string $filePathToGenerate |
|
219
|
|
|
*/ |
|
220
|
|
|
protected function writeClassToFile( |
|
221
|
|
|
OutputInterface $output, |
|
222
|
|
|
ClassGenerator $classGenerator, |
|
223
|
|
|
$filePathToGenerate |
|
224
|
|
|
) { |
|
225
|
|
|
$fileGenerator = FileGenerator::fromArray( |
|
226
|
|
|
[ |
|
227
|
|
|
'classes' => [$classGenerator], |
|
228
|
|
|
] |
|
229
|
|
|
); |
|
230
|
|
|
|
|
231
|
|
|
$this->getCurrentModuleDirectoryWriter() |
|
232
|
|
|
->writeFile($filePathToGenerate, $fileGenerator->generate()); |
|
233
|
|
|
|
|
234
|
|
|
$output->writeln('<info>generated </info><comment>' . $filePathToGenerate . '</comment>'); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Reset internal caches etc. |
|
239
|
|
|
*/ |
|
240
|
|
|
protected function reset() |
|
241
|
|
|
{ |
|
242
|
|
|
self::$currentModuleDirWriter = null; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|