diff --git a/library/think/Console.php b/library/think/Console.php
index d0388864..991e3a7d 100644
--- a/library/think/Console.php
+++ b/library/think/Console.php
@@ -50,7 +50,6 @@ class Console
"think\\console\\command\\Lists",
"think\\console\\command\\Build",
"think\\console\\command\\make\\Controller",
- "think\\console\\command\\make\\File",
"think\\console\\command\\make\\Model",
"think\\console\\command\\optimize\\Autoload",
];
diff --git a/library/think/console/command/Make.php b/library/think/console/command/Make.php
index b9ea6fdb..6dd754c7 100644
--- a/library/think/console/command/Make.php
+++ b/library/think/console/command/Make.php
@@ -11,82 +11,110 @@
namespace think\console\command;
-use think\App;
-use think\Exception;
+use think\Config;
+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;
- // 创建文件
- protected static function buildFile($file, $content)
+ /** @var Output */
+ protected $output;
+
+ protected $type;
+
+ abstract protected function getStub();
+
+ protected function configure()
{
- if (is_file($file)) {
- throw new Exception('file already exists');
- }
-
- if (!is_dir(dirname($file))) {
- mkdir(strtolower(dirname($file)), 0755, true);
- }
-
- file_put_contents($file, $content);
+ $this->addArgument('name', Argument::REQUIRED, "The name of the class");
}
- // 生成类库文件
- protected function build($namespace, $extend, $content = '')
+ public function run(Input $input, Output $output)
{
- $tpl = file_get_contents(THINK_PATH . 'tpl' . DS . 'make.tpl');
-
- // comminute namespace
- $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);
+ $this->input = $input;
+ $this->output = $output;
+ return parent::run($input, $output);
}
- protected function getResult($layer, $namespace, $module, $extend, $content = '')
+ protected function execute(Input $input, Output $output)
{
- // 处理命名空间
- if (!empty($module)) {
- $namespace = App::$namespace . "\\" . $module . "\\" . $layer . "\\" . $namespace;
+ $name = trim($input->getArgument('name'));
+
+ $classname = $this->getClassName($name);
+
+ $pathname = $this->getPathName($classname);
+
+ if (is_file($pathname)) {
+ $output->writeln('' . $this->type . ' already exists!');
+ return false;
}
- // 处理继承
- if (empty($extend)) {
- switch ($layer) {
- case 'model':
- $extend = '\\think\\Model';
- break;
- case 'validate':
- $extend = '\\think\\Validate';
- break;
- case 'controller':
- default:
- $extend = '';
- break;
+ if (!is_dir(dirname($pathname))) {
+ mkdir(strtolower(dirname($pathname)), 0755, true);
+ }
+
+ file_put_contents($pathname, $this->buildClass($name));
+
+ $output->writeln('' . $this->type . ' created successfully.');
+
+ }
+
+ protected function buildClass($name)
+ {
+ $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 {
- if (!preg_match("/\\\/", $extend)) {
- if (!empty($module)) {
- $extend = "\\" . App::$namespace . "\\" . $module . "\\" . $layer . "\\" . $extend;
- }
- }
+ $module = null;
}
- 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;
+ }
+
}
diff --git a/library/think/console/command/make/Controller.php b/library/think/console/command/make/Controller.php
index df727048..afa7be90 100644
--- a/library/think/console/command/make/Controller.php
+++ b/library/think/console/command/make/Controller.php
@@ -11,34 +11,40 @@
namespace think\console\command\make;
+use think\Config;
use think\console\command\Make;
-use think\console\Input;
-use think\console\input\Argument;
use think\console\input\Option;
-use think\console\Output;
class Controller extends Make
{
- /**
- * {@inheritdoc}
- */
+
+ protected $type = "Controller";
+
protected function configure()
{
- $this
- ->setName('make:controller')
- ->setDescription('Create a new controller class')
- ->addArgument('namespace', Argument::REQUIRED)
- ->addOption('module', 'm', Option::VALUE_OPTIONAL, 'Module Name', 'index')
- ->addOption('extend', 'e', Option::VALUE_OPTIONAL, 'Base on Controller class', null);
+ parent::configure();
+ $this->setName('make:controller')
+ ->addOption('plain', null, Option::VALUE_NONE, 'Generate an empty controller class.')
+ ->setDescription('Create a new resource controller class');
}
- protected function execute(Input $input, Output $output)
+ protected function getStub()
{
- $namespace = $input->getArgument('namespace');
- $module = $input->getOption('module');
- $extend = $input->getOption('extend');
- $result = $this->getResult('controller', $namespace, $module, $extend);
- $output->writeln("output:" . $result);
+ if ($this->input->getOption('plain')) {
+ return __DIR__ . '/stubs/controller.plain.stub';
+ }
+
+ 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';
}
}
diff --git a/library/think/console/command/make/File.php b/library/think/console/command/make/File.php
deleted file mode 100644
index 9b3b9b5f..00000000
--- a/library/think/console/command/make/File.php
+++ /dev/null
@@ -1,49 +0,0 @@
-
-// +----------------------------------------------------------------------
-
-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);
- }
-
-}
diff --git a/library/think/console/command/make/Model.php b/library/think/console/command/make/Model.php
index af741382..d4e9b5dd 100644
--- a/library/think/console/command/make/Model.php
+++ b/library/think/console/command/make/Model.php
@@ -12,33 +12,25 @@
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 Model extends Make
{
- /**
- * {@inheritdoc}
- */
+ protected $type = "Model";
+
protected function configure()
{
- $this
- ->setName('make:model')
- ->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);
+ parent::configure();
+ $this->setName('make:model')
+ ->setDescription('Create a new model class');
}
- protected function execute(Input $input, Output $output)
+ protected function getStub()
{
- $namespace = $input->getArgument('namespace');
- $module = $input->getOption('module');
- $extend = $input->getOption('extend');
- $result = $this->getResult('model', $namespace, $module, $extend);
- $output->writeln("output:" . $result);
+ return __DIR__ . '/stubs/model.stub';
}
+ protected function getNamespace($appNamespace, $module)
+ {
+ return parent::getNamespace($appNamespace, $module) . '\model';
+ }
}
diff --git a/library/think/console/command/make/stubs/controller.plain.stub b/library/think/console/command/make/stubs/controller.plain.stub
new file mode 100644
index 00000000..b7539dcf
--- /dev/null
+++ b/library/think/console/command/make/stubs/controller.plain.stub
@@ -0,0 +1,10 @@
+
-// +----------------------------------------------------------------------
-
-namespace {%namespace%};
-
-class {%className%} {%extend%}
-{
-{%content%}
-}