Skip to content

Commit 5a99ba1

Browse files
authored
Use PSR-14 events to replace hook infrastructure (simplesamlphp#2560)
* Replace hook infrastructure with PSR-14 events * fix lookup for module event listeners * remove redunant registration of module event listeners * use correct config key to resolve enabled modules * fix template reference * add missing event variable type hints * refactor: remove event dispatcher property and instantiate it locally * refactor: move enabled modules discovery to ModuleListenerProvider
1 parent 61559a7 commit 5a99ba1

22 files changed

Lines changed: 540 additions & 117 deletions

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"gettext/gettext": "~5.7.3",
6767
"gettext/translator": "~1.2.1",
6868
"phpmailer/phpmailer": "~6.12.0",
69+
"psr/event-dispatcher": "~1.0.0",
6970
"psr/log": "~3.0.2",
7071
"robrichards/xmlseclibs": "~3.1.3",
7172
"simplesamlphp/assert": "^1.9",
@@ -79,6 +80,7 @@
7980
"symfony/config": "~7.3.0",
8081
"symfony/console": "~7.3.0",
8182
"symfony/dependency-injection": "~7.3.0",
83+
"symfony/event-dispatcher": "~7.3.0",
8284
"symfony/expression-language": "~7.3.0",
8385
"symfony/filesystem": "~7.3.0",
8486
"symfony/finder": "~7.3.0",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/admin/src/Controller/Config.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
namespace SimpleSAML\Module\admin\Controller;
66

77
use SimpleSAML\Configuration;
8+
use SimpleSAML\Event\Dispatcher\ModuleEventDispatcherFactory;
89
use SimpleSAML\Locale\Translate;
910
use SimpleSAML\Module;
11+
use SimpleSAML\Module\admin\Event\ConfigPageEvent;
12+
use SimpleSAML\Module\admin\Event\SanityCheckEvent;
1013
use SimpleSAML\Session;
1114
use SimpleSAML\Utils;
1215
use SimpleSAML\XHTML\Template;
@@ -162,6 +165,10 @@ public function main(/** @scrutinizer ignore-unused */ Request $request): Respon
162165
'modulelist' => $this->getModuleList(),
163166
];
164167

168+
$eventDispatcher = ModuleEventDispatcherFactory::getInstance();
169+
/** @var CronEvent $event */
170+
$event = $eventDispatcher->dispatch(new ConfigPageEvent($t));
171+
$t = $event->getTemplate();
165172
Module::callHooks('configpage', $t);
166173
$this->menu->addOption('logout', $this->authUtils->getAdminLogoutURL(), Translate::noop('Log out'));
167174
return $this->menu->insert($t);
@@ -398,8 +405,25 @@ protected function getPrerequisiteChecks(): array
398405

399406

