| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace keeko\tools\command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use gossi\codegen\model\PhpClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use gossi\codegen\model\PhpTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use keeko\core\schema\ActionSchema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use keeko\tools\generator\GeneratorFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use keeko\tools\helpers\QuestionHelperTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use keeko\tools\utils\NamespaceResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use keeko\tools\utils\NameUtils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use phootwork\file\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use phootwork\lang\Text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Console\Question\ConfirmationQuestion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Console\Question\Question; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use keeko\tools\generator\BlankActionGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class GenerateActionCommand extends AbstractGenerateCommand { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	use QuestionHelperTrait; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 20 |  | 	protected function configure() { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 20 |  | 		$this | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 20 |  | 			->setName('generate:action') | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 20 |  | 			->setDescription('Generates an action') | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 20 |  | 			->addArgument( | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 20 |  | 				'name', | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 20 |  | 				InputArgument::OPTIONAL, | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 				'The name of the action, which should be generated. Typically in the form %nomen%-%verb% (e.g. user-create)' | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 20 |  | 			) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 20 |  | 			->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 20 |  | 				'classname', | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 20 |  | 				'c', | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 20 |  | 				InputOption::VALUE_OPTIONAL, | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 20 |  | 				'The main class name (If ommited, class name will be guessed from action name)', | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 				null | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 20 |  | 			) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 20 |  | 			->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 20 |  | 				'model', | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 20 |  | 				'm', | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 20 |  | 				InputOption::VALUE_OPTIONAL, | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 				'The model for which the actions should be generated, when there is no name argument (if ommited all models will be generated)' | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 20 |  | 			) | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 20 |  | 			->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 20 |  | 				'title', | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 20 |  | 				'', | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 20 |  | 				InputOption::VALUE_OPTIONAL, | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 				'The title for the generated option' | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 20 |  | 			) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 20 |  | 			->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 20 |  | 				'type', | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 20 |  | 				'', | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 20 |  | 				InputOption::VALUE_OPTIONAL, | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 				'The type of this action (list|create|read|update|delete) (if ommited template is guessed from action name)' | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 20 |  | 			)->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 20 |  | 				'acl', | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 20 |  | 				'', | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 20 |  | 				InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  | 				'The acl\s for this action (guest, user and/or admin)' | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 20 |  | 			) | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | // 			->addOption( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | // 				'schema', | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | // 				's', | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | // 				InputOption::VALUE_OPTIONAL, | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | // 				'Path to the database schema (if ommited, database/schema.xml is used)', | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | // 				null | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | // 			) | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 1 |  | 		; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 		 | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 20 |  | 		$this->configureGenerateOptions(); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 		 | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 20 |  | 		parent::configure(); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 20 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 * Checks whether actions can be generated at all by reading composer.json and verify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 * all required information are available | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 10 |  | 	private function preCheck() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 10 |  | 		$module = $this->packageService->getModule(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 10 |  | 		if ($module === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  | 			throw new \DomainException('No module definition found in composer.json - please run `keeko init`.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 4 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 9 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	protected function interact(InputInterface $input, OutputInterface $output) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		$this->preCheck(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		// check if the dialog can be skipped | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		$name = $input->getArgument('name'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$model = $input->getOption('model'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		if ($model !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		} else if ($name !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			$generateModel = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 			$modelQuestion = new ConfirmationQuestion('Do you want to generate an action based off a model?'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			$generateModel = $this->askConfirmation($modelQuestion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		// ask questions for a model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		if ($generateModel && !($this->package->getVendor() === 'keeko' && $this->modelService->isCoreSchema())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			$schema = str_replace(getcwd(), '', $this->getSchema()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			$allQuestion = new ConfirmationQuestion(sprintf('For all models in the schema (%s)?', $schema)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			$allModels = $this->askConfirmation($allQuestion); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			if (!$allModels) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 				$modelQuestion = new Question('Which model'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 				$modelQuestion->setAutocompleterValues($this->modelService->getModelNames()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 				$model = $this->askQuestion($modelQuestion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 				$input->setOption('model', $model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		} else if (!$generateModel) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 			$action = $this->getAction($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			// ask for title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 			$pkgTitle = $action->getTitle(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 			$title = $input->getOption('title'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 			if ($title === null && !empty($pkgTitle)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 				$title = $pkgTitle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 			$titleQuestion = new Question('What\'s the title for your action?', $title); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			$title = $this->askQuestion($titleQuestion); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 			$input->setOption('title', $title); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 			// ask for classname | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 			$pkgClass = $action->getClass(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			$classname = $input->getOption('classname'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			if ($classname === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 				if (!empty($pkgClass)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 					$classname = $pkgClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 				} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 					$classname = $this->guessClassname($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 			$classname = $this->askQuestion(new Question('Classname', $classname)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 			$input->setOption('classname', $classname); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 			// ask for acl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			$acls = $this->getAcl($action); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 			$aclQuestion = new Question('ACL (comma separated list, with these options: guest, user, admin)', implode(', ', $acls)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 			$acls = $this->askQuestion($aclQuestion); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 			$input->setOption('acl', $acls); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 10 |  | 	protected function execute(InputInterface $input, OutputInterface $output) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 10 |  | 		$this->preCheck(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		// 1. find out which action(s) to generate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 		// 2. generate the information in the package | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 		// 3. generate the code for the action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 9 |  | 		$name = $input->getArgument('name'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 9 |  | 		$model = $input->getOption('model'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		// only a specific action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 9 |  | 		if ($name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 3 |  | 			$this->generateAction($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 2 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 		// create action(s) from a model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 6 |  | 		else if ($model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 2 |  | 			$this->generateModel($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 2 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 		// if this is a core-module, find the related model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 4 |  | 		else if ($this->package->getVendor() == 'keeko' && $this->modelService->isCoreSchema()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 3 |  | 			$model = $this->package->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 3 |  | 			if ($this->modelService->hasModel($model)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 2 |  | 				$input->setOption('model', $model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 2 |  | 				$this->generateModel($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 2 |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 1 |  | 				$this->logger->error('Tried to find model on my own, wasn\'t lucky - please provide model with the --model option'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 3 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 		// anyway, generate all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 		else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 1 |  | 			foreach ($this->modelService->getModels() as $model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 1 |  | 				$this->generateModel($model->getOriginCommonName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 1 |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 8 |  | 		$this->packageService->savePackage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 8 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 5 |  | 	private function generateModel($modelName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 5 |  | 		$this->logger->info('Generate Action from Model: ' . $modelName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 | 5 |  | 		$input = $this->io->getInput(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 5 |  | 		$typeDump = $input->getOption('type'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 5 |  | 		if ($typeDump !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 1 |  | 			$types = [$typeDump]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 | 1 |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 4 |  | 			$types = ['create', 'read', 'list', 'update', 'delete']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 | 5 |  | 		foreach ($types as $type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 | 5 |  | 			$input->setOption('acl', ['admin']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 | 5 |  | 			$input->setOption('type', $type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 5 |  | 			$actionName = $modelName . '-' . $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 | 5 |  | 			$action = $this->getAction($actionName); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 5 |  | 			if (Text::create($action->getTitle())->isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 | 4 |  | 				$action->setTitle($this->getActionTitle($modelName, $type)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 | 5 |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 | 5 |  | 			$this->generateAction($actionName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 | 5 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 | 5 |  | 		$input->setOption('type', $typeDump); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 | 5 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 | 4 |  | 	private function getActionTitle($modelName, $type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  | 		switch ($type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 | 4 |  | 			case 'list': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 | 3 |  | 				return 'List all ' . NameUtils::pluralize($modelName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 | 4 |  | 			case 'create': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 | 4 |  | 			case 'read': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 | 4 |  | 			case 'update': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 | 4 |  | 			case 'delete': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 | 4 |  | 				return ucfirst($type) . 's ' . (in_array($modelName[0], ['a', 'e', 'i', 'o', 'u']) ? 'an' : 'a') . ' ' . $modelName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  | 	 * Generates an action. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  | 	 *   | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  | 	 * @param string $actionName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  | 	 * @param ActionSchema $action the action node from composer.json | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 | 8 |  | 	private function generateAction($actionName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 | 8 |  | 		$this->logger->info('Generate Action: ' . $actionName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 | 8 |  | 		$input = $this->io->getInput(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  | 		// get action and create it if it doesn't exist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 | 8 |  | 		$action = $this->getAction($actionName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 | 8 |  | 		if (($title = $input->getOption('title')) !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 | 2 |  | 			$action->setTitle($title); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 | 2 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 | 8 |  | 		if (Text::create($action->getTitle())->isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 | 1 |  | 			throw new \RuntimeException(sprintf('Cannot create action %s, because I am missing a title for it', $actionName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 | 7 |  | 		if (($classname = $input->getOption('classname')) !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 | 2 |  | 			$action->setClass($classname); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 | 2 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  | 		// guess classname if there is none set yet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 | 7 |  | 		if (Text::create($action->getClass())->isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 | 4 |  | 			$action->setClass($this->guessClassname($actionName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 | 4 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  | 		// guess title if there is none set yet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 | 7 |  | 		if (Text::create($action->getTitle())->isEmpty()  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 | 7 |  | 				&& $this->modelService->isModelAction($action) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 | 7 |  | 				&& $this->modelService->isCrudAction($action)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  | 			$modelName = $this->modelService->getModelNameByAction($action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  | 			$type = $this->modelService->getOperationByAction($action); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  | 			$action->setTitle($this->getActionTitle($modelName, $type)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  | 		// set acl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 | 7 |  | 		$action->setAcl($this->getAcl($action)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  | 		// generate code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 | 7 |  | 		$this->generateCode($action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 | 7 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 | 4 |  | 	private function guessClassname($name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 | 4 |  | 		$namespace = NamespaceResolver::getNamespace('src/action', $this->package); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 | 4 |  | 		return $namespace . '\\' . NameUtils::toStudlyCase($name) . 'Action'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  | 	 * @param string $actionName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  | 	 * @return ActionSchema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 | 8 |  | 	private function getAction($actionName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 | 8 |  | 		$action = $this->packageService->getAction($actionName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 | 8 |  | 		if ($action == null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 | 7 |  | 			$action = new ActionSchema($actionName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 | 7 |  | 			$module = $this->packageService->getModule(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 | 7 |  | 			$module->addAction($action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 | 7 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 | 8 |  | 		return $action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 | 7 |  | 	private function getAcl(ActionSchema $action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 | 7 |  | 		$acls = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 | 7 |  | 		$acl = $this->io->getInput()->getOption('acl'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 | 7 |  | 		if ($acl !== null && count($acl) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 | 7 |  | 			if (!is_array($acl)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 | 2 |  | 				$acl = [$acl]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 | 2 |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 | 7 |  | 			foreach ($acl as $group) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 | 7 |  | 				if (strpos($group, ',') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 | 1 |  | 					$groups = explode(',', $group); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 | 1 |  | 					foreach ($groups as $g) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 | 1 |  | 						$acls[] = trim($g); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 | 1 |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 | 1 |  | 				} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 | 6 |  | 					$acls[] = $group; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 | 7 |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 | 7 |  | 			return $acls; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  | 		// read default from package | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  | 		if (!$action->getAcl()->isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  | 			return $action->getAcl()->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  | 		return $acls; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  | 	 * Generates code for an action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  | 	 * @param ActionSchema $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 | 7 |  | 	private function generateCode(ActionSchema $action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 | 7 |  | 		$input = $this->io->getInput(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 | 7 |  | 		$trait = null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  | 		// class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 | 7 |  | 		$class = new PhpClass($action->getClass()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 | 7 |  | 		$filename = $this->codegenService->getFilename($class); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 | 7 |  | 		$traitNs = $class->getNamespace() . '\\base'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 | 7 |  | 		$traitName = $class->getName() . 'Trait'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 | 7 |  | 		$overwrite = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  | 		// load from reflection, when class exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 | 7 |  | 		if (file_exists($filename)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  | 			// load trait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 | 1 |  | 			$trait = new PhpTrait($traitNs . '\\' . $traitName); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 | 1 |  | 			$traitFile = new File($this->codegenService->getFilename($trait)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 | 1 |  | 			if ($traitFile->exists()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 | 1 |  | 				require_once($traitFile->getPathname()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 | 1 |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  | 			// load class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 | 1 |  | 			require_once($filename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 | 1 |  | 			$class = PhpClass::fromReflection(new \ReflectionClass($action->getClass())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 | 1 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  | 		// anyway seed class information | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  | 		else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 | 6 |  | 			$class->addUseStatement('keeko\\core\\action\\AbstractAction'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 | 6 |  | 			$class->setParentClassName('AbstractAction'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 | 6 |  | 			$class->setDescription($action->getTitle()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 | 6 |  | 			$class->setLongDescription($action->getDescription()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 | 6 |  | 			$this->codegenService->addAuthors($class, $this->package); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  | 		// create base trait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 | 7 |  | 		if ($input->getOption('model') !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 | 5 |  | 			if (($type = $input->getOption('type')) === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 | 1 |  | 				$text = Text::create($action->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 | 1 |  | 				$type = $text->substring($text->indexOf('-') + 1)->toString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 | 1 |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 | 5 |  | 			$generator = GeneratorFactory::createActionTraitGenerator($type, $this->service); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 | 5 |  | 			$trait = $generator->generate($traitNs . '\\' . $traitName, $action); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 | 5 |  | 			$this->codegenService->addAuthors($trait, $this->package); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 | 5 |  | 			$this->codegenService->dumpStruct($trait, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 | 5 |  | 			if (!$class->hasTrait($trait)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 | 4 |  | 				$class->addTrait($trait); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 | 4 |  | 				$overwrite = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 | 4 |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 | 5 |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  | 			// create blank action methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 | 2 |  | 			if (!$class->hasMethod('run')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 | 2 |  | 				$generator = new BlankActionGenerator($this->service); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 | 2 |  | 				$class = $generator->generate($class); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 | 2 |  | 				$overwrite = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 | 2 |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 | 7 |  | 		$this->codegenService->dumpStruct($class, $overwrite); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 | 7 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 394 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 395 |  |  |  | 
            
                        
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.