aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/Router.php
blob: c78ab8b4272c39b1684749d7c94f83f1de32bde9 (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
<?php

final class Router
{
    protected $routes = [
        'GET'  => [],
        'POST' => [],
        'HEAD' => []
    ];

    public function get($uri, $controller)
    {
        $this->routes['GET'][$uri] = $controller;
    }

    public function post($uri, $controller)
    {
        $this->routes['POST'][$uri] = $controller;
    }

    public function head($uri, $controller)
    {
        $this->routes['HEAD'][$uri] = $controller;
    }

    public static function load($file)
    {
        $router = new static;
        include $file;
        return $router;
    }

    public function direct($uri, $requestType)
    {
        if (is_array($this->routes[$requestType]) && array_key_exists($uri, $this->routes[$requestType])) {
            return $this->routes[$requestType][$uri];
        }
        die(http_response_code(404));
    }
}