|
1
|
|
|
<?php |
|
2
|
|
|
namespace LmcUser\EventManager; |
|
3
|
|
|
|
|
4
|
|
|
use Traversable; |
|
5
|
|
|
use Laminas\EventManager\EventManagerAwareInterface; |
|
6
|
|
|
use Laminas\EventManager\EventManagerInterface; |
|
7
|
|
|
use Laminas\EventManager\EventManager; |
|
8
|
|
|
use Laminas\EventManager\SharedEventManager; |
|
9
|
|
|
|
|
10
|
|
|
abstract class EventProvider implements EventManagerAwareInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var EventManagerInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $events; |
|
16
|
|
|
/** |
|
17
|
|
|
* Set the event manager instance used by this context |
|
18
|
|
|
* |
|
19
|
|
|
* @param EventManagerInterface $events |
|
20
|
|
|
* @return mixed |
|
21
|
|
|
*/ |
|
22
|
|
|
public function setEventManager(EventManagerInterface $events) |
|
23
|
|
|
{ |
|
24
|
|
|
$identifiers = array(__CLASS__, get_called_class()); |
|
25
|
|
|
if (isset($this->eventIdentifier)) { |
|
26
|
|
|
if ((is_string($this->eventIdentifier)) |
|
27
|
|
|
|| (is_array($this->eventIdentifier)) |
|
28
|
|
|
|| ($this->eventIdentifier instanceof Traversable) |
|
29
|
|
|
) { |
|
30
|
|
|
$identifiers = array_unique(array_merge($identifiers, (array) $this->eventIdentifier)); |
|
31
|
|
|
} elseif (is_object($this->eventIdentifier)) { |
|
|
|
|
|
|
32
|
|
|
$identifiers[] = $this->eventIdentifier; |
|
33
|
|
|
} |
|
34
|
|
|
// silently ignore invalid eventIdentifier types |
|
35
|
|
|
} |
|
36
|
|
|
$events->setIdentifiers($identifiers); |
|
37
|
|
|
$this->events = $events; |
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
/** |
|
41
|
|
|
* Retrieve the event manager |
|
42
|
|
|
* |
|
43
|
|
|
* Lazy-loads an EventManager instance if none registered. |
|
44
|
|
|
* |
|
45
|
|
|
* @return EventManagerInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getEventManager() |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$this->events instanceof EventManagerInterface) { |
|
50
|
|
|
$this->setEventManager(new EventManager()); |
|
51
|
|
|
} |
|
52
|
|
|
return $this->events; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: