HexMakina /
kadro
| 1 | <?php |
||
| 2 | |||
| 3 | namespace HexMakina\kadro\Controllers; |
||
| 4 | |||
| 5 | use Psr\Container\{ContainerInterface,ContainerExceptionInterface,NotFoundExceptionInterface}; |
||
| 6 | use HexMakina\BlackBox\RouterInterface; |
||
| 7 | use HexMakina\BlackBox\Controllers\BaseControllerInterface; |
||
| 8 | use Psr\Log\LoggerInterface; |
||
| 9 | use HexMakina\LeMarchand\LeMarchand; |
||
| 10 | |||
| 11 | class Base implements BaseControllerInterface, ContainerInterface |
||
| 12 | { |
||
| 13 | use \HexMakina\Traitor\Traitor; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 14 | |||
| 15 | protected $route_back = null; |
||
| 16 | protected $errors = []; |
||
| 17 | |||
| 18 | public function errors(): array |
||
| 19 | { |
||
| 20 | return $this->errors; |
||
| 21 | } |
||
| 22 | |||
| 23 | public function container(): ContainerInterface |
||
| 24 | { |
||
| 25 | return LeMarchand::box(); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function has($key) |
||
| 29 | { |
||
| 30 | return $this->container()->has($key); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function get($key) |
||
| 34 | { |
||
| 35 | return $this->container()->get($key); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function addError($message, $context = []) |
||
| 39 | { |
||
| 40 | $this->errors[] = [$message, $context]; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function logger(): LoggerInterface |
||
| 44 | { |
||
| 45 | return $this->get('Psr\Log\LoggerInterface'); |
||
| 46 | } |
||
| 47 | |||
| 48 | // -------- Router |
||
| 49 | |||
| 50 | public function router(): RouterInterface |
||
| 51 | { |
||
| 52 | return $this->get('HexMakina\BlackBox\RouterInterface'); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function prepare() |
||
| 56 | { |
||
| 57 | return true; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function execute($method) |
||
| 61 | { |
||
| 62 | $ret = null; |
||
| 63 | |||
| 64 | // before and after hooks, should they be in basecontroller ? |
||
| 65 | // i think so, but pascal just proposed me pastis.. tomorrow |
||
| 66 | |||
| 67 | foreach (['prepare', "before_$method", $method, "after_$method"] as $step => $chainling) { |
||
| 68 | $this->traitor($chainling); |
||
| 69 | |||
| 70 | if (method_exists($this, $chainling) && empty($this->errors())) { |
||
| 71 | $res = $this->$chainling(); |
||
| 72 | |||
| 73 | if ($chainling === $method) { |
||
| 74 | $ret = $res; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | $this->conclude(); |
||
| 80 | |||
| 81 | return $ret; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function conclude() |
||
| 85 | { |
||
| 86 | return true; |
||
| 87 | } |
||
| 88 | |||
| 89 | /* |
||
| 90 | * returns string, a URL formatted by RouterInterface::pre_hop() |
||
| 91 | * |
||
| 92 | * USAGE |
||
| 93 | * routeBack($route_name=null) returns previously set $route_back or RouterInterface::ROUTE_HOME_NAME |
||
| 94 | * routeBack($route_name [,$route_params]), sets $route_back using routeFactory() |
||
| 95 | * |
||
| 96 | */ |
||
| 97 | public function routeBack($route_name = null, $route_params = []): string |
||
| 98 | { |
||
| 99 | if (is_null($route_name)) { |
||
| 100 | return $this->route_back ?? $this->router()->hyp(RouterInterface::ROUTE_HOME_NAME); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $this->route_back = $this->routeFactory($route_name, $route_params); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function routeFactory($route_name = null, $route_params = []): string |
||
| 107 | { |
||
| 108 | $route = null; |
||
| 109 | |||
| 110 | if (is_string($route_name) && !empty($route_name)) { |
||
| 111 | if ($this->router()->routeExists($route_name)) { |
||
| 112 | $route = $this->router()->hyp($route_name, $route_params); |
||
| 113 | } else { |
||
| 114 | $route = $route_name; |
||
| 115 | } |
||
| 116 | |||
| 117 | return $route; |
||
| 118 | } |
||
| 119 | |||
| 120 | throw new \Exception('ROUTE_FACTORY_PARAM_TYPE_ERROR'); |
||
| 121 | } |
||
| 122 | } |
||
| 123 |