1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Developer\Console; |
4
|
|
|
|
5
|
|
|
use Magento\Framework\Code\Generator\ClassGenerator; |
6
|
|
|
use Magento\Framework\Controller\ResultFactory; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
class MakeControllerCommand extends AbstractGeneratorCommand |
13
|
|
|
{ |
14
|
|
|
protected function configure() |
15
|
|
|
{ |
16
|
|
|
$this |
17
|
|
|
->setName('make:controller') |
18
|
|
|
->addArgument('classpath', InputArgument::REQUIRED) |
19
|
|
|
->addOption('result', 'r', InputOption::VALUE_OPTIONAL, 'Result type', 'page') |
20
|
|
|
->setDescription('Creates a controller action class'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param InputInterface $input |
25
|
|
|
* @param OutputInterface $output |
26
|
|
|
* |
27
|
|
|
* @return int|void |
28
|
|
|
*/ |
29
|
|
|
protected function catchedExecute(InputInterface $input, OutputInterface $output) |
30
|
|
|
{ |
31
|
|
|
$actionFileName = $this->getNormalizedPathByArgument($input->getArgument('classpath')); |
32
|
|
|
$classNameToGenerate = $this->getCurrentModuleNamespace() |
33
|
|
|
. '\\Controller\\' |
34
|
|
|
. $this->getNormalizedClassnameByArgument($input->getArgument('classpath')); |
35
|
|
|
$filePathToGenerate = 'Controller/' . $actionFileName . '.php'; |
36
|
|
|
|
37
|
|
|
$classGenerator = $this->create(ClassGenerator::class); |
38
|
|
|
|
39
|
|
|
/** @var $classGenerator ClassGenerator */ |
40
|
|
|
$classGenerator->setExtendedClass('Action'); |
41
|
|
|
|
42
|
|
|
$body = $this->createClassBody($input); |
43
|
|
|
$executeMethodDefinition = $this->createClassMethodDefinitions($body); |
44
|
|
|
|
45
|
|
|
$classGenerator->addMethods([$executeMethodDefinition]); |
46
|
|
|
$classGenerator->setName($classNameToGenerate); |
47
|
|
|
$classGenerator->addUse('Magento\Framework\App\Action\Action'); |
48
|
|
|
$classGenerator->addUse('Magento\Framework\Controller\ResultFactory'); |
49
|
|
|
|
50
|
|
|
$this->writeClassToFile($output, $classGenerator, $filePathToGenerate); |
51
|
|
|
|
52
|
|
|
if ($input->getOption('result') == ResultFactory::TYPE_PAGE) { |
53
|
|
|
$this->createLayoutFile(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param InputInterface $input |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
private function createClassBody(InputInterface $input) |
62
|
|
|
{ |
63
|
|
|
$body = ''; |
64
|
|
|
|
65
|
|
|
if ($input->getOption('result') == ResultFactory::TYPE_PAGE) { |
66
|
|
|
$body .= 'return $this->resultFactory->create(ResultFactory::TYPE_PAGE);'; |
67
|
|
|
} elseif ($input->getOption('result') == ResultFactory::TYPE_RAW) { |
68
|
|
|
$body .= '$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);'; |
69
|
|
|
$body .= PHP_EOL; |
70
|
|
|
$body .= '$result->setContents(\'ok\');'; |
71
|
|
|
$body .= PHP_EOL; |
72
|
|
|
$body .= PHP_EOL; |
73
|
|
|
$body .= 'return $result;'; |
74
|
|
|
} else { |
75
|
|
|
$body .= '$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);'; |
76
|
|
|
$body .= PHP_EOL; |
77
|
|
|
$body .= '$result->setData(\'ok\');'; |
78
|
|
|
$body .= PHP_EOL; |
79
|
|
|
$body .= PHP_EOL; |
80
|
|
|
$body .= 'return $result;'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $body; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $body |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
private function createClassMethodDefinitions($body) |
91
|
|
|
{ |
92
|
|
|
$executeMethodDefinition = [ |
93
|
|
|
'name' => 'execute', |
94
|
|
|
'parameters' => [], |
95
|
|
|
'body' => $body, |
96
|
|
|
'docblock' => [ |
97
|
|
|
'shortDescription' => 'Dispatch request', |
98
|
|
|
'tags' => [ |
99
|
|
|
[ |
100
|
|
|
'name' => 'return', |
101
|
|
|
'description' => '\Magento\Framework\Controller\ResultInterface|ResponseInterface', |
102
|
|
|
], |
103
|
|
|
[ |
104
|
|
|
'name' => 'throws', |
105
|
|
|
'description' => '\Magento\Framework\Exception\NotFoundException', |
106
|
|
|
], |
107
|
|
|
], |
108
|
|
|
], |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
return $executeMethodDefinition; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function createLayoutFile() |
115
|
|
|
{ |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|