1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the box project. |
7
|
|
|
* |
8
|
|
|
* (c) Kevin Herrera <[email protected]> |
9
|
|
|
* Théo Fidry <[email protected]> |
10
|
|
|
* |
11
|
|
|
* This source file is subject to the MIT license that is bundled |
12
|
|
|
* with this source code in the file LICENSE. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace KevinGH\Box\Console\Command; |
16
|
|
|
|
17
|
|
|
use Assert\Assertion; |
18
|
|
|
use KevinGH\Box\PhpSettingsHandler; |
19
|
|
|
use ParagonIE\Pharaoh\Pharaoh; |
20
|
|
|
use ParagonIE\Pharaoh\PharDiff; |
21
|
|
|
use Symfony\Component\Console\Command\Command; |
22
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
24
|
|
|
use Symfony\Component\Console\Input\InputOption; |
25
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
26
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
27
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
28
|
|
|
use Throwable; |
29
|
|
|
use function array_map; |
30
|
|
|
use function KevinGH\Box\FileSystem\remove; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @private |
34
|
|
|
*/ |
35
|
|
|
final class Diff extends Command |
36
|
|
|
{ |
37
|
|
|
use CreateTemporaryPharFile; |
38
|
|
|
|
39
|
|
|
private const FIRST_PHAR_ARG = 'pharA'; |
40
|
|
|
private const SECOND_PHAR_ARG = 'pharB'; |
41
|
|
|
|
42
|
|
|
private const GNU_DIFF_OPTION = 'gnu-diff'; |
43
|
|
|
private const CHECK_OPTION = 'check'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
protected function configure(): void |
49
|
|
|
{ |
50
|
|
|
parent::configure(); |
51
|
|
|
|
52
|
|
|
$this->setName('diff'); |
53
|
|
|
$this->setDescription('Display the differences between all of the files in two PHARs'); |
54
|
|
|
|
55
|
|
|
$this->addArgument( |
56
|
|
|
self::FIRST_PHAR_ARG, |
57
|
|
|
InputArgument::REQUIRED, |
58
|
|
|
'The first PHAR' |
59
|
|
|
); |
60
|
|
|
$this->addArgument( |
61
|
|
|
self::SECOND_PHAR_ARG, |
62
|
|
|
InputArgument::REQUIRED, |
63
|
|
|
'The second PHAR' |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$this->addOption( |
67
|
|
|
self::GNU_DIFF_OPTION, |
68
|
|
|
'd', |
69
|
|
|
InputOption::VALUE_NONE, |
70
|
|
|
'Displays a GNU diff instead of the default git diff' |
71
|
|
|
); |
72
|
|
|
$this->addOption( |
73
|
|
|
self::CHECK_OPTION, |
74
|
|
|
'c', |
75
|
|
|
InputOption::VALUE_OPTIONAL, |
76
|
|
|
'Verify the authenticity of the contents between the two PHARs with the given hash function.', |
77
|
|
|
'sha384' |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
85
|
|
|
{ |
86
|
|
|
$io = new SymfonyStyle($input, $output); |
87
|
|
|
|
88
|
|
|
(new PhpSettingsHandler(new ConsoleLogger($output)))->check(); |
89
|
|
|
|
90
|
|
|
$paths = [ |
91
|
|
|
$input->getArgument(self::FIRST_PHAR_ARG), |
92
|
|
|
$input->getArgument(self::SECOND_PHAR_ARG), |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
Assertion::allFile($paths); |
|
|
|
|
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
$diff = new PharDiff( |
99
|
|
|
...array_map( |
100
|
|
|
function (string $path): Pharaoh { |
101
|
|
|
$path = false !== realpath($path) ? realpath($path) : $path; |
102
|
|
|
|
103
|
|
|
return new class($path) extends Pharaoh { |
104
|
|
|
// TODO: remove this once https://github.com/paragonie/pharaoh/pull/9 is merged |
105
|
|
|
public function __destruct() |
106
|
|
|
{ |
107
|
|
|
$path = $this->phar->getPath(); |
108
|
|
|
|
109
|
|
|
unset($this->phar); |
110
|
|
|
|
111
|
|
|
\Phar::unlinkArchive($path); |
112
|
|
|
} |
113
|
|
|
}; |
114
|
|
|
}, |
115
|
|
|
$paths |
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
$diff->setVerbose(true); |
119
|
|
|
} catch (Throwable $throwable) { |
120
|
|
|
if ($output->isDebug()) { |
121
|
|
|
throw $throwable; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$io->writeln( |
125
|
|
|
sprintf( |
126
|
|
|
'<error>Could not check the PHARs: %s</error>', |
127
|
|
|
$throwable->getMessage() |
128
|
|
|
) |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
return 1; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($input->hasParameterOption(['-c', '--check'])) { |
135
|
|
|
return $diff->listChecksums($input->getOption(self::CHECK_OPTION) ?? 'sha384'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($input->getOption(self::GNU_DIFF_OPTION)) { |
139
|
|
|
return $diff->printGnuDiff(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $diff->printGitDiff(); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|