-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetClassesUsingInterface.php
More file actions
57 lines (53 loc) · 1.73 KB
/
getClassesUsingInterface.php
File metadata and controls
57 lines (53 loc) · 1.73 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
<?php
namespace example;
class classFinder
{
function run()
{
$interfaceName = 'example\matchRules\matchRule';
$searchLocation = __DIR__.'/matchRules';
$namespaceForFolder= 'example\matchRules';
$rules = $this->whoImplements($interfaceName, $searchLocation, $namespaceForFolder);
}
/**
* get an array of classes from a given folder,
* who a implementing a given interface
* This assumes psr4 is followed .ie the names are the same as their class
* @param string $interfaceName
* @param string $searchLocation
* @param string $namespace
* @return array
*/
function whoImplements($interfaceName, $searchLocation, $namespace = '')
{
$classes = $this->getClassesFromFolder(
$searchLocation,
$namespace
);
$implementsIModule = array();
for ($i=0; $i<sizeof($classes); $i++) {
$reflect = new \ReflectionClass("$classes[$i]");
if($reflect->implementsInterface($interfaceName)){
$implementsIModule[] = $classes[$i];
}
}
return $implementsIModule;
}
/**
* get the classes of all files in a folder.
* This assumes psr4 is followed .ie the names are the same as their class
* @param string $searchLocation
* @param string $namespace
* @return array
*/
function getClassesFromFolder($searchLocation, $namespace)
{
$classes = [];
$files = array_diff(scandir($searchLocation), array('..', '.'));
$files = array_values($files);
for ($i=0; $i<sizeof($files) ; $i++){
$classes[] = $namespace.'\\'.basename($files[$i], '.php');
}
return $classes;
}
}