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