Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
77 / 77 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
RouteGenerator | |
100.00% |
77 / 77 |
|
100.00% |
8 / 8 |
15 | |
100.00% |
1 / 1 |
addDynamicRoute | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
addDynamicRoutes | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
addNonTabRecordAction | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
addNonTabRecordActions | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
addRecordRoute | |
100.00% |
34 / 34 |
|
100.00% |
1 / 1 |
2 | |||
addRecordRoutes | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
addStaticRoute | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
addStaticRoutes | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Route Generator Class |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Route |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/development Wiki |
28 | */ |
29 | |
30 | namespace VuFind\Route; |
31 | |
32 | /** |
33 | * Route Generator Class |
34 | * |
35 | * The data model object representing a user's book cart. |
36 | * |
37 | * @category VuFind |
38 | * @package Route |
39 | * @author Demian Katz <demian.katz@villanova.edu> |
40 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
41 | * @link https://vufind.org/wiki/development Wiki |
42 | */ |
43 | class RouteGenerator |
44 | { |
45 | /** |
46 | * Record sub-routes are generally used to access tab plug-ins, but a few |
47 | * URLs are hard-coded to specific actions; this array lists those actions. |
48 | * |
49 | * @var array |
50 | */ |
51 | protected static $nonTabRecordActions = []; |
52 | |
53 | /** |
54 | * Cache for already added recordActions which need to be used again |
55 | * if additional nonTabRecordActions will be added later. |
56 | * |
57 | * @var array |
58 | */ |
59 | protected static $recordRoutes = []; |
60 | |
61 | /** |
62 | * Add a dynamic route to the configuration. |
63 | * |
64 | * @param array $config Configuration array to update |
65 | * @param string $routeName Name of route to generate |
66 | * @param string $controller Controller name |
67 | * @param string $action Action and any dynamic parts |
68 | * |
69 | * @return void |
70 | */ |
71 | public function addDynamicRoute(&$config, $routeName, $controller, $action) |
72 | { |
73 | [$actionName] = explode('/', $action, 2); |
74 | $config['router']['routes'][$routeName] = [ |
75 | 'type' => 'Laminas\Router\Http\Segment', |
76 | 'options' => [ |
77 | 'route' => "/$controller/$action", |
78 | 'constraints' => [ |
79 | 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', |
80 | 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', |
81 | ], |
82 | 'defaults' => [ |
83 | 'controller' => $controller, |
84 | 'action' => $actionName, |
85 | ], |
86 | ], |
87 | ]; |
88 | } |
89 | |
90 | /** |
91 | * Add dynamic routes to the configuration. |
92 | * |
93 | * @param array $config Configuration array to update |
94 | * @param array $routes Associative array of arrays |
95 | * (controller => [route name => action]) of routes to add. |
96 | * |
97 | * @return void |
98 | */ |
99 | public function addDynamicRoutes(&$config, $routes) |
100 | { |
101 | // Build library card routes |
102 | foreach ($routes as $controller => $controllerRoutes) { |
103 | foreach ($controllerRoutes as $routeName => $action) { |
104 | $this->addDynamicRoute($config, $routeName, $controller, $action); |
105 | } |
106 | } |
107 | } |
108 | |
109 | /** |
110 | * Add non tab record action & re-register all record routes to support it. |
111 | * |
112 | * @param array $config Configuration array to update |
113 | * @param string $action Action to add |
114 | * |
115 | * @return void |
116 | */ |
117 | public function addNonTabRecordAction(&$config, $action) |
118 | { |
119 | self::$nonTabRecordActions[$action] = $action; |
120 | foreach (self::$recordRoutes as $recordRoute) { |
121 | $this->addRecordRoute( |
122 | $config, |
123 | $recordRoute['routeBase'], |
124 | $recordRoute['controller'] |
125 | ); |
126 | } |
127 | } |
128 | |
129 | /** |
130 | * Add non tab record actions & re-register all record routes to support it. |
131 | * |
132 | * @param array $config Configuration array to update |
133 | * @param array $actions Action to add |
134 | * |
135 | * @return void |
136 | */ |
137 | public function addNonTabRecordActions(&$config, $actions) |
138 | { |
139 | foreach ($actions as $action) { |
140 | $this->addNonTabRecordAction($config, $action); |
141 | } |
142 | } |
143 | |
144 | /** |
145 | * Add record route to the configuration. |
146 | * |
147 | * @param array $config Configuration array to update |
148 | * @param string $routeBase Base name to use for routes |
149 | * @param string $controller Controller to point routes toward |
150 | * |
151 | * @return void |
152 | */ |
153 | public function addRecordRoute(&$config, $routeBase, $controller) |
154 | { |
155 | // catch-all "tab" route: |
156 | $config['router']['routes'][$routeBase] = [ |
157 | 'type' => 'Laminas\Router\Http\Segment', |
158 | 'options' => [ |
159 | 'route' => '/' . $controller . '/[:id[/[:tab]]]', |
160 | 'constraints' => [ |
161 | 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', |
162 | 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', |
163 | 'tab' => '[a-zA-Z][a-zA-Z0-9_-]*', |
164 | ], |
165 | 'defaults' => [ |
166 | 'controller' => $controller, |
167 | 'action' => 'Home', |
168 | ], |
169 | ], |
170 | ]; |
171 | // special non-tab actions that each need their own route: |
172 | foreach (self::$nonTabRecordActions as $action) { |
173 | $config['router']['routes'][$routeBase . '-' . strtolower($action)] = [ |
174 | 'type' => 'Laminas\Router\Http\Segment', |
175 | 'options' => [ |
176 | 'route' => '/' . $controller . '/[:id]/' . $action, |
177 | 'constraints' => [ |
178 | 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', |
179 | 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', |
180 | ], |
181 | 'defaults' => [ |
182 | 'controller' => $controller, |
183 | 'action' => $action, |
184 | ], |
185 | ], |
186 | ]; |
187 | } |
188 | |
189 | // Store the added route in case we need to add |
190 | // more nonTabRecordActions later |
191 | self::$recordRoutes["$controller::$routeBase"] = [ |
192 | 'routeBase' => $routeBase, |
193 | 'controller' => $controller, |
194 | ]; |
195 | } |
196 | |
197 | /** |
198 | * Add record routes to the configuration. |
199 | * |
200 | * @param array $config Configuration array to update |
201 | * @param array $routes Associative array (route base name => controller) of |
202 | * routes to add. |
203 | * |
204 | * @return void |
205 | */ |
206 | public function addRecordRoutes(&$config, $routes) |
207 | { |
208 | foreach ($routes as $routeBase => $controller) { |
209 | $this->addRecordRoute($config, $routeBase, $controller); |
210 | } |
211 | } |
212 | |
213 | /** |
214 | * Add a simple static route to the configuration. |
215 | * |
216 | * @param array $config Configuration array to update |
217 | * @param string $route Controller/Action string representing route |
218 | * |
219 | * @return void |
220 | */ |
221 | public function addStaticRoute(&$config, $route) |
222 | { |
223 | [$controller, $action] = explode('/', $route); |
224 | $routeName = str_replace('/', '-', strtolower($route)); |
225 | $config['router']['routes'][$routeName] = [ |
226 | 'type' => 'Laminas\Router\Http\Literal', |
227 | 'options' => [ |
228 | 'route' => '/' . $route, |
229 | 'defaults' => [ |
230 | 'controller' => $controller, |
231 | 'action' => $action, |
232 | ], |
233 | ], |
234 | ]; |
235 | } |
236 | |
237 | /** |
238 | * Add simple static routes to the configuration. |
239 | * |
240 | * @param array $config Configuration array to update |
241 | * @param array $routes Array of Controller/Action strings representing routes |
242 | * |
243 | * @return void |
244 | */ |
245 | public function addStaticRoutes(&$config, $routes) |
246 | { |
247 | foreach ($routes as $route) { |
248 | $this->addStaticRoute($config, $route); |
249 | } |
250 | } |
251 | } |