-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathApplication.php
More file actions
92 lines (75 loc) · 2.67 KB
/
Copy pathApplication.php
File metadata and controls
92 lines (75 loc) · 2.67 KB
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
87
88
89
90
91
92
<?php
declare(strict_types=1);
namespace Cpx;
use Cpx\Commands\AliasCommand;
use Cpx\Commands\AliasesCommand;
use Cpx\Commands\CleanCommand;
use Cpx\Commands\ExecCommand;
use Cpx\Commands\ForgetCommand;
use Cpx\Commands\ListCommand;
use Cpx\Commands\RunPackageCommand;
use Cpx\Commands\TinkerCommand;
use Cpx\Commands\UpdateCommand;
use Cpx\Commands\UpgradeCommand;
use Cpx\Packages\PackageCommandRunner;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Application extends SymfonyApplication
{
public function __construct(?PackageCommandRunner $packageCommandRunner = null)
{
parent::__construct('cpx', $this->resolveVersion());
$this->setAutoExit(false);
$this->registerCommands($packageCommandRunner ?? new PackageCommandRunner);
}
public function run(?InputInterface $input = null, ?OutputInterface $output = null): int
{
$input ??= new ArgvInput;
if ($input instanceof ArgvInput && $this->shouldRunPackageFallback($input)) {
$input = new ArgvInput(['cpx', RunPackageCommand::NAME, '--', ...$input->getRawTokens()]);
}
return parent::run($input, $output);
}
protected function getCommandName(InputInterface $input): ?string
{
$command = parent::getCommandName($input);
return $command === null || $this->has($command)
? $command
: RunPackageCommand::NAME;
}
private function registerCommands(PackageCommandRunner $packageCommandRunner): void
{
$this->addCommands([
new ListCommand,
new AliasCommand,
new AliasesCommand,
new ForgetCommand,
new CleanCommand,
new UpdateCommand,
new UpgradeCommand,
new ExecCommand,
new TinkerCommand,
new RunPackageCommand($packageCommandRunner),
]);
}
private function resolveVersion(): string
{
$contents = file_get_contents(__DIR__.'/../composer.json');
if ($contents === false) {
return 'unknown';
}
$decoded = json_decode($contents, true);
return is_array($decoded) && is_string($decoded['version'] ?? null)
? $decoded['version']
: 'unknown';
}
private function shouldRunPackageFallback(ArgvInput $input): bool
{
$command = $input->getRawTokens()[0] ?? null;
return is_string($command)
&& ! in_array($command, ['--version', '-v'], true)
&& ! $this->has($command);
}
}