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 KevinGH\Box\Box; |
18
|
|
|
use function KevinGH\Box\bump_open_file_descriptor_limit; |
19
|
|
|
use KevinGH\Box\Console\IO\IO; |
20
|
|
|
use function KevinGH\Box\create_temporary_phar; |
21
|
|
|
use function KevinGH\Box\FileSystem\dump_file; |
22
|
|
|
use function KevinGH\Box\FileSystem\remove; |
23
|
|
|
use PharFileInfo; |
24
|
|
|
use function realpath; |
25
|
|
|
use RuntimeException; |
26
|
|
|
use function sprintf; |
27
|
|
|
use Symfony\Component\Console\Exception\RuntimeException as ConsoleRuntimeException; |
28
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
29
|
|
|
use Throwable; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @private |
33
|
|
|
*/ |
34
|
|
|
final class Extract extends BaseCommand |
35
|
|
|
{ |
36
|
|
|
private const PHAR_ARG = 'phar'; |
37
|
|
|
private const OUTPUT_ARG = 'output'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
protected function configure(): void |
43
|
|
|
{ |
44
|
|
|
$this->setName('extract'); |
45
|
|
|
$this->setDescription( |
46
|
|
|
'🚚 Extracts a given PHAR into a directory' |
47
|
|
|
); |
48
|
|
|
$this->addArgument( |
49
|
|
|
self::PHAR_ARG, |
50
|
|
|
InputArgument::REQUIRED, |
51
|
|
|
'The PHAR file.' |
52
|
|
|
); |
53
|
|
|
$this->addArgument( |
54
|
|
|
self::OUTPUT_ARG, |
55
|
|
|
InputArgument::REQUIRED, |
56
|
|
|
'The output directory' |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
protected function executeCommand(IO $io): int |
64
|
|
|
{ |
65
|
|
|
$input = $io->getInput(); |
66
|
|
|
|
67
|
|
|
$file = realpath($input->getArgument(self::PHAR_ARG)); |
|
|
|
|
68
|
|
|
|
69
|
|
|
if (false === $file) { |
70
|
|
|
$io->error( |
71
|
|
|
sprintf( |
72
|
|
|
'The file "%s" could not be found.', |
73
|
|
|
$input->getArgument(self::PHAR_ARG) |
|
|
|
|
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
return 1; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$tmpFile = create_temporary_phar($file); |
81
|
|
|
|
82
|
|
|
try { |
83
|
|
|
$box = Box::create($tmpFile); |
84
|
|
|
} catch (Throwable $throwable) { |
85
|
|
|
if ($io->isDebug()) { |
86
|
|
|
throw new ConsoleRuntimeException( |
87
|
|
|
'The given file is not a valid PHAR', |
88
|
|
|
0, |
89
|
|
|
$throwable |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$io->error('The given file is not a valid PHAR'); |
94
|
|
|
|
95
|
|
|
return 1; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$restoreLimit = bump_open_file_descriptor_limit($box, $io); |
99
|
|
|
|
100
|
|
|
$outputDir = $input->getArgument(self::OUTPUT_ARG); |
101
|
|
|
|
102
|
|
|
try { |
103
|
|
|
remove($outputDir); |
104
|
|
|
|
105
|
|
|
foreach ($box->getPhar() as $pharFile) { |
106
|
|
|
/* @var PharFileInfo $pharFile */ |
107
|
|
|
dump_file( |
108
|
|
|
$outputDir.'/'.$pharFile->getFilename(), |
|
|
|
|
109
|
|
|
(string) $pharFile->getContent() |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} catch (RuntimeException $exception) { |
113
|
|
|
$io->error($exception->getMessage()); |
114
|
|
|
|
115
|
|
|
return 1; |
116
|
|
|
} finally { |
117
|
|
|
$restoreLimit(); |
118
|
|
|
|
119
|
|
|
remove($tmpFile); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$io->success(''); |
123
|
|
|
|
124
|
|
|
return 0; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|