Bitrix-D7  20.0.0
main/lib/event.php
См. документацию.
1 <?php
2 namespace Bitrix\Main;
3 
4 class Event
5 {
6  protected $moduleId;
7  protected $type;
8  protected $parameters = array();
9  /** @var callable */
10  protected $parametersLoader = null;
11  protected $filter = null;
12  protected $sender = null;
13 
14  protected $debugMode = false;
15  protected $debugInfo = array();
16 
17  /** @var EventResult[] */
18  protected $results = array();
19 
20  /** @var \Exception[] */
21  protected $exceptions = array();
22 
23  /**
24  * @param $moduleId
25  * @param $type
26  * @param array $parameters
27  * @param null|string|string[] $filter Filter of module names, mail event names and component names of the event handlers
28  */
29  public function __construct($moduleId, $type, $parameters = array(), $filter = null)
30  {
31  $this->moduleId = $moduleId;
32  $this->type = $type;
33  $this->setParameters($parameters);
34  $this->setFilter($filter);
35 
36  $this->debugMode = false;
37  }
38 
39  public function getModuleId()
40  {
41  return $this->moduleId;
42  }
43 
44  public function getEventType()
45  {
46  return $this->type;
47  }
48 
49  public function setParameters($parameters)
50  {
51  if (is_array($parameters))
52  {
53  $this->parameters = $parameters;
54  }
55  elseif ($parameters instanceof \Closure)
56  {
57  $this->parameters = null;
58  $this->parametersLoader = $parameters;
59  }
60  else
61  {
62  throw new ArgumentTypeException("parameter", "array or closure, which returns array");
63  }
64  }
65 
66  public function getParameters()
67  {
68  $this->loadParameters();
69 
70  return $this->parameters;
71  }
72 
73  public function setParameter($key, $value)
74  {
75  $this->loadParameters();
76 
77  $this->parameters[$key] = $value;
78  }
79 
80  public function getParameter($key)
81  {
82  $this->loadParameters();
83 
84  if (isset($this->parameters[$key]))
85  return $this->parameters[$key];
86 
87  return null;
88  }
89 
90  protected function loadParameters()
91  {
92  if (!$this->parametersLoader)
93  {
94  return false;
95  }
96 
97  $this->setParameters(call_user_func_array($this->parametersLoader, array()));
98  $this->parametersLoader = null;
99 
100  return true;
101  }
102 
103  public function setFilter($filter)
104  {
105  if (!is_array($filter))
106  {
107  if (empty($filter))
108  $filter = null;
109  else
110  $filter = array($filter);
111  }
112 
113  $this->filter = $filter;
114  }
115 
116  public function getFilter()
117  {
118  return $this->filter;
119  }
120 
121  /**
122  * @return EventResult[]
123  */
124  public function getResults()
125  {
126  return $this->results;
127  }
128 
129  public function addResult(EventResult $result)
130  {
131  $this->results[] = $result;
132  }
133 
134  public function getSender()
135  {
136  return $this->sender;
137  }
138 
139  public function send($sender = null)
140  {
141  $this->sender = $sender;
142  EventManager::getInstance()->send($this);
143  }
144 
145  public function addException(\Exception $exception)
146  {
147  $this->exceptions[] = $exception;
148  }
149 
150  public function getExceptions()
151  {
152  return $this->exceptions;
153  }
154 
155  public function turnDebugOn()
156  {
157  $this->debugMode = true;
158  }
159 
160  public function isDebugOn()
161  {
162  return $this->debugMode;
163  }
164 
165  public function addDebugInfo($ar)
166  {
167  if (!$this->debugMode)
168  return;
169 
170  $this->debugInfo[] = $ar;
171  }
172 
173  public function getDebugInfo()
174  {
175  return $this->debugInfo;
176  }
177 }
Bitrix\Main\Event\getModuleId
getModuleId()
Definition: main/lib/event.php:39
Bitrix\Main\Event\__construct
__construct($moduleId, $type, $parameters=array(), $filter=null)
Definition: main/lib/event.php:29
Bitrix\Main\Event\getSender
getSender()
Definition: main/lib/event.php:134
Bitrix\Main\EventResult
Definition: main/lib/eventresult.php:4
Bitrix\Main\Event\getExceptions
getExceptions()
Definition: main/lib/event.php:150
Bitrix\Main
Bitrix\Main\Event\getResults
getResults()
Definition: main/lib/event.php:124
Bitrix\Main\Event\$results
$results
Definition: main/lib/event.php:18
Bitrix\Main\Event\$filter
$filter
Definition: main/lib/event.php:11
Bitrix\Main\Event\getDebugInfo
getDebugInfo()
Definition: main/lib/event.php:173
Bitrix\Main\Event\addResult
addResult(EventResult $result)
Definition: main/lib/event.php:129
Bitrix\Main\EventManager\getInstance
static getInstance()
Definition: main/lib/eventmanager.php:34
Bitrix\Main\Event\$exceptions
$exceptions
Definition: main/lib/event.php:21
Bitrix\Main\Event\isDebugOn
isDebugOn()
Definition: main/lib/event.php:160
Bitrix\Main\ArgumentTypeException
Exception is thrown when the type of an argument is not accepted by function.
Definition: main/lib/exception.php:113
Bitrix\Main\Event\$parametersLoader
$parametersLoader
Definition: main/lib/event.php:10
Bitrix\Main\Event
Definition: main/lib/event.php:4
Bitrix\Main\Event\getParameters
getParameters()
Definition: main/lib/event.php:66
Bitrix\Main\Event\$debugMode
$debugMode
Definition: main/lib/event.php:14
Bitrix\Main\Event\setParameter
setParameter($key, $value)
Definition: main/lib/event.php:73
Bitrix\Main\Event\getEventType
getEventType()
Definition: main/lib/event.php:44
Bitrix\Main\Event\setFilter
setFilter($filter)
Definition: main/lib/event.php:103
Bitrix\Main\Event\loadParameters
loadParameters()
Definition: main/lib/event.php:90
Bitrix\Main\Event\$sender
$sender
Definition: main/lib/event.php:12
Bitrix\Main\Event\getParameter
getParameter($key)
Definition: main/lib/event.php:80
Bitrix\Main\Event\getFilter
getFilter()
Definition: main/lib/event.php:116
Bitrix\Main\Event\addDebugInfo
addDebugInfo($ar)
Definition: main/lib/event.php:165
Bitrix\Main\Event\$parameters
$parameters
Definition: main/lib/event.php:8
Bitrix\Main\Event\send
send($sender=null)
Definition: main/lib/event.php:139
Bitrix\Main\Event\turnDebugOn
turnDebugOn()
Definition: main/lib/event.php:155
Bitrix\Main\Event\addException
addException(\Exception $exception)
Definition: main/lib/event.php:145
Bitrix\Main\Event\setParameters
setParameters($parameters)
Definition: main/lib/event.php:49
Bitrix\Main\Event\$moduleId
$moduleId
Definition: main/lib/event.php:6
Bitrix\Main\Event\$debugInfo
$debugInfo
Definition: main/lib/event.php:15
Bitrix\Main\Event\$type
$type
Definition: main/lib/event.php:7