| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace HotRodCli\Commands\Frontend; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use HotRodCli\Commands\BaseCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use HotRodCli\Jobs\Filesystem\CopyFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use HotRodCli\Jobs\Js\AddJs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use HotRodCli\Jobs\Js\AddMageInit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use HotRodCli\Jobs\Module\IsModuleExists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use HotRodCli\Processors\ProcessRequireJs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class CreateScriptCommand extends BaseCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $jobs = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         CopyFile::class => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         IsModuleExists::class => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         AddJs::class => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         AddMageInit::class => null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $processors = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         ProcessRequireJs::class => null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $configs = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         'arguments' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 'name' => 'namespace', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 'mode' => InputArgument::REQUIRED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 'description' => 'What is the namespace on the module' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 'name' => 'script-name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 'mode' => InputArgument::REQUIRED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 'description' => 'What is the filename of the script' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         'options' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 'name' => 'admin', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 'shortcut' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 'mode' => InputArgument::OPTIONAL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 'description' => 'Do You want to generate the JS in admin scope?' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 'name' => 'template', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 'shortcut' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 'mode' => InputArgument::OPTIONAL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 'description' => 'Do You want to add mage-init for this script in a specific template?' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         'description' => 'Creates a new script', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         'name' => 'create:js-script', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         'help' => 'creates a new Java Script in a given namespace', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         'info' => '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->config(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->setJobs(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->setProcessors(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $this->jobs[IsModuleExists::class]->handle($input->getArgument('namespace'), $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $this->processRequirejs($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $this->processScriptFile($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $this->processTemplate($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } catch (\Throwable $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $output->writeln('<error>' . $e->getMessage() . '</error>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     protected function processScriptFile(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $namespace = explode('_', $input->getArgument('namespace')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $app = $this->appContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $scope = $input->getOption('admin') ? 'adminhtml' : 'frontend'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $this->jobs[AddJs::class]->handle( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $app->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/view/' . $scope . '/requirejs-config.js', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $input->getArgument('script-name'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $input->getArgument('namespace') . '/js/' . $input->getArgument('script-name') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $this->jobs[CopyFile::class]->handle( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $this->appContainer->get('resource_dir') . '/frontend/simple-js.js', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 . $namespace[1] . '/view/' . $scope . '/web/js/' . $input->getArgument('script-name') . '.js' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $output->writeln('<info>' . '/app/code/' . $namespace[0] . '/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             . $namespace[1] . '/view/' . $scope . '/web/js/' . $input->getArgument('script-name') . '.js successfully generated</info>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     protected function processRequirejs(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $namespace = explode('_', $input->getArgument('namespace')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $app = $this->appContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $filesystem = $app->resolve(Filesystem::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $scope = $input->getOption('admin') ? 'adminhtml' : 'frontend'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if (!$filesystem->exists($app->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/view/' . $scope . '/requirejs-config.js')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $this->processors[ProcessRequireJs::class]($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     protected function processTemplate(InputInterface $input, OutputInterface $output) | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         if ($input->getOption('template')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             $this->jobs[AddMageInit::class]->handle( | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |                 $this->appContainer->get('app_dir') . '/app/code/' . $input->getOption('template'), | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |                 [ | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |                     'name' =>  $input->getArgument('script-name'), | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |                     'bind' => '*' | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |                 ] | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |             $output->writeln('<info>mage init was added</info>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 131 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |  |