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; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\Console\Application; |
18
|
|
|
use Humbug\PhpScoper\Console\Command\AddPrefixCommand; |
19
|
|
|
use Humbug\PhpScoper\Console\Command\SelfUpdateCommand; |
20
|
|
|
use Humbug\PhpScoper\Handler\HandleAddPrefix; |
21
|
|
|
use Humbug\PhpScoper\Scoper\Composer\InstalledPackagesScoper; |
22
|
|
|
use Humbug\PhpScoper\Scoper\Composer\JsonFileScoper; |
23
|
|
|
use Humbug\PhpScoper\Scoper\NullScoper; |
24
|
|
|
use Humbug\PhpScoper\Scoper\PatchScoper; |
25
|
|
|
use Humbug\PhpScoper\Scoper\PhpScoper; |
26
|
|
|
use Humbug\SelfUpdate\Exception\RuntimeException as SelfUpdateRuntimeException; |
27
|
|
|
use Humbug\SelfUpdate\Updater; |
28
|
|
|
use PackageVersions\Versions; |
29
|
|
|
use PhpParser\Parser; |
30
|
|
|
use PhpParser\ParserFactory; |
31
|
|
|
use Symfony\Component\Console\Application as SymfonyApplication; |
32
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @private |
36
|
|
|
*/ |
37
|
|
|
function create_application(): SymfonyApplication |
38
|
|
|
{ |
39
|
|
|
$app = new Application('PHP Scoper', get_version()); |
40
|
|
|
|
41
|
|
|
$app->addCommands([ |
42
|
|
|
new AddPrefixCommand( |
43
|
|
|
new Filesystem(), |
44
|
|
|
new HandleAddPrefix( |
45
|
|
|
create_scoper() |
46
|
|
|
) |
47
|
|
|
), |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
if ('phar:' === substr(__FILE__, 0, 5)) { |
51
|
|
|
try { |
52
|
|
|
$updater = new Updater(); |
53
|
|
|
} catch (SelfUpdateRuntimeException $e) { |
54
|
|
|
/* Allow E2E testing of unsigned phar */ |
55
|
|
|
$updater = new Updater(null, false); |
56
|
|
|
} |
57
|
|
|
$app->add( |
58
|
|
|
new SelfUpdateCommand( |
59
|
|
|
$updater |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $app; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @private |
69
|
|
|
*/ |
70
|
|
|
function get_version(): string |
71
|
|
|
{ |
72
|
|
|
if ('phar:' === substr(__FILE__, 0, 5)) { |
73
|
|
|
$gitVersion = '@git-version@'; |
74
|
|
|
$semanticVersion = preg_replace( |
75
|
|
|
["/\.\d\-/", "/\-/"], |
76
|
|
|
['-', '-dev+'], |
77
|
|
|
$gitVersion, |
78
|
|
|
1 |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
return $semanticVersion; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$rawVersion = Versions::getVersion('humbug/php-scoper'); |
85
|
|
|
|
86
|
|
|
list($prettyVersion, $commitHash) = explode('@', $rawVersion); |
87
|
|
|
|
88
|
|
|
return (1 === preg_match('/9{7}/', $prettyVersion)) ? $commitHash : $prettyVersion; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @private |
93
|
|
|
*/ |
94
|
|
|
function create_scoper(): Scoper |
95
|
|
|
{ |
96
|
|
|
return new PatchScoper( |
97
|
|
|
new JsonFileScoper( |
98
|
|
|
new InstalledPackagesScoper( |
99
|
|
|
new PhpScoper( |
100
|
|
|
create_parser(), |
101
|
|
|
new NullScoper() |
102
|
|
|
) |
103
|
|
|
) |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @private |
110
|
|
|
*/ |
111
|
|
|
function create_parser(): Parser |
112
|
|
|
{ |
113
|
|
|
return (new ParserFactory())->create(ParserFactory::ONLY_PHP7); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string[] $paths Absolute paths |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
function get_common_path(array $paths): string |
122
|
|
|
{ |
123
|
|
|
if (0 === count($paths)) { |
124
|
|
|
return ''; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$lastOffset = 1; |
128
|
|
|
$common = DIRECTORY_SEPARATOR; |
129
|
|
|
|
130
|
|
|
while (false !== ($index = strpos($paths[0], DIRECTORY_SEPARATOR, $lastOffset))) { |
131
|
|
|
$dirLen = $index - $lastOffset + 1; |
132
|
|
|
$dir = substr($paths[0], $lastOffset, $dirLen); |
133
|
|
|
|
134
|
|
|
foreach ($paths as $path) { |
135
|
|
|
if (substr($path, $lastOffset, $dirLen) !== $dir) { |
136
|
|
View Code Duplication |
if (0 < strlen($common) && DIRECTORY_SEPARATOR === $common[strlen($common) - 1]) { |
137
|
|
|
$common = substr($common, 0, strlen($common) - 1); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $common; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$common .= $dir; |
145
|
|
|
$lastOffset = $index + 1; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$common = substr($common, 0, -1); |
149
|
|
|
|
150
|
|
View Code Duplication |
if (0 < strlen($common) && DIRECTORY_SEPARATOR === $common[strlen($common) - 1]) { |
151
|
|
|
$common = substr($common, 0, strlen($common) - 1); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $common; |
155
|
|
|
} |
156
|
|
|
|