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 Iterator; |
18
|
|
|
use PackageVersions\Versions; |
19
|
|
|
use function array_pop; |
20
|
|
|
use function count; |
21
|
|
|
use function Safe\substr; |
22
|
|
|
use function str_split; |
23
|
|
|
use function strrpos; |
24
|
|
|
|
25
|
|
|
function get_php_scoper_version(): string |
26
|
|
|
{ |
27
|
|
|
// Since PHP-Scoper relies on COMPOSER_ROOT_VERSION the version parsed by PackageVersions, we rely on Box |
28
|
|
|
// placeholders in order to get the right version for the PHAR. |
29
|
|
|
if (0 === strpos(__FILE__, 'phar:')) { |
30
|
|
|
return '@git_version_placeholder@'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$rawVersion = Versions::getVersion('humbug/php-scoper'); |
34
|
|
|
|
35
|
|
|
[$prettyVersion, $commitHash] = explode('@', $rawVersion); |
36
|
|
|
|
37
|
|
|
return $prettyVersion.'@'.substr($commitHash, 0, 7); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string[] $paths Absolute paths |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
function get_common_path(array $paths): string |
46
|
|
|
{ |
47
|
|
|
$nbPaths = count($paths); |
48
|
|
|
|
49
|
|
|
if (0 === $nbPaths) { |
50
|
|
|
return ''; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$pathRef = (string) array_pop($paths); |
54
|
|
|
|
55
|
|
|
if (1 === $nbPaths) { |
56
|
|
|
$commonPath = $pathRef; |
57
|
|
|
} else { |
58
|
|
|
$commonPath = ''; |
59
|
|
|
|
60
|
|
|
foreach (str_split($pathRef) as $pos => $char) { |
61
|
|
|
foreach ($paths as $path) { |
62
|
|
|
if (!isset($path[$pos]) || $path[$pos] !== $char) { |
63
|
|
|
break 2; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$commonPath .= $char; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
foreach (['/', '\\'] as $separator) { |
72
|
|
|
$lastSeparatorPos = strrpos($commonPath, $separator); |
73
|
|
|
|
74
|
|
|
if (false !== $lastSeparatorPos) { |
75
|
|
|
$commonPath = rtrim(substr($commonPath, 0, $lastSeparatorPos), $separator); |
76
|
|
|
|
77
|
|
|
break; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $commonPath; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
function chain(iterable ...$iterables): Iterator |
85
|
|
|
{ |
86
|
|
|
foreach ($iterables as $iterable) { |
87
|
|
|
yield from $iterable; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|