|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\EventDispatcher; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\EventDispatcher\Discovery\RegisterDiscoveredEvents; |
|
6
|
|
|
use ByTIC\EventDispatcher\Dispatcher\EventDispatcher; |
|
7
|
|
|
use ByTIC\EventDispatcher\Dispatcher\EventDispatcherInterface; |
|
8
|
|
|
use Nip\Container\ServiceProviders\Providers\AbstractSignatureServiceProvider; |
|
9
|
|
|
use Nip\Container\ServiceProviders\Providers\BootableServiceProviderInterface; |
|
10
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface as PsrEventDispatcherInterface; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class EventServiceProvider |
|
15
|
|
|
* @package ByTIC\EventDispatcher |
|
16
|
|
|
*/ |
|
17
|
|
|
class EventServiceProvider extends AbstractSignatureServiceProvider implements BootableServiceProviderInterface |
|
18
|
|
|
{ |
|
19
|
3 |
|
public const DIPATCHER_NAME = 'events'; |
|
20
|
|
|
|
|
21
|
3 |
|
/** |
|
22
|
3 |
|
* @inheritdoc |
|
23
|
3 |
|
*/ |
|
24
|
|
|
public function register() |
|
25
|
3 |
|
{ |
|
26
|
|
|
$this->registerDispatcher(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
3 |
|
public function boot() |
|
30
|
3 |
|
{ |
|
31
|
|
|
$basePath = $this->getContainer()->get('app'); |
|
32
|
3 |
|
$basePath .= DIRECTORY_SEPARATOR . 'Listeners'; |
|
33
|
|
|
if (false === is_dir($basePath)) { |
|
34
|
3 |
|
return; |
|
35
|
3 |
|
} |
|
36
|
|
|
$dispatcher = $this->getContainer()->get(static::DIPATCHER_NAME); |
|
37
|
|
|
|
|
38
|
|
|
$provider = new RegisterDiscoveredEvents($dispatcher); |
|
39
|
2 |
|
$provider->addDiscoveryPath($basePath); |
|
40
|
3 |
|
$provider->register(); |
|
41
|
3 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function registerDispatcher() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->getContainer()->share('events', function () { |
|
46
|
|
|
return new EventDispatcher(); |
|
47
|
|
|
}); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public function provides(): array |
|
55
|
|
|
{ |
|
56
|
|
|
return [ |
|
57
|
|
|
self::DIPATCHER_NAME, |
|
58
|
|
|
EventDispatcherInterface::class, |
|
59
|
|
|
PsrEventDispatcherInterface::class, |
|
60
|
|
|
SymfonyEventDispatcherInterface::class |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|