/
EnableCommand.php
86 lines (67 loc) · 2.52 KB
/
EnableCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Psalm\Internal\PluginManager\Command;
use InvalidArgumentException;
use Psalm\Internal\PluginManager\PluginListFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use UnexpectedValueException;
use function assert;
use function getcwd;
use function is_string;
use const DIRECTORY_SEPARATOR;
/**
* @internal
*/
class EnableCommand extends Command
{
/** @var PluginListFactory */
private $plugin_list_factory;
public function __construct(PluginListFactory $plugin_list_factory)
{
$this->plugin_list_factory = $plugin_list_factory;
parent::__construct();
}
protected function configure(): void
{
$this
->setName('enable')
->setDescription('Enables a named plugin')
->addArgument(
'pluginName',
InputArgument::REQUIRED,
'Plugin name (fully qualified class name or composer package name)'
)
->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Path to Psalm config file')
->addUsage('vendor/plugin-package-name [-c path/to/psalm.xml]');
$this->addUsage('\'Plugin\Class\Name\' [-c path/to/psalm.xml]');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$current_dir = (string) getcwd() . DIRECTORY_SEPARATOR;
$config_file_path = $input->getOption('config');
if ($config_file_path !== null && !is_string($config_file_path)) {
throw new UnexpectedValueException('Config file path should be a string');
}
$plugin_list = ($this->plugin_list_factory)($current_dir, $config_file_path);
$plugin_name = $input->getArgument('pluginName');
assert(is_string($plugin_name));
try {
$plugin_class = $plugin_list->resolvePluginClass($plugin_name);
} catch (InvalidArgumentException $e) {
$io->error('Unknown plugin class ' . $plugin_name);
return 2;
}
if ($plugin_list->isEnabled($plugin_class)) {
$io->note('Plugin already enabled');
return 3;
}
$plugin_list->enable($plugin_class);
$io->success('Plugin enabled');
return 0;
}
}