bluzphp /
bluzman
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @copyright Bluz PHP Team |
||
| 5 | * @link https://github.com/bluzphp/bluzman |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace Bluzman\Command\Generate; |
||
| 9 | |||
| 10 | use Bluz\Db\Table; |
||
|
0 ignored issues
–
show
|
|||
| 11 | use Bluz\Proxy\Db; |
||
|
0 ignored issues
–
show
The type
Bluz\Proxy\Db was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 12 | use Bluz\Validator\Validator; |
||
|
0 ignored issues
–
show
The type
Bluz\Validator\Validator was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 13 | use Bluzman\Command\AbstractCommand; |
||
| 14 | use Bluzman\Generator\Generator; |
||
| 15 | use Bluzman\Generator\GeneratorException; |
||
| 16 | use Bluzman\Input\InputArgument; |
||
| 17 | use Bluzman\Input\InputException; |
||
| 18 | use Bluzman\Input\InputOption; |
||
| 19 | use Symfony\Component\Console\Input\InputInterface; |
||
|
0 ignored issues
–
show
The type
Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 20 | use Symfony\Component\Console\Output\OutputInterface; |
||
|
0 ignored issues
–
show
The type
Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 21 | |||
| 22 | /** |
||
| 23 | * AbstractCommand |
||
| 24 | * |
||
| 25 | * @package Bluzman\Command\Generate |
||
| 26 | */ |
||
| 27 | abstract class AbstractGenerateCommand extends AbstractCommand |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @param InputInterface $input |
||
| 31 | * @param OutputInterface $output |
||
| 32 | * @return mixed |
||
| 33 | */ |
||
| 34 | abstract public function verify(InputInterface $input, OutputInterface $output): void; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Add Force Option |
||
| 38 | * |
||
| 39 | * @return void |
||
| 40 | 15 | */ |
|
| 41 | protected function addForceOption(): void |
||
| 42 | 15 | { |
|
| 43 | 15 | $force = new InputOption('--force', '-f', InputOption::VALUE_NONE, 'Rewrite previously generated files'); |
|
| 44 | 15 | $this->getDefinition()->addOption($force); |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Add Model Argument |
||
| 49 | * |
||
| 50 | * @return void |
||
| 51 | 15 | */ |
|
| 52 | protected function addModelArgument(): void |
||
| 53 | 15 | { |
|
| 54 | 15 | $model = new InputArgument('model', InputArgument::REQUIRED, 'Model name is required'); |
|
| 55 | 15 | $this->getDefinition()->addArgument($model); |
|
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Validate Model Argument |
||
| 60 | * |
||
| 61 | * @return void |
||
| 62 | * @throws InputException |
||
| 63 | 9 | */ |
|
| 64 | protected function validateModelArgument(): void |
||
| 65 | 9 | { |
|
| 66 | $model = $this->getInput()->getArgument('model'); |
||
| 67 | 9 | ||
| 68 | 9 | $validator = Validator::create() |
|
| 69 | 9 | ->string() |
|
| 70 | 9 | ->alphaNumeric() |
|
| 71 | ->noWhitespace(); |
||
| 72 | 9 | ||
| 73 | 9 | if ( |
|
| 74 | 5 | $this->getDefinition()->getArgument('model')->isRequired() |
|
| 75 | && !$validator->validate($model) |
||
| 76 | 4 | ) { |
|
| 77 | throw new InputException($validator->getError()); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Add Table Argument |
||
| 83 | 15 | * |
|
| 84 | * @return void |
||
| 85 | 15 | */ |
|
| 86 | 15 | protected function addTableArgument(): void |
|
| 87 | 15 | { |
|
| 88 | $table = new InputArgument('table', InputArgument::REQUIRED, 'Table name is required'); |
||
| 89 | $this->getDefinition()->addArgument($table); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Validate Table Argument |
||
| 94 | * |
||
| 95 | 1 | * @return void |
|
| 96 | * @throws InputException |
||
| 97 | 1 | */ |
|
| 98 | protected function validateTableArgument(): void |
||
| 99 | 1 | { |
|
| 100 | 1 | $table = $this->getInput()->getArgument('table'); |
|
| 101 | 1 | ||
| 102 | 1 | $validator = Validator::create() |
|
| 103 | ->string() |
||
| 104 | 1 | ->alphaNumeric('_') |
|
| 105 | 1 | ->noWhitespace(); |
|
| 106 | |||
| 107 | if ( |
||
| 108 | 1 | $this->getDefinition()->getArgument('table')->isRequired() |
|
| 109 | && !$validator->validate($table) |
||
| 110 | ) { |
||
| 111 | throw new InputException($validator->getError()); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get Table instance |
||
| 117 | * |
||
| 118 | * @param string $model |
||
| 119 | * |
||
| 120 | * @return Table |
||
| 121 | * @throws GeneratorException |
||
| 122 | */ |
||
| 123 | protected function getTableInstance(string $model): Table |
||
| 124 | { |
||
| 125 | $file = $this->getApplication()->getModelPath($model) . DS . 'Table.php'; |
||
|
0 ignored issues
–
show
|
|||
| 126 | if (!file_exists($file)) { |
||
| 127 | throw new GeneratorException( |
||
| 128 | "Model $model is not exist, run command `bluzman generate:model $model` before" |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | include_once $file; |
||
| 132 | $class = '\\Application\\' . ucfirst($model) . '\\Table'; |
||
| 133 | if (!class_exists($class)) { |
||
| 134 | throw new GeneratorException("Bluzman can't found `Table` class for model `$model`"); |
||
| 135 | } |
||
| 136 | return $class::getInstance(); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | 1 | * Required for correct mock it |
|
| 141 | * |
||
| 142 | 1 | * @param string $class |
|
| 143 | 1 | * @return mixed |
|
| 144 | */ |
||
| 145 | protected function getTemplate(string $class) |
||
| 146 | { |
||
| 147 | $class = '\\Bluzman\\Generator\\Template\\' . $class; |
||
| 148 | return new $class(); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Small wrapper for simplify code |
||
| 153 | * |
||
| 154 | * @param string $class |
||
| 155 | 5 | * @param string $file |
|
| 156 | * @param array $data |
||
| 157 | 5 | * |
|
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | protected function generateFile(string $class, string $file, array $data = []): void |
||
| 161 | { |
||
| 162 | 5 | if (file_exists($file) && !$this->getInput()->getOption('force')) { |
|
| 163 | 5 | $this->comment(" |> File <info>$file</info> already exists"); |
|
| 164 | 5 | return; |
|
| 165 | } |
||
| 166 | 5 | ||
| 167 | 5 | $template = $this->getTemplate($class); |
|
| 168 | 5 | $template->setFilePath($file); |
|
| 169 | $template->setTemplateData($data); |
||
| 170 | |||
| 171 | $generator = new Generator($template); |
||
| 172 | $generator->make(); |
||
| 173 | 2 | } |
|
| 174 | |||
| 175 | 2 | /** |
|
| 176 | 2 | * @param string $module |
|
| 177 | 2 | * @param string $controller |
|
| 178 | 2 | * @return string |
|
| 179 | */ |
||
| 180 | protected function getControllerPath(string $module, string $controller): string |
||
| 181 | { |
||
| 182 | return $this->getApplication()->getModulePath($module) |
||
| 183 | . DS . 'controllers' |
||
|
0 ignored issues
–
show
|
|||
| 184 | 1 | . DS . $controller |
|
| 185 | . '.php'; |
||
| 186 | 1 | } |
|
| 187 | 1 | ||
| 188 | 1 | /** |
|
| 189 | 1 | * @param string $module |
|
| 190 | * @param string $controller |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | protected function getViewPath(string $module, string $controller): string |
||
| 194 | { |
||
| 195 | return $this->getApplication()->getModulePath($module) |
||
| 196 | . DS . 'views' |
||
|
0 ignored issues
–
show
|
|||
| 197 | . DS . $controller |
||
| 198 | . '.phtml'; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @todo move it to DB class |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | protected function getPrimaryKey($table) |
||
| 206 | { |
||
| 207 | $connect = Db::getOption('connect'); |
||
| 208 | |||
| 209 | return Db::fetchColumn( |
||
| 210 | ' |
||
| 211 | SELECT COLUMN_NAME |
||
| 212 | FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE |
||
| 213 | WHERE TABLE_SCHEMA = ? |
||
| 214 | AND TABLE_NAME = ? |
||
| 215 | AND CONSTRAINT_NAME = ? |
||
| 216 | ', |
||
| 217 | [$connect['name'], $table, 'PRIMARY'] |
||
| 218 | ); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @todo move it to DB class |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | protected function getColumns($table) |
||
| 226 | { |
||
| 227 | $connect = Db::getOption('connect'); |
||
| 228 | |||
| 229 | return Db::fetchAll( |
||
| 230 | ' |
||
| 231 | SELECT |
||
| 232 | COLUMN_NAME as name, |
||
| 233 | COLUMN_TYPE as type |
||
| 234 | FROM INFORMATION_SCHEMA.COLUMNS |
||
| 235 | WHERE TABLE_SCHEMA = ? |
||
| 236 | AND TABLE_NAME = ? |
||
| 237 | ', |
||
| 238 | [$connect['name'], $table] |
||
| 239 | ); |
||
| 240 | } |
||
| 241 | } |
||
| 242 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths