static-php-cli/src/SPC/ConsoleApplication.php

54 lines
1.7 KiB
PHP
Raw Normal View History

2023-03-15 20:40:49 +08:00
<?php
declare(strict_types=1);
namespace SPC;
use SPC\store\FileSystem;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\HelpCommand;
use Symfony\Component\Console\Command\ListCommand;
/**
* static-php-cli console app entry
2023-03-15 20:40:49 +08:00
*/
class ConsoleApplication extends Application
{
2023-09-08 22:03:40 +08:00
public const VERSION = '2.0-rc6';
2023-03-15 20:40:49 +08:00
/**
* @throws \ReflectionException
* @throws exception\FileSystemException
*/
public function __construct()
{
parent::__construct('static-php-cli', self::VERSION);
global $argv;
// Detailed debugging errors are not displayed in the production environment. Only the error display provided by Symfony console is used.
2023-03-15 20:40:49 +08:00
$this->setCatchExceptions(file_exists(ROOT_DIR . '/.prod') || !in_array('--debug', $argv));
// Add subcommands by scanning the directory src/static-php-cli/command/
2023-03-15 20:40:49 +08:00
$commands = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/command', 'SPC\\command');
$phar = class_exists('\\Phar') && \Phar::running() || !class_exists('\\Phar');
$commands = array_filter($commands, function ($y) use ($phar) {
$archive_blacklist = [
'SPC\command\dev\SortConfigCommand',
'SPC\command\DeployCommand',
];
if ($phar && in_array($y, $archive_blacklist)) {
2023-03-15 20:40:49 +08:00
return false;
}
$reflection = new \ReflectionClass($y);
return !$reflection->isAbstract() && !$reflection->isInterface();
});
$this->addCommands(array_map(function ($x) { return new $x(); }, $commands));
2023-03-15 20:40:49 +08:00
}
protected function getDefaultCommands(): array
{
return [new HelpCommand(), new ListCommand()];
}
}