Skip to content

Commit

Permalink
add "-rest" feature in create:controller
Browse files Browse the repository at this point in the history
  • Loading branch information
monkenWu committed Apr 9, 2020
1 parent 38e8cde commit f11a711
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 24 deletions.
33 changes: 24 additions & 9 deletions src/Commands/CliCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

namespace monken\Commands;
use CodeIgniter\CLI\CLI;
use phpDocumentor\Reflection\Types\Boolean;

class CliCreate {

/**
* 取得使用者名稱
* Gets the user name.
* 取得檔案名稱
* Gets the File name.
*
* @param array $params CLI所取得的 params 陣列 / The params array that CLI has gotten.
* @param String $type 將影響到提示中的文字 / $type will impact the words in prompt.
Expand Down Expand Up @@ -256,11 +257,13 @@ public static function getAllNamespace(String $appPath,String $type){
* Display the table, and return the id that selected by user.
*
* @param Array $namespaceArr 傳入由命名空間組成的陣列 / The array built by namespaces.
* @param String $type 將會影響到表格的標題 / Will affect the title of the Table.
* @param Boolean $multiValue 使用者是否可以選擇多個選項,默認值為true。 / Whether the user can select multiple options. The default is true.
* @return array 使用者所選擇的 id / The id that selected by user.
*/
public static function selectTable(Array $namespaceArr){
public static function selectTable(Array $namespaceArr,String $type,bool $multiValue = true){

$thead = ['ID', 'Namespace', 'ID', 'Namespace'];
$thead = ['ID', $type, 'ID', $type];
$tbody = [];
foreach ($namespaceArr as $key => $value) {
if($key%2==0){
Expand All @@ -271,13 +274,25 @@ public static function selectTable(Array $namespaceArr){
}

CLI::table($tbody,$thead);
$useID = CLI::prompt(
CLI::color("Please type the ID of the Namespace you want to use.\nIf you want to use muiltiple Namespace, then type muiltiple ID and separated it by \",\" ","blue")
);
while($useID == ""){

if($multiValue){
$useID = CLI::prompt(
CLI::color("Please type the ID of the Namespace you want to use.\nIf you want to use muiltiple Namespace, then type muiltiple ID and separated it by \",\" ","blue")
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")
);
while($useID == ""){
$useID = CLI::prompt(
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")
);
}
}else{
$useID = CLI::prompt(
CLI::color("Please type the ID you want to use.","blue")
);
while($useID == ""){
$useID = CLI::prompt(
CLI::color("Please type the ID you want to use.","blue")
);
}
}

return $useID;
Expand Down
177 changes: 162 additions & 15 deletions src/Commands/Controller/CreateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,105 @@ class CreateController extends BaseCommand{
protected $options = [
'-nobase' => 'Do not extends BaseControllers Class.',
'-usemodel' => 'Choose models.',
'-space' => 'Create folders and files according to the path you typed.'
'-space' => 'Create folders and files according to the path you typed.',
'-rest' => 'Generate files related to Resource Routes',
// '-rest -p' => 'Generate files related to Presenter Routes, then use the "-rest -p" options.',
'-rest -d' => 'The names of controller and router are different.',
'-rest -o' => 'Select options to create the function what you want. ',
'-rest -w' => "Generate update and delete methods that work with HTML forms"
];

private $controllerName;
private $routerName;
private $useModel = false;
private $useBase = true;
private $nameSpace = false;
private $differntNames = false;
private $selectFunctions = false;
private $useWebsafe = false;
private $templatePath;
private $appPath;

public function run(array $params = []){
$this->controllerName = ucfirst(CliCreate::getName($params,"controller"));
$userNameInput = CliCreate::getName($params,"controller");
$this->controllerName = ucfirst($userNameInput);
$this->appPath = APPPATH;
$this->templatePath = CliCreate::getPath(dirname(__FILE__),"template");
$this->getOption();

$option = $this->getOption();
if($this->differntNames){
$this->routerName = CliCreate::getName($params,"router");
}else{
$this->routerName = $userNameInput;
}

if($option == "rest"){
$this->writeRest();
}else if($option == "restP"){
//$this->writeRestP();
}else{
$this->writeBasic();
}

return;
}

/**
* 取得可能被使用者輸入的 options
* Get options that may be typed by the user.
*
* @return Void
*/
private function getOption(){
$isMulti = CliCreate::isMulti([
!empty(CLI::getOption('nobase')),
!empty(CLI::getOption('usemodel')),
!empty(CLI::getOption('rest'))
]);
if($isMulti){
CLI::error("If you use the -rest option, you can no longer use the -nobase and -usemodel options.");
exit();
}

$this->useBase = !empty(CLI::getOption('nobase'))?false:true;
$this->useModel = !empty(CLI::getOption('usemodel'))?true:false;
$this->nameSpace = !empty(CLI::getOption('space'))?true:false;
$rest = !empty(CLI::getOption('rest'))?true:false;
$restP = !empty(CLI::getOption('p'))?true:false;
$restD = !empty(CLI::getOption('d'))?true:false;
$restO = !empty(CLI::getOption("o"))?true:false;
$restW = !empty(CLI::getOption("w"))?true:false;

if(!$rest && $restP){
CLI::error("The -p option must be used after -rest.");
exit();
}
if(!$rest && $restD){
CLI::error("The -d option must be used after -rest.");
exit();
}
$this->differntNames = $restD;
if(!$rest && $restO){
CLI::error("The -O option must be used after -rest.");
exit();
}
$this->selectFunctions = $restO;
if(!$rest && $restW){
CLI::error("The -W option must be used after -rest.");
exit();
}
$this->useWebsafe = $restW;

if($rest){
if($restP){
return "restP";
}
return "rest";
}else{
return "basic";
}
}

private function writeBasic(){
$useController = "";
$extendsController = "BaseController";
if(!$this->useBase){
Expand All @@ -67,7 +150,7 @@ public function run(array $params = []){
if($this->useModel){
$modelList = CliCreate::getAllNamespace($this->appPath,"Models");

$useID = CliCreate::selectTable($modelList);
$useID = CliCreate::selectTable($modelList,"Namespace");

$numArr = explode(",",$useID);
foreach ($numArr as $key => $value) {
Expand All @@ -90,16 +173,80 @@ public function run(array $params = []){
return;
}

/**
* 取得可能被使用者輸入的 options
* Get options that may be typed by the user.
*
* @return Void
*/
private function getOption(){
$this->useBase = !empty(CLI::getOption('nobase'))?false:true;
$this->useModel = !empty(CLI::getOption('usemodel'))?true:false;
$this->nameSpace = !empty(CLI::getOption('space'))?true:false;
private function writeRest(){
//get Model Namespace
$modelList = CliCreate::getAllNamespace($this->appPath,"Models");
$modelID = CliCreate::selectTable($modelList,"Namespace",false);
$useModel = $modelList[((int)$modelID-1)];
//get want use RestFul functions
$functions = "";
$onlySetting = "";
$resurceFunctions = [
"index","show","create","update","new","edit","delete"
];
$functionsPath = CliCreate::getPath($this->templatePath,"rest","\\resourceFunctions");
if($this->selectFunctions){
$useID = CliCreate::selectTable($resurceFunctions,"function");
$useArr = explode(",",$useID);
$onlySetting .= "[";
foreach ($useArr as $key => $value) {
$id = (int)$value - 1;
$functions .= CliCreate::getTemplate($functionsPath,$resurceFunctions[($id)]);
$functions .= "\r\n\r\n";
$onlySetting .= $key == 0 ? "'{$resurceFunctions[$id]}'" : ",'{$resurceFunctions[$id]}'";
}
$onlySetting .= "]";
}else{
foreach ($resurceFunctions as $key => $value) {
$functions .= CliCreate::getTemplate($functionsPath,$value);
$functions .= "\r\n\r\n";
}
}
//get namespace
$space = "";
if($this->nameSpace){
$space = CliCreate::getNameSpace("Controllers");
}
//create controller
$templateData = [
"name" => $this->controllerName,
"namespace" => $space,
"useModel" => $useModel,
"functions" => $functions
];
$template = CliCreate::getTemplate($this->templatePath,"resourceController");
$replaceTemplate = CliCreate::replaceText($template,$templateData);
$writePath = CliCreate::getPath($this->appPath,"Controllers",$space,$this->controllerName);
CliCreate::checkFileEexists($writePath,"Controller");
CliCreate::writeFile($writePath,$replaceTemplate);

//add router setting
$routerSetPath = CliCreate::getPath($this->appPath,"Config","","Routes");
$routerSetString = CliCreate::getTemplate($routerSetPath,"");
$setBefore = strstr($routerSetString,"Route Definitions",true);
$setAfter = strstr($routerSetString,"Route Definitions");
$writeBefore = strstr($setAfter,"/**",true);
$writeAfter = strstr($setAfter,"/**");
$time = date('Y-m-d H:i:s');
$ctrlNameSpace = $space == "" ? "\\{$this->controllerName}" : "{$space}\\$this->controllerName";
$only = $this->selectFunctions ? " 'only' => {$onlySetting},\n":"";
$websafe = $this->useWebsafe ? " 'websafe' => 1,\n" : "";
$writeAfter = "//CliCreate-add-in-{$time}
\$routes->resource('{$this->routerName}',[
'controller' =>'\App\Controllers{$ctrlNameSpace}',\n{$only}{$websafe}]);\n
{$writeAfter}
";
$dataString = $setBefore.$writeBefore.$writeAfter;
CliCreate::writeFile($routerSetPath,$dataString);
return;
}

// private function writeRestP(){
// //get namespace
// $space = "";
// if($this->nameSpace){
// $space = CliCreate::getNameSpace("Controllers");
// }
// }

}
12 changes: 12 additions & 0 deletions src/Commands/Controller/template/resourceController
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php namespace App\Controllers{namespace};

use CodeIgniter\RESTful\ResourceController;

class {name} extends ResourceController
{

protected $modelName = '{useModel}';
protected $format = 'json';

{functions}
}
12 changes: 12 additions & 0 deletions src/Commands/Controller/template/resourcePresenter
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php namespace App\Controllers{namespace};

use CodeIgniter\RESTful\ResourcePresenter;

class {name} extends ResourcePresenter
{

protected $modelName = '{useModels}';

{functions}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public function create(){
return $this->respond([
"status" => true,
"msg" => "create method successful."
]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public function delete($id = null){
return $this->respond([
"status" => true,
"id" => $id,
"msg" => "delede method successful."
]);
}
7 changes: 7 additions & 0 deletions src/Commands/Controller/template/rest/resourceFunctions/edit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public function edit($id = null){
return $this->respond([
"status" => true,
"id" => $id,
"msg" => "edit method successful."
]);
}
6 changes: 6 additions & 0 deletions src/Commands/Controller/template/rest/resourceFunctions/index
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public function index(){
return $this->respond([
"status" => true,
"msg" => "index method successful."
]);
}
6 changes: 6 additions & 0 deletions src/Commands/Controller/template/rest/resourceFunctions/new
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public function new(){
return $this->respond([
"status" => true,
"msg" => "new method successful."
]);
}
7 changes: 7 additions & 0 deletions src/Commands/Controller/template/rest/resourceFunctions/show
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public function show($id = null){
return $this->respond([
"status" => true,
"id" => $id,
"msg" => "show method successful."
]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public function update($id = null){
return $this->respond([
"status" => true,
"id" => $id,
"msg" => "update method successful."
]);
}

0 comments on commit f11a711

Please sign in to comment.