| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Console\Commands; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Console\Traits\CodeGenerate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Question\Question; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * GenerateCommandCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class GenerateCommandCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     use CodeGenerate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * Configuration of command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             ->setName('generate:command') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             ->setDescription('Generate new command') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             ->setHelp('<info>php partisan generate:command</info>') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Execute method of command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $output->writeln(['<comment>Welcome to the command generator</comment>']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $helper   = $this->getHelper('question'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $question = new Question('<info>Please enter the name of the command class: </info>', 'DefaultCommand'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $question->setValidator(function($answer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             if ('Command' !== substr($answer, -7)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 throw new \RunTimeException('The name of the command should be suffixed with \'Command\''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $baseName = $answer.'.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             if (true === file_exists($this->getPath($baseName, COMMANDS_PATH))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 throw new \RunTimeException('This command already exists'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             return $answer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $commandClass = $helper->ask($input, $output, $question); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $commandName  = $this->colonize($commandClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $baseName     = $commandClass.'.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $path         = $this->getPath($baseName, COMMANDS_PATH); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $placeHolders = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             '<class>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             '<name>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $replacements = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $commandClass, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $commandName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->generateCode($placeHolders, $replacements, 'CommandTemplate.tpl', $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $output->writeln(sprintf('Generated new command class to "<info>%s</info>"', realpath($path))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * Colonize command name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param $word | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |     private function colonize($word) | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |         $word = str_replace('Command', '', $word); | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |         return strtolower(preg_replace('/[^A-Z^a-z^0-9]+/', ':', | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |             preg_replace('/([a-zd])([A-Z])/', '\1:\2', | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |                 preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1:\2', $word))) | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         ); | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  |