-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommitCommand.php
89 lines (76 loc) · 3.24 KB
/
CommitCommand.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
88
89
<?php declare(strict_types=1);
namespace PhpTuf\ComposerStagerConsole\Console\Command;
use PhpTuf\ComposerStager\API\Core\CommitterInterface;
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 CommitCommand extends AbstractCommand
{
private const NAME = 'commit';
public function __construct(private readonly CommitterInterface $committer, PathFactoryInterface $pathFactory)
{
parent::__construct(self::NAME, $pathFactory);
}
protected function configure(): void
{
$this->setDescription(
'Makes the staged changes live by syncing the active directory with 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->committer->commit(
$this->getStagingDir(),
$this->getActiveDir(),
null,
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');
assert(is_bool($noInteraction));
} catch (InvalidArgumentException $e) {
throw new LogicException($e->getMessage(), $e->getCode(), $e);
}
if ($noInteraction) {
return true;
}
$helper = $this->getHelper('question');
assert($helper instanceof QuestionHelper);
$question = new ConfirmationQuestion(
'You are about to make the staged changes live. 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);
}
}
}