Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
action.php
1<?php
2
4
5
10
16class Action implements Errorable
17{
19 protected $binder;
23 protected $controller;
25 protected $config;
27 protected $name;
28
29 public function __construct($name, Controller $controller, $config = array())
30 {
31 $this->errorCollection = new ErrorCollection;
32 $this->controller = $controller;
33 $this->config = $config;
34 $this->name = $name;
35
36 if (isset($config['configure']))
37 {
38 $this->configure($config['configure']);
39 }
40
41 $this->init();
42 }
43
50 public function configure($params)
51 {}
52
53 protected function init()
54 {}
55
62 final public function getArguments()
63 {
64 $binder = $this->buildBinder()->getBinder();
65
66 return $binder->getMethodParams();
67 }
68
79 final public function setArguments(array $arguments)
80 {
81 $binder = $this->buildBinder()->getBinder();
82
83 return $binder->setMethodParams($arguments);
84 }
85
86 protected function buildBinder()
87 {
88 if ($this->binder === null)
89 {
90 if (!method_exists($this, 'run'))
91 {
92 throw new SystemException(static::className() . ' must implement run()');
93 }
94
95 $controller = $this->getController();
96 $this->binder = AutoWire\ControllerBinder::buildForMethod($this, 'run')
97 ->setController($controller)
98 ->setSourcesParametersToMap($controller->getSourceParametersList())
99 ->setAutoWiredParameters(
100 array_filter(array_merge(
101 [$controller->getPrimaryAutoWiredParameter()],
102 $controller->getAutoWiredParameters()
103 ))
104 )
105 ;
106 }
107
108 return $this;
109 }
110
112 {
113 $binder = $this->buildBinder()->getBinder();
114 if ($this->onBeforeRun())
115 {
117 $result = $binder->invoke();
118
119 $this->onAfterRun();
120
121 return $result;
122 }
123
124 return null;
125 }
126
130 final public function getBinder()
131 {
132 return $this->binder;
133 }
134
138 final public function getController()
139 {
140 return $this->controller;
141 }
142
146 final public function getName()
147 {
148 return $this->name;
149 }
150
154 final public function getConfig()
155 {
156 return $this->config;
157 }
158
159 protected function onBeforeRun()
160 {
161 return true;
162 }
163
164 protected function onAfterRun()
165 {
166 }
167
168 final public function getCurrentUser()
169 {
170 return $this->getController()->getCurrentUser();
171 }
172
180 public function convertKeysToCamelCase($data)
181 {
182 return $this->getController()->convertKeysToCamelCase($data);
183 }
184
189 final public static function className()
190 {
191 return get_called_class();
192 }
193
200 protected function addError(Error $error)
201 {
202 $this->errorCollection[] = $error;
203
204 return $this;
205 }
206
213 protected function addErrors(array $errors)
214 {
215 $this->errorCollection->add($errors);
216
217 return $this;
218 }
219
224 final public function getErrors()
225 {
226 return $this->errorCollection->toArray();
227 }
228
234 final public function getErrorByCode($code)
235 {
236 return $this->errorCollection->getErrorByCode($code);
237 }
238}
addError(Error $error)
Definition action.php:200
addErrors(array $errors)
Definition action.php:213
__construct($name, Controller $controller, $config=array())
Definition action.php:29
setArguments(array $arguments)
Definition action.php:79