Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
rulecontrollerfactory.php
1<?php
2
4
8use ReflectionClass;
9
11{
12 protected const SUFFIX = 'Rule';
13
14 public function createFromAction(string $action, AccessibleController $controller): ?RuleInterface
15 {
16 $className = $this->getClassName($action, $controller);
17 if (!$className || !class_exists($className ))
18 {
19 return null;
20 }
21
22 $ref = new ReflectionClass($className);
23 if ($ref->implementsInterface(RuleInterface::class))
24 {
25 return $ref->newInstance($controller);
26 }
27
28 return null;
29 }
30
31 protected function getClassName(string $action, AccessibleController $controller): ?string
32 {
33 $action = explode('_', $action);
34 $action = array_map(fn($el) => ucfirst(mb_strtolower($el)), $action);
35
36 return $this->getNamespace($controller) . implode($action) . static::SUFFIX;
37 }
38
39 protected function getNamespace(AccessibleController $controller): string
40 {
41 $class = new ReflectionClass($controller);
42 $namespace = $class->getNamespaceName();
43
44 return $namespace.'\\'.static::SUFFIX.'\\';
45 }
46}
createFromAction(string $action, AccessibleController $controller)
getClassName(string $action, AccessibleController $controller)