PluginProbe
Depicter — Popup & Slider Builder / 1.2.0
Depicter — Popup & Slider Builder v1.2.0
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / htmlburger / wpemerge / src / Routing / RouteBlueprint.php

RouteBlueprint.php in Depicter — Popup & Slider Builder 1.2.0, at vendor/htmlburger/wpemerge/src/Routing/RouteBlueprint.php

269 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package WPEmerge
4 * @author Atanas Angelov <hi@atanas.dev>
5 * @copyright 2017-2019 Atanas Angelov
6 * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7 * @link https://wpemerge.com/
8 */
9
10 namespace WPEmerge\Routing;
11
12 use Closure;
13 use WPEmerge\Helpers\HasAttributesTrait;
14 use WPEmerge\Routing\Conditions\ConditionInterface;
15 use WPEmerge\View\ViewService;
16
17 /**
18 * Provide a fluent interface for registering routes with the router.
19 */
20 class RouteBlueprint {
21 use HasAttributesTrait;
22
23 /**
24 * Router.
25 *
26 * @var Router
27 */
28 protected $router = null;
29
30 /**
31 * View service.
32 *
33 * @var ViewService
34 */
35 protected $view_service = null;
36
37 /**
38 * Constructor.
39 *
40 * @codeCoverageIgnore
41 * @param Router $router
42 * @param ViewService $view_service
43 */
44 public function __construct( Router $router, ViewService $view_service ) {
45 $this->router = $router;
46 $this->view_service = $view_service;
47 }
48
49 /**
50 * Match requests using one of the specified methods.
51 *
52 * @param string[] $methods
53 * @return static $this
54 */
55 public function methods( $methods ) {
56 $methods = $this->router->mergeMethodsAttribute(
57 (array) $this->getAttribute( 'methods', [] ),
58 (array) $methods
59 );
60
61 return $this->attribute( 'methods', $methods );
62 }
63
64 /**
65 * Set the condition attribute to a URL.
66 *
67 * @param string $url
68 * @param array<string, string> $where
69 * @return static $this
70 */
71 public function url( $url, $where = [] ) {
72 return $this->where( 'url', $url, $where );
73 }
74
75 /**
76 * Set the condition attribute.
77 *
78 * @param string|array|ConditionInterface $condition
79 * @param mixed ,...$arguments
80 * @return static $this
81 */
82 public function where( $condition ) {
83 if ( ! $condition instanceof ConditionInterface ) {
84 $condition = func_get_args();
85 }
86
87 $condition = $this->router->mergeConditionAttribute(
88 $this->getAttribute( 'condition', null ),
89 $condition
90 );
91
92 return $this->attribute( 'condition', $condition );
93 }
94
95 /**
96 * Set the middleware attribute.
97 *
98 * @param string|string[] $middleware
99 * @return static $this
100 */
101 public function middleware( $middleware ) {
102 $middleware = $this->router->mergeMiddlewareAttribute(
103 (array) $this->getAttribute( 'middleware', [] ),
104 (array) $middleware
105 );
106
107 return $this->attribute( 'middleware', $middleware );
108 }
109
110 /**
111 * Set the namespace attribute.
112 * This should be renamed to namespace for consistency once minimum PHP
113 * version is increased to 7+.
114 *
115 * @param string $namespace
116 * @return static $this
117 */
118 public function setNamespace( $namespace ) {
119 $namespace = $this->router->mergeNamespaceAttribute(
120 $this->getAttribute( 'namespace', '' ),
121 $namespace
122 );
123
124 return $this->attribute( 'namespace', $namespace );
125 }
126
127 /**
128 * Set the query attribute.
129 *
130 * @param callable $query
131 * @return static $this
132 */
133 public function query( $query ) {
134 $query = $this->router->mergeQueryAttribute(
135 $this->getAttribute( 'query', null ),
136 $query
137 );
138
139 return $this->attribute( 'query', $query );
140 }
141
142 /**
143 * Set the name attribute.
144 *
145 * @param string $name
146 * @return static $this
147 */
148 public function name( $name ) {
149 return $this->attribute( 'name', $name );
150 }
151
152 /**
153 * Create a route group.
154 *
155 * @param Closure|string $routes Closure or path to file.
156 * @return void
157 */
158 public function group( $routes ) {
159 $this->router->group( $this->getAttributes(), $routes );
160 }
161
162 /**
163 * Create a route.
164 *
165 * @param string|Closure $handler
166 * @return void
167 */
168 public function handle( $handler = '' ) {
169 if ( ! empty( $handler ) ) {
170 $this->attribute( 'handler', $handler );
171 }
172
173 $route = $this->router->route( $this->getAttributes() );
174
175 $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1 );
176
177 if ( ! empty( $trace ) && ! empty( $trace[0]['file'] ) ) {
178 $route->attribute( '__definition', $trace[0]['file'] . ':' . $trace[0]['line'] );
179 }
180
181 $this->router->addRoute( $route );
182 }
183
184 /**
185 * Handle a request by directly rendering a view.
186 *
187 * @param string|string[] $views
188 * @return void
189 */
190 public function view( $views ) {
191 $this->handle( function () use ( $views ) {
192 return $this->view_service->make( $views );
193 } );
194 }
195
196 /**
197 * Match ALL requests.
198 *
199 * @param string|Closure $handler
200 * @return void
201 */
202 public function all( $handler = '' ) {
203 $this->any()->url( '*' )->handle( $handler );
204 }
205
206 /**
207 * Match requests with a method of GET or HEAD.
208 *
209 * @return static $this
210 */
211 public function get() {
212 return $this->methods( ['GET', 'HEAD'] );
213 }
214
215 /**
216 * Match requests with a method of POST.
217 *
218 * @return static $this
219 */
220 public function post() {
221 return $this->methods( ['POST'] );
222 }
223
224 /**
225 * Match requests with a method of PUT.
226 *
227 * @return static $this
228 */
229 public function put() {
230 return $this->methods( ['PUT'] );
231 }
232
233 /**
234 * Match requests with a method of PATCH.
235 *
236 * @return static $this
237 */
238 public function patch() {
239 return $this->methods( ['PATCH'] );
240 }
241
242 /**
243 * Match requests with a method of DELETE.
244 *
245 * @return static $this
246 */
247 public function delete() {
248 return $this->methods( ['DELETE'] );
249 }
250
251 /**
252 * Match requests with a method of OPTIONS.
253 *
254 * @return static $this
255 */
256 public function options() {
257 return $this->methods( ['OPTIONS'] );
258 }
259
260 /**
261 * Match requests with any method.
262 *
263 * @return static $this
264 */
265 public function any() {
266 return $this->methods( ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'] );
267 }
268 }
269