1
|
|
|
<?php namespace MaartenStaa\Routing; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2015 by Maarten Staa. |
5
|
|
|
* |
6
|
|
|
* Some rights reserved. |
7
|
|
|
* |
8
|
|
|
* Redistribution and use in source and binary forms, with or without |
9
|
|
|
* modification, are permitted provided that the following conditions are |
10
|
|
|
* met: |
11
|
|
|
* |
12
|
|
|
* * Redistributions of source code must retain the above copyright |
13
|
|
|
* notice, this list of conditions and the following disclaimer. |
14
|
|
|
* |
15
|
|
|
* * Redistributions in binary form must reproduce the above |
16
|
|
|
* copyright notice, this list of conditions and the following |
17
|
|
|
* disclaimer in the documentation and/or other materials provided |
18
|
|
|
* with the distribution. |
19
|
|
|
* |
20
|
|
|
* * The names of the contributors may not be used to endorse or |
21
|
|
|
* promote products derived from this software without specific |
22
|
|
|
* prior written permission. |
23
|
|
|
* |
24
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
25
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
26
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
27
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
28
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
29
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
30
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
31
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
32
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
33
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
34
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
use Illuminate\Routing\RouteCollection as LaravelRouteCollection; |
38
|
|
|
|
39
|
|
|
class RouteCollection extends LaravelRouteCollection |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* A backup of the routes we had before clearing the collection in order to |
43
|
|
|
* cache a part of the complete list of routes. The list of routes is put into |
44
|
|
|
* the backup, new routes are defined and cached, and the backup is added back. |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $backup; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the backed up routes. |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
32 |
|
public function getBackup() |
56
|
|
|
{ |
57
|
32 |
|
return $this->backup; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Save the current collection of routes to the backup to start with an empty |
62
|
|
|
* collection to cache. |
63
|
|
|
*/ |
64
|
32 |
|
public function saveRouteCollection() |
65
|
|
|
{ |
66
|
32 |
|
$this->backup = $this->getCacheableRouteContents(); |
67
|
|
|
|
68
|
32 |
|
foreach (array_keys($this->backup) as $k) { |
69
|
32 |
|
$this->$k = array(); |
70
|
32 |
|
} |
71
|
32 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Restore the backed up routes. |
75
|
|
|
*/ |
76
|
32 |
|
public function restoreRouteCollection() |
77
|
|
|
{ |
78
|
32 |
|
$this->restoreRoutes($this->getBackup(), true); |
79
|
32 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the routes in a form that can be saved to the cache. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
32 |
|
public function getCacheableRoutes() |
87
|
|
|
{ |
88
|
32 |
|
return serialize($this->getCacheableRouteContents()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the data that should be sent to the cache. |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
32 |
|
public function getCacheableRouteContents() |
97
|
|
|
{ |
98
|
32 |
|
return array_except(get_object_vars($this), array('backup')); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Restore a set of cached routes into this collection. |
103
|
|
|
* |
104
|
|
|
* @param string $cache |
105
|
|
|
*/ |
106
|
28 |
|
public function restoreRouteCache($cache) |
107
|
|
|
{ |
108
|
28 |
|
$routes = unserialize($cache); |
109
|
|
|
|
110
|
28 |
|
$this->restoreRoutes($routes); |
111
|
28 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Add a set of routes back into this collection. |
115
|
|
|
* |
116
|
|
|
* @param bool $prepend |
117
|
|
|
* @param array $routes |
118
|
|
|
*/ |
119
|
32 |
|
protected function restoreRoutes($routes, $prepend = false) |
120
|
|
|
{ |
121
|
32 |
|
foreach ($routes as $k => $v) { |
122
|
32 |
|
if ($k === 'routes') { |
123
|
32 |
|
$this->$k = $prepend === false ? |
124
|
32 |
|
$this->mergeGroupedRoutes($this->$k, $v) : |
125
|
32 |
|
$this->mergeGroupedRoutes($v, $this->$k); |
126
|
32 |
|
} else { |
127
|
32 |
|
$this->$k = $prepend === false ? |
128
|
32 |
|
$this->mergeRoutes($this->$k, $v) : |
129
|
32 |
|
$this->mergeRoutes($v, $this->$k); |
130
|
|
|
} |
131
|
32 |
|
} |
132
|
32 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Merge two array, each containing routes grouped by method. |
136
|
|
|
* |
137
|
|
|
* @param array |
138
|
|
|
* @param array |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
32 |
|
public function mergeGroupedRoutes(array $r1, array $r2) |
142
|
|
|
{ |
143
|
32 |
|
$methods = array('GET', 'POST', 'HEAD', 'PATH', 'PUT', 'DELETE'); |
144
|
32 |
|
foreach ($methods as $method) { |
145
|
32 |
|
if (isset($r2[$method]) === false) { |
146
|
32 |
|
continue; |
147
|
|
|
} |
148
|
32 |
|
if (isset($r1[$method]) === false) { |
149
|
32 |
|
$r1[$method] = array(); |
150
|
32 |
|
} |
151
|
|
|
|
152
|
32 |
|
$r1[$method] = $this->mergeRoutes($r1[$method], $r2[$method]); |
153
|
32 |
|
} |
154
|
|
|
|
155
|
32 |
|
return $r1; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Merge two arrays, each containing routes. |
160
|
|
|
* |
161
|
|
|
* @param array |
162
|
|
|
* @param array |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
32 |
|
public function mergeRoutes(array $r1, array $r2) |
166
|
|
|
{ |
167
|
32 |
|
foreach ($r2 as $uri => $route) { |
168
|
32 |
|
$r1[$uri] = $route; |
169
|
32 |
|
} |
170
|
|
|
|
171
|
32 |
|
return $r1; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|