-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathpaths.php
More file actions
56 lines (48 loc) · 1.52 KB
/
paths.php
File metadata and controls
56 lines (48 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace Edge\QA;
function buildToolBinary($tool, $customBinary = null)
{
$composerBinaryName = file_exists(COMPOSER_BINARY_DIR . "{$tool}.phar") ? "{$tool}.phar" : $tool;
return buildSafeBinary($customBinary ?: (COMPOSER_BINARY_DIR . $composerBinaryName));
}
function buildSafeBinary($unsafeBinary)
{
if (!$unsafeBinary || !is_file($unsafeBinary)) {
return "";
} elseif (is_executable($unsafeBinary)) {
return escapePath($unsafeBinary);
} else {
return 'php ' . escapePath($unsafeBinary);
}
}
function escapePath($path)
{
return "\"{$path}\"";
}
function commonPath(array $dirsOrFiles)
{
if (!$dirsOrFiles) {
return;
}
$isNotWindows = strtoupper(substr(PHP_OS, 0, 3)) != 'WIN';
$dirs = [];
$dirsCount = [];
foreach (array_values($dirsOrFiles) as $i => $fileOrDir) {
$path = is_dir($fileOrDir) ? $fileOrDir : dirname($fileOrDir);
$dirs[$i] = explode(DIRECTORY_SEPARATOR, $path);
if ($isNotWindows) {
unset($dirs[$i][0]);
}
$dirsCount[] = count($dirs[$i]);
}
$minDirsCount = min($dirsCount);
for ($i = 0; $i < count($dirs); $i++) {
$firstSlash = $isNotWindows ? DIRECTORY_SEPARATOR : '';
$dirs[$i] = $firstSlash . implode(DIRECTORY_SEPARATOR, array_slice($dirs[$i], 0, $minDirsCount));
}
$commonDirs = array_unique($dirs);
while (count($commonDirs) !== 1) {
$commonDirs = array_unique(array_map('dirname', $commonDirs));
}
return reset($commonDirs);
}