Skip to content

Commit ac54a96

Browse files
committed
Implement Hook attribute
1 parent 1b1fbe5 commit ac54a96

5 files changed

Lines changed: 144 additions & 74 deletions

File tree

Sources/PluginLoader/HasInvoke.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* HasInvoke.php
5+
*
6+
* @package Plugin Loader
7+
* @link https://github.com/dragomano/Plugin-Loader
8+
* @author Bugo <bugo@dragomano.ru>
9+
* @copyright 2023-2024 Bugo
10+
* @license https://opensource.org/licenses/BSD-3-Clause The 3-Clause BSD License
11+
*
12+
* @version 0.6
13+
*/
14+
15+
namespace Bugo\PluginLoader;
16+
17+
use ReflectionClass;
18+
19+
trait HasInvoke
20+
{
21+
public function __invoke(): void
22+
{
23+
$reflectionClass = new ReflectionClass(static::class);
24+
foreach ($reflectionClass->getMethods() as $method) {
25+
$attributes = $method->getAttributes(Hook::class);
26+
27+
foreach ($attributes as $attribute) {
28+
$attribute->newInstance();
29+
}
30+
}
31+
}
32+
}

Sources/PluginLoader/Hook.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php /** @noinspection PhpMultipleClassDeclarationsInspection */
2+
3+
/**
4+
* Hook.php
5+
*
6+
* @package Plugin Loader
7+
* @link https://github.com/dragomano/Plugin-Loader
8+
* @author Bugo <bugo@dragomano.ru>
9+
* @copyright 2023-2024 Bugo
10+
* @license https://opensource.org/licenses/BSD-3-Clause The 3-Clause BSD License
11+
*
12+
* @version 0.6
13+
*/
14+
15+
namespace Bugo\PluginLoader;
16+
17+
use Attribute;
18+
19+
#[Attribute(Attribute::TARGET_METHOD)]
20+
final class Hook
21+
{
22+
public function __construct(string $name, string $method, string $file)
23+
{
24+
add_integration_function($name, $method, false, $file);
25+
}
26+
}

Sources/PluginLoader/Integration.php

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,28 @@
66
* @package Plugin Loader
77
* @link https://github.com/dragomano/Plugin-Loader
88
* @author Bugo <bugo@dragomano.ru>
9-
* @copyright 2023 Bugo
9+
* @copyright 2023-2024 Bugo
1010
* @license https://opensource.org/licenses/BSD-3-Clause The 3-Clause BSD License
1111
*
12-
* @version 0.5
12+
* @version 0.6
1313
*/
1414

1515
namespace Bugo\PluginLoader;
1616

1717
use SimpleXMLElement;
1818
use ZipArchive;
1919

20-
if (!defined('SMF'))
20+
if (! defined('SMF'))
2121
die('No direct access...');
2222

