Completed
Push — master ( bbb486...6c9a0a )
by Restu
14:10
created

Router::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
namespace JayaCode\Framework\Core\Router;
3
4
use JayaCode\Framework\Core\Http\Request;
5
use JayaCode\Framework\Core\Http\Response;
6
7
class Router
8
{
9
    /**
10
     * @var array
11
     */
12
    public $routes = array(
13
        [
14
            "id"    => "home",
15
            "path"  => "/",
16
            "controller"    => "HomeController",
17
            "action"        => "index"
18
        ]
19
    );
20
21
    /**
22
     * @var Request
23
     */
24
    public $request;
25
26
    /**
27
     * @var Response
28
     */
29
    public $response;
30
31
    /**
32
     * Router constructor.
33
     * @param Request $request
34
     * @param Response $response
35
     */
36
    public function __construct(Request &$request = null, Response &$response = null)
37
    {
38
        $this->initialize($request, $response);
39
    }
40
41
    /**
42
     * initialize Router
43
     * @param Request $request
44
     * @param Response $response
45
     */
46
    public function initialize(Request &$request = null, Response &$response = null)
47
    {
48
        $this->request = &$request;
49
        $this->response = &$response;
50
    }
51
52
    /**
53
     * Create Router object from static function
54
     * @param Request $request
55
     * @param Response $response
56
     * @return Router
57
     */
58
    public static function create(Request &$request = null, Response &$response = null)
59
    {
60
        return new Router($request, $response);
61
    }
62
63
    /**
64
     * Handle request
65
     */
66
    public function handle()
67
    {
68
        $path = $this->request->path();
69
        $route = $this->getRouteByPath($path);
70
71
        if (arr_has_key($route, "action") && is_callable(arr_get($route, "action"))) {
72
            // if use callback function
73
            $content = call_user_func(arr_get($route, "action"));
74
            $this->response->setContent($content);
75
        } elseif (arr_has_key($route, "controller") && arr_has_key($route, "action")
76
            && is_string(arr_get($route, "action"))) {
77
            // if use controller
78
            $controller_class = "App\\Controller\\" . arr_get($route, "controller");
79
80
            if (!class_exists($controller_class)) {
81
                throw new \InvalidArgumentException("controller " . $controller_class . " not found");
82
            }
83
84
            $controller = new $controller_class($this->request, $this->response);
85
86
            $action = arr_get($route, "action");
87
88
            if (!method_exists($controller, $action)) {
89
                throw new \InvalidArgumentException("method " . $action . " not found");
90
            }
91
92
            $content = $controller->$action();
93
            $this->response->setContent($content);
94
        } else {
95
            $this->response->setNotFound($path);
96
        }
97
98
        return $this->response;
99
    }
100
101
    /**
102
     * @param string $id
103
     * @return array
104
     */
105
    public function getRoute($id = null)
106
    {
107
        if (is_null($id)) {
108
            return $this->routes;
109
        }
110
111
        if (!is_string($id)) {
112
            throw new \InvalidArgumentException("var id must be a string");
113
        }
114
115
        foreach ($this->routes as $route) {
116
            if (arr_has_key($route, "id") && arr_get($route, "id") == $id) {
117
                return $route;
118
            }
119
        }
120
121
        throw new \OutOfBoundsException("not found route with id " . $id);
122
    }
123
124
    /**
125
     * @param string $path
126
     * @return array
127
     */
128
    public function getRouteByPath($path = null)
129
    {
130
        if (!is_string($path)) {
131
            throw new \InvalidArgumentException("var path must be a string");
132
        }
133
134
        foreach ($this->routes as $route) {
135
            if (arr_has_key($route, "path") && arr_get($route, "path") == $path) {
136
                return $route;
137
            }
138
        }
139
140
        return null;
141
    }
142
143
    /**
144
     * @param array $routes
145
     */
146
    public function setRoutes($routes)
147
    {
148
        $this->routes = $routes;
149
    }
150
}
151