-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCleanCommand.php
87 lines (73 loc) · 3.13 KB
/
CleanCommand.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
87
<?php declare(strict_types=1);
namespace PhpTuf\ComposerStagerConsole\Console\Command;
use PhpTuf\ComposerStager\API\Core\CleanerInterface;
use PhpTuf\ComposerStager\API\Exception\ExceptionInterface;
use PhpTuf\ComposerStager\API\Path\Factory\PathFactoryInterface;
use PhpTuf\ComposerStagerConsole\Console\Output\ProcessOutputCallback;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
/** @internal */
final class CleanCommand extends AbstractCommand
{
private const NAME = 'clean';
public function __construct(private readonly CleanerInterface $cleaner, PathFactoryInterface $pathFactory)
{
parent::__construct(self::NAME, $pathFactory);
}
protected function configure(): void
{
$this->setDescription('Removes the staging directory');
}
/** @throws \Symfony\Component\Console\Exception\LogicException */
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$this->confirm($input, $output)) {
return self::FAILURE;
}
// ---------------------------------------------------------------------
// (!) Here is the substance of the example. Invoke the Composer Stager
// API; be sure to catch the appropriate exceptions.
// ---------------------------------------------------------------------
try {
$this->cleaner->clean(
$this->getActiveDir(),
$this->getStagingDir(),
new ProcessOutputCallback($input, $output),
);
return self::SUCCESS;
} catch (ExceptionInterface $e) {
// Error-handling specifics may differ for your application. This
// example outputs errors to the terminal.
$output->writeln("<error>{$e->getMessage()}</error>");
return self::FAILURE;
}
}
/** @throws \Symfony\Component\Console\Exception\LogicException */
private function confirm(InputInterface $input, OutputInterface $output): bool
{
try {
$noInteraction = $input->getOption('no-interaction');
} catch (InvalidArgumentException $e) {
throw new LogicException($e->getMessage(), $e->getCode(), $e);
}
assert(is_bool($noInteraction));
if ($noInteraction) {
return true;
}
$helper = $this->getHelper('question');
assert($helper instanceof QuestionHelper);
$question = new ConfirmationQuestion(
'You are about to permanently remove the staging directory. This action cannot be undone. Continue? [Y/n] ',
);
try {
return (bool) $helper->ask($input, $output, $question);
} catch (RuntimeException $e) {
throw new LogicException($e->getMessage(), $e->getCode(), $e);
}
}
}