|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: tom |
|
6
|
|
|
* Date: 18/06/2016 |
|
7
|
|
|
* Time: 16:23 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace twhiston\twLib\Discovery; |
|
11
|
|
|
|
|
12
|
|
|
use RecursiveDirectoryIterator; |
|
13
|
|
|
use RecursiveIteratorIterator; |
|
14
|
|
|
use RegexIterator; |
|
15
|
|
|
|
|
16
|
|
|
class FindByNamespace |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
protected $path; |
|
20
|
|
|
|
|
21
|
|
|
protected $data; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* FindByNamespace constructor. |
|
25
|
|
|
* @param $path |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public function __construct($path = null) |
|
28
|
|
|
{ |
|
29
|
2 |
|
$this->path = ($path === null || $path === '') ? __DIR__ : $path; |
|
30
|
2 |
|
$this->data = []; |
|
31
|
2 |
|
$this->data[$path] = []; |
|
32
|
2 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function setPath($path) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->path = $path; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
public function find($needle = null, $rebuild = false) |
|
40
|
|
|
{ |
|
41
|
2 |
|
$this->buildData($rebuild); |
|
42
|
2 |
|
return $this->filterData($needle); |
|
43
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
protected function buildData($rebuild) |
|
47
|
|
|
{ |
|
48
|
2 |
|
if ($rebuild === true || empty($this->data[$this->path])) { |
|
49
|
2 |
|
$allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->path)); |
|
50
|
2 |
|
$phpFiles = new RegexIterator($allFiles, '/\.php$/'); |
|
51
|
2 |
|
foreach ($phpFiles as $phpFile) { |
|
52
|
|
|
//tokenize file |
|
53
|
2 |
|
if (file_exists($phpFile->getRealPath())) { |
|
54
|
2 |
|
$content = file_get_contents($phpFile->getRealPath()); |
|
55
|
2 |
|
$this->processTokens(token_get_all($content)); |
|
56
|
2 |
|
} |
|
57
|
2 |
|
} |
|
58
|
2 |
|
} |
|
59
|
2 |
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
protected function processTokens(array $tokens) |
|
62
|
|
|
{ |
|
63
|
2 |
|
$namespace = ''; |
|
64
|
2 |
|
for ($index = 0; isset($tokens[$index]); $index++) { |
|
65
|
2 |
|
if (!isset($tokens[$index][0])) { |
|
66
|
|
|
continue; |
|
67
|
|
|
} |
|
68
|
2 |
|
if (T_NAMESPACE === $tokens[$index][0]) { |
|
69
|
2 |
|
$index += 2; // Skip namespace keyword and whitespace |
|
70
|
2 |
|
while (isset($tokens[$index]) && is_array($tokens[$index])) { |
|
71
|
2 |
|
$namespace .= $tokens[$index++][1]; |
|
72
|
2 |
|
} |
|
73
|
2 |
|
} |
|
74
|
2 |
|
if (T_CLASS === $tokens[$index][0]) { |
|
75
|
2 |
|
$index += 2; // Skip class keyword and whitespace |
|
76
|
2 |
|
if (is_array($tokens[$index]) && array_key_exists(1, $tokens[$index])) { |
|
77
|
2 |
|
$this->data[$this->path][] = $namespace . '\\' . $tokens[$index][1]; |
|
78
|
2 |
|
} |
|
79
|
2 |
|
} |
|
80
|
2 |
|
} |
|
81
|
2 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
protected function filterData($needle) |
|
85
|
|
|
{ |
|
86
|
2 |
|
return array_values(array_filter($this->data[$this->path], function ($var) use ($needle) { |
|
87
|
2 |
|
if (strpos($var, $needle) !== false) { |
|
88
|
1 |
|
return true; |
|
89
|
|
|
} |
|
90
|
2 |
|
return false; |
|
91
|
2 |
|
})); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |