-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathCollection.php
130 lines (119 loc) · 3 KB
/
Collection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Collection of events
*
* @author Magento Core Team <core@magentocommerce.com>
*/
namespace Magento\Framework\Event;
use Magento\Framework\Event;
class Collection
{
/**
* Array of events in the collection
*
* @var array
*/
protected $events;
/**
* Global observers
*
* For example regex observers will watch all events that
*
* @var Observer\Collection
*/
protected $globalObservers;
/**
* Initializes global observers collection
*
* @param array $events
* @param Observer\Collection $observerCollection
*/
public function __construct(array $events = [], Observer\Collection $observerCollection = null)
{
$this->events = $events;
$this->globalObservers = !$observerCollection ? new Observer\Collection() : $observerCollection;
}
/**
* Returns all registered events in collection
*
* @return array
*/
public function getAllEvents()
{
return $this->events;
}
/**
* Returns all registered global observers for the collection of events
*
* @return Observer\Collection
*/
public function getGlobalObservers()
{
return $this->globalObservers;
}
/**
* Returns event by its name
*
* If event doesn't exist creates new one and returns it
*
* @param string $eventName
* @return Event
*/
public function getEventByName($eventName)
{
if (!isset($this->events[$eventName])) {
$this->addEvent(new Event(['name' => $eventName]));
}
return $this->events[$eventName];
}
/**
* Register an event for this collection
*
* @param Event $event
* @return $this
*/
public function addEvent(Event $event)
{
$this->events[$event->getName()] = $event;
return $this;
}
/**
* Register an observer
*
* If observer has event_name property it will be registered for this specific event.
* If not it will be registered as global observer
*
* @param Observer $observer
* @return $this
*/
public function addObserver(Observer $observer)
{
$eventName = $observer->getEventName();
if ($eventName) {
$this->getEventByName($eventName)->addObserver($observer);
} else {
$this->getGlobalObservers()->addObserver($observer);
}
return $this;
}
/**
* Dispatch event name with optional data
*
* Will dispatch specific event and will try all global observers
*
* @param string $eventName
* @param array $data
* @return $this
*/
public function dispatch($eventName, array $data = [])
{
$event = $this->getEventByName($eventName);
$event->addData($data)->dispatch();
$this->getGlobalObservers()->dispatch($event);
return $this;
}
}