|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Commands; |
| 4 | + |
| 5 | +use CodeIgniter\CLI\BaseCommand; |
| 6 | +use CodeIgniter\CLI\CLI; |
| 7 | + |
| 8 | +/** |
| 9 | + * Generates a controller in the specified module. |
| 10 | + */ |
| 11 | +class ModuleController extends BaseCommand |
| 12 | +{ |
| 13 | + /** |
| 14 | + * The Command's Group |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $group = 'SimpleTine'; |
| 19 | + |
| 20 | + /** |
| 21 | + * The Command's Name |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $name = 'module:controller'; |
| 26 | + |
| 27 | + /** |
| 28 | + * The Command's Description |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected $description = 'Generate a controller into a module'; |
| 33 | + |
| 34 | + /** |
| 35 | + * The Command's Usage |
| 36 | + * |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + protected $usage = 'module:controller <module> <file>'; |
| 40 | + |
| 41 | + /** |
| 42 | + * The Command's Arguments |
| 43 | + * |
| 44 | + * @var array |
| 45 | + */ |
| 46 | + protected $arguments = [ |
| 47 | + 'module' => 'The name of the existing module directory', |
| 48 | + 'file' => 'The name of the file to create', |
| 49 | + ]; |
| 50 | + |
| 51 | + /** |
| 52 | + * The Command's Options |
| 53 | + * |
| 54 | + * @var array |
| 55 | + */ |
| 56 | + protected $options = []; |
| 57 | + |
| 58 | + /** |
| 59 | + * Execute the command. |
| 60 | + * |
| 61 | + * @param array $params |
| 62 | + */ |
| 63 | + public function run(array $params) |
| 64 | + { |
| 65 | + helper('inflector'); |
| 66 | + |
| 67 | + $directoryMainFolder = 'Modules'; |
| 68 | + if (!is_dir(APPPATH . $directoryMainFolder)) { |
| 69 | + if (mkdir(APPPATH . $directoryMainFolder, 0755, true)) { |
| 70 | + CLI::write('Modules Folder created', 'green'); |
| 71 | + } else { |
| 72 | + CLI::error('Modules Folder creation failed. Please create a new folder (Modules) inside APP or try again.'); |
| 73 | + return; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + $directoryName = array_shift($params); |
| 78 | + $fileName = array_shift($params); |
| 79 | + |
| 80 | + if (!$directoryName || !$fileName) { |
| 81 | + CLI::error('Both module name and file name are required.'); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + $moduleDirectory = "$directoryMainFolder/$directoryName"; |
| 86 | + if (!is_dir(APPPATH . $moduleDirectory)) { |
| 87 | + mkdir(APPPATH . $moduleDirectory, 0755, true); |
| 88 | + CLI::write('Module folder created - ' . APPPATH . $moduleDirectory, 'green'); |
| 89 | + } |
| 90 | + |
| 91 | + $controllerDirectory = APPPATH . $moduleDirectory . '/Controllers'; |
| 92 | + if (!is_dir($controllerDirectory)) { |
| 93 | + mkdir($controllerDirectory, 0755, true); |
| 94 | + } |
| 95 | + |
| 96 | + $namespace = str_replace('/', '\\', $moduleDirectory); |
| 97 | + $className = pascalize($fileName); |
| 98 | + $controllerTemplate = $this->getTemplate( |
| 99 | + 'controller.new.tpl.php', |
| 100 | + [ |
| 101 | + '{namespace}' => "$namespace\\Controllers", |
| 102 | + '{useModelStatement}' => "$namespace\\Models\\$className", |
| 103 | + '{useStatement}' => 'App\Controllers\BaseController', |
| 104 | + '{class}' => $className, |
| 105 | + '{modelClass}' => $className, |
| 106 | + '{extends}' => 'BaseController', |
| 107 | + '{directoryName}' => $directoryName, |
| 108 | + ] |
| 109 | + ); |
| 110 | + |
| 111 | + $controllerPath = APPPATH . $moduleDirectory . "/Controllers/$className.php"; |
| 112 | + file_put_contents($controllerPath, $controllerTemplate); |
| 113 | + |
| 114 | + CLI::write("Controller '$className' created in module '$directoryName'.", 'green'); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Get the template content with placeholders replaced. |
| 119 | + * |
| 120 | + * @param string $templateFile |
| 121 | + * @param array $placeholders |
| 122 | + * @return string |
| 123 | + */ |
| 124 | + protected function getTemplate(string $templateFile, array $placeholders): string |
| 125 | + { |
| 126 | + $templatePath = APPPATH . 'Commands/Views/' . $templateFile; |
| 127 | + |
| 128 | + if (!file_exists($templatePath)) { |
| 129 | + CLI::write('Template file not found: ' . $templateFile, 'red'); |
| 130 | + return ''; |
| 131 | + } |
| 132 | + |
| 133 | + $templateContent = file_get_contents($templatePath); |
| 134 | + |
| 135 | + foreach ($placeholders as $placeholder => $value) { |
| 136 | + $templateContent = str_replace($placeholder, $value, $templateContent); |
| 137 | + } |
| 138 | + |
| 139 | + return str_replace('<@php', '<?php', $templateContent); |
| 140 | + } |
| 141 | +} |
0 commit comments