改进make命令

This commit is contained in:
yunwuxin
2016-07-14 18:05:30 +08:00
parent 2b88407f7a
commit d3c5c6f166
9 changed files with 227 additions and 164 deletions

View File

@@ -50,7 +50,6 @@ class Console
"think\\console\\command\\Lists", "think\\console\\command\\Lists",
"think\\console\\command\\Build", "think\\console\\command\\Build",
"think\\console\\command\\make\\Controller", "think\\console\\command\\make\\Controller",
"think\\console\\command\\make\\File",
"think\\console\\command\\make\\Model", "think\\console\\command\\make\\Model",
"think\\console\\command\\optimize\\Autoload", "think\\console\\command\\optimize\\Autoload",
]; ];

View File

@@ -11,82 +11,110 @@
namespace think\console\command; namespace think\console\command;
use think\App; use think\Config;
use think\Exception; use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
class Make extends Command abstract class Make extends Command
{ {
/** @var Input */
protected $input;
// 创建文件 /** @var Output */
protected static function buildFile($file, $content) protected $output;
protected $type;
abstract protected function getStub();
protected function configure()
{ {
if (is_file($file)) { $this->addArgument('name', Argument::REQUIRED, "The name of the class");
throw new Exception('file already exists');
} }
if (!is_dir(dirname($file))) { public function run(Input $input, Output $output)
mkdir(strtolower(dirname($file)), 0755, true);
}
file_put_contents($file, $content);
}
// 生成类库文件
protected function build($namespace, $extend, $content = '')
{ {
$tpl = file_get_contents(THINK_PATH . 'tpl' . DS . 'make.tpl'); $this->input = $input;
$this->output = $output;
// comminute namespace return parent::run($input, $output);
$namespace = explode('\\', $namespace);
$className = array_pop($namespace);
if ($extend) {
$extend = 'extends \\' . ltrim($extend, '\\');
}
// 处理内容
$content = str_replace(['{%extend%}', '{%className%}', '{%namespace%}', '{%content%}'],
[$extend, $className, implode('\\', $namespace), $content],
$tpl);
// 处理文件名
array_shift($namespace);
$file = APP_PATH . implode(DS, $namespace) . DS . $className . '.php';
// 生成类库文件
self::buildFile($file, $content);
return realpath($file);
} }
protected function getResult($layer, $namespace, $module, $extend, $content = '') protected function execute(Input $input, Output $output)
{ {
// 处理命名空间 $name = trim($input->getArgument('name'));
if (!empty($module)) {
$namespace = App::$namespace . "\\" . $module . "\\" . $layer . "\\" . $namespace; $classname = $this->getClassName($name);
$pathname = $this->getPathName($classname);
if (is_file($pathname)) {
$output->writeln('<error>' . $this->type . ' already exists!</error>');
return false;
} }
// 处理继承 if (!is_dir(dirname($pathname))) {
if (empty($extend)) { mkdir(strtolower(dirname($pathname)), 0755, true);
switch ($layer) { }
case 'model':
$extend = '\\think\\Model'; file_put_contents($pathname, $this->buildClass($name));
break;
case 'validate': $output->writeln('<info>' . $this->type . ' created successfully.</info>');
$extend = '\\think\\Validate';
break; }
case 'controller':
default: protected function buildClass($name)
$extend = ''; {
break; $stub = file_get_contents($this->getStub());
$namespace = trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
$class = str_replace($namespace . '\\', '', $name);
return str_replace(['{%className%}', '{%namespace%}', '{%app_namespace%}'], [
$class,
$namespace,
Config::get('app_namespace')
], $stub);
}
protected function getPathName($name)
{
$name = str_replace(Config::get('app_namespace') . '\\', '', $name);
return APP_PATH . str_replace('\\', '/', $name) . '.php';
}
protected function getClassName($name)
{
$appNamespace = Config::get('app_namespace');
if (strpos($name, $appNamespace . '\\') === 0) {
return $name;
}
if (Config::get('app_multi_module')) {
if (strpos($name, '/')) {
list($module, $name) = explode('/', $name, 2);
} else {
$module = 'common';
} }
} else { } else {
if (!preg_match("/\\\/", $extend)) { $module = null;
if (!empty($module)) {
$extend = "\\" . App::$namespace . "\\" . $module . "\\" . $layer . "\\" . $extend;
}
}
} }
return $this->build($namespace, $extend, $content); if (strpos($name, '/') !== false) {
$name = str_replace('/', '\\', $name);
} }
return $this->getNamespace($appNamespace, $module) . '\\' . $name;
}
protected function getNamespace($appNamespace, $module)
{
return $module ? ($appNamespace . '\\' . $module) : $appNamespace;
}
} }

