1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Events\Package; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Contracts\Application\ContainerConfiguratorInterface; |
22
|
|
|
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface; |
23
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
24
|
|
|
use Limoncello\Events\Contracts\EventDispatcherInterface; |
25
|
|
|
use Limoncello\Events\Contracts\EventEmitterInterface; |
26
|
|
|
use Limoncello\Events\Package\EventSettings as C; |
27
|
|
|
use Limoncello\Events\SimpleEventEmitter; |
28
|
|
|
use Psr\Container\ContainerInterface as PsrContainerInterface; |
29
|
|
|
use function call_user_func; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @package Limoncello\Events |
33
|
|
|
*/ |
34
|
|
|
class EventsContainerConfigurator implements ContainerConfiguratorInterface |
35
|
|
|
{ |
36
|
|
|
/** @var callable */ |
37
|
|
|
const CONFIGURATOR = [self::class, self::CONTAINER_METHOD_NAME]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
1 |
|
public static function configureContainer(LimoncelloContainerInterface $container): void |
43
|
|
|
{ |
44
|
1 |
|
$emitter = null; |
45
|
|
|
$getOrCreateEmitter = function (PsrContainerInterface $container) use (&$emitter): SimpleEventEmitter { |
46
|
1 |
|
if ($emitter === null) { |
47
|
1 |
|
$emitter = new SimpleEventEmitter(); |
48
|
1 |
|
$cacheData = $container->get(SettingsProviderInterface::class)->get(C::class)[C::KEY_CACHED_DATA]; |
49
|
1 |
|
$emitter->setData($cacheData); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return $emitter; |
53
|
1 |
|
}; |
54
|
|
|
|
55
|
1 |
|
$container[EventEmitterInterface::class] = |
56
|
|
|
function (PsrContainerInterface $container) use ($getOrCreateEmitter): EventEmitterInterface { |
57
|
1 |
|
return call_user_func($getOrCreateEmitter, $container); |
58
|
|
|
}; |
59
|
|
|
|
60
|
1 |
|
$container[EventDispatcherInterface::class] = |
61
|
|
|
function (PsrContainerInterface $container) use ($getOrCreateEmitter): EventDispatcherInterface { |
62
|
1 |
|
return call_user_func($getOrCreateEmitter, $container); |
63
|
|
|
}; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|