|
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 function configure() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->setName('create:model') |
|
33
|
|
|
->setDescription('Creates a new model') |
|
34
|
|
|
->addArgument( |
|
35
|
|
|
'namespace', |
|
36
|
|
|
InputArgument::REQUIRED, |
|
37
|
|
|
'What is the namespace on the module' |
|
38
|
|
|
) |
|
39
|
|
|
->addArgument( |
|
40
|
|
|
'name', |
|
41
|
|
|
InputArgument::REQUIRED, |
|
42
|
|
|
'What is the name of the new Model' |
|
43
|
|
|
) |
|
44
|
|
|
->addArgument( |
|
45
|
|
|
'table-name', |
|
46
|
|
|
InputArgument::REQUIRED, |
|
47
|
|
|
'What is the table name for the new Model' |
|
48
|
|
|
) |
|
49
|
|
|
->addArgument( |
|
50
|
|
|
'id-field', |
|
51
|
|
|
InputArgument::REQUIRED, |
|
52
|
|
|
'What is the id-field of the table' |
|
53
|
|
|
) |
|
54
|
|
|
->setHelp('creates a new Resource Model in a given namespace'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->setJobs(); |
|
60
|
|
|
$this->setProcessors(); |
|
61
|
|
|
$jobs = $this->jobs; |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
|
|
$jobs[IsModuleExists::class]->handle($input->getArgument('namespace'), $output); |
|
65
|
|
|
|
|
66
|
|
|
$this->processModelFile($input, $output); |
|
67
|
|
|
|
|
68
|
|
|
$this->runProcessors($input, $output); |
|
69
|
|
|
} catch (\Throwable $e) { |
|
70
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function processModelFile(InputInterface $input, OutputInterface $output) |
|
75
|
|
|
{ |
|
76
|
|
|
$namespace = explode('_', $input->getArgument('namespace')); |
|
77
|
|
|
|
|
78
|
|
|
$this->jobs[CopyFile::class]->handle( |
|
79
|
|
|
$this->appContainer->get('resource_dir') . '/classes/Model.tphp', |
|
80
|
|
|
$this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Model/' . $input->getArgument('name') . '.php' |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$this->replaceTextsSequence([ |
|
84
|
|
|
'{{namespace}}' => str_replace('_', '\\', $input->getArgument('namespace')), |
|
85
|
|
|
'{{ModelName}}' => $input->getArgument('name'), |
|
86
|
|
|
'{{tag}}' => $input->getArgument('table-name'), |
|
87
|
|
|
'{{ResourceModel}}' => str_replace('_', '\\', $input->getArgument('namespace')) . '\\Model\\ResourceModel\\' . $input->getArgument('name') |
|
88
|
|
|
], $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Model/'); |
|
89
|
|
|
|
|
90
|
|
|
$output->writeln('<info>Model ' . $input->getArgument('name') . ' was successfully created</info>'); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|