options = $options; } public function doGenerate() { $fs = new Filesystem(); $templateDir = __DIR__ . '/templates/'; $resultStr = ''; if (!empty($this->options['controller'])) { $controllerPath = __DIR__ . "/../../../{$this->options['controller']['module']}/controller/".$this->rName($this->options['controller']['name'])."Controller.php"; $fs->mkdir(__DIR__ . "/../../../{$this->options['controller']['module']}/controller"); $this->deleteFileIfExists($controllerPath); $ControllerStub = $this->generateFile($templateDir, 'ControllerStub.stub', $controllerPath, 'controller'); $resultStr .= $ControllerStub; } if (!empty($this->options['service'])) { $servicePath = __DIR__ . "/../../../{$this->options['service']['module']}/service/".$this->rName($this->options['service']['name'])."Service.php"; $fs->mkdir(__DIR__ . "/../../../{$this->options['service']['module']}/service"); $this->deleteFileIfExists($servicePath); $ServiceStub = $this->generateFile($templateDir, 'ServiceStub.stub', $servicePath, 'service'); $resultStr .= $ServiceStub; } if (!empty($this->options['logic'])) { $logicPath = __DIR__ . "/../../../{$this->options['logic']['module']}/logic/".$this->rName($this->options['logic']['name'])."Logic.php"; $fs->mkdir(__DIR__ . "/../../../{$this->options['logic']['module']}/logic"); $this->deleteFileIfExists($logicPath); $LogicStub = $this->generateFile($templateDir, 'LogicStub.stub', $logicPath, 'logic'); $resultStr .= $LogicStub; } if (!empty($this->options['model'])) { $modelPath = __DIR__ . "/../../../{$this->options['model']['module']}/model/".$this->rName($this->options['model']['name']).".php"; $fs->mkdir(__DIR__ . "/../../../{$this->options['model']['module']}/model"); $this->deleteFileIfExists($modelPath); $ModelStub = $this->generateFile($templateDir, 'ModelStub.stub', $modelPath, 'model'); $resultStr .= $ModelStub; } if (!empty($this->options['validate'])) { $validatePath = __DIR__ . "/../../../{$this->options['validate']['module']}/validate/".$this->rName($this->options['validate']['name'])."Validate.php"; $fs->mkdir(__DIR__ . "/../../../{$this->options['validate']['module']}/validate"); $this->deleteFileIfExists($validatePath); $ValidatorStub = $this->generateFile($templateDir, 'ValidateStub.stub', $validatePath, 'validate'); $resultStr .= $ValidatorStub; } return $resultStr ? $resultStr . "文件生成成功!
" : '未生成任何文件'; } /** * 根据模板生成文件 * * @param string $templateDir 模板目录 * @param string $templateFile 模板文件名 * @param string $outputPath 输出文件路径 */ protected function generateFile($templateDir, $templateFile, $outputPath, $fileType) { $templatePath = $templateDir . $templateFile; if (!file_exists($templatePath)) { echo "模板文件 {$templateFile} 不存在! \n"; return; } $content = file_get_contents($templatePath); switch ($fileType) { case 'controller': $content = str_replace('{{controllerModule}}', $this->options['controller']['module'], $content); $content = str_replace('{{serviceModule}}', $this->options['service']['module'], $content); $content = str_replace('{{serviceName}}', $this->rName($this->options['service']['name']), $content); $content = str_replace('{{controllerName}}', $this->rName($this->options['controller']['name']), $content); break; case 'service': $content = str_replace('{{serviceModule}}', $this->options['service']['module'], $content); $content = str_replace('{{validateModule}}', $this->options['validate']['module'], $content); $content = str_replace('{{validateName}}', $this->rName($this->options['validate']['name']), $content); $content = str_replace('{{modelModule}}', $this->options['model']['module'], $content); $content = str_replace('{{modelName}}', $this->rName($this->options['model']['name']), $content); $content = str_replace('{{serviceName}}', $this->rName($this->options['service']['name']), $content); break; case 'logic': $content = str_replace('{{logicModule}}', $this->options['logic']['module'], $content); $content = str_replace('{{modelModule}}', $this->options['model']['module'], $content); $content = str_replace('{{modelName}}', $this->rName($this->options['model']['name']), $content); $content = str_replace('{{logicName}}', $this->rName($this->options['logic']['name']), $content); break; case 'model': $content = str_replace('{{modelModule}}', $this->options['model']['module'], $content); $content = str_replace('{{modelName}}', $this->rName($this->options['model']['name']), $content); $content = str_replace('{{table}}', $this->options['table'], $content); $content = str_replace('{{fillable}}', $this->fillableFields(), $content); break; case 'validate': $content = str_replace('{{validateModule}}', $this->options['validate']['module'], $content); $content = str_replace('{{validateName}}', $this->rName($this->options['validate']['name']), $content); break; } file_put_contents($outputPath, $content); return "生成文件: {$outputPath}
"; } /** * 设置字段 * * @param array $fields 字段数组 */ public function fillableFields() { $tableName = $this->options['table']; $fields = TableInspector::getColumnNames($tableName); $str = ''; foreach ($fields as $key => $val) { if ($key < (count($fields)-1)) { $str .= "'{$val}', "; } else { $str .= "'{$val}'"; } } $fields = trim($str, '"'); return $fields; } /** * 如果文件存在,删除它 * * @param string $path 文件路径 */ protected function deleteFileIfExists($path) { if (file_exists($path)) { unlink($path); echo "删除已存在文件: {$path} \n"; } } /** * 设置名称 */ public function rName($name = '') { return ucfirst(camelize($name)); } } /** * 下划线转驼峰 * 思路: * step1.原字符串转小写,原字符串中的分隔符用空格替换,在字符串开头加上分隔符 * step2.将字符串中每个单词的首字母转换为大写,再去空格,去字符串首部附加的分隔符. */ function camelize($uncamelized_words, $separator='_') { $uncamelized_words = $separator . str_replace($separator, " ", strtolower($uncamelized_words)); return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator ); } /** * 驼峰命名转下划线命名 * 思路: * 小写和大写紧挨一起的地方,加上分隔符,然后全部转小写 */ function uncamelize($camelCaps, $separator='_') { return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps)); }