| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Rougin\Combustor\Commands; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Rougin\Combustor\Common\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Rougin\Combustor\Common\Tools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Rougin\Combustor\Validator\ModelValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Rougin\Combustor\Generator\ModelGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Create Model Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Generates a Wildfire or Doctrine-based model for CodeIgniter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @package Combustor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author  Rougin Royce Gutib <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class CreateModelCommand extends AbstractCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * Checks whether the command is enabled or not in the current environment. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function isEnabled() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return Tools::isCommandEnabled(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 6 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Sets the configurations of the specified command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     protected function configure() | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $this | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 15 |  |             ->setName('create:model') | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             ->setDescription('Creates a new model') | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 15 |  |             ->addArgument( | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 15 |  |                 'name', | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 15 |  |                 InputArgument::REQUIRED, | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 15 |  |                 'Name of the table' | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 15 |  |             )->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 15 |  |                 'camel', | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 NULL, | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 15 |  |                 InputOption::VALUE_NONE, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 15 |  |                 'Uses the camel case naming convention' | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 15 |  |             )->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 15 |  |                 'lowercase', | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 NULL, | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 15 |  |                 InputOption::VALUE_NONE, | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 15 |  |                 'Keeps the first character of the name to lowercase' | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 15 |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 15 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 15 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 15 |  |      * Executes the command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 15 |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 15 |  |      * @param \Symfony\Component\Console\Input\InputInterface   $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param \Symfony\Component\Console\Output\OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 15 |  |      * @return object|\Symfony\Component\Console\Output\OutputInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 15 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 15 |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 15 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $fileName = ucfirst(singular($input->getArgument('name'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 15 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 15 |  |         $path = APPPATH . 'models' . DIRECTORY_SEPARATOR . $fileName . '.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $info = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             'name' => $fileName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             'type' => 'model', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             'path' => $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $validator = new ModelValidator($input->getOption('camel'), $info); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 12 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if ($validator->fails()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 12 |  |             $message = $validator->getMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 12 |  |             return $output->writeln('<error>' . $message . '</error>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 12 |  |         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 12 |  |             'file' => $info, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             'isCamel' => $input->getOption('camel'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 12 |  |             'name' => $input->getArgument('name'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'type' => $validator->getLibrary() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 12 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 12 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 12 |  |         $generator = new ModelGenerator($this->describe, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 12 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $result = $generator->generate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 12 |  |         $model = $this->renderer->render('Model.tpl', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $message = 'The model "' . $fileName . '" has been created successfully!'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 12 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 3 |  |         $file = new File($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 3 |  |         $file->putContents($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $file->close(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $output->writeln('<info>' . $message . '</info>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 9 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |  |