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 = [__DIR__.'/**', __DIR__.'/**/**']; |
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
|
|
|
$this->addSearchExps($path); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
10 |
|
public function addSearchExps($exps) |
|
|
|
|
38
|
|
|
{ |
39
|
10 |
|
$exps = \is_string($exps) ? [$exps] : $exps; |
40
|
|
|
|
41
|
10 |
|
$this->searchExps = array_merge($this->searchExps, $exps); |
42
|
10 |
|
$this->searchExps = array_unique($this->searchExps); |
43
|
10 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @return string[] |
47
|
|
|
*/ |
48
|
57 |
|
public function find() |
49
|
|
|
{ |
50
|
57 |
|
$fs = Finder::create(); |
51
|
57 |
|
$res = []; |
52
|
57 |
|
foreach ($this->searchExps as $searchExp) { |
53
|
|
|
try { |
54
|
57 |
|
$files = $fs->in($searchExp)->name('Plugin.php')->files(); |
55
|
|
|
|
56
|
57 |
|
foreach ($files as $f) { |
57
|
|
|
/* @var \SplFileInfo $f */ |
58
|
|
|
|
59
|
57 |
|
$code = $f->getContents(); |
|
|
|
|
60
|
57 |
|
$class = $this->getConfigClass($code); |
61
|
|
|
|
62
|
57 |
|
if (is_subclass_of($class, CheckPluginInterface::class)) { |
63
|
57 |
|
$res[] = $class; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
57 |
|
} catch (\InvalidArgumentException $e) {} |
|
|
|
|
68
|
|
|
}; |
69
|
|
|
|
70
|
57 |
|
return array_unique($res); |
71
|
|
|
} |
72
|
|
|
|
73
|
57 |
|
private function getConfigClass($contents) |
|
|
|
|
74
|
|
|
{ |
75
|
57 |
|
$namespace = []; |
76
|
57 |
|
$class = []; |
77
|
|
|
|
78
|
57 |
|
$tokens = token_get_all($contents); |
79
|
|
|
|
80
|
|
|
do { |
81
|
57 |
|
$token = current($tokens); |
82
|
|
|
|
83
|
57 |
|
if (isset($token[0]) && T_NAMESPACE === $token[0]) { |
84
|
57 |
|
next($tokens); |
85
|
|
|
do { |
86
|
57 |
|
$token = current($tokens); |
87
|
57 |
|
if (';' === $token) { |
88
|
57 |
|
break 1; |
89
|
|
|
} |
90
|
57 |
|
$namespace[] = $token[1]; |
91
|
57 |
|
} while (next($tokens)); |
92
|
|
|
|
93
|
57 |
|
$namespace = trim(implode('', $namespace)); |
94
|
|
|
} |
95
|
|
|
|
96
|
57 |
|
if (isset($token[0]) && T_CLASS === $token[0]) { |
97
|
57 |
|
next($tokens); |
98
|
|
|
do { |
99
|
57 |
|
$token = current($tokens); |
100
|
|
|
|
101
|
57 |
|
if (T_STRING === $token[0]) { |
102
|
57 |
|
$class[] = $token[1]; |
103
|
57 |
|
break 1; |
104
|
|
|
} |
105
|
57 |
|
} while (next($tokens)); |
106
|
|
|
|
107
|
57 |
|
$class = trim(implode('', $class)); |
108
|
|
|
|
109
|
57 |
|
break; |
110
|
|
|
} |
111
|
57 |
|
} while (next($tokens)); |
112
|
|
|
|
113
|
57 |
|
return (string) $namespace.'\\'.(string) $class; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|