2323
/**
2424
* Generated by Simple Mod Maker
2525
*/
26-
class Integration
26+
final class Integration
2727
{
28-
public function hooks(): void
29-
{
30-
add_integration_function('integrate_update_settings_file', __CLASS__ . '::updateSettingsFile#', false, __FILE__);
31-
add_integration_function('integrate_admin_areas', __CLASS__ . '::adminAreas#', false, __FILE__);
32-
}
28+
use HasInvoke;
3329

30+
#[Hook('integrate_update_settings_file', self::class . '::updateSettingsFile#', __FILE__)]
3431
public function updateSettingsFile(array &$settings_defs): void
3532
{
3633
$settings_defs['plugins'] = [
@@ -46,6 +43,7 @@ public function updateSettingsFile(array &$settings_defs): void
4643
];
4744
}
4845

46+
#[Hook('integrate_admin_areas', self::class . '::adminAreas#', __FILE__)]
4947
public function adminAreas(array &$admin_areas): void
5048
{
5149
global $txt;
@@ -109,9 +107,7 @@ public function browseList(): void
109107
$context['sub_template'] = 'main';
110108

111109
$this->handleSave();
112-
113110
$this->handleToggle();
114-
115111
$this->handleRemove();
116112
}
117113

@@ -167,7 +163,7 @@ private function handleSave(): void
167163
{
168164
global $context;
169165

170-
if (!isset($_REQUEST['save']) || empty($_REQUEST['plugin_name']))
166+
if (! isset($_REQUEST['save']) || empty($_REQUEST['plugin_name']))
171167
return;
172168

173169
checkSession();
@@ -194,21 +190,22 @@ private function handleToggle(): void
194190
{
195191
global $context, $sourcedir;
196192

197-
if (!isset($_REQUEST['toggle']))
193+
if (! isset($_REQUEST['toggle']))
198194
return;
199195

200196
$input = file_get_contents('php://input');
201197
$data = smf_json_decode($input, true) ?? [];
202198

203-
if (empty($data) || empty($data['status']) || empty($data['plugin']))
199+
if (empty($data) || empty($data['status']) || empty($data['plugin'])) {
204200
redirectexit('action=admin;area=plugins');
201+
}
205202

206203
if ($data['status'] === 'on') {
207204
$context['pl_enabled_plugins'] = array_filter($context['pl_enabled_plugins'], function ($item) use ($data) {
208205
return $item !== $data['plugin'];
209206
});
210207
} else {
211-
if (!empty($context['pl_plugins'][$data['plugin']]['database'])) {
208+
if (! empty($context['pl_plugins'][$data['plugin']]['database'])) {
212209
db_extend('packages');
213210

214211
require PLUGINS_DIR . DIRECTORY_SEPARATOR . $data['plugin'] . DIRECTORY_SEPARATOR . $context['pl_plugins'][$data['plugin']]['database'];
@@ -228,14 +225,15 @@ private function handleRemove(): void
228225
{
229226
global $sourcedir;
230227

231-
if (!isset($_REQUEST['remove']))
228+
if (! isset($_REQUEST['remove']))
232229
return;
233230

234231
$input = file_get_contents('php://input');
235232
$data = smf_json_decode($input, true) ?? [];
236233

237-
if (empty($data) || empty($data['plugin']))
234+
if ($data === [] || empty($data['plugin'])) {
238235
redirectexit('action=admin;area=plugins');
236+
}
239237

240238
require_once $sourcedir . '/Subs-Package.php';
241239

@@ -246,41 +244,43 @@ private function prepareSettings(string $id): void
246244
{
247245
global $context, $txt;
248246

249-
if (!empty($context['pl_plugins'][$id]['settings'])) {
250-
$settings = $this->getSettings(PLUGINS_DIR . DIRECTORY_SEPARATOR . $id);
251-
$languages = $this->getLanguages($id);
247+
if (empty($context['pl_plugins'][$id]['settings']))
248+
return;
249+
250+
$settings = $this->getSettings(PLUGINS_DIR . DIRECTORY_SEPARATOR . $id);
251+
$languages = $this->getLanguages($id);
252252

253-
$options = [];
254-
if (isset($context['pl_plugins'][$id]['settings']['setting']['@attributes'])) {
255-
$option = $context['pl_plugins'][$id]['settings']['setting']['@attributes'];
253+
$options = [];
254+
if (isset($context['pl_plugins'][$id]['settings']['setting']['@attributes'])) {
255+
$option = $context['pl_plugins'][$id]['settings']['setting']['@attributes'];
256+
$options[$option['name']] = [
257+
'name' => $languages[$option['name']] ?? $txt['not_applicable'],
258+
'type' => $option['type'],
259+
'value' => $settings[$option['name']] ?? $option['default'],
260+
];
261+
} else {
262+
foreach ($context['pl_plugins'][$id]['settings']['setting'] as $setting) {
263+
$option = $setting['@attributes'];
256264
$options[$option['name']] = [
257265
'name' => $languages[$option['name']] ?? $txt['not_applicable'],
258266
'type' => $option['type'],
259267
'value' => $settings[$option['name']] ?? $option['default'],
260268
];
261-
} else {
262-
foreach ($context['pl_plugins'][$id]['settings']['setting'] as $setting) {
263-
$option = $setting['@attributes'];
264-
$options[$option['name']] = [
265-
'name' => $languages[$option['name']] ?? $txt['not_applicable'],
266-
'type' => $option['type'],
267-
'value' => $settings[$option['name']] ?? $option['default'],
268-
];
269-
}
270269
}
271-
272-
$context['pl_plugins'][$id]['settings'] = $options;
273270
}
271+
272+
$context['pl_plugins'][$id]['settings'] = $options;
274273
}
275274

276275
private function getSettings(string $path): array
277276
{
278277
return is_file($path . '/settings.ini') ? parse_ini_file($path . '/settings.ini') : [];
279278
}
280279

281-
private function saveSettings(string $plugin, array $settings): void
280+
private function saveSettings(string $plugin, array $settings = []): void
282281
{
283-
if (empty($settings)) return;
282+
if ($settings === [])
283+
return;
284284

285285
$iniString = '';
286286
foreach ($settings as $key => $value) {
@@ -356,7 +356,7 @@ private function extractPackage(): bool
356356
{
357357
global $txt, $context;
358358

359-
if (!isset($_REQUEST['get']))
359+
if (! isset($_REQUEST['get']))
360360
return false;
361361

362362
$package = $_FILES['package'];

Sources/PluginLoader/Plugin.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,27 @@
66
* @package Plugin Loader
77
* @link https://github.com/dragomano/Plugin-Loader
88
* @author Bugo <bugo@dragomano.ru>
9-
* @copyright 2023 Bugo
9+
* @copyright 2023-2024 Bugo
1010
* @license https://opensource.org/licenses/BSD-3-Clause The 3-Clause BSD License
1111
*
12-
* @version 0.5
12+
* @version 0.6
1313
*/
1414

1515
namespace Bugo\PluginLoader;
1616

1717
use MatthiasMullie\Minify\CSS;
1818
use MatthiasMullie\Minify\JS;
1919

20-
if (!defined('SMF'))
20+
if (! defined('SMF'))
2121
die('No direct access...');
2222

2323
abstract class Plugin
2424
{
25-
protected string $name;
25+
use HasInvoke;
2626

27-
protected array $txt;
28-
29-
public function __construct()
30-
{
31-
$this->name = $this->getName();
32-
}
27+
protected const NAME = '';
3328

34-
abstract public function getName();
35-
36-
abstract public function hooks();
29+
protected array $txt;
3730

3831
protected function loadLanguage(string $lang_name = ''): void
3932
{
@@ -48,8 +41,9 @@ protected function loadLanguage(string $lang_name = ''): void
4841
$pluginLanguages[$language] = is_file($langFile) ? require_once $langFile : [];
4942
}
5043

51-
if (is_array($pluginLanguages['english']))
44+
if (is_array($pluginLanguages['english'])) {
5245
$this->txt = array_merge($pluginLanguages['english'], $pluginLanguages[$lang]);
46+
}
5347
}
5448

5549
protected function loadTemplate(string $template_name): void
@@ -64,15 +58,15 @@ protected function loadCSS(string $css_name, string $extension = '.css'): void
6458
$css_name = str_replace($extension, '', $css_name) . $extension;
6559

6660
$source_file = $this->getPath() . '/styles/' . $css_name;
67-
$target_file = $settings['default_theme_dir'] . '/css/' . $this->name . '_' . $css_name;
61+
$target_file = $settings['default_theme_dir'] . '/css/' . static::NAME . '_' . $css_name;
6862

69-
if (!is_file($target_file) || filemtime($target_file) < filemtime($source_file)) {
63+
if (! is_file($target_file) || filemtime($target_file) < filemtime($source_file)) {
7064
$css = new CSS;
7165
$css->add($source_file);
7266
$css->minify($target_file);
7367
}
7468

75-
loadCSSFile($this->name . '_' . $css_name);
69+
loadCSSFile(static::NAME . '_' . $css_name);
7670
}
7771

7872
protected function loadJS(string $js_name, string $extension = '.js'): void
@@ -82,15 +76,15 @@ protected function loadJS(string $js_name, string $extension = '.js'): void
8276
$js_name = str_replace($extension, '', $js_name) . $extension;
8377

8478
$source_file = $this->getPath() . '/scripts/' . $js_name;
85-
$target_file = $settings['default_theme_dir'] . '/scripts/' . $this->name . '_' . $js_name;
79+
$target_file = $settings['default_theme_dir'] . '/scripts/' . static::NAME . '_' . $js_name;
8680

87-
if (!is_file($target_file) || filemtime($target_file) < filemtime($source_file)) {
81+
if (! is_file($target_file) || filemtime($target_file) < filemtime($source_file)) {
8882
$js = new JS;
8983
$js->add($source_file);
9084
$js->minify($target_file);
9185
}
9286

93-
loadJavaScriptFile($this->name . '_' . $js_name, ['minimize' => true]);
87+
loadJavaScriptFile(static::NAME . '_' . $js_name, ['minimize' => true]);
9488
}
9589

9690
protected function loadSource(string $source_name): void
@@ -100,19 +94,19 @@ protected function loadSource(string $source_name): void
10094

10195
protected function getUrl(string $sub_directory = ''): string
10296
{
103-
return PLUGINS_URL . '/' . $this->name . '/' . ($sub_directory ? $sub_directory . '/' : '');
97+
return PLUGINS_URL . '/' . static::NAME . '/' . ($sub_directory ? $sub_directory . '/' : '');
10498
}
10599

106100
protected function getPath(): string
107101
{
108-
return PLUGINS_DIR . DIRECTORY_SEPARATOR . $this->name;
102+
return PLUGINS_DIR . DIRECTORY_SEPARATOR . static::NAME;
109103
}
110104

111-
protected function getSettings(string $key, $default = null)
105+
protected function getSettings(string $key = '', $default = null)
112106
{
113107
$settings = parse_ini_file($this->getPath() . '/settings.ini');
114108

115-
if (empty($key)) {
109+
if ($key === '') {
116110
return $settings;
117111
}
118112

0 commit comments

Comments
 (0)