Debian Codegraph @

classes/Plugins/PluginManager.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2025 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This file contains the class PluginManager
25 *
26 * @category API
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 namespace Liuch\DmarcSrg\Plugins;
33
34 use Liuch\DmarcSrg\Exception\SoftException;
35 use Liuch\DmarcSrg\Exception\RuntimeException;
36
37 /**
38 * Class for managing the plugin system
39 */
40 class PluginManager
41 {
42 private static $instance = null;
43
44 private $plugins = [];
45
46 /**
47 * Sends an event to plugin that are subscribed to it
48 *
49 * @param string $group Event group
50 * @param string $name Event name
51 * @param array $data Array of data to be passed to each plugin
52 *
53 * @return void
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 * Searches and loads plugins for the specified type, if necessary.
79 *
80 * Splitting plug-ins into groups is necessary to save resources,
81 * so that only necessary plug-ins are loaded.
82 *
83 * @param string $group Event group
84 *
85 * @return array Array of plugins of the specified group sorted by name
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 * Returns an instance of the class
132 *
133 * @return self
134 */
135 private static function instance()
136 {
137 if (!self::$instance) {
138 self::$instance = new self();
139 }
140 return self::$instance;
141 }
142 }