1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HotRodCli\Commands\Classes; |
4
|
|
|
|
5
|
|
|
use HotRodCli\Commands\BaseCommand; |
6
|
|
|
use HotRodCli\Jobs\Filesystem\CopyFile; |
7
|
|
|
use HotRodCli\Jobs\Module\IsModuleExists; |
8
|
|
|
use HotRodCli\Jobs\Module\ReplaceText; |
9
|
|
|
use HotRodCli\Processors\ProcessCollectionFile; |
10
|
|
|
use HotRodCli\Processors\ProcessRepository; |
11
|
|
|
use HotRodCli\Processors\ProcessResourceModelFile; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
|
16
|
|
|
class CreateModelCommand extends BaseCommand |
17
|
|
|
{ |
18
|
|
|
protected $jobs = [ |
19
|
|
|
IsModuleExists::class => null, |
20
|
|
|
CopyFile::class => null, |
21
|
|
|
ReplaceText::class => null |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
protected $processors = [ |
25
|
|
|
ProcessResourceModelFile::class => null, |
26
|
|
|
ProcessCollectionFile::class => null, |
27
|
|
|
ProcessRepository::class => null |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
protected $configs = [ |
31
|
|
|
'arguments' => [ |
32
|
|
|
[ |
33
|
|
|
'name' => 'namespace', |
34
|
|
|
'mode' => InputArgument::REQUIRED, |
35
|
|
|
'description' => 'What is the namespace on the module' |
36
|
|
|
], |
37
|
|
|
[ |
38
|
|
|
'name' => 'name', |
39
|
|
|
'mode' => InputArgument::REQUIRED, |
40
|
|
|
'description' => 'What is the name of the new Model' |
41
|
|
|
], |
42
|
|
|
[ |
43
|
|
|
'name' => 'table-name', |
44
|
|
|
'mode' => InputArgument::REQUIRED, |
45
|
|
|
'description' => 'What is the table name for the new Model' |
46
|
|
|
], |
47
|
|
|
[ |
48
|
|
|
'name' => 'id-field', |
49
|
|
|
'mode' => InputArgument::REQUIRED, |
50
|
|
|
'description' => 'What is the id-field of the table' |
51
|
|
|
] |
52
|
|
|
], |
53
|
|
|
'options' => [], |
54
|
|
|
'description' => 'Creates a new model', |
55
|
|
|
'name' => 'create:model', |
56
|
|
|
'help' => 'creates a new Resource Model in a given namespace' |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
protected function configure() |
60
|
|
|
{ |
61
|
|
|
$this->config(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
65
|
|
|
{ |
66
|
|
|
$this->setJobs(); |
67
|
|
|
$this->setProcessors(); |
68
|
|
|
$jobs = $this->jobs; |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
$jobs[IsModuleExists::class]->handle($input->getArgument('namespace'), $output); |
72
|
|
|
|
73
|
|
|
$this->processModelFile($input, $output); |
74
|
|
|
|
75
|
|
|
$this->runProcessors($input, $output); |
76
|
|
|
} catch (\Throwable $e) { |
77
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function processModelFile(InputInterface $input, OutputInterface $output) |
82
|
|
|
{ |
83
|
|
|
$namespace = explode('_', $input->getArgument('namespace')); |
84
|
|
|
|
85
|
|
|
$this->jobs[CopyFile::class]->handle( |
86
|
|
|
$this->appContainer->get('resource_dir') . '/classes/Model.tphp', |
87
|
|
|
$this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Model/' . $input->getArgument('name') . '.php' |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$this->replaceTextsSequence([ |
91
|
|
|
'{{namespace}}' => str_replace('_', '\\', $input->getArgument('namespace')), |
92
|
|
|
'{{ModelName}}' => $input->getArgument('name'), |
93
|
|
|
'{{tag}}' => $input->getArgument('table-name'), |
94
|
|
|
'{{ResourceModel}}' => str_replace('_', '\\', $input->getArgument('namespace')) . '\\Model\\ResourceModel\\' . $input->getArgument('name') |
95
|
|
|
], $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Model/'); |
96
|
|
|
|
97
|
|
|
$output->writeln('<info>Model ' . $input->getArgument('name') . ' was successfully created</info>'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|