Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
listener.php
1<?php
9
16
17Loc::loadMessages(__FILE__);
18
19class Listener extends Base
20{
21 const REQUEST_METHOD_POST = 'POST';
22 const REQUEST_METHOD_GET = 'GET';
23
25 protected $errors;
26
28 protected $action;
29
31 protected $actionName;
32
34 protected $actions = array();
35
37 protected $request;
38
40 protected $response;
41
47 public static function create()
48 {
49 return new static();
50 }
51
55 public function __construct()
56 {
57 $this->errors = new ErrorCollection;
58 }
59
65 public function setActions(array $actions)
66 {
67 foreach ($actions as $action)
68 {
69 $this->addAction($action);
70 }
71
72 return $this;
73 }
74
80 public function addAction(Action $action)
81 {
82 $this->actions[] = $action;
83 return $this;
84 }
85
86 protected function giveResponse()
87 {
88 global $APPLICATION;
89 $APPLICATION->restartBuffer();
90
91 $this->response->getContent()->getErrorCollection()->add($this->errors->toArray());
92 $this->response->flushContent();
93
94 \CMain::finalActions();
95 exit;
96 }
97
103 public function getErrorCollection()
104 {
105 return $this->errors;
106 }
107
108 protected function check()
109 {
110 if(!$this->action)
111 {
112 $messageText = 'Action';
113 if ($this->actionName)
114 {
115 $messageText .= ' "' . htmlspecialcharsbx($this->actionName) . '"';
116 }
117 $messageText .= ' not found.';
118
119 $this->errors->setError(new Error($messageText));
120 return;
121 }
122
123 if(!check_bitrix_sessid() || ($this->action->getRequestMethod() != $this->request->getRequestMethod()))
124 {
125 $this->errors->setError(new Error('Security error.'));
126 return;
127 }
128 }
129
130 protected function findAction()
131 {
132 $this->action = null;
133 foreach ($this->actions as $action)
134 {
135 if ($action->getName() != $this->actionName)
136 {
137 continue;
138 }
139
140 $this->action = $action;
141 break;
142 }
143
144 return $this->action;
145 }
146
147 protected function process()
148 {
149 // check
150 $this->check();
151 if (!$this->errors->isEmpty())
152 {
153 return;
154 }
155
156 // checkers
157 $checkResult = new Result();
158 $checkers = array_merge($this->getCheckers(), $this->action->getCheckers());
159 static::callList($checkers, array($checkResult, $this->request));
160 $this->errors->add($checkResult->getErrors());
161 if (!$this->errors->isEmpty())
162 {
163 return;
164 }
165
166 // run action
167 $this->action->run($this->request, $this->response);
168
169 // modify response
170 $modifiers = array_merge($this->getResponseModifiers(), $this->action->getResponseModifiers());
171 static::callList($modifiers, array($this->response, $this->request));
172 }
173
177 public function run()
178 {
179 $this->request = $this->request ?: Context::getCurrent()->getRequest();
180 $this->response = $this->response ?: new Response(Context::getCurrent());
181 $this->actionName = $this->request->get('action');
182
183 // find action by name
184 $this->findAction();
185
186 // process action
187 $this->process();
188
189 // give response
190 $this->giveResponse();
191 }
192
193
194
202 protected function callList(array $list, array $parameters = array())
203 {
204 foreach ($list as $callee)
205 {
206 $result = static::call($callee, $parameters);
207 if ($result instanceof Error)
208 {
209 $this->errors->setError($result);
210 return;
211 }
212 }
213 }
214}
static getCurrent()
Definition context.php:241
static loadMessages($file)
Definition loc.php:64
callList(array $list, array $parameters=array())
Definition listener.php:202