From f11a711ed10fede6e31c5364058bff67ded379a1 Mon Sep 17 00:00:00 2001 From: monkenWu <610877102@mail.nknu.edu.tw> Date: Fri, 10 Apr 2020 02:52:17 +0800 Subject: [PATCH] add "-rest" feature in create:controller --- src/Commands/CliCreate.php | 33 +++- src/Commands/Controller/CreateController.php | 177 ++++++++++++++++-- .../Controller/template/resourceController | 12 ++ .../Controller/template/resourcePresenter | 12 ++ .../template/rest/resourceFunctions/create | 6 + .../template/rest/resourceFunctions/delete | 7 + .../template/rest/resourceFunctions/edit | 7 + .../template/rest/resourceFunctions/index | 6 + .../template/rest/resourceFunctions/new | 6 + .../template/rest/resourceFunctions/show | 7 + .../template/rest/resourceFunctions/update | 7 + 11 files changed, 256 insertions(+), 24 deletions(-) create mode 100644 src/Commands/Controller/template/resourceController create mode 100644 src/Commands/Controller/template/resourcePresenter create mode 100644 src/Commands/Controller/template/rest/resourceFunctions/create create mode 100644 src/Commands/Controller/template/rest/resourceFunctions/delete create mode 100644 src/Commands/Controller/template/rest/resourceFunctions/edit create mode 100644 src/Commands/Controller/template/rest/resourceFunctions/index create mode 100644 src/Commands/Controller/template/rest/resourceFunctions/new create mode 100644 src/Commands/Controller/template/rest/resourceFunctions/show create mode 100644 src/Commands/Controller/template/rest/resourceFunctions/update diff --git a/src/Commands/CliCreate.php b/src/Commands/CliCreate.php index ec48ac2..61f2845 100644 --- a/src/Commands/CliCreate.php +++ b/src/Commands/CliCreate.php @@ -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. @@ -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){ @@ -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; diff --git a/src/Commands/Controller/CreateController.php b/src/Commands/Controller/CreateController.php index 62ecfb1..a19aac1 100755 --- a/src/Commands/Controller/CreateController.php +++ b/src/Commands/Controller/CreateController.php @@ -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){ @@ -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) { @@ -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"); + // } + // } + } \ No newline at end of file diff --git a/src/Commands/Controller/template/resourceController b/src/Commands/Controller/template/resourceController new file mode 100644 index 0000000..d38bcee --- /dev/null +++ b/src/Commands/Controller/template/resourceController @@ -0,0 +1,12 @@ +respond([ + "status" => true, + "msg" => "create method successful." + ]); + } \ No newline at end of file diff --git a/src/Commands/Controller/template/rest/resourceFunctions/delete b/src/Commands/Controller/template/rest/resourceFunctions/delete new file mode 100644 index 0000000..d917f1d --- /dev/null +++ b/src/Commands/Controller/template/rest/resourceFunctions/delete @@ -0,0 +1,7 @@ + public function delete($id = null){ + return $this->respond([ + "status" => true, + "id" => $id, + "msg" => "delede method successful." + ]); + } \ No newline at end of file diff --git a/src/Commands/Controller/template/rest/resourceFunctions/edit b/src/Commands/Controller/template/rest/resourceFunctions/edit new file mode 100644 index 0000000..9bf836f --- /dev/null +++ b/src/Commands/Controller/template/rest/resourceFunctions/edit @@ -0,0 +1,7 @@ + public function edit($id = null){ + return $this->respond([ + "status" => true, + "id" => $id, + "msg" => "edit method successful." + ]); + } \ No newline at end of file diff --git a/src/Commands/Controller/template/rest/resourceFunctions/index b/src/Commands/Controller/template/rest/resourceFunctions/index new file mode 100644 index 0000000..c16007d --- /dev/null +++ b/src/Commands/Controller/template/rest/resourceFunctions/index @@ -0,0 +1,6 @@ + public function index(){ + return $this->respond([ + "status" => true, + "msg" => "index method successful." + ]); + } \ No newline at end of file diff --git a/src/Commands/Controller/template/rest/resourceFunctions/new b/src/Commands/Controller/template/rest/resourceFunctions/new new file mode 100644 index 0000000..7b49228 --- /dev/null +++ b/src/Commands/Controller/template/rest/resourceFunctions/new @@ -0,0 +1,6 @@ + public function new(){ + return $this->respond([ + "status" => true, + "msg" => "new method successful." + ]); + } \ No newline at end of file diff --git a/src/Commands/Controller/template/rest/resourceFunctions/show b/src/Commands/Controller/template/rest/resourceFunctions/show new file mode 100644 index 0000000..f45b8d0 --- /dev/null +++ b/src/Commands/Controller/template/rest/resourceFunctions/show @@ -0,0 +1,7 @@ + public function show($id = null){ + return $this->respond([ + "status" => true, + "id" => $id, + "msg" => "show method successful." + ]); + } \ No newline at end of file diff --git a/src/Commands/Controller/template/rest/resourceFunctions/update b/src/Commands/Controller/template/rest/resourceFunctions/update new file mode 100644 index 0000000..32c21a0 --- /dev/null +++ b/src/Commands/Controller/template/rest/resourceFunctions/update @@ -0,0 +1,7 @@ + public function update($id = null){ + return $this->respond([ + "status" => true, + "id" => $id, + "msg" => "update method successful." + ]); + } \ No newline at end of file