2025-06-14 18:55:09 +08:00

61 lines
1.7 KiB
PHP

#!/usr/bin/env php
<?php
use libs\core\Config;
use libs\db\Db;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command as Commands;
require_once __DIR__ . '/vendor/autoload.php';
$application = new Application();
// 加载配置文件
loadAllConfig();
// 指定路径
$path = __DIR__ . '/app/command';
// 指定要搜索的命名空间
$namespace = 'app\command';
// 注册全部命令类
installCommands($application, $path, $namespace);
function loadAllConfig(array $excludes = [])
{
Config::load(config_path(), $excludes);
}
// 加载数据库实例
Db::instance();
function installCommands($application, $path, $namespace)
{
$dir_iterator = new \RecursiveDirectoryIterator($path);
$iterator = new \RecursiveIteratorIterator($dir_iterator);
foreach ($iterator as $file) {
/** @var \SplFileInfo $file */
if (strpos($file->getFilename(), '.') === 0) {
continue;
}
if ($file->getExtension() !== 'php') {
continue;
}
// abc\def.php
$relativePath = str_replace(str_replace('/', '\\', $path . '\\'), '', str_replace('/', '\\', $file->getRealPath()));
// app\command\abc
$realNamespace = trim($namespace . '\\' . trim(dirname(str_replace('\\', DIRECTORY_SEPARATOR, $relativePath)), '.'), '\\');
$realNamespace = str_replace('/', '\\', $realNamespace);
// app\command\doc\def
$class_name = trim($realNamespace . '\\' . $file->getBasename('.php'), '\\');
if (!class_exists($class_name) || !is_a($class_name, Commands::class, true)) {
continue;
}
$command = new $class_name;
$application->add($command);
}
}
$application->run();