-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
665 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "monken/cli-create", | ||
"description": "Cli-Create is based on CodeIgniter4. It will help you generate template files more quickly when developing projects with CodeIgniter4.", | ||
"keywords": [ | ||
"codeigniter", | ||
"codeigniter4", | ||
"cli", | ||
"create" | ||
], | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "monkenWu", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"require": { | ||
"php" : "^7.2" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"CliCreate\\Commands\\": "src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
<?php | ||
/** | ||
* Cli-Create | ||
* | ||
* Cli-Create 基於 CodeIgniter4 。它將可以幫助你在使用 CodeIgniter4 開發專案時,更加迅速地產生各式模板檔案。 | ||
* Cli-Create is based on CodeIgniter4. It will help you generate template files more quickly when developing projects with CodeIgniter4. | ||
* @package CodeIgniter4 | ||
* @subpackage libraries | ||
* @category library | ||
* @version 1.0.0 | ||
* @author monkenWu <[email protected]> | ||
* @link https://github.com/monkenWu/Codeigniter4-Easy-create | ||
* | ||
*/ | ||
|
||
namespace CliCreate\Commands; | ||
use CodeIgniter\CLI\CLI; | ||
|
||
class CliCreate { | ||
|
||
/** | ||
* 取得使用者名稱 | ||
* | ||
* @param array $params CLI所取得的 params 陣列 | ||
* @param String $type 將影響到提示中的文字 | ||
* @return string | ||
*/ | ||
public static function getName(array &$params,String $type){ | ||
$name = array_shift($params); | ||
if (empty($name)){ | ||
$name = CLI::prompt( | ||
CLI::color("Name the {$type} file","blue") | ||
); | ||
} | ||
if (empty($name)){ | ||
CLI::error("You must provide a {$type} file name."); | ||
exit(); | ||
} | ||
return $name; | ||
} | ||
|
||
/** | ||
* 取得使用者輸入的命名空間 | ||
* | ||
* 將會判斷使用者所輸入的值是否符合規則,並且將所有的路徑強制轉換成第一個字母大寫的形式。 | ||
* @param String $type Models、Controllers或App下的任何資料夾。 | ||
* @return string 將回傳取得的命名空間。 | ||
*/ | ||
public static function getNameSpace(String $type){ | ||
$name = CLI::prompt( | ||
CLI::color("The current namespace is \"App \ {$type}\", and what you type will be concatenated after this ","blue") | ||
); | ||
while(strstr($name, '/')){ | ||
$name = CLI::prompt( | ||
CLI::color("Namespace cannot be separated by '\',Please retype ","blue") | ||
); | ||
} | ||
if( (str_split($name)[0] != "\\") && ($name != "")){ | ||
$name = "\\".$name; | ||
} | ||
$tempArray = explode("\\",$name); | ||
$returnName = ""; | ||
foreach ($tempArray as $key => $value) { | ||
if($value != "") $returnName .= "\\".ucfirst($value); | ||
} | ||
return $returnName; | ||
} | ||
|
||
/** | ||
* 判斷是否有一個以上的true存在。 | ||
* | ||
* @param Array $boolArr | ||
* @return boolean | ||
*/ | ||
public static function isMulti(Array $boolArr){ | ||
$num = 0; | ||
foreach ($boolArr as $key => $value) { | ||
if($value) $num++; | ||
} | ||
return $num > 1 ? true : false; | ||
} | ||
|
||
/** | ||
* 寫入檔案。 | ||
* | ||
* @param String $writePath 寫入檔案的絕對路徑 | ||
* @param String $fileText 檔案名稱 | ||
* @return string | ||
*/ | ||
public static function writeFile(String $writePath,String $fileText){ | ||
helper('filesystem'); | ||
|
||
if (! write_file($writePath, $fileText)){ | ||
CLI::error("Failed to create file."); | ||
exit(); | ||
} | ||
|
||
CLI::write('Created successfully: ' . | ||
CLI::color($writePath, 'green') | ||
); | ||
} | ||
|
||
/** | ||
* 取得模板。 | ||
* | ||
* @param String $path 絕對路徑 | ||
* @param String $name 模板名稱 | ||
* @return string | ||
*/ | ||
public static function getTemplate(String $path,String $name){ | ||
$filePath = $path.$name; | ||
$handle = fopen($filePath, "r"); | ||
$template = fread($handle, filesize($filePath)); | ||
fclose($handle); | ||
if($template){ | ||
return $template; | ||
}else{ | ||
CLI::error("Template loading error."); | ||
exit(); | ||
} | ||
} | ||
|
||
/** | ||
* 確認檔案是否存在。 | ||
* | ||
* 若檔案存在,將會詢問使用者是否需要覆寫檔案,或取消寫入。 | ||
* @param String $filePath 檔案完整路徑,包含檔名 | ||
* @param String $type 本次操作的分類,將影響到提示文字 | ||
* @return void | ||
*/ | ||
public static function checkFileEexists(String $filePath,String $type){ | ||
if(file_exists($filePath)){ | ||
$check = CLI::prompt( | ||
CLI::color("Found the same {$type} file name,Do you need overwrite the file?","yellow"), | ||
['y','n'] | ||
); | ||
if($check == "n"){ | ||
CLI::write("{$type} creation process has ". | ||
CLI::color("stopped.", 'yellow') | ||
); | ||
exit(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 替換模板檔案的 Tag 。 | ||
* | ||
* @param String $template 模板字串。 | ||
* @param Array $repalceData 索引陣列, key 值為模板檔案中的 Tag 名稱,Value 為替換值。 | ||
* @return string 替換後的結果 | ||
*/ | ||
public static function replaceText(String $template,Array $repalceData){ | ||
foreach ($repalceData as $key => $value) { | ||
$template = str_replace("{{$key}}", $value, $template); | ||
} | ||
return $template; | ||
} | ||
|
||
/** | ||
* 取得檔案路徑。 | ||
* | ||
* 將會判斷當前執行的系統是否為windows,將會回傳符合系統要求的絕對路徑。 | ||
* @param String $appPath 專案app資料夾路徑 | ||
* @param String $type Models、Controllers或App下的任何資料夾。 | ||
* @param String $dir 是否有更多層資料夾,以 "\\" 分隔。預設為空。 | ||
* @param String $fileName 檔案名稱,不須傳入 ".php" 副檔名。預設為空。 | ||
* @return String 回傳絕對路徑。 | ||
*/ | ||
static public function getPath(String $appPath,String $type,String $dir = "",String $fileName = ""){ | ||
$word = ""; | ||
|
||
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { | ||
$word = "\\"; | ||
}else{ | ||
$word = "/"; | ||
} | ||
|
||
$dirArr = explode("\\",$dir); | ||
$appPath = $appPath.$type; | ||
|
||
foreach ($dirArr as $key => $value) { | ||
$temp = $appPath; | ||
if($key > 0){ | ||
$temp .= $word; | ||
} | ||
$temp .= $value; | ||
if(!is_dir($temp)){ | ||
mkdir($temp,0755); | ||
} | ||
$appPath = $temp; | ||
} | ||
|
||
$fileName .= $fileName != "" ? ".php" : ""; | ||
|
||
return $appPath.$word.$fileName; | ||
} | ||
|
||
/** | ||
* 取得資料夾下所有的命名空間。 | ||
* | ||
* @param String $appPath | ||
* @param String $type Models、Controllers或App下的任何資料夾。 | ||
* @return array 命名空間陣列 | ||
*/ | ||
public static function getAllNamespace(String $appPath,String $type){ | ||
$map = directory_map("{$appPath}{$type}/", FALSE, TRUE); | ||
$nameArr = []; | ||
|
||
$getDeepMap = function($map,$key,&$nameArr) use (&$getDeepMap){ | ||
$nowDir = $key; | ||
foreach ($map as $key => $value) { | ||
if(gettype($key) != "integer"){ | ||
$getDeepMap($map,"{$nowDir}{$key}",$nameArr); | ||
} | ||
$path = $nowDir.str_replace(".php","",$value); | ||
$path = str_replace("/","\\",$path); | ||
$nameArr[] = $path; | ||
} | ||
}; | ||
|
||
foreach ($map as $key => $value) { | ||
if(gettype($key) != "integer"){ | ||
$getDeepMap($value,"App/{$type}/{$key}",$nameArr); | ||
}else{ | ||
if(!strstr($value,".php")) continue; | ||
$path = "App/{$type}/".str_replace(".php","",$value); | ||
$path = str_replace("/","\\",$path); | ||
$nameArr[] = $path; | ||
} | ||
} | ||
|
||
return $nameArr; | ||
} | ||
|
||
/** | ||
* 顯示表格,並且回傳使用者所選擇的 ID 。 | ||
* | ||
* @param Array $namespaceArr 傳入由命名空間組成的陣列 | ||
* @return array 使用者所選擇的 id | ||
*/ | ||
public static function selectTable(Array $namespaceArr){ | ||
|
||
$thead = ['ID', 'Namespace', 'ID', 'Namespace']; | ||
$tbody = []; | ||
foreach ($namespaceArr as $key => $value) { | ||
if($key%2==0){ | ||
$tbody[] = [$key,$value]; | ||
}else{ | ||
array_push($tbody[count($tbody)-1],$key,$value); | ||
} | ||
} | ||
|
||
CLI::table($tbody,$thead); | ||
$useID = CLI::prompt( | ||
CLI::color("Please enter the ID of the model you want to use.\nIf there are multiple, they are separated by \",\" ","blue") | ||
); | ||
|
||
return $useID; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
/** | ||
* Cli-Create | ||
* | ||
* Cli-Create 基於 CodeIgniter4 。它將可以幫助你在使用 CodeIgniter4 開發專案時,更加迅速地產生各式檔案。 | ||
* Cli-Create is based on CodeIgniter4. It will help you generate template files more quickly when developing projects with CodeIgniter4. | ||
* @package CodeIgniter4 | ||
* @subpackage libraries | ||
* @category library | ||
* @version 1.0.0 | ||
* @author monkenWu <[email protected]> | ||
* @link https://github.com/monkenWu/Codeigniter4-Easy-create | ||
* | ||
*/ | ||
|
||
namespace CliCreate\Commands\Controller; | ||
use CodeIgniter\CLI\BaseCommand; | ||
use CodeIgniter\CLI\CLI; | ||
use CliCreate\Commands\CliCreate; | ||
|
||
class CreateController extends BaseCommand{ | ||
|
||
protected $group = 'Cli-Create'; | ||
protected $name = 'create:controller'; | ||
protected $description = 'Create a new controller file.'; | ||
protected $usage = 'create:controller [controller_name] [Options]'; | ||
protected $arguments = [ | ||
'controller_name' => 'The controller name.', | ||
]; | ||
protected $options = [ | ||
'-nobase' => 'Do not extends BaseControllers Class.', | ||
'-usemodel' => 'Choose models.', | ||
'-space' => 'Using this option will create folders and files according to the path you typed.' | ||
]; | ||
|
||
private $controllerName; | ||
private $useModel = false; | ||
private $useBase = true; | ||
private $nameSpace = false; | ||
private $templatePath; | ||
private $appPath; | ||
|
||
public function run(array $params = []){ | ||
$this->controllerName = ucfirst(CliCreate::getName($params,"controller")); | ||
$this->appPath = APPPATH; | ||
$this->templatePath = dirname(__FILE__)."/template/"; | ||
$this->getOption(); | ||
|
||
$useController = ""; | ||
$extendsController = "BaseController"; | ||
if(!$this->useBase){ | ||
$useController = "\nuse CodeIgniter\Controller;"; | ||
$extendsController = "Controller"; | ||
} | ||
|
||
//get namespace | ||
$space = ""; | ||
if($this->nameSpace){ | ||
$space = CliCreate::getNameSpace("Controllers"); | ||
} | ||
|
||
//get model | ||
$useModels = ""; | ||
if($this->useModel){ | ||
$modelList = CliCreate::getAllNamespace($this->appPath,"Models"); | ||
|
||
$useID = CliCreate::selectTable($modelList); | ||
|
||
$numArr = explode(",",$useID); | ||
foreach ($numArr as $key => $value) { | ||
$useModels .= "use {$modelList[$value]};\n" ?? ""; | ||
} | ||
} | ||
|
||
$templateData = [ | ||
"name" => $this->controllerName, | ||
"namespace" => $space, | ||
"useController" => $useController, | ||
"extendsController" => $extendsController, | ||
"useModels" => $useModels | ||
]; | ||
$template = CliCreate::getTemplate($this->templatePath,"controller"); | ||
$replaceTemplate = CliCreate::replaceText($template,$templateData); | ||
$writePath = CliCreate::getPath($this->appPath,"Controllers",$space,$this->controllerName); | ||
CliCreate::checkFileEexists($writePath,"Controller"); | ||
CliCreate::writeFile($writePath,$replaceTemplate); | ||
return; | ||
} | ||
|
||
/** | ||
* 取得可能被使用者輸入的 options | ||
* | ||
* @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; | ||
} | ||
|
||
} |
Oops, something went wrong.