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\Jobs\Xml\AddPreference; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
|
14
|
|
|
class CreateRepositoryCommand extends BaseCommand |
15
|
|
|
{ |
16
|
|
|
protected $jobs = [ |
17
|
|
|
CopyFile::class => null, |
18
|
|
|
ReplaceText::class => null, |
19
|
|
|
IsModuleExists::class => null, |
20
|
|
|
AddPreference::class => null |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
protected function configure() |
24
|
|
|
{ |
25
|
|
|
$this->setName('create:repository') |
26
|
|
|
->setDescription('Creates a repository class') |
27
|
|
|
->addArgument( |
28
|
|
|
'namespace', |
29
|
|
|
InputArgument::REQUIRED, |
30
|
|
|
'What is the namespace of the module' |
31
|
|
|
) |
32
|
|
|
->addArgument( |
33
|
|
|
'interface-name', |
34
|
|
|
InputArgument::REQUIRED, |
35
|
|
|
'What is the interface name' |
36
|
|
|
) |
37
|
|
|
->addArgument( |
38
|
|
|
'model-class', |
39
|
|
|
InputArgument::REQUIRED, |
40
|
|
|
'What is the CRUD model class' |
41
|
|
|
) |
42
|
|
|
->setHelp('creates an upgrade data script in a given namespace'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
|
|
$this->setJobs(); |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
$this->jobs[IsModuleExists::class]->handle($input->getArgument('namespace'), $output); |
51
|
|
|
|
52
|
|
|
$this->processInterfaceFile($input, $output); |
53
|
|
|
$this->processRepositoryFile($input, $output); |
54
|
|
|
$this->processDiFile($input, $output); |
55
|
|
|
} catch (\Throwable $e) { |
56
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function processInterfaceFile(InputInterface $input, OutputInterface $output) |
61
|
|
|
{ |
62
|
|
|
$namespace = explode('_', $input->getArgument('namespace')); |
63
|
|
|
|
64
|
|
|
$this->jobs[CopyFile::class]->handle( |
65
|
|
|
$this->appContainer->get('resource_dir') . '/api/Repository.tphp', |
66
|
|
|
$this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Api/' . $input->getArgument('interface-name') . '.php' |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$this->jobs[ReplaceText::class]->handle('{{namespace}}', $namespace[0] . '\\' . $namespace[1], $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Api/'); |
70
|
|
|
|
71
|
|
|
$this->jobs[ReplaceText::class]->handle('{{name}}', $input->getArgument('interface-name'), $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Api/'); |
72
|
|
|
|
73
|
|
|
$this->jobs[ReplaceText::class]->handle('{{Model}}', $input->getArgument('model-class'), $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Api/'); |
74
|
|
|
|
75
|
|
|
$output->writeln('<info>Interface ' . $input->getArgument('interface-name') . ' was successfully created</info>'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function processRepositoryFile(InputInterface $input, OutputInterface $output) |
79
|
|
|
{ |
80
|
|
|
$namespace = explode('_', $input->getArgument('namespace')); |
81
|
|
|
$modelName = explode('\\', $input->getArgument('model-class'))[3]; |
82
|
|
|
|
83
|
|
|
$this->jobs[CopyFile::class]->handle( |
84
|
|
|
$this->appContainer->get('resource_dir') . '/classes/Repository.tphp', |
85
|
|
|
$this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Model/' . $modelName . 'Repository.php' |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->replaceTextsSequence([ |
89
|
|
|
'{{namespace}}' => $namespace[0] . '\\' . $namespace[1], |
90
|
|
|
'{{interface}}' => $input->getArgument('interface-name'), |
91
|
|
|
'{{Model}}' => $input->getArgument('model-class'), |
92
|
|
|
'{{modelName}}' => $modelName |
93
|
|
|
], $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Model/'); |
94
|
|
|
|
95
|
|
|
$output->writeln('<info>Repository ' . $modelName . 'Repository was successfully created</info>'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function processDiFile(InputInterface $input, OutputInterface $output) |
99
|
|
|
{ |
100
|
|
|
$namespace = explode('_', $input->getArgument('namespace')); |
101
|
|
|
$modelName = explode('\\', $input->getArgument('model-class'))[3]; |
102
|
|
|
|
103
|
|
|
$this->jobs[CopyFile::class]->handle( |
104
|
|
|
$this->appContainer->get('resource_dir') . '/xml/di.xml', |
105
|
|
|
$this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/etc/di.xml' |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$this->jobs[AddPreference::class]->handle($this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/etc/di.xml', [ |
109
|
|
|
'for' => $namespace[0] . '\\' . $namespace[1] . '\\Api\\' . $input->getArgument('interface-name'), |
110
|
|
|
'type' => $namespace[0] . '\\' . $namespace[1] . '\\Model\\' . $modelName . 'Repository' |
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
$output->writeln('<info>Preference was successfully added for ' . $namespace[0] . '\\' . $namespace[1] . '\\Model\\' . $modelName . 'Repository' . '</info>'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|