/
SingleActionRouter.php
118 lines (100 loc) · 2.53 KB
/
SingleActionRouter.php
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
<?php declare(strict_types=1);
/**
* Part of Windwalker project.
*
* @copyright Copyright (C) 2019 LYRASOFT.
* @license LGPL-2.0-or-later
*/
namespace Windwalker\Router;
use Windwalker\Router\Matcher\MatcherInterface;
use Windwalker\Router\Matcher\SequentialMatcher;
/**
* Class SingleActionRouter
*
* @since 2.0
*/
class SingleActionRouter extends Router
{
/**
* Property requests.
*
* @var array
*/
protected $variables = [];
/**
* Class init.
*
* @param array $routes
* @param MatcherInterface $matcher
*/
public function __construct(array $routes = [], MatcherInterface $matcher = null)
{
$this->addMaps($routes);
$this->matcher = $matcher ?: new SequentialMatcher();
}
/**
* addRoute
*
* @param string $pattern
* @param string $controller
*
* @throws \LogicException
* @throws \InvalidArgumentException
* @return static
*/
public function addMap($pattern, $controller = null)
{
if (!is_string($controller)) {
throw new \InvalidArgumentException(
'Please give me controller name string. ' . ucfirst(gettype($controller)) . ' given.'
);
}
if ($pattern instanceof Route) {
throw new \LogicException('Do not use Route object in ' . get_called_class());
}
return parent::addRoute(null, $pattern, ['_controller' => $controller]);
}
/**
* parseRoute
*
* @param string $route
* @param string $method
* @param array $options
*
* @throws \UnexpectedValueException
* @return array|boolean
*/
public function match($route, $method = 'GET', $options = [])
{
$matched = parent::match($route, $method, $options);
$vars = $matched->getVariables();
if (!array_key_exists('_controller', $vars)) {
throw new \UnexpectedValueException('Controller not found.', 500);
}
$controller = $vars['_controller'];
unset($vars['_controller']);
$this->setVariables($vars);
return $controller;
}
/**
* getRequests
*
* @return array
*/
public function getVariables()
{
return $this->variables;
}
/**
* setRequests
*
* @param array $variables
*
* @return static Return self to support chaining.
*/
public function setVariables($variables)
{
$this->variables = $variables;
return $this;
}
}