28 $configuration =
null;
29 if (empty($annotations))
31 return $configuration;
34 if (isset($annotations[
'Action']) || array_key_exists(
'Action', $annotations))
36 $configuration = array(
37 'prefilters' => array(),
38 'postfilters' => array(),
41 if (isset($annotations[
'Action'][
'method']))
43 if (!is_array($annotations[
'Action'][
'method']))
45 $annotations[
'Action'][
'method'] = array($annotations[
'Action'][
'method']);
51 if (isset($annotations[
'Action'][
'csrf']) && is_bool($annotations[
'Action'][
'csrf']))
53 $configuration[
'prefilters'][] =
new ActionFilter\Csrf($annotations[
'Action'][
'csrf']);
56 if (!$configuration && !isset($configuration[
'postfilters']))
58 $configuration[
'postfilters'] = array();
62 return $configuration;
67 $newConfiguration = $this->onBuildConfigurationOfActions($controller);
70 if ($this->mode & self::MODE_ANNOTATIONS)
74 $lengthSuffix = mb_strlen(Controllerable::METHOD_ACTION_SUFFIX);
75 $reflectionClass = new \ReflectionClass($controller);
76 foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method)
78 if (!$method->isPublic())
83 $probablySuffix = mb_substr($method->getName(), -$lengthSuffix);
84 if ($probablySuffix !== Controllerable::METHOD_ACTION_SUFFIX)
89 $actionName = mb_substr($method, 0, -$lengthSuffix);
90 if ($this->isExists($configuration, $actionName))
96 $annotations = $annotationReader->getMethodAnnotations($method);
97 if (!$this->checkReflectionMethodAsAction($method, $annotations))
106 $configuration = array_merge($newConfiguration, $configuration);
107 $configuration = $this->wrapClosure($configuration);
109 $this->checkConfigurations($configuration);
111 return $configuration;
114 private function isExists(array $configuration, $actionName)
116 $listOfActions = array_change_key_case($configuration, CASE_LOWER);
117 $actionName = mb_strtolower($actionName);
119 return isset($listOfActions[$actionName]);
122 private function checkReflectionMethodAsAction(\ReflectionMethod $reflectionMethod, array $annotations =
null)
124 if (!$reflectionMethod->isPublic())
129 if ($annotations ===
null)
131 $annotationReader =
new AnnotationReader();
132 $annotations = $annotationReader->getMethodAnnotations($reflectionMethod);
135 if (!is_array($annotations) || !array_key_exists(
'Action', $annotations))
143 private function onBuildConfigurationOfActions(
Controller $controller)
148 static::EVENT_ON_BUILD_ACTIONS,
150 'controller' => $controller,
155 $newConfiguration = array();
156 foreach ($event->getResults() as $eventResult)
158 if ($eventResult->getType() != EventResult::SUCCESS)
163 $parameters = $eventResult->getParameters();
164 if (isset($parameters[
'extraActions']) && is_array($parameters[
'extraActions']))
167 $newConfiguration = array_merge($newConfiguration, $parameters[
'extraActions']);
171 return $newConfiguration;
176 foreach ($filters as $i => $filter)
178 if (!($filter instanceof \Closure))
189 private function wrapClosure(array $configurations)
191 foreach ($configurations as $actionName => $configuration)
193 if (!empty($configuration[
'prefilters']))
195 $configurations[$actionName][
'prefilters'] = $this->
wrapFiltersClosure($configuration[
'prefilters']);
198 if (!empty($configuration[
'+prefilters']))
200 $configurations[$actionName][
'+prefilters'] = $this->
wrapFiltersClosure($configuration[
'+prefilters']);
203 if (!empty($configuration[
'postfilters']))
205 $configurations[$actionName][
'postfilters'] = $this->
wrapFiltersClosure($configuration[
'postfilters']);
208 if (!empty($configuration[
'+postfilters']))
210 $configurations[$actionName][
'+postfilters'] = $this->
wrapFiltersClosure($configuration[
'+postfilters']);
214 return $configurations;
217 private function checkConfigurations(array $configurations)
219 foreach ($configurations as $actionName => $configuration)
221 if (!is_string($actionName))
223 throw new SystemException(
'Invalid configuration of actions. Action has to be string');
226 if (!is_array($configuration))
228 throw new SystemException(
'Invalid configuration of actions. Configuration has to be array');
231 if (!empty($configuration[
'prefilters']))
233 $this->checkFilters($configuration[
'prefilters']);
236 if (!empty($configuration[
'postfilters']))
238 $this->checkFilters($configuration[
'postfilters']);
243 private function checkFilters(array $filters)
245 foreach ($filters as $filter)
247 if (!($filter instanceof ActionFilter\Base))
249 throw new SystemException(
'Filter has to be subclass of ' . ActionFilter\Base::className());
const EVENT_ON_BUILD_ACTIONS
getConfigurationByController(Controller $controller)
getConfigurationByAnnotations(array $annotations)
wrapFiltersClosure(array $filters)