1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the composer-link plugin. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2021-2022 Sander Visser <[email protected]>. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @link https://github.com/SanderSander/composer-link |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace ComposerLink; |
17
|
|
|
|
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
|
20
|
|
|
class PathHelper |
21
|
|
|
{ |
22
|
|
|
protected string $path; |
23
|
|
|
|
24
|
|
|
public function __construct(string $path) |
25
|
|
|
{ |
26
|
|
|
$this->path = $path; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function isWildCard(): bool |
30
|
|
|
{ |
31
|
|
|
return substr($this->path, -2) === DIRECTORY_SEPARATOR . '*'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return PathHelper[] |
36
|
|
|
*/ |
37
|
|
|
public function getPathsFromWildcard(): array |
38
|
|
|
{ |
39
|
|
|
/** @var string[] $entries */ |
40
|
|
|
$entries = glob($this->path, GLOB_ONLYDIR); |
41
|
|
|
$helpers = []; |
42
|
|
|
foreach ($entries as $entry) { |
43
|
|
|
if (!file_exists($entry . DIRECTORY_SEPARATOR . 'composer.json')) { |
44
|
|
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$helpers[] = new PathHelper($entry); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $helpers; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function toAbsolutePath(string $workingDirectory): PathHelper |
54
|
|
|
{ |
55
|
|
|
if ($this->isAbsolutePath($this->path)) { |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$path = $this->isWildCard() ? substr($this->path, 0, -1) : $this->path; |
60
|
|
|
$real = realpath($workingDirectory . DIRECTORY_SEPARATOR . $path); |
61
|
|
|
|
62
|
|
|
if ($real === false) { |
63
|
|
|
throw new InvalidArgumentException( |
64
|
|
|
sprintf('Cannot resolve absolute path to %s from %s.', $path, $workingDirectory) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($this->isWildCard()) { |
69
|
|
|
$real .= DIRECTORY_SEPARATOR . '*'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return new PathHelper($real); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getNormalizedPath(): string |
76
|
|
|
{ |
77
|
|
|
if (substr($this->path, -1) === DIRECTORY_SEPARATOR) { |
78
|
|
|
return substr($this->path, 0, -1); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->path; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function isAbsolutePath(string $path): bool |
85
|
|
|
{ |
86
|
|
|
return strpos($path, '/') === 0 || substr($path, 1, 1) === ':' || strpos($path, '\\\\') === 0; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|