|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the humbug/php-scoper package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
|
9
|
|
|
* Pádraic Brady <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper\Console\Command; |
|
16
|
|
|
|
|
17
|
|
|
use Fidry\Console\IO; |
|
18
|
|
|
use InvalidArgumentException; |
|
19
|
|
|
use Symfony\Component\Console\Command\Command; |
|
20
|
|
|
use Symfony\Component\Console\Exception\RuntimeException; |
|
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
23
|
|
|
use function chdir as native_chdir; |
|
24
|
|
|
use function file_exists; |
|
25
|
|
|
use function Safe\getcwd; |
|
26
|
|
|
use function Safe\sprintf; |
|
27
|
|
|
|
|
28
|
|
|
final class ChangeableDirectory |
|
29
|
|
|
{ |
|
30
|
|
|
private const WORKING_DIR_OPT = 'working-dir'; |
|
31
|
|
|
|
|
32
|
|
|
private function __construct() |
|
33
|
|
|
{ |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function createOption(): InputOption |
|
37
|
|
|
{ |
|
38
|
|
|
return new InputOption( |
|
39
|
|
|
self::WORKING_DIR_OPT, |
|
40
|
|
|
'd', |
|
41
|
|
|
InputOption::VALUE_REQUIRED, |
|
42
|
|
|
'If specified, use the given directory as working directory.', |
|
43
|
|
|
null |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public static function changeWorkingDirectory(IO $io): void |
|
48
|
|
|
{ |
|
49
|
|
|
$workingDir = $io->getNullableStringOption(self::WORKING_DIR_OPT); |
|
50
|
|
|
|
|
51
|
|
|
if (null === $workingDir) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (false === file_exists($workingDir)) { |
|
56
|
|
|
throw new InvalidArgumentException( |
|
57
|
|
|
sprintf( |
|
58
|
|
|
'Could not change the working directory to "%s": directory does not exists.', |
|
59
|
|
|
$workingDir |
|
60
|
|
|
) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (false === native_chdir($workingDir)) { |
|
65
|
|
|
throw new RuntimeException( |
|
66
|
|
|
sprintf( |
|
67
|
|
|
'Failed to change the working directory to "%s" from "%s".', |
|
68
|
|
|
$workingDir, |
|
69
|
|
|
getcwd() |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|