1: <?php
2: namespace Define\Core;
3: use Define\Core\IDefine;
4: use Define\Traits\SingletonTrait;
5: use Define\Exceptions\FrameworkException;
6:
7: if(!defined('DIRECT_ACCESS')) {
8: die("Direct access is forbidden.");
9: }
10:
11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36:
37: final class Router implements IRouter, IDefine {
38:
39: use SingletonTrait;
40:
41: const DEFAULT_CONTROLLER = DEFAULT_CONTROLLER;
42: const DEFAULT_ACTION = DEFAULT_ACTION;
43: private $controller = self::DEFAULT_CONTROLLER;
44: private $action = self::DEFAULT_ACTION;
45: private $params = array();
46:
47: 48: 49:
50: public function init(array $options = array()) {
51: empty($options) ? $this->doSetup() : $this->get($options);
52: }
53:
54: 55: 56: 57:
58: private function get(array $options = array()) {
59: isset($options["controller"]) && $options['controller'] != '' ? $this->setController($options["controller"]) : CONTROLLER_NAMESPACE . $this->controller;
60: isset($options["action"]) ? $this->setAction($options["action"]) : $this->action;
61: isset($options["params"]) ? $this->setParams($options["params"]) : $this->params;
62: }
63:
64: 65: 66:
67: public function doSetup() {
68: $path = parse_url(filter_var($_SERVER["REQUEST_URI"], FILTER_SANITIZE_URL), PHP_URL_PATH);
69:
70: substr($path, -1) != NEED_SLASH ? $this->setController(ERROR_CONTROLLER) : $path = trim($path, '/');
71:
72: $path = preg_replace('/[^a-zA-Z0-9]\//', "", $path);
73:
74: @list($controller, $action, $params) = explode(SEPARATOR, $path, 3);
75:
76: !empty($controller) ? $this->setController($controller) : $this->setController($this->controller);
77: !empty($action) ? $this->setAction($action) : $this->setAction($this->action);
78: !empty($params) ? $this->setParams(explode("-", $params)) : $this->setParams($this->params);
79: }
80:
81: 82: 83:
84: public function setController($controller) {
85:
86: $controller = \str_replace("-", "", \mb_convert_case(\mb_strtolower($controller), \MB_CASE_TITLE, "UTF-8"));
87: $this->controller = CONTROLLER_NAMESPACE . CONTROLLER_PREFIX . $controller . CONTROLLER_SUFFIX;
88: $file = CONTROLLER_PATH . CONTROLLER_PREFIX . $controller . CONTROLLER_SUFFIX.'.php';
89:
90: if (!\file_exists($file)) {
91: $this->controller = CONTROLLER_NAMESPACE . CONTROLLER_PREFIX . ERROR_CONTROLLER . CONTROLLER_SUFFIX;
92: }
93: return $this;
94: }
95:
96: 97: 98:
99: public function setAction($action) {
100: $action = str_replace("-", "", \mb_convert_case(\mb_strtolower($action), \MB_CASE_TITLE, "UTF-8"));
101: $reflector = new \ReflectionClass($this->controller);
102: $this->action = lcfirst($action . ACTION_SUFFIX);
103: if (!$reflector->hasMethod($this->action)) {
104: $this->controller = CONTROLLER_NAMESPACE . CONTROLLER_PREFIX . ERROR_CONTROLLER . CONTROLLER_SUFFIX;
105: $this->action = self::DEFAULT_ACTION . ACTION_SUFFIX;
106: }
107: return $this;
108: }
109:
110: 111: 112:
113: public function setParams(array $params) {
114: $this->params = $params;
115: return $this;
116: }
117:
118: 119: 120:
121: public function run() {
122: try {
123: $controller = new $this->controller();
124: if(!$controller instanceof BaseController) {
125: throw new FrameworkException("Controller {$this->controller} is not of type Define\Core\BaseController.", 1003);
126: }
127: \call_user_func_array(array($controller, $this->action), $this->params);
128: } catch (\Exception $e) {
129: echo $e;
130: }
131: }
132: }