Repository navigation
Expand file tree
/
Copy pathApplication.php
More file actions
116 lines (95 loc) · 3.6 KB
/
Copy pathApplication.php
File metadata and controls
116 lines (95 loc) · 3.6 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Phpactor;
use Phpactor\Extension\Logger\Formatter\PrettyFormatter;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Phpactor\Container\Container;
use Monolog\Handler\StreamHandler;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputOption;
use PackageVersions\Versions;
use Phpactor\Extension\Logger\LoggingExtension;
use Phpactor\Extension\Console\ConsoleExtension;
use Exception;
class Application extends SymfonyApplication
{
/**
* @var Container
*/
private $container;
/**
* @var string
*/
private $vendorDir;
public function __construct(string $vendorDir)
{
parent::__construct('Phpactor', Versions::getVersion('phpactor/phpactor'));
$this->vendorDir = $vendorDir;
}
public function doRun(InputInterface $input, OutputInterface $output)
{
$this->initialize($input);
$this->setCatchExceptions(false);
if ($output->isVerbose()) {
$handler = new StreamHandler(STDERR);
$handler->setFormatter($this->container->get(PrettyFormatter::class));
$this->container->get(LoggingExtension::SERVICE_LOGGER)->pushHandler($handler);
}
$formatter = $output->getFormatter();
$formatter->setStyle('highlight', new OutputFormatterStyle('red', null, [ 'bold' ]));
$formatter->setStyle('diff-add', new OutputFormatterStyle('green', null, [ ]));
$formatter->setStyle('diff-remove', new OutputFormatterStyle('red', null, [ ]));
try {
return parent::doRun($input, $output);
} catch (Exception $e) {
if (
$input->hasArgument('command')
&& ($command = $input->getArgument('command'))
&& $command !== 'list'
&& $input->hasOption('format')
&& $input->getOption('format')
) {
return $this->handleException($output, $input->getOption('format'), $e);
}
if ($output instanceof ConsoleOutputInterface) {
$this->renderThrowable($e, $output->getErrorOutput());
}
return 255;
}
}
protected function getDefaultInputDefinition()
{
$definition = parent::getDefaultInputDefinition();
$definition->addOption(new InputOption('working-dir', 'd', InputOption::VALUE_REQUIRED, 'Working directory'));
return $definition;
}
private function handleException(OutputInterface $output, string $dumper, Exception $e)
{
$errors = [
'error' => $this->serializeException($e),
'previous' => [
],
];
$this->container->get('logging.logger')->error($e->getMessage());
while ($e = $e->getPrevious()) {
$errors['previous'][] = $this->serializeException($e);
}
$this->container->get('console.dumper_registry')->get($dumper)->dump($output, $errors);
return 64;
}
private function serializeException(Exception $e)
{
return [
'class' => get_class($e),
'code' => $e->getCode(),
'message' => $e->getMessage(),
];
}
private function initialize(InputInterface $input): void
{
$this->container = Phpactor::boot($input, $this->vendorDir);
$this->setCommandLoader($this->container->get(ConsoleExtension::SERVICE_COMMAND_LOADER));
}
}