| 1 |
<?php |
| 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 |
namespace Liuch\DmarcSrg\Plugins; |
| 33 |
|
| 34 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 35 |
use Liuch\DmarcSrg\Exception\RuntimeException; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class PluginManager |
| 41 |
{ |
| 42 |
private static $instance = null; |
| 43 |
|
| 44 |
private $plugins = []; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static function dispatchEvent(string $group, string $name, array &$data): void |
| 56 |
{ |
| 57 |
$plist = self::instance()->loadPlugins($group); |
| 58 |
$data['event'] = $name; |
| 59 |
$plugin = null; |
| 60 |
try { |
| 61 |
foreach ($plist as &$it) { |
| 62 |
$handler = $it['handlers'][$name] ?? null; |
| 63 |
if ($handler) { |
| 64 |
$plugin = $it['plugin']; |
| 65 |
$plugin->$handler($data); |
| 66 |
} |
| 67 |
} |
| 68 |
unset($it); |
| 69 |
} catch (SoftException $e) { |
| 70 |
if ($plugin) { |
| 71 |
$e = new SoftException('[' . $plugin->name() . '] ' . $e->getMessage(), $e->getCode()); |
| 72 |
} |
| 73 |
throw $e; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private function loadPlugins(string $group): array |
| 88 |
{ |
| 89 |
if (isset($this->plugins[$group])) { |
| 90 |
return $this->plugins[$group]; |
| 91 |
} |
| 92 |
|
| 93 |
$group_path = ROOT_PATH . 'plugins/' . $group; |
| 94 |
if (!is_dir($group_path) || !is_readable($group_path)) { |
| 95 |
return []; |
| 96 |
} |
| 97 |
|
| 98 |
$res = []; |
| 99 |
try { |
| 100 |
foreach (new \FilesystemIterator($group_path) as $plugin_fi) { |
| 101 |
if ($plugin_fi->isDir() && $plugin_fi->isReadable()) { |
| 102 |
$plugin_name = $plugin_fi->getFilename(); |
| 103 |
$plugin_path = $plugin_fi->getPathname() . "/{$plugin_name}.php"; |
| 104 |
if (is_file($plugin_path) && is_readable($plugin_path)) { |
| 105 |
include_once($plugin_path); |
| 106 |
$plugin_ns_name = __NAMESPACE__ . '\\' . $plugin_name; |
| 107 |
$plugin = new $plugin_ns_name(); |
| 108 |
if ($plugin instanceof PluginInterface) { |
| 109 |
$res[] = [ |
| 110 |
'plugin' => $plugin, |
| 111 |
'name' => $plugin_name, |
| 112 |
'handlers' => $plugin->subscribedEvents() |
| 113 |
]; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} catch (\Exception $e) { |
| 119 |
throw new RuntimeException('Plugin loading error', -1, $e); |
| 120 |
} |
| 121 |
|
| 122 |
usort($res, function ($a, $b) { |
| 123 |
return strcmp($a['name'], $b['name']); |
| 124 |
}); |
| 125 |
|
| 126 |
$this->plugins[$group] = $res; |
| 127 |
return $res; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
private static function instance() |
| 136 |
{ |
| 137 |
if (!self::$instance) { |
| 138 |
self::$instance = new self(); |
| 139 |
} |
| 140 |
return self::$instance; |
| 141 |
} |
| 142 |
} |