1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Developer\Console; |
4
|
|
|
|
5
|
|
|
use Magento\Framework\Code\Generator\ClassGenerator; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Zend\Code\Generator\FileGenerator; |
10
|
|
|
|
11
|
|
|
class MakeHelperCommand extends AbstractGeneratorCommand |
12
|
|
|
{ |
13
|
|
|
protected function configure() |
14
|
|
|
{ |
15
|
|
|
$this |
16
|
|
|
->setName('make:helper') |
17
|
|
|
->addArgument('classpath', InputArgument::REQUIRED) |
18
|
|
|
->setDescription('Creates a helper class'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param InputInterface $input |
23
|
|
|
* @param OutputInterface $output |
24
|
|
|
* |
25
|
|
|
* @return int|void |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
protected function catchedExecute(InputInterface $input, OutputInterface $output) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$classFileName = $this->getNormalizedPathByArgument($input->getArgument('classpath')); |
30
|
|
|
$classNameToGenerate = $this->getCurrentModuleNamespace() |
31
|
|
|
. '\\Helper\\' |
32
|
|
|
. $this->getNormalizedClassnameByArgument($input->getArgument('classpath')); |
33
|
|
|
$filePathToGenerate = 'Helper/' . $classFileName . '.php'; |
34
|
|
|
|
35
|
|
|
$classGenerator = $this->create(ClassGenerator::class); |
36
|
|
|
|
37
|
|
|
/** @var $classGenerator ClassGenerator */ |
38
|
|
|
$classGenerator->setExtendedClass('AbstractHelper'); |
39
|
|
|
$classGenerator->addUse('Magento\Framework\App\Helper\AbstractHelper'); |
40
|
|
|
|
41
|
|
|
$classGenerator->setName($classNameToGenerate); |
42
|
|
|
|
43
|
|
|
$fileGenerator = FileGenerator::fromArray( |
44
|
|
|
[ |
45
|
|
|
'classes' => [$classGenerator], |
46
|
|
|
] |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$directoryWriter = $this->getCurrentModuleDirectoryWriter(); |
50
|
|
|
$directoryWriter->writeFile($filePathToGenerate, $fileGenerator->generate()); |
51
|
|
|
|
52
|
|
|
$output->writeln('<info>generated </info><comment>' . $filePathToGenerate . '</comment>'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
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.