|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Robo\ClassDiscovery; |
|
4
|
|
|
|
|
5
|
|
|
use Robo\Common\ClassLoaderAwareTrait; |
|
6
|
|
|
use Robo\Contract\ClassLoaderAwareInterface; |
|
7
|
|
|
use Symfony\Component\Finder\Finder; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class RelativeNamespaceDiscovery |
|
11
|
|
|
* |
|
12
|
|
|
* @package Robo\Plugin\ClassDiscovery |
|
13
|
|
|
*/ |
|
14
|
|
|
class RelativeNamespaceDiscovery extends AbstractClassDiscovery implements ClassLoaderAwareInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use ClassLoaderAwareTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $relativeNamespace; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Psr4Discovery constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param $relativeNamespace |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($relativeNamespace) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->relativeNamespace = $relativeNamespace; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getRelativeNamespace() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->relativeNamespace; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getRelativeNamespacePath() |
|
45
|
|
|
{ |
|
46
|
|
|
return str_replace("\\", '/', $this->getRelativeNamespace()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritDoc |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getClasses() |
|
53
|
|
|
{ |
|
54
|
|
|
$classes = []; |
|
55
|
|
|
|
|
56
|
|
|
foreach ($this->getClassLoader()->getPrefixesPsr4() as $baseNamespace => $directories) { |
|
57
|
|
|
$path = $this->getRelativeNamespacePath(); |
|
58
|
|
|
|
|
59
|
|
|
$directories = array_map(function ($directory) use ($path) { |
|
60
|
|
|
return $directory.$path; |
|
61
|
|
|
}, $directories); |
|
62
|
|
|
|
|
63
|
|
|
$directories = array_filter($directories, function ($path) { |
|
64
|
|
|
return is_dir($path); |
|
65
|
|
|
}); |
|
66
|
|
|
|
|
67
|
|
|
foreach ($this->search($directories, $this->searchPattern) as $file) { |
|
68
|
|
|
$relativePathName = $file->getRelativePathname(); |
|
69
|
|
|
$classes[] = $baseNamespace.str_replace(['/', '.php'], ['\\', ''], $relativePathName); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $classes; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getFile($class) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->getClassLoader()->findFile($class); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param $directories |
|
86
|
|
|
* @param $pattern |
|
87
|
|
|
* |
|
88
|
|
|
* @return \Symfony\Component\Finder\Finder |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function search($directories, $pattern) |
|
91
|
|
|
{ |
|
92
|
|
|
$finder = new Finder(); |
|
93
|
|
|
$finder->files() |
|
94
|
|
|
->name($pattern) |
|
95
|
|
|
->in($directories); |
|
96
|
|
|
|
|
97
|
|
|
return $finder; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|