123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- declare(strict_types=1);
- namespace catcher;
- use catcher\library\Composer;
- use catcher\facade\FileSystem;
- use Symfony\Component\Finder\SplFileInfo;
- use think\App;
- use think\console\Command;
- class CatchConsole
- {
- protected $app;
- protected $namespace = '';
- protected $path = __DIR__ . DIRECTORY_SEPARATOR . 'command';
- public function __construct(App $app)
- {
- $this->app = $app;
- }
-
- public function commands()
- {
- $commandFiles = FileSystem::allFiles($this->path);
- $commands = [];
-
- foreach ($commandFiles as $command) {
- if ($command->getExtension() === 'php') {
- $lastPath = str_replace($this->parseNamespace(), '', pathinfo($command->getPathname(), PATHINFO_DIRNAME));
- $namespace = $this->namespace . str_replace(DIRECTORY_SEPARATOR, '\\', $lastPath) . '\\';
- $commandClass = $namespace . pathinfo($command->getPathname(), PATHINFO_FILENAME);
- $commands[] = $commandClass;
- }
- }
- return $commands;
- }
-
- protected function parseNamespace()
- {
-
- if (!$this->namespace) {
- return root_path(). 'extend';
- }
- $psr4 = (new Composer())->psr4Autoload();
- $rootNamespace = substr($this->namespace, 0, strpos($this->namespace, '\\') + 1);
- return root_path(). $psr4[$rootNamespace] . DIRECTORY_SEPARATOR .
- str_replace('\\', DIRECTORY_SEPARATOR, substr($this->namespace, strpos($this->namespace, '\\') + 1));
- }
-
- public function path($path)
- {
- $this->path = $path;
- return $this;
- }
-
- public function setNamespace($namespace)
- {
- $this->namespace = $namespace;
- return $this;
- }
- }
|