Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
8 / 10 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| DevToolsPathResolver | |
80.00% |
8 / 10 |
|
83.33% |
5 / 6 |
9.65 | |
0.00% |
0 / 1 |
| getPackagePath | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| getBinaryPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getBinaryCommand | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getResourcesPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isInstalledAsDependency | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| isRepositoryCheckout | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * Fast Forward Development Tools for PHP projects. |
| 7 | * |
| 8 | * This file is part of fast-forward/dev-tools project. |
| 9 | * |
| 10 | * @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 11 | * @license https://opensource.org/licenses/MIT MIT License |
| 12 | * |
| 13 | * @see https://github.com/php-fast-forward/ |
| 14 | * @see https://github.com/php-fast-forward/dev-tools |
| 15 | * @see https://github.com/php-fast-forward/dev-tools/issues |
| 16 | * @see https://php-fast-forward.github.io/dev-tools/ |
| 17 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 18 | */ |
| 19 | |
| 20 | namespace FastForward\DevTools\Path; |
| 21 | |
| 22 | use InvalidArgumentException; |
| 23 | use Symfony\Component\Filesystem\Path; |
| 24 | |
| 25 | /** |
| 26 | * Resolves canonical paths for the DevTools package itself. |
| 27 | */ |
| 28 | class DevToolsPathResolver |
| 29 | { |
| 30 | /** |
| 31 | * @var string the relative path to the packaged DevTools binary |
| 32 | */ |
| 33 | public const string BINARY = 'bin/dev-tools'; |
| 34 | |
| 35 | /** |
| 36 | * @var string the resources directory segment within the package |
| 37 | */ |
| 38 | public const string RESOURCES = 'resources'; |
| 39 | |
| 40 | /** |
| 41 | * @var string the vendor install path fragment used when DevTools runs as a dependency |
| 42 | */ |
| 43 | private const string VENDOR_PACKAGE_PATH = \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR |
| 44 | . 'fast-forward' . \DIRECTORY_SEPARATOR . 'dev-tools'; |
| 45 | |
| 46 | /** |
| 47 | * Returns the DevTools package directory or a path under it. |
| 48 | * |
| 49 | * @param string $path the optional relative segment to append under the package directory |
| 50 | */ |
| 51 | public static function getPackagePath(string $path = ''): string |
| 52 | { |
| 53 | $packageDirectory = \dirname(__DIR__, 2); |
| 54 | |
| 55 | if ('' !== $path && Path::isAbsolute($path)) { |
| 56 | throw new InvalidArgumentException('The DevTools package path MUST be relative to the package root.'); |
| 57 | } |
| 58 | |
| 59 | return Path::join($packageDirectory, $path); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns the packaged DevTools binary path. |
| 64 | */ |
| 65 | public static function getBinaryPath(): string |
| 66 | { |
| 67 | return self::getPackagePath(self::BINARY); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns the packaged DevTools binary command with a subcommand. |
| 72 | * |
| 73 | * @param string $command the DevTools subcommand to append |
| 74 | */ |
| 75 | public static function getBinaryCommand(string $command): string |
| 76 | { |
| 77 | $binaryPath = self::getPackagePath(self::BINARY); |
| 78 | |
| 79 | return \sprintf('%s %s', $binaryPath, $command); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the packaged resources directory or a path under it. |
| 84 | * |
| 85 | * @param string $path the optional relative segment to append under resources |
| 86 | */ |
| 87 | public static function getResourcesPath(string $path = ''): string |
| 88 | { |
| 89 | return self::getPackagePath(Path::join(self::RESOURCES, $path)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Detects whether the provided path belongs to an installed vendor copy of DevTools. |
| 94 | * |
| 95 | * @param string $packagePath an optional path within the package; defaults to the package root |
| 96 | */ |
| 97 | public static function isInstalledAsDependency(string $packagePath = ''): bool |
| 98 | { |
| 99 | return str_contains('' === $packagePath ? self::getPackagePath() : $packagePath, self::VENDOR_PACKAGE_PATH); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Detects whether the provided path belongs to the DevTools repository checkout itself. |
| 104 | * |
| 105 | * @param string $packagePath an optional path within the package; defaults to the package root |
| 106 | */ |
| 107 | public static function isRepositoryCheckout(string $packagePath = ''): bool |
| 108 | { |
| 109 | return ! self::isInstalledAsDependency($packagePath); |
| 110 | } |
| 111 | } |