-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevToolsPathResolver.php
More file actions
264 lines (226 loc) · 9.69 KB
/
DevToolsPathResolver.php
File metadata and controls
264 lines (226 loc) · 9.69 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
declare(strict_types=1);
/**
* Fast Forward Development Tools for PHP projects.
*
* This file is part of fast-forward/dev-tools project.
*
* @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*
* @see https://github.com/php-fast-forward/
* @see https://github.com/php-fast-forward/dev-tools
* @see https://github.com/php-fast-forward/dev-tools/issues
* @see https://php-fast-forward.github.io/dev-tools/
* @see https://datatracker.ietf.org/doc/html/rfc2119
*/
namespace FastForward\DevTools\Path;
use InvalidArgumentException;
use Symfony\Component\Filesystem\Path;
/**
* Resolves canonical paths for the DevTools package itself.
*/
final class DevToolsPathResolver
{
/**
* @var string the relative path to the packaged DevTools binary
*/
public const string BINARY = 'bin/dev-tools';
/**
* @var string the resources directory segment within the package
*/
public const string RESOURCES = 'resources';
/**
* @var string the vendor install path fragment used when DevTools runs as a dependency
*/
private const string VENDOR_PACKAGE_PATH = '/vendor/fast-forward/dev-tools';
/**
* Returns the DevTools package directory or a path under it.
*
* @param string $path the optional relative segment to append under the package directory
*/
public static function getPackagePath(string $path = ''): string
{
$packageDirectory = \dirname(__DIR__, 2);
if ('' !== $path && Path::isAbsolute($path)) {
throw new InvalidArgumentException('The DevTools package path MUST be relative to the package root.');
}
return Path::join($packageDirectory, $path);
}
/**
* Returns the packaged DevTools binary path.
*/
public static function getBinaryPath(): string
{
return self::getPackagePath(self::BINARY);
}
/**
* Returns the packaged DevTools binary command with a subcommand.
*
* @param string $command the DevTools subcommand to append
*/
public static function getBinaryCommand(string $command): string
{
$binaryPath = self::getPackagePath(self::BINARY);
return \sprintf('%s %s', $binaryPath, $command);
}
/**
* Returns the packaged resources directory or a path under it.
*
* @param string $path the optional relative segment to append under resources
*/
public static function getResourcesPath(string $path = ''): string
{
return self::getPackagePath(Path::join(self::RESOURCES, $path));
}
/**
* Returns a package-relative path rendered relative to the active project root.
*
* @param string $path the relative path under the package root
* @param string $projectPath an optional project root path; defaults to the working project root
* @param string $packagePath an optional package root path; defaults to the current package root
*/
public static function getPackagePathRelativeToProject(
string $path,
string $projectPath = '',
string $packagePath = '',
): string {
if (Path::isAbsolute($path)) {
throw new InvalidArgumentException('The DevTools package path MUST be relative to the package root.');
}
$projectPath = Path::canonicalize(WorkingProjectPathResolver::getProjectPath($projectPath));
$packagePath = Path::canonicalize('' === $packagePath ? self::getPackagePath() : $packagePath);
return Path::makeRelative(Path::join($packagePath, $path), $projectPath);
}
/**
* Returns the active Composer autoload file for the current DevTools installation mode.
*
* When DevTools runs as a dependency, the runtime autoloader lives at the
* Composer vendor root. Repository checkouts instead use the package-local
* `vendor/autoload.php`.
*
* @param string $packagePath an optional package root path; defaults to the current package root
*/
public static function getRuntimeAutoloadPath(string $packagePath = ''): string
{
$packagePath = Path::canonicalize('' === $packagePath ? self::getPackagePath() : $packagePath);
if (self::isInstalledAsDependency($packagePath)) {
return Path::canonicalize(Path::join($packagePath, '..', '..', 'autoload.php'));
}
return Path::join($packagePath, 'vendor', 'autoload.php');
}
/**
* Returns the active Composer runtime binary path for the current DevTools installation mode.
*
* Repository checkouts use the package-local `vendor/bin/<binary>`, while
* dependency installs resolve binaries from the active Composer vendor root.
*
* @param string $binary the binary name relative to `vendor/bin`
* @param string $packagePath an optional package root path; defaults to the current package root
*/
public static function getRuntimeToolBinaryPath(string $binary, string $packagePath = ''): string
{
$packagePath = Path::canonicalize('' === $packagePath ? self::getPackagePath() : $packagePath);
if (self::isInstalledAsDependency($packagePath)) {
return Path::canonicalize(Path::join($packagePath, '..', '..', 'bin', $binary));
}
return Path::join($packagePath, 'vendor', 'bin', $binary);
}
/**
* Returns the active Composer vendor path for the current DevTools installation mode.
*
* Relative vendor paths MAY be passed either with or without a leading
* `vendor/` prefix.
*
* @param string $path the vendor-relative path to resolve
* @param string $packagePath an optional package root path; defaults to the current package root
*/
public static function getRuntimeVendorPath(string $path, string $packagePath = ''): string
{
$packagePath = Path::canonicalize('' === $packagePath ? self::getPackagePath() : $packagePath);
$vendorPath = self::normalizeVendorRelativePath($path);
if (self::isInstalledAsDependency($packagePath)) {
return Path::canonicalize(Path::join($packagePath, '..', '..', $vendorPath));
}
return Path::join($packagePath, 'vendor', $vendorPath);
}
/**
* Returns the preferred tooling binary path for the active project and DevTools runtime.
*
* Consumer projects SHOULD take precedence when they provide a local
* `vendor/bin/<binary>` entry. If the binary is absent locally, the method
* MUST fall back to the active DevTools runtime binary path.
*
* @param string $binary the binary name relative to `vendor/bin`
* @param string $projectPath an optional project root path; defaults to the working project root
* @param string $packagePath an optional package root path; defaults to the current package root
*/
public static function getPreferredToolBinaryPath(
string $binary,
string $projectPath = '',
string $packagePath = '',
): string {
$projectPath = '' === $projectPath ? WorkingProjectPathResolver::getProjectPath() : $projectPath;
$projectBinaryPath = Path::join($projectPath, 'vendor', 'bin', $binary);
if (file_exists($projectBinaryPath)) {
return $projectBinaryPath;
}
return self::getRuntimeToolBinaryPath($binary, $packagePath);
}
/**
* Returns the preferred Composer vendor path for the active project and DevTools runtime.
*
* Consumer projects SHOULD take precedence when they provide the requested
* vendor path locally. If the path is absent locally, the method MUST fall
* back to the active DevTools runtime vendor path.
*
* @param string $path the vendor-relative path to resolve
* @param string $projectPath an optional project root path; defaults to the working project root
* @param string $packagePath an optional package root path; defaults to the current package root
*/
public static function getPreferredVendorPath(
string $path,
string $projectPath = '',
string $packagePath = '',
): string {
$projectPath = '' === $projectPath ? WorkingProjectPathResolver::getProjectPath() : $projectPath;
$vendorPath = self::normalizeVendorRelativePath($path);
$projectVendorPath = Path::join($projectPath, 'vendor', $vendorPath);
if (file_exists($projectVendorPath)) {
return $projectVendorPath;
}
return self::getRuntimeVendorPath($vendorPath, $packagePath);
}
/**
* Detects whether the provided path belongs to an installed vendor copy of DevTools.
*
* @param string $packagePath an optional path within the package; defaults to the package root
*/
public static function isInstalledAsDependency(string $packagePath = ''): bool
{
$packagePath = Path::canonicalize('' === $packagePath ? self::getPackagePath() : $packagePath);
return str_contains($packagePath, self::VENDOR_PACKAGE_PATH);
}
/**
* Detects whether the provided path belongs to the DevTools repository checkout itself.
*
* @param string $packagePath an optional path within the package; defaults to the package root
*/
public static function isRepositoryCheckout(string $packagePath = ''): bool
{
return ! self::isInstalledAsDependency($packagePath);
}
/**
* Normalizes a path relative to the Composer vendor root.
*
* @param string $path the vendor-relative path to normalize
*/
private static function normalizeVendorRelativePath(string $path): string
{
$path = Path::canonicalize($path);
if (str_starts_with($path, 'vendor/')) {
return substr($path, 7);
}
return $path;
}
}