400407
// Add module specific checks via the sanitycheck hook that a module can provide.
408+
$eventDispatcher = ModuleEventDispatcherFactory::getInstance();
409+
/** @var SanityCheckEvent $event */
410+
$event = $eventDispatcher->dispatch(new SanityCheckEvent());
411+
401412
$hookinfo = [ 'info' => [], 'errors' => [] ];
402413
Module::callHooks('sanitycheck', $hookinfo);
414+
415+
// Merge results from the event into $hookinfo. Can be removed when hook infrastructure is removed.
416+
$hookinfo = [
417+
'info' => array_merge(
418+
$event->getInfo(),
419+
$hookinfo['info'],
420+
),
421+
'errors' => array_merge(
422+
$event->getErrors(),
423+
$hookinfo['errors'],
424+
),
425+
];
426+
403427
foreach (['info', 'errors'] as $resulttype) {
404428
foreach ($hookinfo[$resulttype] as $result) {
405429
$matrix[] = [

modules/admin/src/Controller/Federation.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\Auth;
1010
use SimpleSAML\Configuration;
11+
use SimpleSAML\Event\Dispatcher\ModuleEventDispatcherFactory;
1112
use SimpleSAML\Locale\Translate;
1213
use SimpleSAML\Logger;
1314
use SimpleSAML\Metadata\MetaDataStorageHandler;
@@ -16,6 +17,7 @@
1617
use SimpleSAML\Metadata\Signer;
1718
use SimpleSAML\Module;
1819
use SimpleSAML\Module\adfs\IdP\ADFS as ADFS_IdP;
20+
use SimpleSAML\Module\admin\Event\FederationPageEvent;
1921
use SimpleSAML\Module\saml\IdP\SAML2 as SAML2_IdP;
2022
use SimpleSAML\SAML2\Constants as C;
2123
use SimpleSAML\SAML2\Exception\ArrayValidationException;
@@ -27,6 +29,7 @@
2729
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
2830
use Symfony\Component\VarExporter\VarExporter;
2931

32+
3033
use function array_merge;
3134
use function array_pop;
3235
use function array_values;
@@ -190,6 +193,11 @@ public function main(/** @scrutinizer ignore-unused */ Request $request): Respon
190193
'logouturl' => $this->authUtils->getAdminLogoutURL(),
191194
];
192195

196+
$eventDispatcher = ModuleEventDispatcherFactory::getInstance();
197+
/** @var FederationPageEvent $event */
198+
$event = $eventDispatcher->dispatch(new FederationPageEvent($t));
199+
$t = $event->getTemplate();
200+
193201
Module::callHooks('federationpage', $t);
194202
Assert::isInstanceOf($t, Template::class);
195203

modules/admin/src/Controller/Menu.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace SimpleSAML\Module\admin\Controller;
66

77
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\Event\Dispatcher\ModuleEventDispatcherFactory;
89
use SimpleSAML\Locale\Translate;
910
use SimpleSAML\Module;
11+
use SimpleSAML\Module\admin\Event\AdminMenuEvent;
1012
use SimpleSAML\XHTML\Template;
1113

1214
/**
@@ -92,6 +94,10 @@ public function addOption(string $id, string $url, string $name): void
9294
public function insert(Template $template): Template
9395
{
9496
$template->data['menu'] = $this->options;
97+
$eventDispatcher = ModuleEventDispatcherFactory::getInstance();
98+
/** @var AdminMenuEvent $event */
99+
$event = $eventDispatcher->dispatch(new AdminMenuEvent($template));
100+
$template = $event->getTemplate();
95101
Module::callHooks('adminmenu', $template);
96102

97103
Assert::isInstanceOf($template, Template::class);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Module\admin\Event;
6+
7+
use SimpleSAML\XHTML;
8+
9+
class AdminMenuEvent
10+
{
11+
private XHTML\Template $template;
12+
13+
public function __construct(XHTML\Template $template)
14+
{
15+
$this->template = $template;
16+
}
17+
18+
public function getTemplate(): XHTML\Template
19+
{
20+
return $this->template;
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Module\admin\Event;
6+
7+
use SimpleSAML\XHTML;
8+
9+
class ConfigPageEvent
10+
{
11+
public function __construct(
12+
private readonly XHTML\Template $template,
13+
)
14+
{}
15+
16+
public function getTemplate(): XHTML\Template
17+
{
18+
return $this->template;
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Module\admin\Event;
6+
7+
use SimpleSAML\XHTML;
8+
9+
class FederationPageEvent
10+
{
11+
public function __construct(
12+
private readonly XHTML\Template $template,
13+
) {}
14+
15+
public function getTemplate(): XHTML\Template
16+
{
17+
return $this->template;
18+
}
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Module\admin\Event;
6+
7+
class SanityCheckEvent
8+
{
9+
private array $info = [];
10+
private array $errors = [];
11+
12+
public function __construct() {
13+
}
14+
15+
public function addInfo(string $message): void
16+
{
17+
$this->info[] = $message;
18+
}
19+
20+
public function addError(string $message): void
21+
{
22+
$this->errors[] = $message;
23+
}
24+
25+
public function getInfo(): array
26+
{
27+
return $this->info;
28+
}
29+
30+
public function getErrors(): array
31+
{
32+
return $this->errors;
33+
}
34+
}

modules/cron/hooks/hook_configpage.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)