View File

@@ -11,34 +11,40 @@
namespace think\console\command\make; namespace think\console\command\make;
use think\Config;
use think\console\command\Make; use think\console\command\Make;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option; use think\console\input\Option;
use think\console\Output;
class Controller extends Make class Controller extends Make
{ {
/**
* {@inheritdoc} protected $type = "Controller";
*/
protected function configure() protected function configure()
{ {
$this parent::configure();
->setName('make:controller') $this->setName('make:controller')
->setDescription('Create a new controller class') ->addOption('plain', null, Option::VALUE_NONE, 'Generate an empty controller class.')
->addArgument('namespace', Argument::REQUIRED) ->setDescription('Create a new resource controller class');
->addOption('module', 'm', Option::VALUE_OPTIONAL, 'Module Name', 'index')
->addOption('extend', 'e', Option::VALUE_OPTIONAL, 'Base on Controller class', null);
} }
protected function execute(Input $input, Output $output) protected function getStub()
{ {
$namespace = $input->getArgument('namespace'); if ($this->input->getOption('plain')) {
$module = $input->getOption('module'); return __DIR__ . '/stubs/controller.plain.stub';
$extend = $input->getOption('extend'); }
$result = $this->getResult('controller', $namespace, $module, $extend);
$output->writeln("output:" . $result); return __DIR__ . '/stubs/controller.stub';
}
protected function getClassName($name)
{
return parent::getClassName($name) . (Config::get('controller_suffix') ? ucfirst(Config::get('url_controller_layer')) : '');
}
protected function getNamespace($appNamespace, $module)
{
return parent::getNamespace($appNamespace, $module) . '\controller';
} }
} }

View File

@@ -1,49 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 刘志淳 <chun@engineer.com>
// +----------------------------------------------------------------------
namespace think\console\command\make;
use think\console\command\Make;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class File extends Make
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('make:file')
->setDescription('Create a new applcation class')
->addArgument('namespace', Argument::REQUIRED)
->addOption('layer', 'l', Option::VALUE_OPTIONAL, 'Layer Name', null)
->addOption('extend', 'e', Option::VALUE_OPTIONAL, 'Extend Base class', null);
}
protected function execute(Input $input, Output $output)
{
$namespace = $input->getArgument('namespace');
$extend = $input->getOption('extend');
if (!$layer = $input->getOption('layer')) {
// 自动识别layer
$item = explode('\\', $namespace);
$layer = basename(dirname(implode(DS, $item)));
}
$result = $this->getResult($layer, $namespace, '', $extend);
$output->writeln("output:" . $result);
}
}

View File

@@ -12,33 +12,25 @@
namespace think\console\command\make; namespace think\console\command\make;
use think\console\command\Make; use think\console\command\Make;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class Model extends Make class Model extends Make
{ {
/** protected $type = "Model";
* {@inheritdoc}
*/
protected function configure() protected function configure()
{ {
$this parent::configure();
->setName('make:model') $this->setName('make:model')
->setDescription('Create a new model class') ->setDescription('Create a new model class');
->addArgument('namespace', Argument::REQUIRED)
->addOption('module', 'm', Option::VALUE_OPTIONAL, 'Module Name', 'index')
->addOption('extend', 'e', Option::VALUE_OPTIONAL, 'Base on Model class', null);
} }
protected function execute(Input $input, Output $output) protected function getStub()
{ {
$namespace = $input->getArgument('namespace'); return __DIR__ . '/stubs/model.stub';
$module = $input->getOption('module');
$extend = $input->getOption('extend');
$result = $this->getResult('model', $namespace, $module, $extend);
$output->writeln("output:" . $result);
} }
protected function getNamespace($appNamespace, $module)
{
return parent::getNamespace($appNamespace, $module) . '\model';
}
} }

View File

@@ -0,0 +1,10 @@
<?php
namespace {%namespace%};
use think\Controller;
class {%className%} extends Controller
{
//
}

View File

@@ -0,0 +1,84 @@
<?php
namespace {%namespace%};
use think\Controller;
class {%className%} extends Controller
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace {%namespace%};
use think\Model;
class {%className%} extends Model
{
//
}

View File

@@ -1,17 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace {%namespace%};
class {%className%} {%extend%}
{
{%content%}
}