-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClassFinder.php
More file actions
100 lines (83 loc) · 2.43 KB
/
ClassFinder.php
File metadata and controls
100 lines (83 loc) · 2.43 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
<?php
namespace ProAI\Annotations\Metadata;
use Illuminate\Console\AppNamespaceDetectorTrait;
use ProAI\Annotations\Filesystem\ClassFinder as FilesystemClassFinder;
class ClassFinder
{
/**
* The class finder instance.
*
* @var \Illuminate\Filesystem\ClassFinder
*/
protected $finder;
/**
* The application namespace.
*
* @var string
*/
protected $namespace;
/**
* Create a new metadata builder instance.
*
* @param \Illuminate\Filesystem\ClassFinder $finder
* @param array $config
* @return void
*/
public function __construct(FilesystemClassFinder $finder)
{
$this->finder = $finder;
}
/**
* Get all classes for a given namespace.
*
* @param string $namespace
* @return array
*/
public function getClassesFromNamespace($namespace = null)
{
$namespace = $namespace ?: $this->getAppNamespace();
$path = $this->convertNamespaceToPath($namespace);
return $this->finder->findClasses($path);
}
/**
* Convert given namespace to file path.
*
* @param string $namespace
* @return string|null
*/
protected function convertNamespaceToPath($namespace)
{
// strip app namespace
$appNamespace = $this->getAppNamespace();
if (substr($namespace, 0, strlen($appNamespace)) != $appNamespace) {
return null;
}
$subNamespace = substr($namespace, strlen($appNamespace));
// replace \ with / to get the correct file path
$subPath = str_replace('\\', '/', $subNamespace);
// create path
return app('path') . '/' . $subPath;
}
/**
* Get the application namespace.
*
* @return string
*
* @throws \RuntimeException
*/
public function getAppNamespace()
{
if (! is_null($this->namespace)) {
return $this->namespace;
}
$composer = json_decode(file_get_contents(base_path().'/composer.json'), true);
foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) {
foreach ((array) $path as $pathChoice) {
if (realpath(app('path')) == realpath(base_path().'/'.$pathChoice)) {
return $this->namespace = $namespace;
}
}
}
throw new RuntimeException('Unable to detect application namespace.');
}
}