优化命令行make指令

This commit is contained in:
thinkphp
2016-07-03 14:28:31 +08:00
parent c57667b949
commit f51c498c9b
5 changed files with 130 additions and 90 deletions

View File

@@ -49,9 +49,10 @@ class Console
"think\\console\\command\\Help",
"think\\console\\command\\Lists",
"think\\console\\command\\Build",
"think\\console\\command\\Make",
"think\\console\\command\\make\\Controller",
"think\\console\\command\\make\\Model",
"think\\console\\command\\optimize\\Autoload"
"think\\console\\command\\optimize\\Autoload",
];
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
@@ -130,7 +131,7 @@ class Console
$exitCode = $e->getCode();
if (is_numeric($exitCode)) {
$exitCode = (int)$exitCode;
$exitCode = (int) $exitCode;
if (0 === $exitCode) {
$exitCode = 1;
}
@@ -241,7 +242,7 @@ class Console
*/
public function setCatchExceptions($boolean)
{
$this->catchExceptions = (bool)$boolean;
$this->catchExceptions = (bool) $boolean;
}
/**
@@ -251,7 +252,7 @@ class Console
*/
public function setAutoExit($boolean)
{
$this->autoExit = (bool)$boolean;
$this->autoExit = (bool) $boolean;
}
/**
@@ -419,7 +420,7 @@ class Console
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
return preg_quote($matches[1]) . '[^:]*';
}, $namespace);
$namespaces = preg_grep('{^' . $expr . '}', $allNamespaces);
$namespaces = preg_grep('{^' . $expr . '}', $allNamespaces);
if (empty($namespaces)) {
$message = sprintf('There are no commands defined in the "%s" namespace.', $namespace);
@@ -457,7 +458,7 @@ class Console
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
return preg_quote($matches[1]) . '[^:]*';
}, $name);
$commands = preg_grep('{^' . $expr . '}', $allCommands);
$commands = preg_grep('{^' . $expr . '}', $allCommands);
if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) {
if (false !== $pos = strrpos($name, ':')) {
@@ -646,19 +647,19 @@ class Console
if ('\\' === DS) {
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
return [(int)$matches[1], (int)$matches[2]];
return [(int) $matches[1], (int) $matches[2]];
}
if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) {
return [(int)$matches[1], (int)$matches[2]];
return [(int) $matches[1], (int) $matches[2]];
}
}
if ($sttyString = $this->getSttyColumns()) {
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
return [(int)$matches[2], (int)$matches[1]];
return [(int) $matches[2], (int) $matches[1]];
}
if (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
return [(int)$matches[2], (int)$matches[1]];
return [(int) $matches[2], (int) $matches[1]];
}
}

View File

@@ -11,37 +11,111 @@
namespace think\console\command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class Make extends Command
{
// 创建目录
protected static function buildDir($dir)
/**
* {@inheritdoc}
*/
protected function configure()
{
if (!is_dir(APP_PATH . $dir)) {
mkdir(APP_PATH . strtolower($dir), 0777, true);
$this
->setName('make')
->setDescription('Create a new applcation class')
->addArgument('namespace', Argument::OPTIONAL, null)
->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);
}
// 创建文件
protected static function buildFile($file, $content)
{
if (is_file(APP_PATH . $file)) {
if (is_file($file)) {
exception('file already exists');
}
file_put_contents(APP_PATH . $file, $content);
if (!is_dir(dirname($file))) {
mkdir(strtolower(dirname($file)), 0777, true);
}
file_put_contents($file, $content);
}
protected static function formatNameSpace($namespace)
// 生成类库文件
protected function build($namespace, $extend)
{
$namespace = explode('\\', $namespace);
$tpl = file_get_contents(THINK_PATH . 'tpl' . DS . 'make.tpl');
foreach ($namespace as $key => $value) {
if ($key == count($namespace) - 1) {
$newNameSpace[1] = $value;
} else {
$newNameSpace[0][$key] = $value;
// comminute namespace
$namespace = explode('\\', $namespace);
$className = array_pop($namespace);
if ($extend) {
$extend = 'extends \\' . ltrim($extend, '\\');
}
// 处理内容
$content = str_replace(['{%extend%}', '{%className%}', '{%namespace%}'],
[$extend, $className, implode('\\', $namespace)],
$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)
{
// 处理命名空间
if (!empty($module)) {
$namespace = App::$namespace . "\\" . $module . "\\" . $layer . "\\" . $namespace;
}
// 处理继承
if (empty($extend)) {
switch ($layer) {
case 'model':
$extend = '\\think\\Model';
break;
case 'validate':
$extend = '\\think\Validate';
break;
case 'controller':
default:
$extend = '';
break;
}
} else {
if (!preg_match("/\\\/", $extend)) {
if (!empty($module)) {
$extend = "\\" . App::$namespace . "\\" . $module . "\\" . $layer . "\\" . $extend;
}
}
}
return $newNameSpace;
return $this->build($namespace, $extend);
}
}

View File

@@ -11,7 +11,6 @@
namespace think\console\command\make;
use think\App;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
@@ -35,61 +34,10 @@ class Controller extends \think\console\command\Make
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);
$module = $input->getOption('module');
$extend = $input->getOption('extend');
$result = $this->getResult('controller', $namespace, $module, $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;
}
}

View File

@@ -2,25 +2,42 @@
// +----------------------------------------------------------------------
// | 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: 刘志淳 <chun@engineer.com>
// +----------------------------------------------------------------------
namespace think\console\command\make;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\console\command\Command;
class Model extends Command
class Model extends \think\console\command\Make
{
public function __construct()
/**
* {@inheritdoc}
*/
protected function configure()
{
parent::__construct("make:model");
$this
->setName('make:model')
->setDescription('Create a new model class')
->addArgument('namespace', Argument::OPTIONAL, null)
->addOption('module', 'm', Option::VALUE_OPTIONAL, 'Module Name', null)
->addOption('extend', 'e', Option::VALUE_OPTIONAL, 'Base on Model class', null);
}
}
protected function execute(Input $input, Output $output)
{
$namespace = $input->getArgument('namespace');
$module = $input->getOption('module');
$extend = $input->getOption('extend');
$result = $this->getResult('model', $namespace, $module, $extend);
$output->writeln("output:" . $result);
}
}

View File

@@ -11,7 +11,7 @@
namespace {%namespace%};
class {%className%} extends {%extend%}
class {%className%} {%extend%}
{
}