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 Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
12
|
|
|
|
13
|
|
|
class CreateCollectionCommand extends BaseCommand |
14
|
|
|
{ |
15
|
|
|
protected $jobs = [ |
16
|
|
|
CopyFile::class => null, |
17
|
|
|
ReplaceText::class => null, |
18
|
|
|
IsModuleExists::class => null |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
protected $configs = [ |
22
|
|
|
'arguments' => [ |
23
|
|
|
[ |
24
|
|
|
'name' => 'namespace', |
25
|
|
|
'mode' => InputArgument::REQUIRED, |
26
|
|
|
'description' => 'What is the namespace on the module' |
27
|
|
|
], |
28
|
|
|
[ |
29
|
|
|
'name' => 'name', |
30
|
|
|
'mode' => InputArgument::REQUIRED, |
31
|
|
|
'description' => 'What is the name of the model' |
32
|
|
|
], |
33
|
|
|
[ |
34
|
|
|
'name' => 'table-name', |
35
|
|
|
'mode' => InputArgument::REQUIRED, |
36
|
|
|
'description' => 'What is the table name for the model' |
37
|
|
|
], |
38
|
|
|
[ |
39
|
|
|
'name' => 'id-field', |
40
|
|
|
'mode' => InputArgument::REQUIRED, |
41
|
|
|
'description' => 'What is the id-field of the table' |
42
|
|
|
] |
43
|
|
|
], |
44
|
|
|
'options' => [], |
45
|
|
|
'description' => 'Creates a new collection', |
46
|
|
|
'name' => 'create:collection', |
47
|
|
|
'help' => 'creates a new Collection in a given namespace' |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
protected function configure() |
51
|
|
|
{ |
52
|
|
|
$this->config(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
56
|
|
|
{ |
57
|
|
|
$this->setJobs(); |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
$this->jobs[IsModuleExists::class]->handle($input->getArgument('namespace'), $output); |
61
|
|
|
|
62
|
|
|
$this->processCollectionFile($input, $output); |
63
|
|
|
} catch (\Throwable $e) { |
64
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function processCollectionFile(InputInterface $input, OutputInterface $output) |
69
|
|
|
{ |
70
|
|
|
$namespace = explode('_', $input->getArgument('namespace')); |
71
|
|
|
|
72
|
|
|
$this->jobs[CopyFile::class]->handle( |
73
|
|
|
$this->appContainer->get('resource_dir') . '/classes/Collection.tphp', |
74
|
|
|
$this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Model/ResourceModel/' . $input->getArgument('name') . '/Collection.php' |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$this->replaceTextsSequence([ |
78
|
|
|
'{{namespace}}' => str_replace('_', '\\', $input->getArgument('namespace')), |
79
|
|
|
'{{name}}' => $input->getArgument('name'), |
80
|
|
|
'{{table_name}}' => $input->getArgument('table-name'), |
81
|
|
|
'{{id_name}}' => $input->getArgument('id-field'), |
82
|
|
|
'{{ResourceModel}}' => str_replace('_', '\\', $input->getArgument('namespace')) . '\\Model\\ResourceModel\\' . $input->getArgument('name'), |
83
|
|
|
'{{Model}}' => str_replace('_', '\\', $input->getArgument('namespace')) . '\\Model\\' . $input->getArgument('name') |
84
|
|
|
], $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Model/'); |
85
|
|
|
|
86
|
|
|
$output->writeln('<info>Collection ' . $input->getArgument('name') . ' was successfully created</info>'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|