|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace drupol\Yaroc\Utilities; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class ClassFinder. |
|
7
|
|
|
* |
|
8
|
|
|
* @package drupol\Yaroc\Utilities |
|
9
|
|
|
*/ |
|
10
|
|
|
class ClassFinder { |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
const appRoot = __DIR__ . "/../../"; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param $namespace |
|
19
|
|
|
* |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
13 |
|
public static function getClassesInNamespace($namespace) { |
|
23
|
13 |
|
$files = scandir(self::getNamespaceDirectory($namespace)); |
|
24
|
|
|
|
|
25
|
|
|
$classes = array_map(function($file) use ($namespace){ |
|
|
|
|
|
|
26
|
13 |
|
return $namespace . '\\' . str_replace('.php', '', $file); |
|
27
|
13 |
|
}, $files); |
|
28
|
|
|
|
|
29
|
13 |
|
return array_filter($classes, function($possibleClass){ |
|
|
|
|
|
|
30
|
13 |
|
return class_exists($possibleClass); |
|
31
|
13 |
|
}); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
13 |
|
private static function getDefinedNamespaces() { |
|
38
|
13 |
|
$composerJsonPath = self::appRoot . 'composer.json'; |
|
39
|
13 |
|
$composerConfig = json_decode(file_get_contents($composerJsonPath)); |
|
40
|
|
|
|
|
41
|
|
|
//Apparently PHP doesn't like hyphens, so we use variable variables instead. |
|
42
|
13 |
|
$psr4 = "psr-4"; |
|
43
|
13 |
|
return (array) $composerConfig->autoload->$psr4; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param $namespace |
|
48
|
|
|
* |
|
49
|
|
|
* @return bool|string |
|
|
|
|
|
|
50
|
|
|
*/ |
|
51
|
13 |
|
private static function getNamespaceDirectory($namespace) { |
|
52
|
13 |
|
$composerNamespaces = self::getDefinedNamespaces(); |
|
53
|
|
|
|
|
54
|
13 |
|
$namespaceFragments = explode('\\', $namespace); |
|
55
|
13 |
|
$undefinedNamespaceFragments = []; |
|
56
|
|
|
|
|
57
|
13 |
|
while($namespaceFragments) { |
|
|
|
|
|
|
58
|
13 |
|
$possibleNamespace = implode('\\', $namespaceFragments) . '\\'; |
|
59
|
|
|
|
|
60
|
13 |
|
if(array_key_exists($possibleNamespace, $composerNamespaces)){ |
|
|
|
|
|
|
61
|
13 |
|
return realpath(self::appRoot . $composerNamespaces[$possibleNamespace] . implode('/', array_reverse($undefinedNamespaceFragments))); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
13 |
|
$undefinedNamespaceFragments[] = array_pop($namespaceFragments); |
|
65
|
13 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|