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\AddEvent; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
14
|
|
|
|
15
|
|
|
class CreateObserverCommand extends BaseCommand |
16
|
|
|
{ |
17
|
|
|
protected $jobs = [ |
18
|
|
|
IsModuleExists::class => null, |
19
|
|
|
CopyFile::class => null, |
20
|
|
|
ReplaceText::class => null, |
21
|
|
|
AddEvent::class => null |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
protected function configure() |
25
|
|
|
{ |
26
|
|
|
$this->setName('create:observer') |
27
|
|
|
->setDescription('Creates an observer') |
28
|
|
|
->addArgument('namespace', InputArgument::REQUIRED, 'What is the namespace of the module') |
29
|
|
|
->addArgument('event', InputArgument::REQUIRED, 'What is the event name') |
30
|
|
|
->addArgument('observer', InputArgument::REQUIRED, 'What is the observer name') |
31
|
|
|
->setHelp('creates an install schema script in a given namespace'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
35
|
|
|
{ |
36
|
|
|
$this->setJobs(); |
37
|
|
|
|
38
|
|
|
try { |
39
|
|
|
$this->jobs[IsModuleExists::class]->handle($input->getArgument('namespace'), $output); |
40
|
|
|
|
41
|
|
|
$this->processObserverFile($input, $output); |
42
|
|
|
|
43
|
|
|
$this->processEventsFile($input, $output); |
44
|
|
|
} catch (\Throwable $e) { |
45
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function processObserverFile(InputInterface $input, OutputInterface $output) |
50
|
|
|
{ |
51
|
|
|
$jobs = $this->jobs; |
52
|
|
|
$namespace = explode('_', $input->getArgument('namespace')); |
53
|
|
|
$app = $this->appContainer; |
54
|
|
|
$jobs[CopyFile::class]->handle($app->get('resource_dir') . '/classes/Observer.tphp', $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Observer/' . ucwords($input->getArgument('observer')) . '.php'); |
55
|
|
|
|
56
|
|
|
$this->replaceTextsSequence([ |
57
|
|
|
'{{namespace}}' => $namespace[0] . '\\' . $namespace[1] . '\\', |
58
|
|
|
'{{observer-name}}' => ucwords($input->getArgument('observer')) |
59
|
|
|
], $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Observer/'); |
60
|
|
|
|
61
|
|
|
$output->writeln('<info>' . $namespace[0] . '\\' . $namespace[1] . '\\Observer\\' . ucwords(ucwords($input->getArgument('observer'))) . ' was successfully created</info>'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function processEventsFile(InputInterface $input, OutputInterface $output) |
65
|
|
|
{ |
66
|
|
|
$jobs = $this->jobs; |
67
|
|
|
$namespace = explode('_', $input->getArgument('namespace')); |
68
|
|
|
$app = $this->appContainer; |
69
|
|
|
$filesystem = $this->appContainer->resolve(Filesystem::class); |
70
|
|
|
|
71
|
|
|
if (!$filesystem->exists($app->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/etc/events.xml')) { |
72
|
|
|
$jobs[CopyFile::class]->handle( |
73
|
|
|
$app->get('resource_dir') . '/xml/events.xml', |
74
|
|
|
$this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/etc/events.xml' |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$jobs[AddEvent::class]->handle($this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/etc/events.xml', [ |
79
|
|
|
'event-name' => $input->getArgument('event'), |
80
|
|
|
'observer-name' => strtolower($namespace[0] . '_' . $namespace[1] . '_' . $input->getArgument('observer')), |
81
|
|
|
'instance' => $namespace[0] . '\\' . $namespace[1] . '\\Observer\\' . $input->getArgument('observer'), |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$output->writeln('<info>Event was successfully added</info>'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|