├── .gitignore ├── README.md ├── README_zh-TW.md ├── composer.json └── src └── Commands ├── CliCreate.php ├── Controller ├── CreateController.php └── template │ ├── controller │ ├── resourceController │ ├── resourcePresenter │ └── rest │ └── resourceFunctions │ ├── create │ ├── delete │ ├── edit │ ├── index │ ├── new │ ├── show │ └── update └── Model ├── CreateModel.php └── template ├── basicModel ├── entity ├── entityModel ├── manualModel └── model /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeIgniter4_CLI-Create 2 | 3 | [![Latest Stable Version](https://poser.pugx.org/monken/cli-create/v)](//packagist.org/packages/monken/cli-create) [![Total Downloads](https://poser.pugx.org/monken/cli-create/downloads)](//packagist.org/packages/monken/cli-create) [![License](https://poser.pugx.org/monken/cli-create/license)](//packagist.org/packages/monken/cli-create) 4 | 5 | Cli-Create is based on CodeIgniter4. It will help you generate template files more quickly when developing projects with CodeIgniter4. 6 | 7 | [中文使用說明](https://hackmd.io/@monkenWu/ByZF1n4HL) 8 | 9 | [Guide](https://hackmd.io/@monkenWu/HJndHeESU) 10 | 11 | ## Install 12 | 13 | ### Prerequisites 14 | 1. CodeIgniter Framework 4.* 15 | 2. Composer 16 | 17 | ### Composer Install 18 | 19 | ``` 20 | composer require monken/cli-create 21 | ``` 22 | ### Use Library 23 | 24 | Open Terminal in Mac/Linux or go to Run > “cmd” in Windows and navigate to CodeIgniter4 project’s root: 25 | 26 | ``` 27 | php spark list 28 | ``` 29 | 30 | Now, if you see the following message, the installation is successful. 31 | 32 | ``` 33 | CodeIgniter CLI Tool - Version 4.0.2 - Server-Time: 2020-03-09 12:04:21pm 34 | 35 | 36 | Cli-Create 37 | create:controller Create a new controller file. 38 | create:model Create a new model file. 39 | ``` 40 | 41 | # Guide 42 | 43 | ## create:controller 44 | 45 | Create a new controller file. 46 | 47 | * Use 48 | ``` 49 | $ php spark create:controller [controller_name] [Options] 50 | ``` 51 | 52 | * Description: 53 | ``` 54 | Create a new controller file. 55 | ``` 56 | * Arguments: 57 | 1. controller_name : The controller name. 58 | 59 | * Options: 60 | ``` 61 | -nobase Do not extends BaseControllers Class. 62 | -usemodel Choose models. 63 | -space Create folders and files according to the path you typed. 64 | ``` 65 | 66 | ### Create a normal Controller 67 | 68 | You can use : 69 | 70 | ``` 71 | $ php spark create:contorller [controller_name] 72 | ``` 73 | 74 | Now, in "app/Controllers" You can see the new Contorller File like this : 75 | 76 | ```php 77 | The namespace in Codeigniter usually maps the actual file storage path, so using this command will automatically create folders according to the value you entered. 157 | 158 | Now, in “app/Controllers/System/Admin” You can see the new "-nobase" Controller File like this : 159 | 160 | ```php 161 | respond([ 224 | "status" => true, 225 | "msg" => "index method successful." 226 | ]); 227 | } 228 | 229 | /*****/ 230 | ``` 231 | 232 | Then, go to "app / Config / Routes.php" and you will see that the routing settings for this Controller have been registered in your configuration file : 233 | 234 | ```php 235 | /** 236 | * -------------------------------------------------------------------- 237 | * Route Definitions 238 | * -------------------------------------------------------------------- 239 | */ 240 | 241 | // We get a performance increase by specifying the default 242 | // route since we don't have to scan directories. 243 | $routes->get('/', 'Home::index'); 244 | 245 | //CliCreate-add-in-2020-04-10 05:42:27 246 | $routes->resource('user',[ 247 | 'controller' =>'\App\Controllers\User', 248 | ]); 249 | ``` 250 | 251 | > Please note that the function of automatically writing to the configuration file is to find the relative position through the comments in the file. Please do not change the comment in the official configuration file, so as not to write the wrong place and cause a program error. 252 | 253 | ### Create RESTFul API with different route name and controller name 254 | 255 | You may use this option frequently. Sometimes, our router will not be associated with the location where the Controllers are stored, then it is very suitable to use this option. 256 | 257 | ``` 258 | php spark create:controller [controller_name] -rest -d -space 259 | ``` 260 | 261 | The "-d" option allows you to customize the router, and "-space" allows you to customize the controller's namespace. We believe that this requirement needs to be met using these two options, so please use this example to demo. 262 | 263 | 264 | ![](https://i.imgur.com/v9wi0WX.png) 265 | 266 | 267 | The “-d” option can be declared after other options. 268 | 269 | Now, in “app/Controllers/System/Api” You can see the new RESTFul Controller File like this : 270 | 271 | ```php 272 | 273 | respond([ 285 | "status" => true, 286 | "msg" => "index method successful." 287 | ]); 288 | } 289 | 290 | /*********/ 291 | 292 | ``` 293 | 294 | Then, go to "app / Config / Routes.php" and you will see that the routing settings for this Controller have been registered in your configuration file : 295 | 296 | ```php 297 | /** 298 | * -------------------------------------------------------------------- 299 | * Route Definitions 300 | * -------------------------------------------------------------------- 301 | */ 302 | 303 | // We get a performance increase by specifying the default 304 | // route since we don't have to scan directories. 305 | $routes->get('/', 'Home::index'); 306 | 307 | //CliCreate-add-in-2020-04-10 05:49:09 308 | $routes->resource('api/user',[ 309 | 'controller' =>'\App\Controllers\System\Api\User', 310 | ]); 311 | 312 | ``` 313 | 314 | 315 | ### Create "WebSafe" RESTFul API 316 | 317 | codeigniter allows you to add the "web safe" attribute when setting routes to have it generate update and delete methods that work with HTML forms 318 | 319 | ``` 320 | php spark create:controller [controller_name] -rest -w 321 | ``` 322 | 323 | The “-w” option can be declared after other options. 324 | 325 | ![](https://i.imgur.com/ae0gkXi.png) 326 | 327 | Then, go to "app / Config / Routes.php" and you will see the websafe attribute appear in the router's settings. 328 | 329 | ```php 330 | //CliCreate-add-in-2020-04-10 05:54:38 331 | $routes->resource('user',[ 332 | 'controller' =>'\App\Controllers\User', 333 | 'websafe' => 1, 334 | ]); 335 | ``` 336 | 337 | ### Create RESTFul API and limit the routes made 338 | 339 | You can restrict the routes generated with the option. Only routes that match one of these methods will be created. The rest will be ignored. 340 | 341 | ``` 342 | php spark create:controller [controller_name] -rest -o 343 | ``` 344 | 345 | ![](https://i.imgur.com/NlzcKem.png) 346 | 347 | 348 | The “-o” option can be declared after other options. 349 | 350 | Now, in “app/Controllers/System/Api” You can see the new RESTFul Controller File like this : 351 | 352 | ![](https://i.imgur.com/oReZWr7.png) 353 | 354 | Then, go to "app / Config / Routes.php" and you will see that the routes you allow to be created are recorded in the router setting : 355 | 356 | ```php 357 | /** 358 | * -------------------------------------------------------------------- 359 | * Route Definitions 360 | * -------------------------------------------------------------------- 361 | */ 362 | 363 | // We get a performance increase by specifying the default 364 | // route since we don't have to scan directories. 365 | $routes->get('/', 'Home::index'); 366 | 367 | //CliCreate-add-in-2020-04-10 05:59:28 368 | $routes->resource('user',[ 369 | 'controller' =>'\App\Controllers\User', 370 | 'only' => ['index','show','create','update','delete'], 371 | ]); 372 | 373 | ``` 374 | 375 | ## create:controller -model 376 | 377 | Create a new model for use with the new controller. 378 | 379 | * USE 380 | 1. Use the options provided by the "create: model" directive. 381 | ``` 382 | $ php spark create:controller [controller_name] [controller_options] -model [model_name] 383 | ``` 384 | 3. The options provided using the "Create: Model" command are not used. 385 | ``` 386 | $ php spark create:controller [controller_name] [controller_options] -model=[model_options] [model_name] [entity_name] 387 | ``` 388 | 389 | * Description: 390 | ``` 391 | Create new controller and model. 392 | ``` 393 | * Arguments: 394 | 1. controller_name : controller name 395 | 2. controller_options : Options provid by the controller. 396 | 3. model_options : Options provid by the [create:model](#create:model) .If you want to use more than one option, please note that they must be separated by a comma, with "-" removed, and immediately after "=". 397 | 1. model_name : model name. 398 | 2. entity_name : entity name.If you use the entity option, you can type this argument. 399 | 400 | ### Create new controller and model 401 | 402 | Cli-Create provides a way for you to create a controller and a model at the same time. You can accomplish this with a single line of "spark" command. 403 | 404 | ``` 405 | php spark create:controller [controller_name] -model [model_name] 406 | ``` 407 | 408 | ![](https://i.imgur.com/u9jhIon.png) 409 | 410 | Now, open app / Controllers / Blog.php and you should see the new controller file, and the correct model namespace has been written: 411 | 412 | ```php 413 | The options you use must be immediately after "=". You can refer to all the options mentioned in [create:model](#create:model) for these options. 504 | 505 | Now, open app / Controllers / Blog.php and you should see the new controller file, and the required model has been declared: 506 | 507 | ```php 508 | respond([ 574 | "status" => true, 575 | "msg" => "index method successful." 576 | ]); 577 | } 578 | /********/ 579 | ``` 580 | 581 | ## create:model 582 | 583 | Create a new model file. 584 | 585 | * Use 586 | ``` 587 | $ php spark create:model [model_name] [entity_name] [Options] 588 | ``` 589 | 590 | * Description: 591 | ``` 592 | Create a new model file. 593 | ``` 594 | * Arguments: 595 | 1. model_name : The model name 596 | 2. entity_name : The entity name. If you selected -entity option. You can type this arguments. 597 | * Options: 598 | ``` 599 | -basic Creates a basic model file. 600 | -entity Uses Entity Classes. 601 | -manual Creates a Manual Model. 602 | -space Creates folders and files according to the path you typed. 603 | ``` 604 | 605 | ### Create a normal model 606 | 607 | The model class has a few configuration options that can be set to allow the class’ methods to work seamlessly for you. 608 | 609 | You can use : 610 | 611 | ``` 612 | $ php spark create:model [model_name] 613 | ``` 614 | 615 | Now, in "app/Models" You can see the new Model File like this : 616 | 617 | ```php 618 | db =& $db; 684 | } 685 | } 686 | ``` 687 | 688 | ### Create a entity model 689 | 690 | CodeIgniter supports Entity classes as a first-class citizen in it’s database layer, while keeping them completely optional to use. They are commonly used as part of the Repository pattern, but can be used directly with the Model if that fits your needs better. 691 | 692 | You can use this command : 693 | 694 | ``` 695 | $ php spark create:model [model_name] [entity_name] -entity 696 | ``` 697 | Now, in “app/Models” You can see the new Manual Model File like this : 698 | 699 | ```php 700 | The namespace in Codeigniter usually maps the actual file storage path, so using this command will automatically create folders according to the value you entered. 741 | 742 | Now, in “app/Models/Api/System” You can see the new Manual Model File like this : 743 | 744 | ```php 745 | 在 Codeigniter 中的命名空間通常映射自實際檔案位置。所以使用這個指令時,將會自動創建符合你所輸入的命名空間規則的資料夾。 153 | 154 | 現在,打開「 app/Controllers/System/Admin 」你應該可以看見新的「 -nobase 」控制器檔案: 155 | 156 | ```php 157 | respond([ 221 | "status" => true, 222 | "msg" => "index method successful." 223 | ]); 224 | } 225 | 226 | /*****/ 227 | ``` 228 | 229 | 然後,打開 app / Config / Routes.php 你會看到這個控制器所需的路由設定,已經在你的設定檔案中被註冊了: 230 | 231 | ```php 232 | /** 233 | * -------------------------------------------------------------------- 234 | * Route Definitions 235 | * -------------------------------------------------------------------- 236 | */ 237 | 238 | // We get a performance increase by specifying the default 239 | // route since we don't have to scan directories. 240 | $routes->get('/', 'Home::index'); 241 | 242 | //CliCreate-add-in-2020-04-10 05:42:27 243 | $routes->resource('user',[ 244 | 'controller' =>'\App\Controllers\User', 245 | ]); 246 | ``` 247 | 248 | > 請注意,自動寫入設定檔案的功能,是通過檔案中的註解位置定位到要寫入的確切位置。請不要修改正式設定檔案中的任何註解,以免寫入錯誤,造成程式錯誤。 249 | 250 | ### 使用不同的路由名稱和控制器名稱創建 RESTFul API 251 | 252 | 你可能會經常使用這個選項,因為有的時候,我們的路由不會與控制器的存放位置(或命名空間)有關聯,那麼使用這個選項將會非常合適。 253 | 254 | ``` 255 | php spark create:controller [controller_name] -rest -d -space 256 | ``` 257 | 258 | -d 選項可以自訂義路由, -space 選項可以自訂義控制器的命名空間。我們認為需要用這兩個選項才能滿足這個需求,所以請用這個範例來進行演示。 259 | 260 | ![](https://i.imgur.com/v9wi0WX.png) 261 | 262 | -d 這個選項可以與任何選項混用。 263 | 264 | 現在,在 app/Controllers 路徑下,你可以看到新的 RESTFul 控制器檔案,就像這樣: 265 | 266 | ```php 267 | 268 | respond([ 280 | "status" => true, 281 | "msg" => "index method successful." 282 | ]); 283 | } 284 | 285 | /*********/ 286 | 287 | ``` 288 | 289 | 然後,打開 app / Config / Routes.php 你會看到這個控制器所需的路由設定,已經在你的設定檔案中被註冊了: 290 | 291 | ```php 292 | /** 293 | * -------------------------------------------------------------------- 294 | * Route Definitions 295 | * -------------------------------------------------------------------- 296 | */ 297 | 298 | // We get a performance increase by specifying the default 299 | // route since we don't have to scan directories. 300 | $routes->get('/', 'Home::index'); 301 | 302 | //CliCreate-add-in-2020-04-10 05:49:09 303 | $routes->resource('api/user',[ 304 | 'controller' =>'\App\Controllers\System\Api\User', 305 | ]); 306 | 307 | ``` 308 | 309 | ### 新建 WebSafe 的 RESTFul API 310 | 311 | codeigniter 允許你在設定路由時添加 web safe 屬性,它將會自動生成友善於 HTML 表單的更新與刪除方法。 312 | 313 | ``` 314 | php spark create:controller [controller_name] -rest -w 315 | ``` 316 | 317 | -w 這個選項可以與任何選項混用。 318 | 319 | ![](https://i.imgur.com/ae0gkXi.png) 320 | 321 | 然後,打開 app / Config / Routes.php 你會看到路由的設定中出現了 websafe 屬性。 322 | 323 | ```php 324 | //CliCreate-add-in-2020-04-10 05:54:38 325 | $routes->resource('user',[ 326 | 'controller' =>'\App\Controllers\User', 327 | 'websafe' => 1, 328 | ]); 329 | ``` 330 | 331 | ### Create RESTFul API and limit the routes made 332 | 333 | 你可以限制所要生成的路由為何,只有符合你所選擇的路由才會被創建,其他的路由將會被忽略。 334 | 335 | ``` 336 | php spark create:controller [controller_name] -rest -o 337 | ``` 338 | 339 | ![](https://i.imgur.com/NlzcKem.png) 340 | 341 | 342 | -o 這個選項可以與任何選項混用。 343 | 344 | 現在,在 app/Controllers 路徑下,你可以看到新的 RESTFul 控制器檔案,就像這樣: 345 | 346 | ![](https://i.imgur.com/oReZWr7.png) 347 | 348 | 然後,打開 app / Config / Routes.php 你會看到你所允許創建的路由將會被紀錄在路由設定中。 349 | 350 | ```php 351 | /** 352 | * -------------------------------------------------------------------- 353 | * Route Definitions 354 | * -------------------------------------------------------------------- 355 | */ 356 | 357 | // We get a performance increase by specifying the default 358 | // route since we don't have to scan directories. 359 | $routes->get('/', 'Home::index'); 360 | 361 | //CliCreate-add-in-2020-04-10 05:59:28 362 | $routes->resource('user',[ 363 | 'controller' =>'\App\Controllers\User', 364 | 'only' => ['index','show','create','update','delete'], 365 | ]); 366 | 367 | ``` 368 | 369 | ## create:controller -model 370 | 371 | 同時創建新的模型檔案以及控制器檔案。 372 | 373 | * 使用規則 374 | 1. 不使用 create:model 指令的 options 375 | ``` 376 | $ php spark create:controller [controller_name] [controller_options] -model [model_name] 377 | ``` 378 | 2. 使用 create:model 指令的 option 379 | ``` 380 | $ php spark create:controller [controller_name] [controller_options] -model=[model_options] [model_name] [entity_name] 381 | ``` 382 | 383 | * 簡介: 384 | ``` 385 | 同時新建控制器以及模型檔案。 386 | ``` 387 | * 說明: 388 | 1. controller_name : 控制器名稱。 389 | 2. controller_options : 控制器可使用的選項。 390 | 3. model_options : 可用選項,與 [create:model](#create:model) 指令提供的選項相同,若你想使用的選項不只一個,請注意,它們必須以逗號分隔且去除 「-」 且串接在 「=」 後面。 391 | 1. model_name : 模型名稱。 392 | 2. entity_name : 實體名稱。如果你使用了 entity 這個選項,你就可以輸入這個引數。 393 | 394 | ### 同時新建控制器與模型 395 | 396 | Cli-Create 提供了讓你同時創建控制器以及模型的方法,你可以使用一行 spark 指令就完成這件事。 397 | 398 | ``` 399 | php spark create:controller [controller_name] -model [model_name] 400 | ``` 401 | 402 | ![](https://i.imgur.com/u9jhIon.png) 403 | 404 | 現在,打開 app/Controllers/Blog.php 你應該可以看見新的控制器檔案,並且已經寫入了正確的模型命名空間: 405 | 406 | ```php 407 | 你所使用的選項都必須緊接在「=」之後。這些選項你可以參考 [create:model](#create:model) 提到的所有選項。 498 | 499 | 500 | 現在,打開 app/Controllers/Blog.php你應該可以看見新控制器檔案,並且所需的模型已經被宣告了: 501 | 502 | ```php 503 | respond([ 569 | "status" => true, 570 | "msg" => "index method successful." 571 | ]); 572 | } 573 | /********/ 574 | ``` 575 | 576 | ## create:model 577 | 578 | 創建新的模型檔案。 579 | 580 | * 使用規則 581 | ``` 582 | $ php spark create:model [model_name] [entity_name] [Options] 583 | ``` 584 | 585 | * 簡介: 586 | ``` 587 | Create a new controller file. 588 | ``` 589 | * 引數: 590 | 1. model_name : 模型名稱。 591 | 2. entity_name : 實體名稱。如果你使用了 -entity 這個選項,你就可以輸入這個引數。 592 | * 選項: 593 | ``` 594 | -basic 新建基本的模型。 595 | -entity 新建模型與實體。 596 | -manual 新建手動模型。 597 | -space 根據你鍵入的路徑創建文件夾和文件。 598 | ``` 599 | 600 | ### 新建一個普通模型 601 | 602 | CodeIgniter\Model 提供一些設定的選項,通過設定這些選項模型類別將可以更好地運作。 603 | 604 | 你可以使用以下指令 : 605 | 606 | ``` 607 | $ php spark create:model [model_name] 608 | ``` 609 | 610 | 現在,打開「 app/Models 」你應該可以看見新的模型檔案: 611 | 612 | ```php 613 | db =& $db; 679 | } 680 | } 681 | ``` 682 | 683 | ### 新建一個實體模型 684 | 685 | CodeIgniter 支援將實體類別作為第一類物件,並且這是一個可選的功能。它通常是資料庫模式中的一個部分,若是它符合你的需求,可以與模型一起使用。 686 | 687 | 使用以下指令建立: 688 | 689 | ``` 690 | $ php spark create:model [model_name] [entity_name] -entity 691 | ``` 692 | 現在,打開「 app/Models 」你應該可以看見新的實體模型: 693 | 694 | ```php 695 | 在 Codeigniter 中的命名空間通常映射自實際檔案位置。所以使用這個指令時,將會自動創建符合你所輸入的命名空間規則的資料夾。 736 | 737 | 現在,打開「 app/Models/Api/System 」你應該可以看見新的「 -nobase 」控制器檔案: 738 | 739 | ```php 740 | 12 | * @link https://github.com/monkenWu/Codeigniter4-Easy-create 13 | * 14 | */ 15 | 16 | namespace monken\Commands; 17 | use CodeIgniter\CLI\CLI; 18 | 19 | class CliCreate { 20 | 21 | /** 22 | * 取得檔案名稱 23 | * Gets the File name. 24 | * 25 | * @param array $params CLI所取得的 params 陣列 / The params array that CLI has gotten. 26 | * @param String $type 將影響到提示中的文字 / $type will impact the words in prompt. 27 | * @return string 28 | */ 29 | public static function getName(array &$params,String $type){ 30 | $name = array_shift($params); 31 | if (empty($name)){ 32 | $name = CLI::prompt( 33 | CLI::color("Names the {$type} file","blue") 34 | ); 35 | } 36 | if (empty($name)){ 37 | CLI::error("You must provide a {$type} file name."); 38 | exit(); 39 | } 40 | return $name; 41 | } 42 | 43 | /** 44 | * 取得使用者輸入的命名空間 45 | * Gets the namespace that be entered by user. 46 | * 47 | * 將會判斷使用者所輸入的值是否符合規則,並且將所有的路徑強制轉換成第一個字母大寫的形式。 48 | * It will judge if the value is compliant with the rules that has been entered by user, and change all paths into first letter capitalized. 49 | * @param String $type Models、Controllers或App下的任何資料夾。 / Any folder that under the Models, Controllers or App. 50 | * @return string 將回傳取得的命名空間。 / It will return the namespace that has been gotten. 51 | */ 52 | public static function getNameSpace(String $type){ 53 | $name = CLI::prompt( 54 | CLI::color("The current namespace is \"App \ {$type}\", and what you typed will be concatenated after this ","blue") 55 | ); 56 | while(strstr($name, '/')){ 57 | $name = CLI::prompt( 58 | CLI::color("Namespace cannot be separated by '/', Please retype ","blue") 59 | ); 60 | } 61 | if( (str_split($name)[0] != "\\") && ($name != "")){ 62 | $name = "\\".$name; 63 | } 64 | $tempArray = explode("\\",$name); 65 | $returnName = ""; 66 | foreach ($tempArray as $key => $value) { 67 | if($value != "") $returnName .= "\\".ucfirst($value); 68 | } 69 | return $returnName; 70 | } 71 | 72 | /** 73 | * 判斷是否有一個以上的true存在。 74 | * Finds out that if more than one "true" exists. 75 | * 76 | * @param Array $boolArr 77 | * @return boolean 78 | */ 79 | public static function isMulti(Array $boolArr){ 80 | $num = 0; 81 | foreach ($boolArr as $value) { 82 | if($value) $num++; 83 | } 84 | return $num > 1 ? true : false; 85 | } 86 | 87 | /** 88 | * 寫入檔案。 89 | * Writes file. 90 | * 91 | * @param String $writePath 寫入檔案的絕對路徑 / Writes the file's absolute path. 92 | * @param String $fileText 檔案名稱 the file name 93 | * @return string 94 | */ 95 | public static function writeFile(String $writePath,String $fileText){ 96 | helper('filesystem'); 97 | 98 | if (! write_file($writePath, $fileText)){ 99 | CLI::error("Failed to create file."); 100 | exit(); 101 | } 102 | 103 | CLI::write('Created successfully: ' . 104 | CLI::color($writePath, 'green') 105 | ); 106 | } 107 | 108 | /** 109 | * 取得模板。 110 | * Gets the template. 111 | * 112 | * @param String $path 絕對路徑 / Absolute path 113 | * @param String $name 模板名稱 / Template name 114 | * @return string 115 | */ 116 | public static function getTemplate(String $path,String $name){ 117 | $filePath = $path.$name; 118 | $handle = fopen($filePath, "r"); 119 | $template = fread($handle, filesize($filePath)); 120 | fclose($handle); 121 | if($template){ 122 | return $template; 123 | }else{ 124 | CLI::error("Template loading error."); 125 | exit(); 126 | } 127 | } 128 | 129 | /** 130 | * 確認檔案是否存在。 131 | * Finds out that if the file is exists. 132 | * 133 | * 若檔案存在,將會詢問使用者是否需要覆寫檔案,或取消寫入。 134 | * If the file is exists, it will ask user if it need to overwrite the file or just stop writing. 135 | * @param String $filePath 檔案完整路徑,包含檔名 / The file's fullpath, included the file name. 136 | * @param String $type 本次操作的分類,將影響到提示文字 / The classification of this operation, it will impact the prompt. 137 | * @return void 138 | */ 139 | public static function checkFileEexists(String $filePath,String $type){ 140 | if(file_exists($filePath)){ 141 | $check = CLI::prompt( 142 | CLI::color("Found the same {$type} file name, Do you need overwrite the file?","yellow"), 143 | ['y','n'] 144 | ); 145 | if($check == "n"){ 146 | CLI::write("{$type} creation process has ". 147 | CLI::color("stopped.", 'yellow') 148 | ); 149 | exit(); 150 | } 151 | } 152 | } 153 | 154 | /** 155 | * 替換模板檔案的 Tag 。 156 | * Changes the template file's tag. 157 | * 158 | * @param String $template 模板字串。 / Template string 159 | * @param Array $repalceData 索引陣列, key 值為模板檔案中的 Tag 名稱,Value 為替換值。 / Array, the key is the name of template file's tag, and the value is the words to replace it. 160 | * @return string 替換後的結果 / The result of replacement. 161 | */ 162 | public static function replaceText(String $template,Array $repalceData){ 163 | foreach ($repalceData as $key => $value) { 164 | $template = str_replace("{{$key}}", $value, $template); 165 | } 166 | return $template; 167 | } 168 | 169 | /** 170 | * 取得檔案路徑。 171 | * Gets the file path. 172 | * 173 | * 將會判斷當前執行的系統是否為windows,將會回傳符合系統要求的絕對路徑。 174 | * It will check if the operate systeam is windows, then return the absolute path that the systeam need. 175 | * @param String $appPath 專案app資料夾路徑 / The project app folder's path 176 | * @param String $type Models、Controllers或App下的任何資料夾。 / Any folder that under the Models, Controllers or App. 177 | * @param String $dir 指定路徑,如路徑不存在將自動建立。路徑以 "\\" 分隔。預設為空。 / Specify the path. If the path doesn't exists, it will create it automatically. The input path need to separated by "\\". Defult value is empty. 178 | * @param String $fileName 檔案名稱,不須傳入 ".php" 副檔名。預設為空。 / The file name. It is not need the file extension ".php".Default is empty. 179 | * @return String 回傳絕對路徑。 / Return the absolute path. 180 | */ 181 | static public function getPath(String $appPath,String $type,String $dir = "",String $fileName = ""){ 182 | $word = ""; 183 | 184 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { 185 | $word = "\\"; 186 | }else{ 187 | $word = "/"; 188 | } 189 | 190 | $dirArr = explode("\\",$dir); 191 | 192 | //Compares the last character of the string. 193 | if(substr($appPath,strlen($appPath)-1,1) != $word){ 194 | $appPath .= $word; 195 | } 196 | $appPath = $appPath.$type; 197 | 198 | foreach ($dirArr as $key => $value) { 199 | $temp = $appPath; 200 | if($key > 0){ 201 | $temp .= $word; 202 | } 203 | $temp .= $value; 204 | if(!is_dir($temp)){ 205 | mkdir($temp,0755); 206 | } 207 | $appPath = $temp; 208 | } 209 | 210 | $fileName .= $fileName != "" ? ".php" : ""; 211 | 212 | return $appPath.$word.$fileName; 213 | } 214 | 215 | /** 216 | * 取得指定資料夾下所有的命名空間。 217 | * Get all namespaces under the specific folder. 218 | * 219 | * @param String $appPath 220 | * @param String $type Models、Controllers或App下的任何資料夾。 / Any folder that under the Models, Controllers or App. 221 | * @return array 命名空間陣列 / The namespace array 222 | */ 223 | public static function getAllNamespace(String $appPath,String $type){ 224 | $map = directory_map("{$appPath}{$type}/", FALSE, TRUE); 225 | $nameArr = []; 226 | 227 | $getDeepMap = function($map,$key,&$nameArr) use (&$getDeepMap){ 228 | $nowDir = $key; 229 | foreach ($map as $key => $value) { 230 | if(gettype($key) != "integer"){ 231 | $getDeepMap($value,"{$nowDir}{$key}",$nameArr); 232 | }else{ 233 | $path = $nowDir.str_replace(".php","",$value); 234 | $path = str_replace("/","\\",$path); 235 | $nameArr[] = $path; 236 | } 237 | } 238 | }; 239 | 240 | foreach ($map as $key => $value) { 241 | if(gettype($key) != "integer"){ 242 | $getDeepMap($value,"App/{$type}/{$key}",$nameArr); 243 | }else{ 244 | if(!strstr($value,".php")) continue; 245 | $path = "App/{$type}/".str_replace(".php","",$value); 246 | $path = str_replace("/","\\",$path); 247 | $nameArr[] = $path; 248 | } 249 | } 250 | 251 | return $nameArr; 252 | } 253 | 254 | /** 255 | * 顯示表格,並且回傳使用者所選擇的 ID 。 256 | * Display the table, and return the id that selected by user. 257 | * 258 | * @param Array $namespaceArr 傳入由命名空間組成的陣列 / The array built by namespaces. 259 | * @param String $type 將會影響到表格的標題 / Will affect the title of the Table. 260 | * @param Boolean $multiValue 使用者是否可以選擇多個選項,默認值為true。 / Whether the user can select multiple options. The default is true. 261 | * @return array 使用者所選擇的 id / The id that selected by user. 262 | */ 263 | public static function selectTable(Array $namespaceArr,String $type,bool $multiValue = true){ 264 | 265 | $thead = ['ID', $type, 'ID', $type]; 266 | $tbody = []; 267 | foreach ($namespaceArr as $key => $value) { 268 | if($key%2==0){ 269 | $tbody[] = [($key+1),$value]; 270 | }else{ 271 | array_push($tbody[count($tbody)-1],($key+1),$value); 272 | } 273 | } 274 | 275 | CLI::table($tbody,$thead); 276 | 277 | if($multiValue){ 278 | $useID = CLI::prompt( 279 | CLI::color("Please type the ID you want to use.\nIf you want to use muiltiple Options, then type muiltiple ID and separated it by \",\" ","blue") 280 | ); 281 | while($useID == ""){ 282 | $useID = CLI::prompt( 283 | CLI::color("Please type the ID you want to use.\nIf you want to use muiltiple Options, then type muiltiple ID and separated it by \",\" ","blue") 284 | ); 285 | } 286 | }else{ 287 | $useID = CLI::prompt( 288 | CLI::color("Please type the ID you want to use.","blue") 289 | ); 290 | while($useID == ""){ 291 | $useID = CLI::prompt( 292 | CLI::color("Please type the ID you want to use.","blue") 293 | ); 294 | } 295 | } 296 | 297 | return $useID; 298 | } 299 | 300 | } -------------------------------------------------------------------------------- /src/Commands/Controller/CreateController.php: -------------------------------------------------------------------------------- 1 | 12 | * @link https://github.com/monkenWu/Codeigniter4-Easy-create 13 | * 14 | */ 15 | 16 | namespace monken\Commands\Controller; 17 | use CodeIgniter\CLI\BaseCommand; 18 | use CodeIgniter\CLI\CLI; 19 | use monken\Commands\CliCreate; 20 | 21 | class CreateController extends BaseCommand{ 22 | 23 | protected $group = 'Cli-Create'; 24 | protected $name = 'create:controller'; 25 | protected $description = 'Create a new controller file.'; 26 | protected $usage = 'create:controller [controller_name] [Options]'; 27 | protected $arguments = [ 28 | 'controller_name' => 'The controller name.', 29 | ]; 30 | protected $options = [ 31 | '-nobase' => 'Do not extends BaseControllers Class.', 32 | '-usemodel' => 'Choose models.', 33 | '-model' => 'Create a new model for use with the new controller.', 34 | '-space' => 'Create folders and files according to the path you typed.', 35 | '-rest' => 'Generate files related to Resource Routes', 36 | // '-rest -p' => 'Generate files related to Presenter Routes, then use the "-rest -p" options.', 37 | '-rest -d' => 'The names of controller and router are different.', 38 | '-rest -o' => 'Select options to create the function what you want. ', 39 | '-rest -w' => "Generate update and delete methods that work with HTML forms" 40 | ]; 41 | 42 | private $controllerName; 43 | private $routerName; 44 | private $useModel = false; 45 | private $model = false; 46 | private $useBase = true; 47 | private $nameSpace = false; 48 | private $differntNames = false; 49 | private $selectFunctions = false; 50 | private $useWebsafe = false; 51 | private $templatePath; 52 | private $appPath; 53 | 54 | public function run(array $params = []){ 55 | $userNameInput = CliCreate::getName($params,"controller"); 56 | $this->controllerName = ucfirst($userNameInput); 57 | $this->appPath = APPPATH; 58 | $this->templatePath = CliCreate::getPath(dirname(__FILE__),"template"); 59 | $option = $this->getOption(); 60 | if($this->differntNames){ 61 | $this->routerName = CliCreate::getName($params,"router"); 62 | }else{ 63 | $this->routerName = $userNameInput; 64 | } 65 | $this->extraOption(CLI::getOptions()); 66 | if($option == "rest"){ 67 | $this->writeRest(); 68 | }else if($option == "restP"){ 69 | //$this->writeRestP(); 70 | }else{ 71 | $this->writeBasic(); 72 | } 73 | 74 | return; 75 | } 76 | 77 | /** 78 | * 取得可能被使用者輸入的 options 79 | * Get options that may be typed by the user. 80 | * 81 | * @return Void 82 | */ 83 | private function getOption(){ 84 | if(!empty(CLI::getOption('rest'))){ 85 | $isMulti = CliCreate::isMulti([ 86 | !empty(CLI::getOption('nobase')), 87 | !empty(CLI::getOption('usemodel')), 88 | ]); 89 | if($isMulti){ 90 | CLI::error("If you use the -rest option, you can no longer use the -nobase and -usemodel options."); 91 | exit(); 92 | } 93 | } 94 | 95 | $this->useBase = !empty(CLI::getOption('nobase'))?false:true; 96 | $this->useModel = !empty(CLI::getOption('usemodel'))?true:false; 97 | $this->model = !empty(CLI::getOption('model'))?true:false; 98 | $this->nameSpace = !empty(CLI::getOption('space'))?true:false; 99 | $rest = !empty(CLI::getOption('rest'))?true:false; 100 | $restP = !empty(CLI::getOption('p'))?true:false; 101 | $restD = !empty(CLI::getOption('d'))?true:false; 102 | $restO = !empty(CLI::getOption("o"))?true:false; 103 | $restW = !empty(CLI::getOption("w"))?true:false; 104 | 105 | if(!$rest && $restP){ 106 | CLI::error("The -p option must be used after -rest."); 107 | exit(); 108 | } 109 | if(!$rest && $restD){ 110 | CLI::error("The -d option must be used after -rest."); 111 | exit(); 112 | } 113 | $this->differntNames = $restD; 114 | if(!$rest && $restO){ 115 | CLI::error("The -O option must be used after -rest."); 116 | exit(); 117 | } 118 | $this->selectFunctions = $restO; 119 | if(!$rest && $restW){ 120 | CLI::error("The -W option must be used after -rest."); 121 | exit(); 122 | } 123 | $this->useWebsafe = $restW; 124 | 125 | if($rest){ 126 | if($restP){ 127 | return "restP"; 128 | } 129 | return "rest"; 130 | }else{ 131 | return "basic"; 132 | } 133 | } 134 | 135 | /** 136 | * 額外的特殊選項判斷 137 | * Extra special options 138 | * 139 | * @param array $options 140 | * @return void 141 | */ 142 | private function extraOption(array $options){ 143 | foreach ($options as $key => $value) { 144 | if($key == "model"){ 145 | if($value == ""){ 146 | $value = CliCreate::getName($options,"model"); 147 | } 148 | $this->model = [ucfirst($value)]; 149 | } 150 | if(strstr($key,'model=')){ 151 | if($key == "model="){ 152 | CLI::error("After \"model=\" , you must continue to declare options related to the create:model command."); 153 | exit(); 154 | } 155 | if($value == ""){ 156 | $value = CliCreate::getName($options,"model"); 157 | } 158 | $keyOption = str_replace("model=","",$key); 159 | $options = explode(",",$keyOption); 160 | foreach ($options as $key => $option) { 161 | $options[$key] = "-{$option}"; 162 | } 163 | $options[] = "-call"; 164 | if(in_array("-space",$options)){ 165 | $options[] = CliCreate::getNameSpace("Models"); 166 | } 167 | array_unshift($options, ucfirst($value)); 168 | $this->model = $options; 169 | } 170 | } 171 | } 172 | 173 | private function writeBasic(){ 174 | $useController = ""; 175 | $extendsController = "BaseController"; 176 | if(!$this->useBase){ 177 | $useController = "\nuse CodeIgniter\Controller;"; 178 | $extendsController = "Controller"; 179 | } 180 | 181 | //get namespace 182 | $space = ""; 183 | if($this->nameSpace){ 184 | $space = CliCreate::getNameSpace("Controllers"); 185 | } 186 | if($space != ""){ 187 | $extendsController = "\App\Controllers\BaseController"; 188 | } 189 | 190 | //get model 191 | $useModels = ""; 192 | if($this->useModel){ 193 | $modelList = CliCreate::getAllNamespace($this->appPath,"Models"); 194 | 195 | $useID = CliCreate::selectTable($modelList,"Namespace"); 196 | 197 | $numArr = explode(",",$useID); 198 | foreach ($numArr as $key => $value) { 199 | $useModels .= "use {$modelList[($value-1)]};\n" ?? ""; 200 | } 201 | } 202 | //extend model 203 | if($this->model){ 204 | $this->call('create:model',(array)$this->model); 205 | if(in_array("-space",(array)$this->model)){ 206 | $nameSpace = $this->model[count($this->model)-1]."\\".$this->model[0]; 207 | $useModels .= "use App\\Models{$nameSpace};\n"; 208 | }else{ 209 | $useModels .= "use App\\Models\\{$this->model[0]};\n"; 210 | } 211 | } 212 | 213 | $templateData = [ 214 | "name" => $this->controllerName, 215 | "namespace" => $space, 216 | "useController" => $useController, 217 | "extendsController" => $extendsController, 218 | "useModels" => $useModels 219 | ]; 220 | $template = CliCreate::getTemplate($this->templatePath,"controller"); 221 | $replaceTemplate = CliCreate::replaceText($template,$templateData); 222 | $writePath = CliCreate::getPath($this->appPath,"Controllers",$space,$this->controllerName); 223 | CliCreate::checkFileEexists($writePath,"Controller"); 224 | CliCreate::writeFile($writePath,$replaceTemplate); 225 | return; 226 | } 227 | 228 | private function writeRest(){ 229 | //get want use RestFul functions 230 | $functions = ""; 231 | $onlySetting = ""; 232 | $resurceFunctions = [ 233 | "index","show","create","update","new","edit","delete" 234 | ]; 235 | $functionsPath = CliCreate::getPath($this->templatePath,"rest","\\resourceFunctions"); 236 | if($this->selectFunctions){ 237 | $useID = CliCreate::selectTable($resurceFunctions,"function"); 238 | $useArr = explode(",",$useID); 239 | $onlySetting .= "["; 240 | foreach ($useArr as $key => $value) { 241 | $id = (int)$value - 1; 242 | $functions .= CliCreate::getTemplate($functionsPath,$resurceFunctions[($id)]); 243 | $functions .= "\r\n\r\n"; 244 | $onlySetting .= $key == 0 ? "'{$resurceFunctions[$id]}'" : ",'{$resurceFunctions[$id]}'"; 245 | } 246 | $onlySetting .= "]"; 247 | }else{ 248 | foreach ($resurceFunctions as $key => $value) { 249 | $functions .= CliCreate::getTemplate($functionsPath,$value); 250 | $functions .= "\r\n\r\n"; 251 | } 252 | } 253 | //get namespace 254 | $space = ""; 255 | if($this->nameSpace){ 256 | $space = CliCreate::getNameSpace("Controllers"); 257 | } 258 | 259 | //get Model Namespace 260 | $useModel = ""; 261 | if($this->model){ 262 | $this->call('create:model',(array)$this->model); 263 | if(in_array("-space",(array)$this->model)){ 264 | $nameSpace = $this->model[count($this->model)-1]."\\".$this->model[0]; 265 | $useModel = "App\\Models{$nameSpace};\n"; 266 | }else{ 267 | $useModel = "App\\Models\\{$this->model[0]};\n"; 268 | } 269 | }else{ 270 | $modelList = CliCreate::getAllNamespace($this->appPath,"Models"); 271 | $modelID = CliCreate::selectTable($modelList,"Namespace",false); 272 | $useModel = $modelList[((int)$modelID-1)]; 273 | } 274 | 275 | //create controller 276 | $templateData = [ 277 | "name" => $this->controllerName, 278 | "namespace" => $space, 279 | "useModel" => $useModel, 280 | "functions" => $functions 281 | ]; 282 | $template = CliCreate::getTemplate($this->templatePath,"resourceController"); 283 | $replaceTemplate = CliCreate::replaceText($template,$templateData); 284 | $writePath = CliCreate::getPath($this->appPath,"Controllers",$space,$this->controllerName); 285 | CliCreate::checkFileEexists($writePath,"Controller"); 286 | CliCreate::writeFile($writePath,$replaceTemplate); 287 | 288 | //add router setting 289 | $routerSetPath = CliCreate::getPath($this->appPath,"Config","","Routes"); 290 | $routerSetString = CliCreate::getTemplate($routerSetPath,""); 291 | $setBefore = strstr($routerSetString,"Route Definitions",true); 292 | $setAfter = strstr($routerSetString,"Route Definitions"); 293 | $writeBefore = strstr($setAfter,"/**",true); 294 | $writeAfter = strstr($setAfter,"/**"); 295 | $time = date('Y-m-d H:i:s'); 296 | $ctrlNameSpace = $space == "" ? "\\{$this->controllerName}" : "{$space}\\$this->controllerName"; 297 | $only = $this->selectFunctions ? " 'only' => {$onlySetting},\n":""; 298 | $websafe = $this->useWebsafe ? " 'websafe' => 1,\n" : ""; 299 | $writeAfter = "//CliCreate-add-in-{$time} 300 | \$routes->resource('{$this->routerName}',[ 301 | 'controller' =>'\App\Controllers{$ctrlNameSpace}',\n{$only}{$websafe}]);\n 302 | {$writeAfter} 303 | "; 304 | $dataString = $setBefore.$writeBefore.$writeAfter; 305 | CliCreate::writeFile($routerSetPath,$dataString); 306 | return; 307 | } 308 | 309 | // private function writeRestP(){ 310 | // //get namespace 311 | // $space = ""; 312 | // if($this->nameSpace){ 313 | // $space = CliCreate::getNameSpace("Controllers"); 314 | // } 315 | // } 316 | 317 | } -------------------------------------------------------------------------------- /src/Commands/Controller/template/controller: -------------------------------------------------------------------------------- 1 | respond([ 3 | "status" => true, 4 | "msg" => "create method successful." 5 | ]); 6 | } -------------------------------------------------------------------------------- /src/Commands/Controller/template/rest/resourceFunctions/delete: -------------------------------------------------------------------------------- 1 | public function delete($id = null){ 2 | return $this->respond([ 3 | "status" => true, 4 | "id" => $id, 5 | "msg" => "delede method successful." 6 | ]); 7 | } -------------------------------------------------------------------------------- /src/Commands/Controller/template/rest/resourceFunctions/edit: -------------------------------------------------------------------------------- 1 | public function edit($id = null){ 2 | return $this->respond([ 3 | "status" => true, 4 | "id" => $id, 5 | "msg" => "edit method successful." 6 | ]); 7 | } -------------------------------------------------------------------------------- /src/Commands/Controller/template/rest/resourceFunctions/index: -------------------------------------------------------------------------------- 1 | public function index(){ 2 | return $this->respond([ 3 | "status" => true, 4 | "msg" => "index method successful." 5 | ]); 6 | } -------------------------------------------------------------------------------- /src/Commands/Controller/template/rest/resourceFunctions/new: -------------------------------------------------------------------------------- 1 | public function new(){ 2 | return $this->respond([ 3 | "status" => true, 4 | "msg" => "new method successful." 5 | ]); 6 | } -------------------------------------------------------------------------------- /src/Commands/Controller/template/rest/resourceFunctions/show: -------------------------------------------------------------------------------- 1 | public function show($id = null){ 2 | return $this->respond([ 3 | "status" => true, 4 | "id" => $id, 5 | "msg" => "show method successful." 6 | ]); 7 | } -------------------------------------------------------------------------------- /src/Commands/Controller/template/rest/resourceFunctions/update: -------------------------------------------------------------------------------- 1 | public function update($id = null){ 2 | return $this->respond([ 3 | "status" => true, 4 | "id" => $id, 5 | "msg" => "update method successful." 6 | ]); 7 | } -------------------------------------------------------------------------------- /src/Commands/Model/CreateModel.php: -------------------------------------------------------------------------------- 1 | 12 | * @link https://github.com/monkenWu/Codeigniter4-Cli-Create 13 | * 14 | */ 15 | 16 | namespace monken\Commands\Model; 17 | use CodeIgniter\CLI\BaseCommand; 18 | use CodeIgniter\CLI\CLI; 19 | use monken\Commands\CliCreate; 20 | 21 | class CreateModel extends BaseCommand{ 22 | 23 | protected $group = 'Cli-Create'; 24 | protected $name = 'create:model'; 25 | protected $description = 'Create a new model file.'; 26 | protected $usage = 'create:model [model_name] [entity_name] [Options]'; 27 | protected $arguments = [ 28 | 'model_name' => 'The model name', 29 | 'entity_name' => 'The entity name. If you selected -entity option. You can type this arguments.' 30 | ]; 31 | protected $options = [ 32 | '-basic' => 'Creates a basic model file.', 33 | '-entity' => 'Uses Entity Classes.', 34 | '-manual' => 'Creates a Manual Model.', 35 | '-space' => 'Creates folders and files according to the path you typed.' 36 | ]; 37 | 38 | private $modelName; 39 | private $entityName; 40 | private $nameSpace = false; 41 | private $appPath; 42 | private $templatePath; 43 | private $option; 44 | private $callNameSpace = false; 45 | 46 | public function run(array $params = []){ 47 | $this->modelName = ucfirst(CliCreate::getName($params,"model")); 48 | $this->option = $this->getOption($params); 49 | $this->appPath = APPPATH; 50 | $this->templatePath = CliCreate::getPath(dirname(__FILE__),"template"); 51 | if($this->option == "basic"){ 52 | $this->writeBasicModel(); 53 | }else if($this->option == "entity"){ 54 | $this->entityName = ucfirst(CliCreate::getName($params,"entity")); 55 | $this->writeEntity(); 56 | }else if($this->option == "manual"){ 57 | $this->writeManual(); 58 | }else { 59 | $this->writeModel(); 60 | } 61 | return; 62 | } 63 | 64 | /** 65 | * 取得可能被使用者輸入的 options 66 | * Get options that may be typed by the user. 67 | * 68 | * @return string 判斷 option 後回傳本次使用者希望執行的模式。 69 | * After finding out what option selected by user, return the mode that user whants to execute. 70 | */ 71 | private function getOption(&$params){ 72 | if(in_array("-call",$params)){ 73 | $basic = in_array("-basic",$params) ? true : NULL; 74 | $entity = in_array("-entity",$params) ? true : NULL; 75 | $manual = in_array("-manual",$params) ? true : NULL; 76 | $space = in_array("-space",$params) ? true : NULL; 77 | foreach ($params as $key => $value) { 78 | if(strstr($value,'-')) unset($params[$key]); 79 | } 80 | if($space){ 81 | $params = array_values($params); 82 | $key = count($params)-1; 83 | $this->callNameSpace = $params[$key]; 84 | unset($params[$key]); 85 | } 86 | }else{ 87 | $basic = CLI::getOption('basic'); 88 | $entity = CLI::getOption('entity'); 89 | $manual = CLI::getOption('manual'); 90 | $space = CLI::getOption('space'); 91 | } 92 | 93 | $isMulti = CliCreate::isMulti([ 94 | !empty($basic),!empty($entity),!empty($manual) 95 | ]); 96 | 97 | if($isMulti){ 98 | CLI::error("Only one option can be selected in -basic , -entity or -manual."); 99 | exit(); 100 | } 101 | 102 | if(!empty($space)){ 103 | $this->nameSpace = true; 104 | } 105 | 106 | if(!empty($basic)){ 107 | return "basic"; 108 | }else if(!empty($entity)){ 109 | return "entity"; 110 | }else if(!empty($manual)){ 111 | return "manual"; 112 | }else{ 113 | return "null"; 114 | } 115 | } 116 | 117 | /** 118 | * 讀取基本的 Model 模板,並寫入到新的文件中。 119 | * Writes the basic Model template into the new file. 120 | * 121 | * @return void 122 | */ 123 | private function writeBasicModel(){ 124 | $space = ""; 125 | if($this->nameSpace){ 126 | if($this->callNameSpace){ 127 | $space = $this->callNameSpace; 128 | }else{ 129 | $space = CliCreate::getNameSpace("Models"); 130 | } 131 | } 132 | $sendData = [ 133 | "name" => $this->modelName, 134 | "namespace" => $space 135 | ]; 136 | $template = CliCreate::getTemplate($this->templatePath,"basicModel"); 137 | $replaceTemplate = CliCreate::replaceText($template,$sendData); 138 | $writePath = CliCreate::getPath($this->appPath,"Models",$space,$this->modelName); 139 | CliCreate::checkFileEexists($writePath,"Model"); 140 | CliCreate::writeFile($writePath,$replaceTemplate); 141 | } 142 | 143 | /** 144 | * 讀取 Manual Model 模板,並寫入到新的文件中。 145 | * Writes the Manual Model template into the new file. 146 | * 147 | * @return void 148 | */ 149 | private function writeManual(){ 150 | $space = ""; 151 | if($this->nameSpace){ 152 | if($this->callNameSpace){ 153 | $space = $this->callNameSpace; 154 | }else{ 155 | $space = CliCreate::getNameSpace("Models"); 156 | } 157 | } 158 | $sendData = [ 159 | "name" => $this->modelName, 160 | "namespace" => $space 161 | ]; 162 | $template = CliCreate::getTemplate($this->templatePath,"manualModel"); 163 | $replaceTemplate = CliCreate::replaceText($template,$sendData); 164 | $writePath = CliCreate::getPath($this->appPath,"Models",$space,$this->modelName); 165 | CliCreate::checkFileEexists($writePath,"Model"); 166 | CliCreate::writeFile($writePath,$replaceTemplate); 167 | } 168 | 169 | /** 170 | * 讀取 Model 模板,並寫入到新的文件中。 171 | * Writes the Model template into the new file. 172 | * 173 | * @return void 174 | */ 175 | private function writeModel(){ 176 | $space = ""; 177 | if($this->nameSpace){ 178 | if($this->callNameSpace){ 179 | $space = $this->callNameSpace; 180 | }else{ 181 | $space = CliCreate::getNameSpace("Models"); 182 | } 183 | } 184 | $sendData = [ 185 | "name" => $this->modelName, 186 | "namespace" => $space 187 | ]; 188 | $template = CliCreate::getTemplate($this->templatePath,"model"); 189 | $replaceTemplate = CliCreate::replaceText($template,$sendData); 190 | $writePath = CliCreate::getPath($this->appPath,"Models",$space,$this->modelName); 191 | CliCreate::checkFileEexists($writePath,"Model"); 192 | CliCreate::writeFile($writePath,$replaceTemplate); 193 | } 194 | 195 | /** 196 | * 讀取 Entity 與 Model 模板,皆寫入到新的文件中。 197 | * Writes the Entity and Model template into the new file. 198 | * @return void 199 | */ 200 | private function writeEntity(){ 201 | //get model namespace 202 | $space = ""; 203 | $entitySpace = ""; 204 | if($this->nameSpace){ 205 | if($this->callNameSpace){ 206 | $space = $this->callNameSpace; 207 | }else{ 208 | $space = CliCreate::getNameSpace("Models"); 209 | } 210 | $entitySpace = CliCreate::getNameSpace("Entities"); 211 | } 212 | 213 | $sendData = [ 214 | "name" => $this->modelName, 215 | "entityNames" => $this->entityName, 216 | "namespace" => $space, 217 | "entityNamespace" => $entitySpace 218 | ]; 219 | $template = CliCreate::getTemplate($this->templatePath,"entityModel"); 220 | $replaceTemplateModel = CliCreate::replaceText($template,$sendData); 221 | 222 | $sendData = [ 223 | "name" => $this->entityName, 224 | "namespace" => $entitySpace 225 | ]; 226 | $template = CliCreate::getTemplate($this->templatePath,"entity"); 227 | $replaceTemplateEntity = CliCreate::replaceText($template,$sendData); 228 | 229 | $writeModelPath = CliCreate::getPath($this->appPath,"Models",$space,$this->modelName); 230 | $writeEntityPath = CliCreate::getPath($this->appPath,"Entities",$entitySpace,$this->entityName); 231 | CliCreate::checkFileEexists($writeModelPath,"Model"); 232 | CliCreate::checkFileEexists($writeEntityPath,"Entities",$this->entityName,"Entitiy"); 233 | 234 | CliCreate::writeFile($writeModelPath,$replaceTemplateModel); 235 | CliCreate::writeFile($writeEntityPath,$replaceTemplateEntity); 236 | } 237 | 238 | } -------------------------------------------------------------------------------- /src/Commands/Model/template/basicModel: -------------------------------------------------------------------------------- 1 | db =& $db; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Commands/Model/template/model: -------------------------------------------------------------------------------- 1 |