1
|
|
|
<?php |
2
|
|
|
namespace ZfcUser\EventManager; |
3
|
|
|
use Traversable; |
4
|
|
|
use Zend\EventManager\EventManagerAwareInterface; |
5
|
|
|
use Zend\EventManager\EventManagerInterface; |
6
|
|
|
use Zend\EventManager\EventManager; |
7
|
|
|
use Zend\EventManager\SharedEventManager; |
8
|
|
|
|
9
|
|
|
abstract class EventProvider implements EventManagerAwareInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var EventManagerInterface |
13
|
|
|
*/ |
14
|
|
|
protected $events; |
15
|
|
|
/** |
16
|
|
|
* Set the event manager instance used by this context |
17
|
|
|
* |
18
|
|
|
* @param EventManagerInterface $events |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
public function setEventManager(EventManagerInterface $events) |
22
|
|
|
{ |
23
|
|
|
$identifiers = array(__CLASS__, get_called_class()); |
24
|
|
|
if (isset($this->eventIdentifier)) { |
25
|
|
|
if ((is_string($this->eventIdentifier)) |
26
|
|
|
|| (is_array($this->eventIdentifier)) |
27
|
|
|
|| ($this->eventIdentifier instanceof Traversable) |
28
|
|
|
) { |
29
|
|
|
$identifiers = array_unique(array_merge($identifiers, (array) $this->eventIdentifier)); |
30
|
|
|
} elseif (is_object($this->eventIdentifier)) { |
|
|
|
|
31
|
|
|
$identifiers[] = $this->eventIdentifier; |
32
|
|
|
} |
33
|
|
|
// silently ignore invalid eventIdentifier types |
34
|
|
|
} |
35
|
|
|
$events->setIdentifiers($identifiers); |
36
|
|
|
$this->events = $events; |
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
/** |
40
|
|
|
* Retrieve the event manager |
41
|
|
|
* |
42
|
|
|
* Lazy-loads an EventManager instance if none registered. |
43
|
|
|
* |
44
|
|
|
* @return EventManagerInterface |
45
|
|
|
*/ |
46
|
|
|
public function getEventManager() |
47
|
|
|
{ |
48
|
|
|
if (!$this->events instanceof EventManagerInterface) { |
49
|
|
|
$this->setEventManager(new EventManager(new SharedEventManager())); |
50
|
|
|
} |
51
|
|
|
return $this->events; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
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: