-
Notifications
You must be signed in to change notification settings - Fork 452
/
Route.php
133 lines (117 loc) · 2.94 KB
/
Route.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Concrete\Core\Routing;
use Symfony\Component\Routing\Route as SymfonyRoute;
class Route extends SymfonyRoute
{
/**
* If this route has a custom name, it appears here. Otherwise it is automatically generated
* from the path.
* @var string
*/
protected $customName;
/**
* The action that the route will execute when it is run.
* This could be a callback, a controller string, or something else.
* It is the job of the RouteActionFactory to turn it from whatever
* it currently is into RouteAction object.
* @var mixed
*/
protected $action;
/**
* @var RouteMiddleware[]
*/
protected $middlewares = [];
/**
* @return bool
*/
public function hasCustomName()
{
return isset($this->customName);
}
/**
* @return mixed
*/
public function getAction()
{
return $this->action;
}
/**
* @param mixed $action
*/
public function setAction($action)
{
$this->action = $action;
}
/**
* @return mixed
*/
public function getName()
{
if (isset($this->customName)) {
return $this->customName;
} else {
return $this->getGeneratedName();
}
}
/**
* Sets the custom name. Note: if the route has already been added to the route
* collection you will want to use $route->updateName($name, $router)
* instead
* @param $name
*/
public function setCustomName($name)
{
$this->customName = $name;
}
/**
* @param mixed $name
*/
public function updateName($name, Router $router)
{
$router->getRoutes()->remove($this->getName());
$this->setCustomName($name);
$router->getRoutes()->add($name, $this);
}
private function getGeneratedName()
{
$methods = $this->getMethods();
if (count($methods) == 7) {
$methodName = 'all';
} else {
$methodName = strtolower(implode('_', $methods));
}
$path = $this->getPath();
$path = trim($path, '/');
$name = preg_replace('/[^A-Za-z0-9\_]/', '_', $path);
$name = preg_replace('/\_+/', '_', $name);
$name = trim($name, '_');
$name .= '_' . $methodName;
return $name;
}
/**
* Adds middleware to the route.
*
* @param RouteMiddleware $middleware
*/
public function addMiddleware(RouteMiddleware $middleware)
{
$this->middlewares[] = $middleware;
}
/**
* @return RouteMiddleware[]
*/
public function getMiddlewares()
{
return $this->middlewares;
}
/**
* Explicitly sets an OAuth2 scope to a route. This will be used if the route is consumed in an
* OAuth2 request.
*
* @param string $scope
*/
public function setScopes($scope)
{
$this->setOption('oauth_scopes', $scope);
}
}