From f416f8ee530ab6a13b3311783795f273adbd7838 Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Wed, 15 Jun 2016 17:56:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20console=20=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=20=E5=9C=A8=E7=A8=8B=E5=BA=8F=E5=86=85=E9=83=A8?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Console.php | 45 ++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/library/think/Console.php b/library/think/Console.php index 3dc31467..a28845d4 100644 --- a/library/think/Console.php +++ b/library/think/Console.php @@ -21,6 +21,7 @@ use think\console\input\Argument as InputArgument; use think\console\input\Definition as InputDefinition; use think\console\input\Option as InputOption; use think\console\Output; +use think\console\output\Nothing; use think\console\output\Stream; class Console @@ -66,24 +67,42 @@ class Console } } - public static function init() + public static function init($run = true) { - // 实例化console - $console = new self('Think Console', '0.1'); - // 读取指令集 - if (is_file(CONF_PATH . 'command' . EXT)) { - $commands = include CONF_PATH . 'command' . EXT; - if (is_array($commands)) { - foreach ($commands as $command) { - if (class_exists($command) && is_subclass_of($command, "\\think\\console\\command\\Command")) { - // 注册指令 - $console->add(new $command()); + static $console; + if (!$console) { + // 实例化console + $console = new self('Think Console', '0.1'); + // 读取指令集 + if (is_file(CONF_PATH . 'command' . EXT)) { + $commands = include CONF_PATH . 'command' . EXT; + if (is_array($commands)) { + foreach ($commands as $command) { + if (class_exists($command) && is_subclass_of($command, "\\think\\console\\command\\Command")) { + // 注册指令 + $console->add(new $command()); + } } } } } - // 运行 - $console->run(); + if ($run) { + // 运行 + $console->run(); + } else { + return $console; + } + } + + public static function call($command, array $parameters = []) + { + $console = self::init(false); + + array_unshift($parameters, $command); + + $input = new ConsoleInput($parameters); + + $console->find($command)->run($input, new Nothing()); } /**