-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.php
More file actions
48 lines (40 loc) · 1.78 KB
/
Utils.php
File metadata and controls
48 lines (40 loc) · 1.78 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
<?php
declare(strict_types=1);
namespace MagicPush\CliToolkit;
abstract class Utils {
/**
* Extracts and returns a short name (a namespace is stripped) of a provided fully qualified class name.
*
* Names of classes without namespaces are supported too - those are returned as is.
*/
public static function getClassShortName(string $className): string {
return mb_substr(mb_strrchr("\\{$className}", '\\'), 1);
}
protected static string $topmostProjectRootDirectory;
/**
* Performs bottom-up search for a path with a `vendor` directory inside it, which is located closest to `/`.
* If fails to find one, eventually returns a topmost directory path in a file system (like `/`).
*
* For example: the function will return `/home/user/cool-project`, if starts searching from
* `/home/user/cool-project/vendor/sup-project/vendor/MagicPush/cli-tool/src/Utils.php`
*/
public static function detectTopmostProjectRootDirectory(): string {
if (!isset(static::$topmostProjectRootDirectory)) {
$currentDirPath = __DIR__;
$highestDirPathAboveVendor = null;
while (true) {
if (file_exists($currentDirPath . '/vendor')) {
$highestDirPathAboveVendor = $currentDirPath;
}
$previousDirPath = $currentDirPath;
$currentDirPath = dirname($previousDirPath);
// We can't go higher than a filesystem's top:
if ($currentDirPath === $previousDirPath) {
static::$topmostProjectRootDirectory = $highestDirPathAboveVendor ?? $currentDirPath;
break;
}
}
}
return static::$topmostProjectRootDirectory;
}
}