SimpleRoutesSet::paramRouteExists()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 9
rs 10
cc 3
nc 3
nop 2
1
<?php
2
declare(strict_types = 1);
3
namespace Mezon\Router;
4
5
/**
6
 * Trait SimpleRoutesSet
7
 *
8
 * @package Router
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2021/09/27)
11
 * @copyright Copyright (c) 2021, http://aeon.su
12
 */
13
trait SimpleRoutesSet
14
{
15
16
    use StaticRoutes, ParamRoutes, RoutesSetBase;
17
18
    /**
19
     * Method adds param router
20
     *
21
     * @param string $requestMethod
22
     *            request method
23
     * @param string $route
24
     *            route
25
     * @param array{0: string, 1:string}|callable|string $callback
26
     *            callback method
27
     */
28
    protected function addParamRoute(string $requestMethod, string $route, $callback): void
29
    {
30
        if (empty($this->paramRoutes[$requestMethod])) {
31
            $this->paramRoutes[$requestMethod] = [];
32
        }
33
34
        $this->paramRoutes[$requestMethod][] = [
35
            'pattern' => $route,
36
            'callback' => $callback
37
        ];
38
    }
39
40
    /**
41
     * Method returns true if the param router exists
42
     *
43
     * @param string $route
44
     *            checking route
45
     * @param string $requestMethod
46
     *            HTTP request method
47
     * @return bool true if the param router exists, false otherwise
48
     * @psalm-suppress PossiblyUndefinedArrayOffset
49
     */
50
    protected function paramRouteExists(string $route, string $requestMethod): bool
51
    {
52
        foreach ($this->paramRoutes[$requestMethod] as $item) {
53
            if ($item['pattern'] === $route) {
54
                return true;
55
            }
56
        }
57
58
        return false;
59
    }
60
61
    /**
62
     * Method dumps all routes and their names on disk
63
     *
64
     * @param string $filePath
65
     *            file path to cache
66
     * @codeCoverageIgnore
67
     */
68
    public function dumpOnDisk(string $filePath = './cache/cache.php'): void
69
    {
70
        file_put_contents($filePath, '<?php return ' . var_export([
71
            0 => $this->staticRoutes,
72
            1 => $this->paramRoutes,
73
            2 => $this->routeNames
74
        ], true) . ';');
75
    }
76
77
    /**
78
     * Method loads routes from disk
79
     *
80
     * @param string $filePath
81
     *            file path to cache
82
     * @codeCoverageIgnore
83
     * @psalm-suppress UnresolvableInclude, MixedArrayAccess, MixedAssignment
84
     */
85
    public function loadFromDisk(string $filePath = './cache/cache.php'): void
86
    {
87
        list ($this->staticRoutes, $this->paramRoutes, $this->routeNames) = require ($filePath);
88
    }
89
90
    /**
91
     * Method rturns all available routes
92
     *
93
     * @return string trace
94
     * @psalm-suppress PossiblyUndefinedArrayOffset
95
     */
96
    protected function getAllRoutesTrace(): string
97
    {
98
        $fullTrace = [];
99
100
        foreach (SupportedRequestMethods::getListOfSupportedRequestMethods() as $requestMethod) {
101
            $trace = [
102
                $requestMethod . ' : '
103
            ];
104
            $hasRoutes = false;
105
            if (! empty($this->staticRoutes[$requestMethod])) {
106
                $trace[] = implode(', ', array_keys($this->staticRoutes[$requestMethod]));
107
                $trace[] = ', ';
108
                $hasRoutes = true;
109
            }
110
            if (! empty($this->paramRoutes[$requestMethod])) {
111
                $items = [];
112
                foreach ($this->paramRoutes[$requestMethod] as $item) {
113
                    $items[] = $item['pattern'];
114
                    $hasRoutes = true;
115
                    $trace[] = implode(', ', $items);
116
                }
117
            }
118
119
            if (! $hasRoutes) {
120
                $trace[] = '<none>';
121
            }
122
123
            $fullTrace[] = implode('', $trace);
124
        }
125
126
        return implode('; ', $fullTrace);
127
    }
128
}
129