/** * Kernel constructor. */ public function __construct() { try { $this->Request = new Request(); $this->Router = new Router($this->Request); $ControllerLoader = new ControllerLoader($this->Request->getController()); if ($ControllerLoader->exist()) { $this->Controller = $ControllerLoader->getInstance($this->Request); if (method_exists($this->Controller, $this->Request->getAction())) { call_user_func([$this->Controller, $this->Request->getAction()], $this->Request->getParams()); $Logger = new Logger('/logs/access.log'); $Date = new \DateTime('now'); $Logger->writeLine('[' . $Date->getTimestamp() . '] Path:' . $this->Request->getPath() . ' type:' . $this->Request->getMethod()); } else { $exception = new NotFoundException('Path:' . $this->Request->getPath() . ', Action ' . $this->Request->getAction() . ' not found in Controller ' . ucfirst($this->Request->getController() . 'Controller')); throw $exception; } } else { $exception = new NotFoundException('Path:' . $this->Request->getPath() . ', Controller ' . ucfirst($this->Request->getController()) . 'Controller not found'); throw $exception; } } catch (NotFoundException $e) { echo $e->cry(); } }
/** * @return bool * @throws NotFoundException */ public function match() { foreach ($this->routing as $key => $value) { $url = $value['path']; if (!empty($value['requirements'])) { foreach ($value['requirements'] as $k => $v) { $url = str_replace($k, '(?P<' . str_replace(':', '', $k) . '>' . $v . ')', $url); } } $url = str_replace('/', '\\/', $url); $url = '/^' . $url . '$/'; $result = preg_match_all($url, $this->Request->getPath(), $matches, PREG_SET_ORDER); if ($result) { $this->Request->setRouteName($key); $this->Request->setUrlOrigin($value['path']); $params = $this->array_remove_numerical(!empty($matches[0]) ? $matches[0] : []); $this->Request->setParams(new StorageCollection($params)); return true; } } $exception = new NotFoundException('Path:' . $this->Request->getPath() . ', Route not found'); throw $exception; }