-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBetterWPHooksBundle.php
95 lines (77 loc) · 2.89 KB
/
BetterWPHooksBundle.php
1
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
namespace Snicco\Bundle\BetterWPHooks;
use LogicException;
use Psr\EventDispatcher\EventDispatcherInterface;
use Snicco\Component\BetterWPHooks\EventMapping\EventMapper;
use Snicco\Component\BetterWPHooks\WPEventDispatcher;
use Snicco\Component\BetterWPHooks\WPHookAPI;
use Snicco\Component\EventDispatcher\BaseEventDispatcher;
use Snicco\Component\EventDispatcher\EventDispatcher;
use Snicco\Component\EventDispatcher\ListenerFactory\PsrListenerFactory;
use Snicco\Component\EventDispatcher\Testing\TestableEventDispatcher;
use Snicco\Component\Kernel\Bundle;
use Snicco\Component\Kernel\Configuration\WritableConfig;
use Snicco\Component\Kernel\Kernel;
use Snicco\Component\Kernel\ValueObject\Environment;
use function class_exists;
final class BetterWPHooksBundle implements Bundle
{
/**
* @var string
*/
public const ALIAS = 'snicco/better-wp-hooks-bundle';
public function shouldRun(Environment $env): bool
{
return true;
}
public function configure(WritableConfig $config, Kernel $kernel): void
{
}
public function register(Kernel $kernel): void
{
$container = $kernel->container();
$hook_api = new WPHookAPI();
$container->shared(
EventDispatcher::class,
function () use ($kernel, $container, $hook_api): EventDispatcherInterface {
$listener_factory = new PsrListenerFactory($container);
$dispatcher = new WPEventDispatcher(new BaseEventDispatcher($listener_factory), $hook_api);
if ($kernel->env()->isTesting() && class_exists(TestableEventDispatcher::class)) {
$dispatcher = new TestableEventDispatcher($dispatcher);
}
return $dispatcher;
}
);
$container->shared(
EventDispatcherInterface::class,
fn (): EventDispatcher => $container->make(EventDispatcher::class)
);
$container->shared(
EventMapper::class,
fn (): EventMapper => new EventMapper($container->make(EventDispatcher::class), $hook_api)
);
if (! $kernel->env()->isTesting()) {
return;
}
if (! class_exists(TestableEventDispatcher::class)) {
return;
}
$container->shared(TestableEventDispatcher::class, function () use ($container): TestableEventDispatcher {
$dispatcher = $container->make(EventDispatcher::class);
if (! $dispatcher instanceof TestableEventDispatcher) {
throw new LogicException(
'The testable event dispatcher did not get bound correctly. This should never happen.'
);
}
return $dispatcher;
});
}
public function bootstrap(Kernel $kernel): void
{
}
public function alias(): string
{
return self::ALIAS;
}
}