|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Tvi\MonitorBundle\Check; |
|
12
|
|
|
|
|
13
|
|
|
use Symfony\Component\Finder\Finder; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
|
|
|
|
|
16
|
|
|
* @author Vladimir Turnaev <[email protected]> |
|
17
|
|
|
*/ |
|
|
|
|
|
|
18
|
|
|
class CheckFinder |
|
19
|
|
|
{ |
|
20
|
|
|
protected $searchDirs = [__DIR__.'/**']; |
|
21
|
|
|
|
|
22
|
41 |
|
public function __construct(array $dirs = null) |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
41 |
|
if($dirs) { |
|
|
|
|
|
|
25
|
|
|
$this->searchDirs = array_merge($this->searchDirs, $dirs); |
|
26
|
|
|
} |
|
27
|
41 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
|
|
|
|
|
30
|
|
|
* @return string[] |
|
31
|
|
|
*/ |
|
32
|
41 |
|
public function find() |
|
33
|
|
|
{ |
|
34
|
41 |
|
$fs = Finder::create(); |
|
35
|
41 |
|
$files = $fs->in($this->searchDirs)->name('*.php')->files(); |
|
36
|
|
|
|
|
37
|
41 |
|
$res = []; |
|
38
|
41 |
|
foreach ($files as $f) { |
|
39
|
|
|
/* @var \SplFileInfo $f */ |
|
40
|
|
|
|
|
41
|
41 |
|
$code = $f->getContents(); |
|
|
|
|
|
|
42
|
41 |
|
$class = $this->getConfigClass($code); |
|
43
|
41 |
|
if(is_subclass_of($class, CheckConfigInterface::class)) { |
|
|
|
|
|
|
44
|
41 |
|
$res[] = $class; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
41 |
|
return $res; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
41 |
|
private function getConfigClass($contents) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
41 |
|
$namespace = []; |
|
54
|
41 |
|
$class = []; |
|
55
|
|
|
|
|
56
|
41 |
|
$tokens = token_get_all($contents); |
|
57
|
|
|
|
|
58
|
|
|
do { |
|
|
|
|
|
|
59
|
41 |
|
$token = current($tokens); |
|
60
|
|
|
|
|
61
|
41 |
|
if(isset($token[0]) && $token[0] == T_NAMESPACE) { |
|
|
|
|
|
|
62
|
41 |
|
next($tokens); |
|
63
|
|
|
do { |
|
|
|
|
|
|
64
|
41 |
|
$token = current($tokens); |
|
65
|
41 |
|
if($token == ';') { |
|
|
|
|
|
|
66
|
41 |
|
break 1; |
|
67
|
|
|
} |
|
68
|
41 |
|
$namespace[] = $token[1]; |
|
69
|
41 |
|
} while(next($tokens)); |
|
70
|
|
|
|
|
71
|
41 |
|
$namespace = trim(implode('', $namespace)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
41 |
|
if(isset($token[0]) && $token[0] == T_CLASS) { |
|
|
|
|
|
|
75
|
41 |
|
next($tokens); |
|
76
|
|
|
do { |
|
|
|
|
|
|
77
|
41 |
|
$token = current($tokens); |
|
78
|
|
|
|
|
79
|
41 |
|
if($token[0] == T_EXTENDS) { |
|
|
|
|
|
|
80
|
|
|
break 1; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
41 |
|
if($token[0] == T_STRING) { |
|
|
|
|
|
|
84
|
41 |
|
$class[] = $token[1]; |
|
85
|
41 |
|
break 1; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
41 |
|
} while(next($tokens)); |
|
89
|
|
|
|
|
90
|
41 |
|
$class = trim(implode('', $class)); |
|
91
|
|
|
|
|
92
|
41 |
|
break; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
41 |
|
} while(next($tokens)); |
|
96
|
|
|
|
|
97
|
41 |
|
$configClass = (string)$namespace . '\\' . (string)$class; |
|
98
|
|
|
|
|
99
|
41 |
|
return $configClass; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|