summaryrefslogtreecommitdiff
path: root/app/Core/Router.php
blob: 55ebe4a879d077a2e50a7fc58ffe3bcf979844fc (plain)
1
2
3
4
5
6
7
8
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php

namespace Core;

/**
 * Router class
 *
 * @package core
 * @author  Frederic Guillot
 */
class Router extends Base
{
    /**
     * Controller
     *
     * @access private
     * @var string
     */
    private $controller = '';

    /**
     * Action
     *
     * @access private
     * @var string
     */
    private $action = '';

    /**
     * Store routes for path lookup
     *
     * @access private
     * @var array
     */
    private $paths = array();

    /**
     * Store routes for url lookup
     *
     * @access private
     * @var array
     */
    private $urls = array();

    /**
     * Get action
     *
     * @access public
     * @return string
     */
    public function getAction()
    {
        return $this->action;
    }

    /**
     * Get controller
     *
     * @access public
     * @return string
     */
    public function getController()
    {
        return $this->controller;
    }

    /**
     * Get the path to compare patterns
     *
     * @access public
     * @param  string  $uri
     * @param  string  $query_string
     * @return string
     */
    public function getPath($uri, $query_string = '')
    {
        $path = substr($uri, strlen($this->helper->url->dir()));

        if (! empty($query_string)) {
            $path = substr($path, 0, - strlen($query_string) - 1);
        }

        if (! empty($path) && $path{0} === '/') {
            $path = substr($path, 1);
        }

        return $path;
    }

    /**
     * Add route
     *
     * @access public
     * @param  string   $path
     * @param  string   $controller
     * @param  string   $action
     * @param  array    $params
     */
    public function addRoute($path, $controller, $action, array $params = array())
    {
        $pattern = explode('/', $path);

        $this->paths[] = array(
            'pattern' => $pattern,
            'count' => count($pattern),
            'controller' => $controller,
            'action' => $action,
        );

        $this->urls[$controller][$action][] = array(
            'path' => $path,
            'params' => array_flip($params),
            'count' => count($params),
        );
    }

    /**
     * Find a route according to the given path
     *
     * @access public
     * @param  string   $path
     * @return array
     */
    public function findRoute($path)
    {
        $parts = explode('/', $path);
        $count = count($parts);

        foreach ($this->paths as $route) {

            if ($count === $route['count']) {

                $params = array();

                for ($i = 0; $i < $count; $i++) {

                    if ($route['pattern'][$i]{0} === ':') {
                        $params[substr($route['pattern'][$i], 1)] = $parts[$i];
                    }
                    else if ($route['pattern'][$i] !== $parts[$i]) {
                        break;
                    }
                }

                if ($i === $count) {
                    $_GET = array_merge($_GET, $params);
                    return array($route['controller'], $route['action']);
                }
            }
        }

        return array('app', 'index');
    }

    /**
     * Find route url
     *
     * @access public
     * @param  string   $controller
     * @param  string   $action
     * @param  array    $params
     * @return string
     */
    public function findUrl($controller, $action, array $params = array())
    {
        if (! isset($this->urls[$controller][$action])) {
            return '';
        }

        foreach ($this->urls[$controller][$action] as $pattern) {

            if (array_diff_key($params, $pattern['params']) === array()) {
                $url = $pattern['path'];
                $i = 0;

                foreach ($params as $variable => $value) {
                    $url = str_replace(':'.$variable, $value, $url);
                    $i++;
                }

                if ($i === $pattern['count']) {
                    return $url;
                }
            }
        }

        return '';
    }

    /**
     * Check controller and action parameter
     *
     * @access public
     * @param  string $value Controller or action name
     * @param  string $default_value Default value if validation fail
     * @return string
     */
    public function sanitize($value, $default_value)
    {
        return ! preg_match('/^[a-zA-Z_0-9]+$/', $value) ? $default_value : $value;
    }

    /**
     * Find controller/action from the route table or from get arguments
     *
     * @access public
     * @param  string  $uri
     * @param  string  $query_string
     */
    public function dispatch($uri, $query_string = '')
    {
        if (! empty($_GET['controller']) && ! empty($_GET['action'])) {
            $this->controller = $this->sanitize($_GET['controller'], 'app');
            $this->action = $this->sanitize($_GET['action'], 'index');
            $plugin = ! empty($_GET['plugin']) ? $this->sanitize($_GET['plugin'], '') : '';
        }
        else {
            list($this->controller, $this->action) = $this->findRoute($this->getPath($uri, $query_string)); // TODO: add plugin for routes
            $plugin = '';
        }

        $class = empty($plugin) ? '\Controller\\'.ucfirst($this->controller) : '\Plugin\\'.ucfirst($plugin).'\Controller\\'.ucfirst($this->controller);

        $instance = new $class($this->container);
        $instance->beforeAction($this->controller, $this->action);
        $instance->{$this->action}();
    }
}