From 506b587e56d99a0260db085899dc8714b1cc6af7 Mon Sep 17 00:00:00 2001 From: chunice Date: Sat, 28 May 2016 11:46:09 +0800 Subject: [PATCH] =?UTF-8?q?Console=E5=A2=9E=E5=8A=A0make:controller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/console/command/Make.php | 47 ++++++++++ .../think/console/command/make/Controller.php | 87 +++++++++++++++++-- tpl/make_controller.tpl | 17 ++++ 3 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 library/think/console/command/Make.php create mode 100644 tpl/make_controller.tpl diff --git a/library/think/console/command/Make.php b/library/think/console/command/Make.php new file mode 100644 index 00000000..928976c2 --- /dev/null +++ b/library/think/console/command/Make.php @@ -0,0 +1,47 @@ + +// +---------------------------------------------------------------------- + +namespace think\console\command; + +class Make extends Command +{ + // 创建目录 + protected static function buildDir($dir) + { + if (!is_dir(APP_PATH . $dir)) { + mkdir(APP_PATH . strtolower($dir), 0777, true); + } + } + + // 创建文件 + protected static function buildFile($file, $content) + { + if (is_file(APP_PATH . $file)) { + exception('file already exists'); + } + file_put_contents(APP_PATH . $file, $content); + } + + protected static function formatNameSpace($namespace) + { + $namespace = explode('\\', $namespace); + + foreach ($namespace as $key => $value) { + if ($key == count($namespace) - 1) { + $newNameSpace[1] = $value; + } else { + $newNameSpace[0][$key] = $value; + } + } + + return $newNameSpace; + } +} diff --git a/library/think/console/command/make/Controller.php b/library/think/console/command/make/Controller.php index f5014fec..911c9b1f 100644 --- a/library/think/console/command/make/Controller.php +++ b/library/think/console/command/make/Controller.php @@ -2,22 +2,93 @@ // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- -// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved. +// | Copyright (c) 2016 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- -// | Author: yunwuxin <448901948@qq.com> +// | Author: 刘志淳 // +---------------------------------------------------------------------- namespace think\console\command\make; -use think\console\command\Command; +use think\console\Input; +use think\console\input\Argument; +use think\console\input\Option; +use think\console\Output; -class Controller extends Command +class Controller extends \think\console\command\Make { - - public function __construct() + /** + * {@inheritdoc} + */ + protected function configure() { - parent::__construct("make:controller"); + $this + ->setName('make:controller') + ->setDescription('Create a new controller class') + ->addArgument('namespace', Argument::OPTIONAL, null) + ->addOption('module', 'm', Option::VALUE_OPTIONAL, 'Module Name', null) + ->addOption('extend', 'e', Option::VALUE_OPTIONAL, 'Base on Controller class', null); } -} \ No newline at end of file + + protected function execute(Input $input, Output $output) + { + $namespace = $input->getArgument('namespace'); + $module = $input->getOption('module'); + + + // 处理命名空间 + if (!empty($module)) { + $namespace = APP_NAMESPACE . "\\" . $module . "\\" . 'controller' . "\\" . $namespace; + } + + // 处理继承 + $extend = $input->getOption('extend'); + + if (empty($extend)) { + $extend = "\\think\\Controller"; + } else { + if (!preg_match("/\\\/", $extend)) { + if (!empty($module)) { + $extend = "\\" . APP_NAMESPACE . "\\" . $module . "\\" . 'controller' . "\\" . $extend; + } + } + } + + + $result = $this->build($namespace, $extend); + $output->writeln("output:" . $result); + } + + private function build($namespace, $extend) + { + $tpl = file_get_contents(THINK_PATH . 'tpl' . DS . 'make_controller.tpl'); + + // comminute namespace + $allNamespace = self::formatNameSpace($namespace); + $namespace = implode('\\', $allNamespace[0]); + $className = ucwords($allNamespace[1]); + + // 处理内容 + $content = str_replace("{%extend%}", $extend, + str_replace("{%className%}", $className, + str_replace("{%namespace%}", $namespace, $tpl) + ) + ); + + // 处理文件夹 + $path = ''; + foreach ($allNamespace[0] as $key => $value) { + if ($key >= 1) { + self::buildDir($path . $value); + $path .= $value . "\\"; + } + } + + // 处理文件 + $file = $path . $className . '.php'; + self::buildFile($file, $content); + + return APP_PATH . $file; + } +} diff --git a/tpl/make_controller.tpl b/tpl/make_controller.tpl new file mode 100644 index 00000000..4254f5e2 --- /dev/null +++ b/tpl/make_controller.tpl @@ -0,0 +1,17 @@ + +// +---------------------------------------------------------------------- + +namespace {%namespace%}; + +class {%className%} extends {%extend%} +{ + +}