Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| TemplateCommand | |
0.00% |
0 / 49 |
|
0.00% |
0 / 5 |
306 | |
0.00% |
0 / 1 |
| execute | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 | |||
| getTemplatePath | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getContent | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
42 | |||
| bake | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| _filteredAssociations | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace AdminTheme\Command\Bake; |
| 5 | |
| 6 | use Bake\Command\TemplateCommand as BakeTemplateCommand; |
| 7 | use Bake\Utility\Model\AssociationFilter; |
| 8 | use Cake\Console\Arguments; |
| 9 | use Cake\Console\ConsoleIo; |
| 10 | use Cake\Core\Configure; |
| 11 | use Cake\Utility\Inflector; |
| 12 | |
| 13 | class TemplateCommand extends BakeTemplateCommand |
| 14 | { |
| 15 | /** |
| 16 | * Actions to use for scaffolding |
| 17 | * |
| 18 | * @var array<string> |
| 19 | */ |
| 20 | public array $scaffoldActions = ['index', 'view', 'add', 'edit', 'search_results']; |
| 21 | |
| 22 | /** |
| 23 | * Execute the command. |
| 24 | * |
| 25 | * @param \Cake\Console\Arguments $args The command arguments. |
| 26 | * @param \Cake\Console\ConsoleIo $io The console io |
| 27 | * @return int|null The exit code or null for success |
| 28 | */ |
| 29 | public function execute(Arguments $args, ConsoleIo $io): ?int |
| 30 | { |
| 31 | $this->extractCommonProperties($args); |
| 32 | $name = $args->getArgument('name') ?? ''; |
| 33 | $name = $this->_getName($name); |
| 34 | |
| 35 | if (empty($name)) { |
| 36 | return parent::execute($args, $io); |
| 37 | } |
| 38 | |
| 39 | $controller = $args->getOption('controller'); |
| 40 | $this->controller($args, $name, $controller); |
| 41 | $this->model($name); |
| 42 | |
| 43 | $vars = $this->_loadController($io); |
| 44 | $methods = $this->scaffoldActions; |
| 45 | |
| 46 | foreach ($methods as $method) { |
| 47 | try { |
| 48 | $content = $this->getContent($args, $io, $method, $vars); |
| 49 | $this->bake($args, $io, $method, $content); |
| 50 | } catch (\Exception $e) { |
| 51 | $io->error($e->getMessage()); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return static::CODE_SUCCESS; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get the path base for view templates. |
| 60 | * |
| 61 | * @param \Cake\Console\Arguments $args The arguments |
| 62 | * @param string|null $container Unused. |
| 63 | * @return string |
| 64 | */ |
| 65 | public function getTemplatePath(Arguments $args, ?string $container = null): string |
| 66 | { |
| 67 | $path = Configure::read('App.paths.templates')[0]; |
| 68 | $path .= 'Admin' . DS . $this->controllerName . DS; |
| 69 | |
| 70 | return $path; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Builds content from template and variables |
| 75 | * |
| 76 | * @param \Cake\Console\Arguments $args The CLI arguments |
| 77 | * @param \Cake\Console\ConsoleIo $io The console io |
| 78 | * @param string $action name to generate content to |
| 79 | * @param array|null $vars passed for use in templates |
| 80 | * @return string Content from template |
| 81 | */ |
| 82 | public function getContent(Arguments $args, ConsoleIo $io, string $action, ?array $vars = null): string |
| 83 | { |
| 84 | if (!$vars) { |
| 85 | $vars = $this->_loadController($io); |
| 86 | } |
| 87 | |
| 88 | if (empty($vars['primaryKey'])) { |
| 89 | $io->error('Cannot generate views for models with no primary key'); |
| 90 | $this->abort(); |
| 91 | } |
| 92 | |
| 93 | if (in_array($action, $this->excludeHiddenActions)) { |
| 94 | $vars['fields'] = array_diff($vars['fields'], $vars['hidden']); |
| 95 | } |
| 96 | |
| 97 | $renderer = $this->createTemplateRenderer() |
| 98 | ->set('action', $action) |
| 99 | ->set('plugin', $this->plugin) |
| 100 | ->set($vars); |
| 101 | |
| 102 | $indexColumns = 0; |
| 103 | if ($action === 'index' && $args->getOption('index-columns') !== null) { |
| 104 | $indexColumns = $args->getOption('index-columns'); |
| 105 | } |
| 106 | $renderer->set('indexColumns', $indexColumns); |
| 107 | |
| 108 | return $renderer->generate("AdminTheme.Template/$action"); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Assembles and writes bakes the view file. |
| 113 | * |
| 114 | * @param \Cake\Console\Arguments $args CLI arguments |
| 115 | * @param \Cake\Console\ConsoleIo $io Console io |
| 116 | * @param string $template Template file to use. |
| 117 | * @param string|true $content Content to write. |
| 118 | * @param ?string $outputFile The output file to create. If null will use `$template` |
| 119 | * @return void |
| 120 | */ |
| 121 | public function bake( |
| 122 | Arguments $args, |
| 123 | ConsoleIo $io, |
| 124 | string $template, |
| 125 | string|bool $content = '', |
| 126 | ?string $outputFile = null |
| 127 | ): void { |
| 128 | if ($outputFile === null) { |
| 129 | $outputFile = $template; |
| 130 | } |
| 131 | if ($content === true) { |
| 132 | $content = $this->getContent($args, $io, $template); |
| 133 | } |
| 134 | if (empty($content)) { |
| 135 | $io->err("<warning>No generated content for '{$template}.{$this->ext}', not generating template.</warning>"); |
| 136 | return; |
| 137 | } |
| 138 | $path = $this->getTemplatePath($args); |
| 139 | $filename = $path . Inflector::underscore($outputFile) . '.' . $this->ext; |
| 140 | |
| 141 | $io->out("\n" . sprintf('Baking `%s` view template file...', $outputFile), 1, ConsoleIo::NORMAL); |
| 142 | $io->createFile($filename, $content, $this->force); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get filtered associations |
| 147 | * |
| 148 | * @param \Cake\ORM\Table $model Table |
| 149 | * @return array associations |
| 150 | */ |
| 151 | protected function _filteredAssociations(\Cake\ORM\Table $model): array |
| 152 | { |
| 153 | if ($this->_associationFilter === null) { |
| 154 | $this->_associationFilter = new AssociationFilter(); |
| 155 | } |
| 156 | |
| 157 | return $this->_associationFilter->filterAssociations($model); |
| 158 | } |
| 159 | } |