新增 console 统一注入

This commit is contained in:
JaguarJack
2020-07-02 14:01:05 +08:00
parent e35b14a883
commit 26e26b60a7
4 changed files with 118 additions and 22 deletions

View File

@@ -62,27 +62,7 @@ class CatchAdminService extends Service
*/
protected function registerCommands(): void
{
$this->commands([
InstallProjectCommand::class,
ModuleCacheCommand::class,
MigrateRunCommand::class,
ModelGeneratorCommand::class,
SeedRunCommand::class,
BackupCommand::class,
CompressPackageCommand::class,
CreateModuleCommand::class,
MigrateRollbackCommand::class,
MigrateCreateCommand::class,
WsWorkerCommand::class,
ExportDataCommand::class,
MakeMenuCommand::class,
ExcelTaskCommand::class,
WechatCommand::class,
CacheTrieCommand::class,
ModuleServiceDiscoverCommand::class,
DisableModuleCommand::class,
EnableModuleCommand::class,
]);
$this->commands((new CatchConsole($this->app))->commands());
}
/**
*

View File

@@ -8,3 +8,95 @@
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catcher;
use catcher\library\FileSystem;
use think\App;
use think\console\Command;
use function GuzzleHttp\Psr7\str;
class CatchConsole
{
protected $app;
protected $namespace = '';
protected $path = __DIR__ . DIRECTORY_SEPARATOR . 'command';
public function __construct(App $app)
{
$this->app = $app;
}
/**
* 获取 commands
*
* @time 2020年07月02日
* @return array
*/
public function commands()
{
$commandFiles = (new FileSystem())->allFiles($this->path);
$commands = [];
// dd($this->parseNamespace());
foreach ($commandFiles as $command) {
if (pathinfo($command, PATHINFO_EXTENSION) == 'php') {
$lastPath = str_replace($this->parseNamespace(), '',pathinfo($command, PATHINFO_DIRNAME));
$namespace = $this->namespace . str_replace(DIRECTORY_SEPARATOR, '\\', $lastPath) . '\\';
$commandClass = $namespace . pathinfo($command, PATHINFO_FILENAME);
if (is_subclass_of($commandClass, Command::class)) {
$commands[] = $commandClass;
}
}
}
return $commands;
}
protected function parseNamespace()
{
// 没有设置 namespace 默认使用 extend 目录
if (!$this->namespace) {
return root_path(). 'extend';
}
$composer = \json_decode(file_get_contents(root_path(). 'composer.json'), true);
$rootNamespace = substr($this->namespace, 0, strpos($this->namespace, '\\') + 1);
return root_path(). $composer['autoload']['psr-4'][$rootNamespace] . DIRECTORY_SEPARATOR .
str_replace('\\', DIRECTORY_SEPARATOR, substr($this->namespace, strpos($this->namespace, '\\') + 1));
}
/**
* set path
*
* @time 2020年07月02日
* @param $path
* @return $this
*/
public function path($path)
{
$this->path = $path;
return $this;
}
/**
* 设置命名空间
*
* @time 2020年07月02日
* @param $namespace
* @return $this
*/
public function setNamespace($namespace)
{
$this->namespace = $namespace;
return $this;
}
}

View File

@@ -16,15 +16,23 @@ abstract class ModuleService extends Service
{
abstract public function loadRouteFrom();
/**
* 注册
*
* @time 2020年07月02日
* @return void
*/
public function register()
{
$this->app->make('routePath')->loadRouterFrom($this->loadRouteFrom());
$this->registerEvents();
$this->registerCommands();;
}
/**
* 时间注册
* 事件注册
*
* @time 2020年06月24日
* @return void
@@ -36,6 +44,22 @@ abstract class ModuleService extends Service
}
}
/**
* 注册commands
*
* @time 2020年07月02日
* @return void
*/
protected function registerCommands()
{
if (method_exists($this,'loadCommands')) {
list($namespace, $path) = $this->loadCommands();
$this->commands((new CatchConsole($this->app))
->setNamespace($namespace)
->path($path)
->commands());
}
}
}

