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
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
|
|
public function getClasses() |
45
|
|
|
{ |
46
|
|
|
$classes = []; |
47
|
|
|
$relativePath = $this->convertNamespaceToPath($this->relativeNamespace); |
48
|
|
|
|
49
|
|
|
foreach ($this->getClassLoader()->getPrefixesPsr4() as $baseNamespace => $directories) { |
50
|
|
|
$directories = array_map(function ($directory) use ($relativePath) { |
51
|
|
|
return $directory.$relativePath; |
52
|
|
|
}, $directories); |
53
|
|
|
|
54
|
|
|
$directories = array_filter($directories, function ($path) { |
55
|
|
|
return is_dir($path); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
foreach ($this->search($directories, $this->searchPattern) as $file) { |
59
|
|
|
$relativePathName = $file->getRelativePathname(); |
60
|
|
|
$classes[] = $baseNamespace.$this->convertPathToNamespace($relativePath.DIRECTORY_SEPARATOR.$relativePathName); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $classes; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function getFile($class) |
71
|
|
|
{ |
72
|
|
|
return $this->getClassLoader()->findFile($class); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $directories |
77
|
|
|
* @param $pattern |
78
|
|
|
* |
79
|
|
|
* @return \Symfony\Component\Finder\Finder |
80
|
|
|
*/ |
81
|
|
|
protected function search($directories, $pattern) |
82
|
|
|
{ |
83
|
|
|
$finder = new Finder(); |
84
|
|
|
$finder->files() |
85
|
|
|
->name($pattern) |
86
|
|
|
->in($directories); |
87
|
|
|
|
88
|
|
|
return $finder; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $path |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
protected function convertPathToNamespace($path) |
97
|
|
|
{ |
98
|
|
|
return str_replace([DIRECTORY_SEPARATOR, '.php'], ['\\', ''], trim($path, DIRECTORY_SEPARATOR)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function convertNamespaceToPath($namespace) |
105
|
|
|
{ |
106
|
|
|
return DIRECTORY_SEPARATOR.str_replace("\\", DIRECTORY_SEPARATOR, trim($namespace, '\\')); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|