RouteAwareTrait::fetchMatched()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 21
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Phoole (PHP7.2+)
5
 *
6
 * @category  Library
7
 * @package   Phoole\Route
8
 * @copyright Copyright (c) 2019 Hong Zhang
9
 */
10
declare(strict_types=1);
11
12
namespace Phoole\Route\Util;
13
14
use Phoole\Route\Router;
15
use Phoole\Route\Parser\ParserInterface;
16
17
/**
18
 * RouteAwareTrait
19
 *
20
 * @package Phoole\Route
21
 */
22
trait RouteAwareTrait
23
{
24
    /**
25
     * @var ParserInterface
26
     */
27
    protected $parser;
28
29
    /**
30
     * @var Route[]
31
     */
32
    protected $routes = [];
33
34
    /**
35
     * Add a GET route
36
     *
37
     * @param  string $pattern
38
     * @param  mixed  $handler
39
     * @param  array  $defaults  default parameters if any
40
     * @return $this
41
     */
42
    public function addGet(string $pattern, $handler, array $defaults = [])
43
    {
44
        return $this->addRoute(new Route('GET', $pattern, $handler, $defaults));
45
    }
46
47
    /**
48
     * Add one route
49
     *
50
     * @param  Route $route
51
     * @return $this
52
     */
53
    public function addRoute(Route $route)
54
    {
55
        $pattern = $route->getPattern();
56
        $hash = md5($route->getPattern());
57
        if (isset($this->routes[$hash])) {
58
            $this->routes[$hash]->addMethods($route);
59
        } else {
60
            $this->routes[$hash] = $route;
61
        }
62
63
        $this->parser->parse($hash, $pattern);
64
        return $this;
65
    }
66
67
    /**
68
     * Add a POST route
69
     *
70
     * @param  string $pattern
71
     * @param  mixed  $handler
72
     * @param  array  $defaults  default parameters if any
73
     * @return $this
74
     */
75
    public function addPost(string $pattern, $handler, array $defaults = [])
76
    {
77
        return $this->addRoute(new Route('POST', $pattern, $handler, $defaults));
78
    }
79
80
    /**
81
     * Load routes from config array
82
     * ```php
83
     * $routes = [
84
     *     ['GET', '/user/{uid}', function() {}, ['uid' => 12]],
85
     *     ...
86
     * ];
87
     * ```
88
     *
89
     * @param  array $routes
90
     * @return $this
91
     */
92
    protected function loadRoutes(array $routes)
93
    {
94
        foreach ($routes as $definition) {
95
            $method = $definition[0];
96
            $pattern = $definition[1];
97
            $handler = $definition[2];
98
            $defaults = $definition[3] ?? [];
99
            $this->addRoute(new Route($method, $pattern, $handler, $defaults));
100
        }
101
        return $this;
102
    }
103
104
    /**
105
     * @param  ParserInterface $parser
106
     * @return $this
107
     */
108
    protected function setParser(ParserInterface $parser)
109
    {
110
        $this->parser = $parser;
111
        return $this;
112
    }
113
114
    /**
115
     * Match a route with all predefined routes
116
     *
117
     * @param  Result $result  ;
118
     * @return Result
119
     */
120
    protected function routeMatch(Result $result): Result
121
    {
122
        $uri = $result->getRequest()->getUri()->getPath();
123
124
        $matched = $this->parser->match($uri);
125
        if (!empty($matched)) {
126
            $this->fetchMatched($matched, $result);
127
        }
128
129
        return $result;
130
    }
131
132
    /**
133
     * @param  array  $matched
134
     * @param  Result $result
135
     * @return void
136
     */
137
    protected function fetchMatched(array $matched, Result $result): void
138
    {
139
        // fetch the matched route
140
        list($hash, $params) = $matched;
141
        $route = $this->routes[$hash];
142
143
        // check if method exists
144
        $request = $result->getRequest();
145
        $method = $request->getMethod();
146
        if (isset($route->getMethods()[$method])) {
147
            // update request
148
            list($handler, $defaults) = $route->getMethods()[$method];
149
            $request = $request->withAttribute(
150
                Router::URI_PARAMETERS,
151
                array_merge($defaults, $params)
152
            );
153
154
            // update result
155
            $result->setHandler($handler);
156
            $result->setRoute($route);
157
            $result->setRequest($request);
158
        }
159
    }
160
}