View File

@@ -1,164 +0,0 @@
<?php
/**
* @filename WechatCommand.php
* @date 2020/6/6
* @project https://github.com/yanwenwu/catch-admin
* @document http://doc.catchadmin.com
* @author JaguarJack <njphper@gmail.com>
* @copyright By CatchAdmin
* @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt
*/
namespace catcher\command\publish;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use catcher\CatchAdmin;
class WechatCommand extends Command
{
protected function configure()
{
$this->setName('publish:wechat')
->setDescription('publish wechat config');
}
protected function execute(Input $input, Output $output)
{
file_put_contents(config_path() . 'wechat.php', $this->config());
$this->env();
$output->warning('wechat publish successfully');
}
protected function config()
{
return <<<CONFIG
<?php
return [
/**
* 公众号配置
*
*/
'official_account' => [
/**
* 账号基本信息,请从微信公众平台/开放平台获取
*/
'app_id' => env('wechat.official_app_id'), // AppID
'secret' => env('wechat.official_secret'), // AppSecret
'token' => env('wechat.official_token'), // Token
'aes_key' => env('wechat.official_aes_key'), // EncodingAESKey兼容与安全模式下请一定要填写
'response_type' => 'array',
/**
* OAuth 配置
*
* scopes公众平台snsapi_userinfo / snsapi_base开放平台snsapi_login
* callbackOAuth授权完成后的回调页地址
*/
'oauth' => [
'scopes' => ['snsapi_userinfo'],
'callback' => '/examples/oauth_callback.php',
],
],
/**
* 小程序
*/
'mini_program' => [
// 更多配置查看 https://www.easywechat.com/docs/master/mini-program/index
],
/**
* 开放平台
*/
'open_platform' => [
// 更多配置查看 https://www.easywechat.com/docs/master/open-platform/index
],
/**
* 企业微信
*/
'work' => [
// 更多配置查看 https://www.easywechat.com/docs/master/wework/index
],
/**
* 企业微信开放平台
*/
'open_work' => [
// 配置 https://www.easywechat.com/docs/master/open-work/index
],
/**
* 小微商户
*/
'micro_merchant' => [
// 配置 https://www.easywechat.com/docs/master/micro-merchant/index
],
/**
* wechat pay
*/
'payment' => [
'app_id' => 'xxxx',
'mch_id' => 'your-mch-id',
'key' => 'key-for-signature', // API 密钥
// 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
'cert_path' => 'path/to/your/cert.pem', // XXX: 绝对路径!!!!
'key_path' => 'path/to/your/key', // XXX: 绝对路径!!!!
'notify_url' => '默认的订单回调地址', // 你也可以在下单时单独设置来想覆盖它
],
// 更多配置请查看 https://www.easywechat.com/docs
];
CONFIG;
}
protected function env()
{
$filename = file_exists(root_path() . '.env') ? '.env' : '.example.env';
$env = \parse_ini_file(root_path() . $filename, true);
$env['WECHAT'] = $this->envConfig();
$dotEnv = '';
foreach ($env as $key => $e) {
if (is_string($e)) {
$dotEnv .= sprintf('%s = %s', $key, $e === '1' ? 'true' : ($e === '' ? 'false' : $e)) . PHP_EOL;
$dotEnv .= PHP_EOL;
} else {
$dotEnv .= sprintf('[%s]', $key) . PHP_EOL;
foreach ($e as $k => $v) {
$dotEnv .= sprintf('%s = %s', $k, $v === '1' ? 'true' : ($v === '' ? 'false' : $v)) . PHP_EOL;
}
$dotEnv .= PHP_EOL;
}
}
file_put_contents(root_path() . '.env', $dotEnv);
}
protected function envConfig()
{
return [
"official_app_id" => null,
"official_secret" => null,
"official_token" => null,
"official_aes_key" => null,
];
}
}