| 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 Generator extends AbstractGenerator | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     public function getName(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         return 'Controller Generator'; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     public 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 function attributeLabels(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             'baseClass' => 'Base Class', | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             'controllerClass' => 'Controller Class', | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             'viewsPath' => 'Views Path', | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             'actions' => 'Action IDs', | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function requiredTemplates(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             'controller.php', | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |             'view.php', | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     public function stickyAttributes(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         return ['baseClass']; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     public function hints(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             'controllerClass' => 'This is the name of the controller class to be generated. You should | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 provide a fully qualified namespaced class (e.g. <code>App\Controller\PostController</code>), | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 and class name should be in CamelCase ending with the word <code>Controller</code>. Make sure the class | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 is using the same namespace as specified by your application\'s controllerNamespace property.', | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             'actions' => 'Provide one or multiple action IDs to generate empty action method(s) in the controller. Separate multiple action IDs with commas or spaces. | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 Action IDs should be in lower case. For example: | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                 <ul> | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                     <li><code>index</code> generates <code>index()</code></li> | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                     <li><code>create-order</code> generates <code>createOrder()</code></li> | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 </ul>', | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             'viewsPath' => 'Specify the directory for storing the view scripts for the controller. You may use path alias here, e.g., | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                 <code>/var/www/app/controllers/views/order</code>, <code>@app/views/order</code>. If not set, it will default | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                 to <code>@app/views/ControllerID</code>', | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             'baseClass' => 'This is the class that the new controller class will extend from. Please make sure the class exists and can be autoloaded.', | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     public function successMessage(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         return 'The controller has been generated successfully.'; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function generate(AbstractGeneratorCommand $command): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         if (!$command instanceof ControllerCommand) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             throw new InvalidArgumentException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $files = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $files[] = (new CodeFile( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $this->getControllerFile($command), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $this->render($command, 'controller') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         ))->withBasePath($this->aliases->get('@root')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         foreach ($command->getActions() as $action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $files[] = (new CodeFile( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 $this->getViewFile($command, $action), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 $this->render($command, 'view', ['action' => $action]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ))->withBasePath($this->aliases->get('@root')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  |         return $files; | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * Normalizes [[actions]] into an array of action IDs. | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * @return array an array of action IDs entered by the user | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     public function getActionIDs(ControllerCommand $command): array | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         return $command->getActions(); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      * @return string the controller class file path | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     private function getControllerFile(ControllerCommand $command): string | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         return $this->aliases->get( | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |             sprintf('%s/%s.php', $this->getDirectory() ?? '', $command->getControllerClass()) | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      * @param string $action the action ID | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |      * @return string the action view file path | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     public function getViewFile(ControllerCommand $command, string $action): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if (empty($command->getViewsPath())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             return $this->aliases->get( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 '@views/' . $command->getControllerID() . "/$action.php" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  |         return $this->aliases->get(str_replace('\\', '/', $command->getViewsPath()) . "/$action.php"); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 134 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 135 |  |  |  |