1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* Contains PimpleContainerMediator class. |
5
|
|
|
* |
6
|
|
|
* PHP version 7.0 |
7
|
|
|
* |
8
|
|
|
* LICENSE: |
9
|
|
|
* This file is part of Event Mediator - A general event mediator (dispatcher) |
10
|
|
|
* which has minimal dependencies so it is easy to drop in and use. |
11
|
|
|
* Copyright (C) 2015-2016 Michael Cummings |
12
|
|
|
* |
13
|
|
|
* This program is free software; you can redistribute it and/or modify it |
14
|
|
|
* under the terms of the GNU General Public License as published by the Free |
15
|
|
|
* Software Foundation; version 2 of the License. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
18
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
19
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
20
|
|
|
* details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU General Public License along with |
23
|
|
|
* this program; if not, you may write to |
24
|
|
|
* |
25
|
|
|
* Free Software Foundation, Inc. |
26
|
|
|
* 59 Temple Place, Suite 330 |
27
|
|
|
* Boston, MA 02111-1307 USA |
28
|
|
|
* |
29
|
|
|
* or find a electronic copy at |
30
|
|
|
* <http://spdx.org/licenses/GPL-2.0.html>. |
31
|
|
|
* |
32
|
|
|
* You should also be able to find a copy of this license in the included |
33
|
|
|
* LICENSE file. |
34
|
|
|
* |
35
|
|
|
* @author Michael Cummings <[email protected]> |
36
|
|
|
* @copyright 2015-2016 Michael Cummings |
37
|
|
|
* @license GPL-2.0 |
38
|
|
|
*/ |
39
|
|
|
namespace EventMediator; |
40
|
|
|
|
41
|
|
|
use Pimple\Container; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Class PimpleContainerMediator |
45
|
|
|
* |
46
|
|
|
* @link http://pimple.sensiolabs.org/ Pimple |
47
|
|
|
*/ |
48
|
|
|
class PimpleContainerMediator extends AbstractContainerMediator |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* @param Container|null $serviceContainer |
52
|
|
|
* |
53
|
|
|
* @throws \InvalidArgumentException |
54
|
|
|
*/ |
55
|
26 |
|
public function __construct(Container $serviceContainer = \null) |
56
|
|
|
{ |
57
|
26 |
|
$this->setServiceContainer($serviceContainer); |
58
|
|
|
} |
59
|
|
|
/** @noinspection GenericObjectTypeUsageInspection */ |
60
|
|
|
/** |
61
|
|
|
* This method is used any time the mediator need to get the actual instance |
62
|
|
|
* of the class for an event. |
63
|
|
|
* |
64
|
|
|
* Normal will only be called during actual trigger of an event since lazy |
65
|
|
|
* loading is used. |
66
|
|
|
* |
67
|
|
|
* @param string $serviceName |
68
|
|
|
* |
69
|
|
|
* @return object |
70
|
|
|
* @throws \LogicException |
71
|
|
|
*/ |
72
|
3 |
|
public function getServiceByName(string $serviceName) |
73
|
|
|
{ |
74
|
3 |
|
return $this->getServiceContainer()[$serviceName]; |
75
|
|
|
} |
76
|
|
|
/** |
77
|
|
|
* This is used to bring in the service container that will be used. |
78
|
|
|
* |
79
|
|
|
* Though not required it would be considered best practice for this method |
80
|
|
|
* to create a new instance of the container when given null. Another good |
81
|
|
|
* practice is to call this method from the class constructor to allow |
82
|
|
|
* easier testing. |
83
|
|
|
* |
84
|
|
|
* @param Container|null $value |
85
|
|
|
* |
86
|
|
|
* @return ContainerMediatorInterface Fluent interface. |
87
|
|
|
* @throws \InvalidArgumentException |
88
|
|
|
* |
89
|
|
|
* @link http://pimple.sensiolabs.org/ Pimple |
90
|
|
|
*/ |
91
|
26 |
|
public function setServiceContainer($value = \null): ContainerMediatorInterface |
92
|
|
|
{ |
93
|
26 |
|
if (\null === $value) { |
94
|
26 |
|
$value = new Container(); |
95
|
|
|
} |
96
|
26 |
|
if (!$value instanceof Container) { |
97
|
1 |
|
$mess = \sprintf( |
98
|
1 |
|
'Must be an instance of Pimple Container but given %s', |
99
|
1 |
|
\gettype($value) |
100
|
|
|
); |
101
|
1 |
|
throw new \InvalidArgumentException($mess); |
102
|
|
|
} |
103
|
26 |
|
$this->serviceContainer = $value; |
104
|
26 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
/** |
107
|
|
|
* Used to get the service container. |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
3 |
|
private function getServiceContainer() |
112
|
|
|
{ |
113
|
3 |
|
return $this->serviceContainer; |
114
|
|
|
} |
115
|
|
|
/** |
116
|
|
|
* Holds the container instance to be used. |
117
|
|
|
* |
118
|
|
|
* @var mixed $serviceContainer |
119
|
|
|
*/ |
120
|
|
|
private $serviceContainer; |
121
|
|
|
} |
122
|
|
|
|