2025-06-03
// 自动加载 Command 目录下的所有命令
$commandPath = __DIR__.'/../src/Command';
$namespace = 'App\\Command\\';

foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($commandPath)) as $file) {
    if ($file->isFile() && $file->getExtension() === 'php') {
        $class = $namespace . str_replace(
            ['/', '.php'],
            ['\\', ''],
            substr($file->getPathname(), strlen($commandPath) + 1)
        );

        if (class_exists($class)) {
            $reflection = new ReflectionClass($class);
            if (!$reflection->isAbstract() && $reflection->isSubclassOf('Symfony\\Component\\Console\\Command\\Command')) {
                $application->add(new $class());
            }
        }
    }
}
标签: PHP