|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Copyright (c) 2016 Constantin Galbenu <[email protected]> * |
|
4
|
|
|
******************************************************************************/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Gica\CodeAnalysis; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
use Gica\CodeAnalysis\MethodListenerDiscovery\ClassSorter; |
|
10
|
|
|
use Gica\CodeAnalysis\MethodListenerDiscovery\ListenerClassValidator; |
|
11
|
|
|
use Gica\CodeAnalysis\Traits\FilesInDirectoryExtracter; |
|
12
|
|
|
|
|
13
|
|
|
class ClassDiscovery |
|
14
|
|
|
{ |
|
15
|
|
|
use FilesInDirectoryExtracter; |
|
16
|
|
|
|
|
17
|
|
|
protected $discoveredClasses = []; |
|
18
|
|
|
|
|
19
|
|
|
/** @var ListenerClassValidator */ |
|
20
|
|
|
private $classValidator; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ClassSorter |
|
23
|
|
|
*/ |
|
24
|
|
|
private $classSorter; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var PhpClassInFileInspector |
|
27
|
|
|
*/ |
|
28
|
|
|
private $phpClassInFileInspector; |
|
29
|
|
|
|
|
30
|
1 |
|
public function __construct( |
|
31
|
|
|
ListenerClassValidator $classValidator, |
|
32
|
|
|
ClassSorter $classSorter, |
|
33
|
|
|
PhpClassInFileInspector $phpClassInFileInspector = null |
|
34
|
|
|
) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$this->classValidator = $classValidator; |
|
37
|
1 |
|
$this->classSorter = $classSorter; |
|
38
|
1 |
|
$this->phpClassInFileInspector = $phpClassInFileInspector ?? new PhpClassInFileInspector; |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
1 |
|
public function discover($directory) |
|
43
|
|
|
{ |
|
44
|
1 |
|
$files = $this->getFilesInDirectory($directory); |
|
45
|
|
|
|
|
46
|
1 |
|
$files = $this->filterFiles($files); |
|
47
|
|
|
|
|
48
|
1 |
|
foreach ($files as $file) { |
|
49
|
1 |
|
$fullFilePath = $file; |
|
50
|
|
|
|
|
51
|
1 |
|
$extractedClass = $this->extractClassFromFileIfAccepted($fullFilePath); |
|
52
|
|
|
|
|
53
|
1 |
|
if ($extractedClass) { |
|
54
|
1 |
|
$this->discoveredClasses[] = $extractedClass; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
$this->discoveredClasses = $this->sort($this->discoveredClasses); |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $filePath |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
1 |
|
protected function isListenerFileName($filePath) |
|
66
|
|
|
{ |
|
67
|
1 |
|
return preg_match('#\.php$#ims', $filePath); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param $fullFilePath |
|
72
|
|
|
* @return bool|\ReflectionClass |
|
73
|
|
|
*/ |
|
74
|
1 |
|
protected function extractClassFromFileIfAccepted($fullFilePath) |
|
75
|
|
|
{ |
|
76
|
1 |
|
$fqn = $this->phpClassInFileInspector->getFullyQualifiedClassName($fullFilePath); |
|
77
|
|
|
|
|
78
|
1 |
|
if (null === $fqn) { |
|
79
|
1 |
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
return $this->getClassIfAccepted($fqn); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return \ReflectionClass[] |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function getDiscoveredClasses() |
|
89
|
|
|
{ |
|
90
|
1 |
|
return $this->discoveredClasses; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function filterFiles(array $files) |
|
94
|
|
|
{ |
|
95
|
1 |
|
return array_filter($files, function ($file) { |
|
96
|
1 |
|
return $this->isListenerFileName($file); |
|
97
|
1 |
|
}); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param $className |
|
102
|
|
|
* @return \ReflectionClass|null |
|
103
|
|
|
*/ |
|
104
|
1 |
|
private function getClassIfAccepted($className) |
|
105
|
|
|
{ |
|
106
|
1 |
|
$reflectionClass = new \ReflectionClass($className); |
|
107
|
|
|
|
|
108
|
1 |
|
if ($this->classValidator->isClassAccepted($reflectionClass)) { |
|
109
|
1 |
|
return $reflectionClass; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
return null; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param \ReflectionClass[] $discoveredClasses |
|
117
|
|
|
* @return \ReflectionClass[] |
|
118
|
|
|
*/ |
|
119
|
1 |
|
private function sort($discoveredClasses) |
|
120
|
|
|
{ |
|
121
|
1 |
|
usort($discoveredClasses, $this->classSorter); |
|
122
|
|
|
|
|
123
|
1 |
|
return $discoveredClasses; |
|
124
|
|
|
} |
|
125
|
|
|
} |