|
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\Handler\HandleAddPrefix; |
|
20
|
|
|
use Humbug\PhpScoper\Scoper\ComposerScoper; |
|
21
|
|
|
use Humbug\PhpScoper\Scoper\NullScoper; |
|
22
|
|
|
use Humbug\PhpScoper\Scoper\PhpScoper; |
|
23
|
|
|
use PackageVersions\Versions; |
|
24
|
|
|
use PhpParser\Parser; |
|
25
|
|
|
use PhpParser\ParserFactory; |
|
26
|
|
|
use Symfony\Component\Console\Application as SymfonyApplication; |
|
27
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @private |
|
31
|
|
|
*/ |
|
32
|
|
|
function create_application(): SymfonyApplication |
|
33
|
|
|
{ |
|
34
|
|
|
$app = new Application('PHP Scoper', get_version()); |
|
35
|
|
|
|
|
36
|
|
|
$app->addCommands([ |
|
37
|
|
|
new AddPrefixCommand( |
|
38
|
|
|
new Filesystem(), |
|
39
|
|
|
new HandleAddPrefix( |
|
40
|
|
|
create_scoper() |
|
41
|
|
|
) |
|
42
|
|
|
), |
|
43
|
|
|
]); |
|
44
|
|
|
|
|
45
|
|
|
return $app; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @private |
|
50
|
|
|
*/ |
|
51
|
|
|
function get_version(): string |
|
52
|
|
|
{ |
|
53
|
|
|
$rawVersion = Versions::getVersion('humbug/php-scoper'); |
|
54
|
|
|
|
|
55
|
|
|
list($prettyVersion, $commitHash) = explode('@', $rawVersion); |
|
56
|
|
|
|
|
57
|
|
|
return (1 === preg_match('/9{7}/', $prettyVersion)) ? $commitHash : $prettyVersion; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @private |
|
62
|
|
|
*/ |
|
63
|
|
|
function create_scoper(): Scoper |
|
64
|
|
|
{ |
|
65
|
|
|
return new ComposerScoper( |
|
66
|
|
|
new PhpScoper( |
|
67
|
|
|
create_parser(), |
|
68
|
|
|
new NullScoper() |
|
69
|
|
|
) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @private |
|
75
|
|
|
*/ |
|
76
|
|
|
function create_parser(): Parser |
|
77
|
|
|
{ |
|
78
|
|
|
return (new ParserFactory())->create(ParserFactory::ONLY_PHP7); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string[] $paths Absolute paths |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
function get_common_path(array $paths): string |
|
87
|
|
|
{ |
|
88
|
6 |
|
if (0 === count($paths)) { |
|
89
|
1 |
|
return ''; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
5 |
|
$lastOffset = 1; |
|
93
|
5 |
|
$common = DIRECTORY_SEPARATOR; |
|
94
|
|
|
|
|
95
|
5 |
|
while (false !== ($index = strpos($paths[0], DIRECTORY_SEPARATOR, $lastOffset))) { |
|
96
|
4 |
|
$dirLen = $index - $lastOffset + 1; |
|
97
|
4 |
|
$dir = substr($paths[0], $lastOffset, $dirLen); |
|
98
|
|
|
|
|
99
|
4 |
|
foreach ($paths as $path) { |
|
100
|
4 |
|
if (substr($path, $lastOffset, $dirLen) !== $dir) { |
|
101
|
2 |
View Code Duplication |
if (0 < strlen($common) && DIRECTORY_SEPARATOR === $common[strlen($common) - 1]) { |
|
102
|
2 |
|
$common = substr($common, 0, strlen($common) - 1); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
4 |
|
return $common; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
3 |
|
$common .= $dir; |
|
110
|
3 |
|
$lastOffset = $index + 1; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
3 |
|
$common = substr($common, 0, -1); |
|
114
|
|
|
|
|
115
|
3 |
View Code Duplication |
if (0 < strlen($common) && DIRECTORY_SEPARATOR === $common[strlen($common) - 1]) { |
|
116
|
|
|
$common = substr($common, 0, strlen($common) - 1); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
3 |
|
return $common; |
|
120
|
|
|
} |
|
121
|
|
|
|