Files
swiftadmin/vendor/webman/console/src/Command.php

42 lines
1.5 KiB
PHP
Raw Normal View History

2022-08-19 19:48:37 +08:00
<?php
namespace Webman\Console;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command as Commands;
2023-04-25 20:11:49 +08:00
use support\Container;
2022-08-19 19:48:37 +08:00
class Command extends Application
{
public function installInternalCommands()
{
$this->installCommands(__DIR__ . '/Commands', 'Webman\Console\Commands');
}
public function installCommands($path, $namspace = 'app\command')
{
$dir_iterator = new \RecursiveDirectoryIterator($path);
$iterator = new \RecursiveIteratorIterator($dir_iterator);
foreach ($iterator as $file) {
2022-11-28 19:11:12 +08:00
/** @var \SplFileInfo $file */
if (strpos($file->getFilename(), '.') === 0) {
2022-08-19 19:48:37 +08:00
continue;
}
2022-11-28 19:11:12 +08:00
if ($file->getExtension() !== 'php') {
continue;
}
2023-04-25 20:11:49 +08:00
// abc\def.php
2022-11-28 19:11:12 +08:00
$relativePath = str_replace(str_replace('/', '\\', $path . '\\'), '', str_replace('/', '\\', $file->getRealPath()));
2023-04-25 20:11:49 +08:00
// app\command\abc
$realNamespace = trim($namspace . '\\' . trim(dirname(str_replace('\\', DIRECTORY_SEPARATOR, $relativePath)), '.'), '\\');
$realNamespace = str_replace('/', '\\', $realNamespace);
// app\command\doc\def
2022-11-28 19:11:12 +08:00
$class_name = trim($realNamespace . '\\' . $file->getBasename('.php'), '\\');
2023-04-25 20:11:49 +08:00
if (!class_exists($class_name) || !is_a($class_name, Commands::class, true)) {
2022-08-19 19:48:37 +08:00
continue;
}
2023-04-25 20:11:49 +08:00
$this->add(Container::get($class_name));
2022-08-19 19:48:37 +08:00
}
}
}