|
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) === '/*'; |
|
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
|
|
|
$path = $this->isWildCard() ? substr($this->path, 0, -1) : $this->path; |
|
56
|
|
|
$real = realpath($workingDirectory . DIRECTORY_SEPARATOR . $path); |
|
57
|
|
|
|
|
58
|
|
|
if ($real === false) { |
|
59
|
|
|
throw new InvalidArgumentException( |
|
60
|
|
|
sprintf('Cannot resolve absolute path to %s.', $path) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($this->isWildCard()) { |
|
65
|
|
|
$real .= DIRECTORY_SEPARATOR . '*'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return new PathHelper($real); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getNormalizedPath(): string |
|
72
|
|
|
{ |
|
73
|
|
|
if (substr($this->path, -1) === DIRECTORY_SEPARATOR) { |
|
74
|
|
|
return substr($this->path, 0, -1); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $this->path; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|