catchAdmin/extend/catcher/CatchConsole.php

139 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2020-07-02 13:19:23 +08:00
<?php
2020-11-29 09:29:14 +08:00
declare(strict_types=1);
2020-07-02 13:19:23 +08:00
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
2020-07-02 14:01:05 +08:00
namespace catcher;
2020-07-19 10:06:08 +08:00
use catcher\library\Composer;
use catcher\facade\FileSystem;
2020-07-02 14:01:05 +08:00
use think\App;
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
*/
2021-01-24 20:24:43 +08:00
public function commands(): array
2020-07-02 14:01:05 +08:00
{
2020-07-19 10:06:08 +08:00
$commandFiles = FileSystem::allFiles($this->path);
2020-07-02 14:01:05 +08:00
$commands = [];
2020-11-29 09:29:14 +08:00
/* \Symfony\Component\Finder\SplFileInfo $command */
2020-07-02 14:01:05 +08:00
foreach ($commandFiles as $command) {
2020-07-19 10:06:08 +08:00
if ($command->getExtension() === 'php') {
2020-11-29 09:29:14 +08:00
$lastPath = str_replace($this->parseNamespace(), '', pathinfo($command->getPathname(), PATHINFO_DIRNAME));
2020-07-02 14:01:05 +08:00
$namespace = $this->namespace . str_replace(DIRECTORY_SEPARATOR, '\\', $lastPath) . '\\';
2020-11-29 09:29:14 +08:00
$commandClass = $namespace . pathinfo($command->getPathname(), PATHINFO_FILENAME);
2020-07-28 08:19:51 +08:00
$commands[] = $commandClass;
2020-07-02 14:01:05 +08:00
}
}
return $commands;
}
2020-07-19 10:06:08 +08:00
/**
* 命名空间解析
*
* @time 2020年07月19日
* @return string
*/
2021-01-24 20:24:43 +08:00
protected function parseNamespace(): string
2020-07-02 14:01:05 +08:00
{
2021-01-24 20:24:43 +08:00
$psr4 = (new Composer)->psr4Autoload();
2020-07-02 14:01:05 +08:00
2021-01-17 20:51:05 +08:00
if (strpos($this->namespace, '\\') === false) {
$rootNamespace = $this->namespace . '\\';
} else {
$rootNamespace = substr($this->namespace, 0, strpos($this->namespace, '\\') + 1);
}
$path = root_path(). $psr4[$rootNamespace] . DIRECTORY_SEPARATOR;
2020-07-02 14:01:05 +08:00
2021-01-17 20:51:05 +08:00
if (strpos($this->namespace, '\\') !== false) {
$path .= str_replace('\\', DIRECTORY_SEPARATOR, substr($this->namespace, strpos($this->namespace, '\\') + 1));
}
2020-07-02 14:01:05 +08:00
2021-01-17 20:51:05 +08:00
return rtrim($path, '/');
2020-07-02 14:01:05 +08:00
}
/**
* set path
*
* @time 2020年07月02日
* @param $path
* @return $this
*/
2021-01-24 20:24:43 +08:00
public function path($path): CatchConsole
2020-07-02 14:01:05 +08:00
{
$this->path = $path;
return $this;
}
/**
* 设置命名空间
*
* @time 2020年07月02日
* @param $namespace
* @return $this
*/
2021-01-24 20:24:43 +08:00
public function setNamespace($namespace): CatchConsole
2020-07-02 14:01:05 +08:00
{
$this->namespace = $namespace;
return $this;
}
2021-01-24 20:24:43 +08:00
/**
* 默认 commands
*
* @time 2021年01月24日
* @return array
*/
public function defaultCommands(): array
{
$defaultCommands = FileSystem::allFiles(__DIR__ . DIRECTORY_SEPARATOR . 'command');
$commands = [];
/* \Symfony\Component\Finder\SplFileInfo $command */
foreach ($defaultCommands as $command) {
if ($command->getExtension() === 'php') {
$filename = str_replace('.php', '', str_replace(__DIR__, '', $command->getPathname()));
$class = 'catcher' . str_replace(DIRECTORY_SEPARATOR, '\\', $filename);
if (class_exists($class)) {
$commands[] = $class;
}
}
}
return $commands;
}
2020-07-02 14:01:05 +08:00
}