1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HotRodCli\Commands\Classes; |
4
|
|
|
|
5
|
|
|
use HotRodCli\Commands\BaseCommand; |
6
|
|
|
use HotRodCli\Commands\Xml\CreateLayoutCommand; |
7
|
|
|
use HotRodCli\Jobs\Filesystem\CopyFile; |
8
|
|
|
use HotRodCli\Jobs\Module\IsModuleExists; |
9
|
|
|
use HotRodCli\Jobs\Module\ReplaceText; |
10
|
|
|
use HotRodCli\Jobs\Xml\AddRoute; |
11
|
|
|
use HotRodCli\Processors\ProcessBlockFile; |
12
|
|
|
use HotRodCli\Processors\ProcessLayoutFile; |
13
|
|
|
use HotRodCli\Processors\ProcessRouteFile; |
14
|
|
|
use HotRodCli\Processors\ProcessTemplateFile; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
class CreateControllerCommand extends BaseCommand |
20
|
|
|
{ |
21
|
|
|
protected $jobs = [ |
22
|
|
|
CopyFile::class => null, |
23
|
|
|
IsModuleExists::class => null, |
24
|
|
|
ReplaceText::class => null, |
25
|
|
|
AddRoute::class => null, |
26
|
|
|
CreateLayoutCommand::class => null |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
protected $processors = [ |
30
|
|
|
ProcessBlockFile::class => null, |
31
|
|
|
ProcessLayoutFile::class => null, |
32
|
|
|
ProcessRouteFile::class => null, |
33
|
|
|
ProcessTemplateFile::class => null |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
protected $configs = [ |
37
|
|
|
'arguments' => [ |
38
|
|
|
[ |
39
|
|
|
'name' => 'namespace', |
40
|
|
|
'mode' => InputArgument::REQUIRED, |
41
|
|
|
'description' => 'What is the namespace on the module' |
42
|
|
|
], |
43
|
|
|
[ |
44
|
|
|
'name' => 'route', |
45
|
|
|
'mode' => InputArgument::REQUIRED, |
46
|
|
|
'description' => 'route pattern is "route_name/controller/action"' |
47
|
|
|
] |
48
|
|
|
], |
49
|
|
|
'options' => [ |
50
|
|
|
[ |
51
|
|
|
'name' => 'no-block', |
52
|
|
|
'shortcut' => null, |
53
|
|
|
'mode' => InputArgument::OPTIONAL, |
54
|
|
|
'description' => 'Do you need a block?' |
55
|
|
|
], |
56
|
|
|
[ |
57
|
|
|
'name' => 'no-layout', |
58
|
|
|
'shortcut' => null, |
59
|
|
|
'mode' => InputArgument::OPTIONAL, |
60
|
|
|
'description' => 'Do you need a layout file?' |
61
|
|
|
], |
62
|
|
|
[ |
63
|
|
|
'name' => 'no-routes', |
64
|
|
|
'shortcut' => null, |
65
|
|
|
'mode' => InputArgument::OPTIONAL, |
66
|
|
|
'description' => 'Do you need a routes file?' |
67
|
|
|
], |
68
|
|
|
[ |
69
|
|
|
'name' => 'no-template', |
70
|
|
|
'shortcut' => null, |
71
|
|
|
'mode' => InputArgument::OPTIONAL, |
72
|
|
|
'description' => 'Do you need a template file?' |
73
|
|
|
], |
74
|
|
|
[ |
75
|
|
|
'name' => 'admin', |
76
|
|
|
'shortcut' => null, |
77
|
|
|
'mode' => InputArgument::OPTIONAL, |
78
|
|
|
'description' => 'Is this template for admin part?' |
79
|
|
|
] |
80
|
|
|
], |
81
|
|
|
'description' => 'Creates a new controller', |
82
|
|
|
'name' => 'create:controller', |
83
|
|
|
'help' => 'creates a new controller in a given namespace with a given route' |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
protected function configure() |
87
|
|
|
{ |
88
|
|
|
$this->config($this); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
92
|
|
|
{ |
93
|
|
|
$this->setJobs(); |
94
|
|
|
$this->setProcessors(); |
95
|
|
|
$jobs = $this->jobs; |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
$jobs[IsModuleExists::class]->handle($input->getArgument('namespace'), $output); |
99
|
|
|
|
100
|
|
|
$this->processControllerFile($input, $output); |
101
|
|
|
|
102
|
|
|
$this->runProcessors($input, $output); |
103
|
|
|
} catch (\Throwable $e) { |
104
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function processControllerFile(InputInterface $input, OutputInterface $output): void |
109
|
|
|
{ |
110
|
|
|
$jobs = $this->jobs; |
111
|
|
|
$namespace = explode('_', $input->getArgument('namespace')); |
112
|
|
|
$controller = explode('/', $input->getArgument('route')); |
113
|
|
|
$scopeDir = $input->getOption('admin') ? 'Adminhtml/' : ''; |
114
|
|
|
$scopeNamespace = $input->getOption('admin') ? 'Adminhtml\\' : ''; |
115
|
|
|
$app = $this->appContainer; |
116
|
|
|
|
117
|
|
|
$jobs[CopyFile::class]->handle( |
118
|
|
|
$app->get('resource_dir') . '/classes/Controller.tphp', |
119
|
|
|
$this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . |
120
|
|
|
'/Controller/' . $scopeDir . ucwords($controller[1]) . '/' . ucwords($controller[2]) . '.php' |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$this->replaceTextsSequence( |
124
|
|
|
[ |
125
|
|
|
'{{controller_namespace}}' => $namespace[0] . '\\' . $namespace[1] . '\\Controller\\' . $scopeNamespace . ucwords($controller[1]), |
126
|
|
|
'{{className}}' => ucwords($controller[2]), |
127
|
|
|
'{{route}}' => $input->getArgument('route') |
128
|
|
|
], |
129
|
|
|
$this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Controller/' |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$output->writeln('<info>' |
133
|
|
|
. $namespace[0] . '\\' |
134
|
|
|
. $namespace[1] . '\\Controller\\' |
135
|
|
|
. ucwords($controller[1]) . '\\' |
136
|
|
|
. ucwords($controller[2]) . ' was successfully created</info>'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|