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