| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Yii\Gii\Generator\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Yiisoft\Yii\Gii\CodeFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Yiisoft\Yii\Gii\Generator\AbstractGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Yiisoft\Yii\Gii\Generator\AbstractGeneratorCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * This generator will generate a controller and one or a few action view files. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | final class ControllerGenerator extends AbstractGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public static function getName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         return 'Controller'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     public static function getDescription(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         return 'This generator helps you to quickly generate a new controller class with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             one or several controller actions and their corresponding views.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public static function getId(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         return 'controller'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function requiredTemplates(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             'controller.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'view.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function doGenerate(AbstractGeneratorCommand $command): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         if (!$command instanceof ControllerCommand) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             throw new InvalidArgumentException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $files = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $rootPath = $this->aliases->get('@root'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $codeFile = (new CodeFile( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $this->getControllerFile($command), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $this->render($command, 'controller') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         ))->withBasePath($rootPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $files[$codeFile->getId()] = $codeFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         foreach ($command->getActions() as $action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $codeFile = (new CodeFile( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 $this->getViewFile($command, $action), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 $this->render($command, 'view', ['action' => $action]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             ))->withBasePath($rootPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $files[$codeFile->getId()] = $codeFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return $files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return string the controller class file path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     private function getControllerFile(ControllerCommand $command): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return $this->aliases->get( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             sprintf('%s/%s.php', $this->getDirectory() ?? '', $command->getControllerClass()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param string $action the action ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return string the action view file path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function getViewFile(ControllerCommand $command, string $action): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         if (empty($command->getViewsPath())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             return $this->aliases->get( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 "@views/{$command->getControllerID()}/$action.php" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return $this->aliases->get(str_replace('\\', '/', $command->getViewsPath()) . "/$action.php"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public static function getCommandClass(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return ControllerCommand::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  |