Passed
Push — master ( 667c74...5caefd )
by Alexander
04:36
created
src/components/Routing/Route.php 1 patch
Indentation   +837 added lines, -837 removed lines patch added patch discarded remove patch
@@ -43,857 +43,857 @@
 block discarded – undo
43 43
  */
44 44
 class Route 
45 45
 {
46
-	use Concerns\RouteCondition,
47
-	    Concerns\RouteDependencyResolver;
48
-
49
-	/**
50
-	 * The validators used by the routes.
51
-	 * 
52
-	 * @var array $validators
53
-	 */
54
-	public static $validators;
46
+    use Concerns\RouteCondition,
47
+        Concerns\RouteDependencyResolver;
48
+
49
+    /**
50
+     * The validators used by the routes.
51
+     * 
52
+     * @var array $validators
53
+     */
54
+    public static $validators;
55 55
 	
56
-	/**
57
-	 * Action that the route will use when called.
58
-	 *
59
-	 * @var \Closure|string|array $action
60
-	 */
61
-	public $action;
62
-
63
-	/**
64
-	 * The compiled version of the route.
65
-	 * 
66
-	 * @var \Syscodes\Bundles\ApplicationBundle\Routing\CompiledRoute|string $compiled
67
-	 */
68
-	public $compiled;
69
-
70
-	/**
71
-	 * The computed gathered middleware.
72
-	 * 
73
-	 * @var array|null $computedMiddleware
74
-	 */
75
-	public $computedMiddleware;
76
-
77
-	/**
78
-	 * The container instance used by the route.
79
-	 * 
80
-	 * @var \Syscodes\Components\Container\Container $container
81
-	 */
82
-	protected $container;
83
-
84
-	/**
85
-	 * The controller instance.
86
-	 * 
87
-	 * @var string $controller
88
-	 */
89
-	public $controller;
90
-
91
-	/**
92
-	 * The default values for the route.
93
-	 * 
94
-	 * @var array $defaults
95
-	 */
96
-	public $defaults = [];
97
-
98
-	/**
99
-	 * Indicates whether the route is a fallback route.
100
-	 * 
101
-	 * @var bool $fallback
102
-	 */
103
-	protected $fallback = false;
104
-
105
-	/**
106
-	 * Variable of HTTP method.
107
-	 *  
108
-	 * @var array|string $method
109
-	 */
110
-	public $method;
111
-
112
-	/**
113
-	 * The array of matched parameters.
114
-	 * 
115
-	 * @var array $parameters
116
-	 */
117
-	public $parameters = [];
118
-
119
-	/**
120
-	 * The parameter names for the route.
121
-	 * 
122
-	 * @var string|null $parameterNames
123
-	 */
124
-	public $parameterNames;
125
-
126
-	/**
127
-	 * The URI pattern the route responds to.
128
-	 *
129
-	 * @var string $uri
130
-	 */
131
-	public $uri;
132
-
133
-	/**
134
-	 * Contains the arguments of the current route.
135
-	 *
136
-	 * @var array $where
137
-	 */
138
-	public $wheres = [];
139
-
140
-	/**
141
-	 * Constructor. Initialize route.
142
-	 *
143
-	 * @param  array|string  $method  
144
-	 * @param  string  $uri  
145
-	 * @param  \Closure|array  $action  
146
-	 *
147
-	 * @return void
148
-	 */
149
-	public function __construct($method, $uri, $action)
150
-	{
151
-		$this->uri = $uri;
152
-
153
-		// Set the method
154
-		$this->method = $this->parseMethod($method);
155
-
156
-		// Set the action
157
-		$this->action = Arr::except($this->parseAction($action), ['prefix']);
56
+    /**
57
+     * Action that the route will use when called.
58
+     *
59
+     * @var \Closure|string|array $action
60
+     */
61
+    public $action;
62
+
63
+    /**
64
+     * The compiled version of the route.
65
+     * 
66
+     * @var \Syscodes\Bundles\ApplicationBundle\Routing\CompiledRoute|string $compiled
67
+     */
68
+    public $compiled;
69
+
70
+    /**
71
+     * The computed gathered middleware.
72
+     * 
73
+     * @var array|null $computedMiddleware
74
+     */
75
+    public $computedMiddleware;
76
+
77
+    /**
78
+     * The container instance used by the route.
79
+     * 
80
+     * @var \Syscodes\Components\Container\Container $container
81
+     */
82
+    protected $container;
83
+
84
+    /**
85
+     * The controller instance.
86
+     * 
87
+     * @var string $controller
88
+     */
89
+    public $controller;
90
+
91
+    /**
92
+     * The default values for the route.
93
+     * 
94
+     * @var array $defaults
95
+     */
96
+    public $defaults = [];
97
+
98
+    /**
99
+     * Indicates whether the route is a fallback route.
100
+     * 
101
+     * @var bool $fallback
102
+     */
103
+    protected $fallback = false;
104
+
105
+    /**
106
+     * Variable of HTTP method.
107
+     *  
108
+     * @var array|string $method
109
+     */
110
+    public $method;
111
+
112
+    /**
113
+     * The array of matched parameters.
114
+     * 
115
+     * @var array $parameters
116
+     */
117
+    public $parameters = [];
118
+
119
+    /**
120
+     * The parameter names for the route.
121
+     * 
122
+     * @var string|null $parameterNames
123
+     */
124
+    public $parameterNames;
125
+
126
+    /**
127
+     * The URI pattern the route responds to.
128
+     *
129
+     * @var string $uri
130
+     */
131
+    public $uri;
132
+
133
+    /**
134
+     * Contains the arguments of the current route.
135
+     *
136
+     * @var array $where
137
+     */
138
+    public $wheres = [];
139
+
140
+    /**
141
+     * Constructor. Initialize route.
142
+     *
143
+     * @param  array|string  $method  
144
+     * @param  string  $uri  
145
+     * @param  \Closure|array  $action  
146
+     *
147
+     * @return void
148
+     */
149
+    public function __construct($method, $uri, $action)
150
+    {
151
+        $this->uri = $uri;
152
+
153
+        // Set the method
154
+        $this->method = $this->parseMethod($method);
155
+
156
+        // Set the action
157
+        $this->action = Arr::except($this->parseAction($action), ['prefix']);
158 158
 		
159
-		if (in_array('GET', $this->method) && ! in_array('HEAD', $this->method)) {
160
-			$this->method[] = 'HEAD';
161
-		}
162
-
163
-		$this->prefix(is_array($action) ? Arr::get($action, 'prefix') : '');
164
-	}
165
-
166
-	/**
167
-	 * Get the controller instance for the route.
168
-	 * 
169
-	 * @return mixed
170
-	 */
171
-	public function getController()
172
-	{
173
-		if ( ! $this->controller) {
174
-			$class = $this->parseControllerCallback()[0];
159
+        if (in_array('GET', $this->method) && ! in_array('HEAD', $this->method)) {
160
+            $this->method[] = 'HEAD';
161
+        }
162
+
163
+        $this->prefix(is_array($action) ? Arr::get($action, 'prefix') : '');
164
+    }
165
+
166
+    /**
167
+     * Get the controller instance for the route.
168
+     * 
169
+     * @return mixed
170
+     */
171
+    public function getController()
172
+    {
173
+        if ( ! $this->controller) {
174
+            $class = $this->parseControllerCallback()[0];
175 175
  
176
-			$this->controller = $this->container->make(ltrim($class, '\\'));
177
-		}
178
-
179
-		return $this->controller;
180
-	}
181
-
182
-	/**
183
-	 * Get the controller method used for the route.
184
-	 * 
185
-	 * @return string
186
-	 */
187
-	public function getControllerMethod(): string
188
-	{
189
-		return $this->parseControllerCallback()[1];
190
-	}
191
-
192
-	/**
193
-	 * Get or set the domain for the route.
194
-	 * 
195
-	 * @param  string|null  $domain  
196
-	 * 
197
-	 * @return mixed
198
-	 */
199
-	public function domain($domain = null)
200
-	{
201
-		if (is_null($domain)) {
202
-			return $this->getDomain();
203
-		}
176
+            $this->controller = $this->container->make(ltrim($class, '\\'));
177
+        }
178
+
179
+        return $this->controller;
180
+    }
181
+
182
+    /**
183
+     * Get the controller method used for the route.
184
+     * 
185
+     * @return string
186
+     */
187
+    public function getControllerMethod(): string
188
+    {
189
+        return $this->parseControllerCallback()[1];
190
+    }
191
+
192
+    /**
193
+     * Get or set the domain for the route.
194
+     * 
195
+     * @param  string|null  $domain  
196
+     * 
197
+     * @return mixed
198
+     */
199
+    public function domain($domain = null)
200
+    {
201
+        if (is_null($domain)) {
202
+            return $this->getDomain();
203
+        }
204 204
 		
205
-		$parsed = RouteUri::parse($domain);
206
-
207
-		$this->action['domain'] = $parsed->uri;
208
-
209
-		return $this;
210
-	}
211
-
212
-	/**
213
-	 * Get the domain defined for the route.
214
-	 * 
215
-	 * @return string|null
216
-	 */
217
-	public function getDomain()
218
-	{
219
-		return isset($this->action['domain'])
220
-				? str_replace(['http://', 'https://'], '', $this->action['domain'])
221
-				: null;
222
-	}
223
-
224
-	/**
225
-	 * Parse the controller.
226
-	 * 
227
-	 * @return array
228
-	 */
229
-	public function parseControllerCallback(): array
230
-	{
231
-		return Str::parseCallback($this->action['uses']);
232
-	}
205
+        $parsed = RouteUri::parse($domain);
206
+
207
+        $this->action['domain'] = $parsed->uri;
208
+
209
+        return $this;
210
+    }
211
+
212
+    /**
213
+     * Get the domain defined for the route.
214
+     * 
215
+     * @return string|null
216
+     */
217
+    public function getDomain()
218
+    {
219
+        return isset($this->action['domain'])
220
+                ? str_replace(['http://', 'https://'], '', $this->action['domain'])
221
+                : null;
222
+    }
223
+
224
+    /**
225
+     * Parse the controller.
226
+     * 
227
+     * @return array
228
+     */
229
+    public function parseControllerCallback(): array
230
+    {
231
+        return Str::parseCallback($this->action['uses']);
232
+    }
233 233
 	
234
-	/**
235
-	 * Checks whether the route's action is a controller.
236
-	 * 
237
-	 * @return bool
238
-	 */
239
-	public function isControllerAction(): bool
240
-	{
241
-		return is_string($this->action['uses']);
242
-	}
243
-
244
-	/**
245
-	 * Get the dispatcher for the route's controller.
246
-	 * 
247
-	 * @return \Syscodes\Components\Routing\ControllerDispatcher
248
-	 */
249
-	private function controllerDispatcher(): ControllerDispatcher
250
-	{
251
-		return new ControllerDispatcher($this->container);
252
-	}
234
+    /**
235
+     * Checks whether the route's action is a controller.
236
+     * 
237
+     * @return bool
238
+     */
239
+    public function isControllerAction(): bool
240
+    {
241
+        return is_string($this->action['uses']);
242
+    }
243
+
244
+    /**
245
+     * Get the dispatcher for the route's controller.
246
+     * 
247
+     * @return \Syscodes\Components\Routing\ControllerDispatcher
248
+     */
249
+    private function controllerDispatcher(): ControllerDispatcher
250
+    {
251
+        return new ControllerDispatcher($this->container);
252
+    }
253 253
 	
254
-	/**
255
-	 * Run the route action and return the response.
256
-	 * 
257
-	 * @return mixed
258
-	 */
259
-	public function runResolver()
260
-	{
261
-		$this->container = $this->container ?: new Container;
262
-
263
-		try {
264
-			if ($this->isControllerAction()) {
265
-				return $this->runResolverController();
266
-			}
267
-
268
-			return $this->runResolverCallable();
269
-		} catch (HttpResponseException $e) {
270
-			return $e->getResponse();
271
-		}
272
-	}
273
-
274
-	/**
275
-	 * Run the route action and return the response.
276
-	 *  
277
-	 * @return mixed
278
-	 */
279
-	protected function runResolverCallable()
280
-	{
281
-		$callable = $this->action['uses'];
282
-
283
-		return $callable(...array_values($this->resolveMethodDependencies(
284
-			$this->parametersWithouNulls(), new ReflectionFunction($callable)
285
-		)));
286
-	}
287
-
288
-	/**
289
-	 * Run the route action and return the response.
290
-	 * 
291
-	 * @return mixed
292
-	 */
293
-	protected function runResolverController()
294
-	{
295
-		return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod());
296
-	}
297
-
298
-	/**
299
-	 * Determine if the route matches a given request.
300
-	 * 
301
-	 * @param  \Syscodes\Components\Http\Request  $request
302
-	 * @param  bool  $method
303
-	 * 
304
-	 * @return bool
305
-	 */
306
-	public function matches(Request $request, bool $method = true): bool
307
-	{
308
-		$this->compileRoute();
309
-
310
-		foreach (self::getValidators() as $validator) {
311
-			if ($method && $validator instanceof MethodValidator) {
312
-				continue;
313
-			}
314
-
315
-			if ( ! $validator->matches($this, $request)) {
316
-				return false;
317
-			}
318
-		}
319
-
320
-		return true;
321
-	}
322
-
323
-	/**
324
-	 * Get the route validators for the instance.
325
-	 * 
326
-	 * @return array
327
-	 */
328
-	public function getValidators(): array
329
-	{
330
-		if (isset(static::$validators)) {
331
-			return static::$validators;
332
-		}
333
-
334
-		return static::$validators = [
335
-			new HostValidator, new MethodValidator,
336
-			new SchemeValidator, new UriValidator
337
-		];
338
-	}
339
-
340
-	/**
341
-	 * Parse the route action.
342
-	 *
343
-	 * @param  \callable|array|null  $action
344
-	 *
345
-	 * @return array
346
-	 *
347
-	 * @throws \InvalidArgumentException
348
-	 */
349
-	protected function parseAction($action): array
350
-	{
351
-		if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) {
352
-			throw new InvalidArgumentException(__('route.actionClosureOrFunction'));
353
-		}
354
-
355
-		return RouteAction::parse($this->uri, $action);
356
-	}
357
-
358
-	/**
359
-	 * Set the method of the current route.
360
-	 *
361
-	 * @param  array  $method
362
-	 *
363
-	 * @return array
364
-	 * 
365
-	 * @throws \InvalidArgumentException
366
-	 */
367
-	public function parseMethod($method): array
368
-	{
369
-		if ($method === null || empty($method)) {
370
-			throw new InvalidArgumentException(__('route.methodNotProvided'));			
371
-		}
372
-
373
-		foreach ((array) $method as $httpMethod) {
374
-			if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) {
375
-				throw new InvalidArgumentException(__('route.methodNotAllowed'));				
376
-			}
377
-		}
378
-
379
-	    return array_map('strtoupper', (array) $method);
380
-	}
381
-
382
-	/**
383
-	 * Set the URI that the route responds to.
384
-	 *
385
-	 * @param  string|null  $uri
386
-	 *
387
-	 * @return static
388
-	 *
389
-	 * @throws  \InvalidArgumentException
390
-	 */
391
-	public function setUri($uri): static
392
-	{
393
-		if ($uri === null) {
394
-			throw new InvalidArgumentException(__('route.uriNotProvided'));
395
-		}	
396
-
397
-		$this->uri = $this->parseUri($uri);
398
-
399
-		return $this;
400
-	}
254
+    /**
255
+     * Run the route action and return the response.
256
+     * 
257
+     * @return mixed
258
+     */
259
+    public function runResolver()
260
+    {
261
+        $this->container = $this->container ?: new Container;
262
+
263
+        try {
264
+            if ($this->isControllerAction()) {
265
+                return $this->runResolverController();
266
+            }
267
+
268
+            return $this->runResolverCallable();
269
+        } catch (HttpResponseException $e) {
270
+            return $e->getResponse();
271
+        }
272
+    }
273
+
274
+    /**
275
+     * Run the route action and return the response.
276
+     *  
277
+     * @return mixed
278
+     */
279
+    protected function runResolverCallable()
280
+    {
281
+        $callable = $this->action['uses'];
282
+
283
+        return $callable(...array_values($this->resolveMethodDependencies(
284
+            $this->parametersWithouNulls(), new ReflectionFunction($callable)
285
+        )));
286
+    }
287
+
288
+    /**
289
+     * Run the route action and return the response.
290
+     * 
291
+     * @return mixed
292
+     */
293
+    protected function runResolverController()
294
+    {
295
+        return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod());
296
+    }
297
+
298
+    /**
299
+     * Determine if the route matches a given request.
300
+     * 
301
+     * @param  \Syscodes\Components\Http\Request  $request
302
+     * @param  bool  $method
303
+     * 
304
+     * @return bool
305
+     */
306
+    public function matches(Request $request, bool $method = true): bool
307
+    {
308
+        $this->compileRoute();
309
+
310
+        foreach (self::getValidators() as $validator) {
311
+            if ($method && $validator instanceof MethodValidator) {
312
+                continue;
313
+            }
314
+
315
+            if ( ! $validator->matches($this, $request)) {
316
+                return false;
317
+            }
318
+        }
319
+
320
+        return true;
321
+    }
322
+
323
+    /**
324
+     * Get the route validators for the instance.
325
+     * 
326
+     * @return array
327
+     */
328
+    public function getValidators(): array
329
+    {
330
+        if (isset(static::$validators)) {
331
+            return static::$validators;
332
+        }
333
+
334
+        return static::$validators = [
335
+            new HostValidator, new MethodValidator,
336
+            new SchemeValidator, new UriValidator
337
+        ];
338
+    }
339
+
340
+    /**
341
+     * Parse the route action.
342
+     *
343
+     * @param  \callable|array|null  $action
344
+     *
345
+     * @return array
346
+     *
347
+     * @throws \InvalidArgumentException
348
+     */
349
+    protected function parseAction($action): array
350
+    {
351
+        if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) {
352
+            throw new InvalidArgumentException(__('route.actionClosureOrFunction'));
353
+        }
354
+
355
+        return RouteAction::parse($this->uri, $action);
356
+    }
357
+
358
+    /**
359
+     * Set the method of the current route.
360
+     *
361
+     * @param  array  $method
362
+     *
363
+     * @return array
364
+     * 
365
+     * @throws \InvalidArgumentException
366
+     */
367
+    public function parseMethod($method): array
368
+    {
369
+        if ($method === null || empty($method)) {
370
+            throw new InvalidArgumentException(__('route.methodNotProvided'));			
371
+        }
372
+
373
+        foreach ((array) $method as $httpMethod) {
374
+            if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) {
375
+                throw new InvalidArgumentException(__('route.methodNotAllowed'));				
376
+            }
377
+        }
378
+
379
+        return array_map('strtoupper', (array) $method);
380
+    }
381
+
382
+    /**
383
+     * Set the URI that the route responds to.
384
+     *
385
+     * @param  string|null  $uri
386
+     *
387
+     * @return static
388
+     *
389
+     * @throws  \InvalidArgumentException
390
+     */
391
+    public function setUri($uri): static
392
+    {
393
+        if ($uri === null) {
394
+            throw new InvalidArgumentException(__('route.uriNotProvided'));
395
+        }	
396
+
397
+        $this->uri = $this->parseUri($uri);
398
+
399
+        return $this;
400
+    }
401 401
 	
402
-	/**
403
-	 * Parse the route URI and normalize.
404
-	 * 
405
-	 * @param  string  $uri
406
-	 * 
407
-	 * @return string
408
-	 */
409
-	protected function parseUri($uri): string
410
-	{
411
-		return take(RouteUri::parse($uri), fn ($uri) => $uri)->uri;
412
-	}
413
-
414
-	/**
415
-	 * Add a prefix to the route URI.
416
-	 * 
417
-	 * @param  string  $prefix
418
-	 * 
419
-	 * @return static
420
-	 */
421
-	public function prefix($prefix): static
422
-	{
423
-		if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) {
424
-			$this->action['prefix'] = $newPrefix;
425
-		}
402
+    /**
403
+     * Parse the route URI and normalize.
404
+     * 
405
+     * @param  string  $uri
406
+     * 
407
+     * @return string
408
+     */
409
+    protected function parseUri($uri): string
410
+    {
411
+        return take(RouteUri::parse($uri), fn ($uri) => $uri)->uri;
412
+    }
413
+
414
+    /**
415
+     * Add a prefix to the route URI.
416
+     * 
417
+     * @param  string  $prefix
418
+     * 
419
+     * @return static
420
+     */
421
+    public function prefix($prefix): static
422
+    {
423
+        if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) {
424
+            $this->action['prefix'] = $newPrefix;
425
+        }
426 426
 		
427
-		$uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/');
427
+        $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/');
428 428
 		
429
-		return $this->setUri($uri !== '/' ? trim($uri, '/') : $uri);
430
-	}
431
-
432
-	/**
433
-	 * Set the action array for the route.
434
-	 * 
435
-	 * @param  array  $action
436
-	 * 
437
-	 * @return mixed
438
-	 */
439
-	public function setAction(array $action)
440
-	{
441
-		$this->action = $action;
442
-
443
-		if (isset($this->action['domain'])) {
444
-			$this->domain($this->action['domain']);
445
-		}
429
+        return $this->setUri($uri !== '/' ? trim($uri, '/') : $uri);
430
+    }
431
+
432
+    /**
433
+     * Set the action array for the route.
434
+     * 
435
+     * @param  array  $action
436
+     * 
437
+     * @return mixed
438
+     */
439
+    public function setAction(array $action)
440
+    {
441
+        $this->action = $action;
442
+
443
+        if (isset($this->action['domain'])) {
444
+            $this->domain($this->action['domain']);
445
+        }
446 446
 		
447
-		return $this;
448
-	}
449
-
450
-	/**
451
-	 * Set the name.
452
-	 *
453
-	 * @param  string  $name
454
-	 *
455
-	 * @return static
456
-	 */
457
-	public function name($name): static
458
-	{
459
-		$this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name;
460
-
461
-		return $this;
462
-	}
463
-
464
-	/**
465
-	 * Determine whether the route's name matches the given patterns.
466
-	 * 
467
-	 * @param  mixed  ...$patterns
468
-	 * 
469
-	 * @return bool
470
-	 */
471
-	public function named(...$patterns): bool
472
-	{
473
-		if (is_null($routeName = $this->getName())) {
474
-			return false;
475
-		}
476
-
477
-		foreach ($patterns as $pattern) {
478
-			if (Str::is($pattern, $routeName)) {
479
-				return true;
480
-			}
481
-		}
482
-
483
-		return false;
484
-	}
485
-
486
-	/**
487
-	 * Set a default value for the route.
488
-	 * 
489
-	 * @param  string  $key
490
-	 * @param  mixed   $value
491
-	 * 
492
-	 * @return static
493
-	 */
494
-	public function defaults($key, $value): static
495
-	{
496
-		$this->defaults[$key] = $value;
497
-
498
-		return $this;
499
-	}
500
-
501
-	/**
502
-	 * Set a default values for the route.
503
-	 * 
504
-	 * @param  array  $defaults
505
-	 * 
506
-	 * @return static
507
-	 */
508
-	public function setDefaults(array $defaults): static
509
-	{
510
-		$this->defaults = $defaults;
511
-
512
-		return $this;
513
-	}
514
-
515
-	/**
516
-	 * Set the flag of fallback mode on the route.
517
-	 * 
518
-	 * @return static
519
-	 */
520
-	public function fallback(): static
521
-	{
522
-		$this->fallback = true;
523
-
524
-		return $this;
525
-	}
526
-
527
-	/**
528
-	 * Set the facllback value.
529
-	 * 
530
-	 * @param  bool  $fallback
531
-	 * 
532
-	 * @return static
533
-	 */
534
-	public function setFallback(bool $fallback): static
535
-	{
536
-		$this->fallback = $fallback;
537
-
538
-		return $this;
539
-	}
540
-
541
-	/**
542
-	 * Set the where.
543
-	 *
544
-	 * @param  array|string  $name
545
-	 * @param  string|null  $expression  
546
-	 *
547
-	 * @return static
548
-	 */
549
-	public function where($name, string $expression = null): static
550
-	{
551
-		$wheres = is_array($name) ? $name : [$name => $expression];
447
+        return $this;
448
+    }
449
+
450
+    /**
451
+     * Set the name.
452
+     *
453
+     * @param  string  $name
454
+     *
455
+     * @return static
456
+     */
457
+    public function name($name): static
458
+    {
459
+        $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name;
460
+
461
+        return $this;
462
+    }
463
+
464
+    /**
465
+     * Determine whether the route's name matches the given patterns.
466
+     * 
467
+     * @param  mixed  ...$patterns
468
+     * 
469
+     * @return bool
470
+     */
471
+    public function named(...$patterns): bool
472
+    {
473
+        if (is_null($routeName = $this->getName())) {
474
+            return false;
475
+        }
476
+
477
+        foreach ($patterns as $pattern) {
478
+            if (Str::is($pattern, $routeName)) {
479
+                return true;
480
+            }
481
+        }
482
+
483
+        return false;
484
+    }
485
+
486
+    /**
487
+     * Set a default value for the route.
488
+     * 
489
+     * @param  string  $key
490
+     * @param  mixed   $value
491
+     * 
492
+     * @return static
493
+     */
494
+    public function defaults($key, $value): static
495
+    {
496
+        $this->defaults[$key] = $value;
497
+
498
+        return $this;
499
+    }
500
+
501
+    /**
502
+     * Set a default values for the route.
503
+     * 
504
+     * @param  array  $defaults
505
+     * 
506
+     * @return static
507
+     */
508
+    public function setDefaults(array $defaults): static
509
+    {
510
+        $this->defaults = $defaults;
511
+
512
+        return $this;
513
+    }
514
+
515
+    /**
516
+     * Set the flag of fallback mode on the route.
517
+     * 
518
+     * @return static
519
+     */
520
+    public function fallback(): static
521
+    {
522
+        $this->fallback = true;
523
+
524
+        return $this;
525
+    }
526
+
527
+    /**
528
+     * Set the facllback value.
529
+     * 
530
+     * @param  bool  $fallback
531
+     * 
532
+     * @return static
533
+     */
534
+    public function setFallback(bool $fallback): static
535
+    {
536
+        $this->fallback = $fallback;
537
+
538
+        return $this;
539
+    }
540
+
541
+    /**
542
+     * Set the where.
543
+     *
544
+     * @param  array|string  $name
545
+     * @param  string|null  $expression  
546
+     *
547
+     * @return static
548
+     */
549
+    public function where($name, string $expression = null): static
550
+    {
551
+        $wheres = is_array($name) ? $name : [$name => $expression];
552 552
 		
553
-		foreach ($wheres as $name => $expression) {
554
-			$this->wheres[$name] = $expression;
555
-		}
556
-
557
-		return $this;
558
-	}
559
-
560
-	/**
561
-	 * Set the where when have a variable assign.
562
-	 * 
563
-	 * @param  string  $key
564
-	 * 
565
-	 * @return string|null
566
-	 */
567
-	public function setPattern(string $key): ?string
568
-	{
569
-		return $this->wheres[$key] ?? null;
570
-	}
571
-
572
-	/**
573
-	 * Bind the route to a given request for execution.
574
-	 * 
575
-	 * @param  \Syscodes\Components\Http\Request  $request
576
-	 * 
577
-	 * @return static
578
-	 */
579
-	public function bind(Request $request): static
580
-	{
581
-		$this->compileRoute();
553
+        foreach ($wheres as $name => $expression) {
554
+            $this->wheres[$name] = $expression;
555
+        }
556
+
557
+        return $this;
558
+    }
559
+
560
+    /**
561
+     * Set the where when have a variable assign.
562
+     * 
563
+     * @param  string  $key
564
+     * 
565
+     * @return string|null
566
+     */
567
+    public function setPattern(string $key): ?string
568
+    {
569
+        return $this->wheres[$key] ?? null;
570
+    }
571
+
572
+    /**
573
+     * Bind the route to a given request for execution.
574
+     * 
575
+     * @param  \Syscodes\Components\Http\Request  $request
576
+     * 
577
+     * @return static
578
+     */
579
+    public function bind(Request $request): static
580
+    {
581
+        $this->compileRoute();
582 582
 		
583
-		$this->parameters = (new RouteParameter($this))->parameters($request);
584
-
585
-		return $this;
586
-	}
587
-
588
-	/**
589
-	 * Compile into a Route Compile instance.
590
-	 * 
591
-	 * @return \Syscodes\Bundles\ApplicationBundle\Routing\RouteCompiler
592
-	 */
593
-	protected function compileRoute()
594
-	{
595
-		if ( ! $this->compiled) {
596
-			$this->compiled = $this->toBundleRoute()->compile();
597
-		}
598
-
599
-		return $this->compiled;
600
-	}
601
-
602
-	/**
603
-	 * Get all of the parameter names for the route.
604
-	 * 
605
-	 * @return array
606
-	 */
607
-	public function parameterNames(): array
608
-	{
609
-		if (isset($this->parameterNames)) {
610
-			return $this->parameterNames;
611
-		}
612
-
613
-		return $this->parameterNames = $this->compileParameterNames();
614
-	}
615
-
616
-	/**
617
-	 * Get the parameter names for the route.
618
-	 * 
619
-	 * @return array
620
-	 */
621
-	protected function compileParameterNames(): array
622
-	{
623
-		preg_match_all('~\{(.*?)\}~', $this->getDomain().$this->getUri(), $matches);
624
-
625
-		return array_map(fn ($match) => trim($match, '?'), $matches[1]);
626
-	}
627
-
628
-	/**
629
-	 * Get a given parameter from the route.
630
-	 * 
631
-	 * @param  string  $name
632
-	 * @param  mixed  $default  
633
-	 * 
634
-	 * @return mixed
635
-	 */
636
-	public function parameter($name, $default = null)
637
-	{
638
-		return Arr::get($this->parameters(), $name, $default);
639
-	}
640
-
641
-	/**
642
-	 * Set a parameter to the given value.
643
-	 * 
644
-	 * @param  string  $name
645
-	 * @param  mixed  $value
646
-	 * 
647
-	 * @return void
648
-	 */
649
-	public function setParameter($name, $value): void
650
-	{
651
-		$this->parameters();
652
-
653
-		$this->parameters[$name] = $value;
654
-	}
655
-
656
-	/**
657
-	 * Get the key / value list of parameters without null values.
658
-	 * 
659
-	 * @return array
660
-	 */
661
-	public function parametersWithouNulls(): array
662
-	{
663
-		return array_filter($this->parameters(), fn ($parameter) => ! is_null($parameter));
664
-	}
665
-
666
-	/**
667
-	 * Get the key / value list of parameters for the route.
668
-	 * 
669
-	 * @return array
670
-	 */
671
-	public function parameters(): array
672
-	{
673
-		if (isset($this->parameters)) {
674
-			return $this->parameters;
675
-		}
676
-
677
-		throw new LogicException('The route is not bound');
678
-	}
679
-
680
-	/**
681
-	 * Get all middleware, including the ones from the controller.
682
-	 * 
683
-	 * @return array
684
-	 */
685
-	public function gatherMiddleware(): array
686
-	{
687
-		if ( ! is_null($this->computedMiddleware)) {
688
-			return $this->computedMiddleware;
689
-		}
690
-
691
-		$this->computedMiddleware = [];
692
-
693
-		return $this->computedMiddleware = Router::uniqueMiddleware(array_merge(
694
-			$this->middleware(),
695
-			$this->controllerMiddleware()
696
-		));
697
-	}
698
-
699
-	/**
700
-	 * Get or set the middlewares attached to the route.
701
-	 * 
702
-	 * @param  array|string|null  $middleware
703
-	 * 
704
-	 * @return array|static
705
-	 */
706
-	public function middleware($middleware = null)
707
-	{
708
-		if (is_null($middleware)) {
709
-			return $this->getMiddleware();
710
-		}
711
-
712
-		if (is_string($middleware)) {
713
-			$middleware = func_get_args();
714
-		}
715
-
716
-		foreach ($middleware as $index => $value) {
717
-			$middleware[$index] = (string) $value;
718
-		}
719
-
720
-		$this->action['middleware'] = array_merge(
721
-			$this->getMiddleware(),
722
-			$middleware
723
-		);
724
-
725
-		return $this;
726
-	}
727
-
728
-	/**
729
-	 * Get the middlewares attached to the route.
730
-	 * 
731
-	 * @return array
732
-	 */
733
-	protected function getMiddleware(): array
734
-	{
735
-		return (array) ($this->action['middleware'] ?? []);
736
-	}
737
-
738
-	/**
739
-	 * Get the middleware for the route's controller.
740
-	 * 
741
-	 * @return array
742
-	 */
743
-	public function controllerMiddleware(): array
744
-	{
745
-		if ( ! $this->isControllerAction()) {
746
-			return [];
747
-		}
748
-
749
-		return $this->controllerDispatcher()->getMiddleware(
750
-			$this->getController(),
751
-			$this->getControllerMethod()
752
-		);
753
-	}
754
-
755
-	/**
756
-	 * Determine if the route only responds to HTTP requests.
757
-	 * 
758
-	 * @return bool
759
-	 */
760
-	public function httpOnly(): bool
761
-	{
762
-		return in_array('http', $this->action, true);
763
-	}
764
-
765
-	/**
766
-	 * Determine if the route only responds to HTTPS requests.
767
-	 * 
768
-	 * @return bool
769
-	 */
770
-	public function httpsOnly(): bool
771
-	{
772
-		return $this->secure();
773
-	}
774
-
775
-	/**
776
-	 * Determine if the route only responds to HTTPS requests.
777
-	 * 
778
-	 * @return bool
779
-	 */
780
-	public function secure(): bool
781
-	{
782
-		return in_array('https', $this->action, true);
783
-	}
583
+        $this->parameters = (new RouteParameter($this))->parameters($request);
584
+
585
+        return $this;
586
+    }
587
+
588
+    /**
589
+     * Compile into a Route Compile instance.
590
+     * 
591
+     * @return \Syscodes\Bundles\ApplicationBundle\Routing\RouteCompiler
592
+     */
593
+    protected function compileRoute()
594
+    {
595
+        if ( ! $this->compiled) {
596
+            $this->compiled = $this->toBundleRoute()->compile();
597
+        }
598
+
599
+        return $this->compiled;
600
+    }
601
+
602
+    /**
603
+     * Get all of the parameter names for the route.
604
+     * 
605
+     * @return array
606
+     */
607
+    public function parameterNames(): array
608
+    {
609
+        if (isset($this->parameterNames)) {
610
+            return $this->parameterNames;
611
+        }
612
+
613
+        return $this->parameterNames = $this->compileParameterNames();
614
+    }
615
+
616
+    /**
617
+     * Get the parameter names for the route.
618
+     * 
619
+     * @return array
620
+     */
621
+    protected function compileParameterNames(): array
622
+    {
623
+        preg_match_all('~\{(.*?)\}~', $this->getDomain().$this->getUri(), $matches);
624
+
625
+        return array_map(fn ($match) => trim($match, '?'), $matches[1]);
626
+    }
627
+
628
+    /**
629
+     * Get a given parameter from the route.
630
+     * 
631
+     * @param  string  $name
632
+     * @param  mixed  $default  
633
+     * 
634
+     * @return mixed
635
+     */
636
+    public function parameter($name, $default = null)
637
+    {
638
+        return Arr::get($this->parameters(), $name, $default);
639
+    }
640
+
641
+    /**
642
+     * Set a parameter to the given value.
643
+     * 
644
+     * @param  string  $name
645
+     * @param  mixed  $value
646
+     * 
647
+     * @return void
648
+     */
649
+    public function setParameter($name, $value): void
650
+    {
651
+        $this->parameters();
652
+
653
+        $this->parameters[$name] = $value;
654
+    }
655
+
656
+    /**
657
+     * Get the key / value list of parameters without null values.
658
+     * 
659
+     * @return array
660
+     */
661
+    public function parametersWithouNulls(): array
662
+    {
663
+        return array_filter($this->parameters(), fn ($parameter) => ! is_null($parameter));
664
+    }
665
+
666
+    /**
667
+     * Get the key / value list of parameters for the route.
668
+     * 
669
+     * @return array
670
+     */
671
+    public function parameters(): array
672
+    {
673
+        if (isset($this->parameters)) {
674
+            return $this->parameters;
675
+        }
676
+
677
+        throw new LogicException('The route is not bound');
678
+    }
679
+
680
+    /**
681
+     * Get all middleware, including the ones from the controller.
682
+     * 
683
+     * @return array
684
+     */
685
+    public function gatherMiddleware(): array
686
+    {
687
+        if ( ! is_null($this->computedMiddleware)) {
688
+            return $this->computedMiddleware;
689
+        }
690
+
691
+        $this->computedMiddleware = [];
692
+
693
+        return $this->computedMiddleware = Router::uniqueMiddleware(array_merge(
694
+            $this->middleware(),
695
+            $this->controllerMiddleware()
696
+        ));
697
+    }
698
+
699
+    /**
700
+     * Get or set the middlewares attached to the route.
701
+     * 
702
+     * @param  array|string|null  $middleware
703
+     * 
704
+     * @return array|static
705
+     */
706
+    public function middleware($middleware = null)
707
+    {
708
+        if (is_null($middleware)) {
709
+            return $this->getMiddleware();
710
+        }
711
+
712
+        if (is_string($middleware)) {
713
+            $middleware = func_get_args();
714
+        }
715
+
716
+        foreach ($middleware as $index => $value) {
717
+            $middleware[$index] = (string) $value;
718
+        }
719
+
720
+        $this->action['middleware'] = array_merge(
721
+            $this->getMiddleware(),
722
+            $middleware
723
+        );
724
+
725
+        return $this;
726
+    }
727
+
728
+    /**
729
+     * Get the middlewares attached to the route.
730
+     * 
731
+     * @return array
732
+     */
733
+    protected function getMiddleware(): array
734
+    {
735
+        return (array) ($this->action['middleware'] ?? []);
736
+    }
737
+
738
+    /**
739
+     * Get the middleware for the route's controller.
740
+     * 
741
+     * @return array
742
+     */
743
+    public function controllerMiddleware(): array
744
+    {
745
+        if ( ! $this->isControllerAction()) {
746
+            return [];
747
+        }
748
+
749
+        return $this->controllerDispatcher()->getMiddleware(
750
+            $this->getController(),
751
+            $this->getControllerMethod()
752
+        );
753
+    }
754
+
755
+    /**
756
+     * Determine if the route only responds to HTTP requests.
757
+     * 
758
+     * @return bool
759
+     */
760
+    public function httpOnly(): bool
761
+    {
762
+        return in_array('http', $this->action, true);
763
+    }
764
+
765
+    /**
766
+     * Determine if the route only responds to HTTPS requests.
767
+     * 
768
+     * @return bool
769
+     */
770
+    public function httpsOnly(): bool
771
+    {
772
+        return $this->secure();
773
+    }
774
+
775
+    /**
776
+     * Determine if the route only responds to HTTPS requests.
777
+     * 
778
+     * @return bool
779
+     */
780
+    public function secure(): bool
781
+    {
782
+        return in_array('https', $this->action, true);
783
+    }
784 784
 	
785
-	/**
786
-	 * Convert the route to a bundle route.
787
-	 * 
788
-	 * @return \Syscodes\Bundles\ApplicationBundle\Routing\Route
789
-	 */
790
-	public function toBundleRoute()
791
-	{
792
-		return new BundleRoute(
793
-		            preg_replace('~\{(\w+?)\?\}~', '{$1}', $this->getUri()),
794
-		            $this->getOptionalParameterNames(),
795
-		            $this->getPatterns(),
796
-		            $this->getDomain() ?: ''
797
-		       );
798
-	}
785
+    /**
786
+     * Convert the route to a bundle route.
787
+     * 
788
+     * @return \Syscodes\Bundles\ApplicationBundle\Routing\Route
789
+     */
790
+    public function toBundleRoute()
791
+    {
792
+        return new BundleRoute(
793
+                    preg_replace('~\{(\w+?)\?\}~', '{$1}', $this->getUri()),
794
+                    $this->getOptionalParameterNames(),
795
+                    $this->getPatterns(),
796
+                    $this->getDomain() ?: ''
797
+                );
798
+    }
799 799
 	
800
-	/**
801
-	 * Get the optional parameter names for the route.
802
-	 * 
803
-	 * @return array
804
-	 */
805
-	protected function getOptionalParameterNames(): array
806
-	{
807
-		preg_match_all('~\{(\w+?)\?\}~', $this->getUri(), $matches);
800
+    /**
801
+     * Get the optional parameter names for the route.
802
+     * 
803
+     * @return array
804
+     */
805
+    protected function getOptionalParameterNames(): array
806
+    {
807
+        preg_match_all('~\{(\w+?)\?\}~', $this->getUri(), $matches);
808 808
 		
809
-		return isset($matches[1]) ? array_fill_keys($matches[1], null) : [];
810
-	}
811
-
812
-	/**
813
-	 * Get the action of the current route.
814
-	 *
815
-	 * @return \Closure|string|array
816
-	 */
817
-	public function getAction()
818
-	{
819
-		return $this->action;
820
-	}
809
+        return isset($matches[1]) ? array_fill_keys($matches[1], null) : [];
810
+    }
811
+
812
+    /**
813
+     * Get the action of the current route.
814
+     *
815
+     * @return \Closure|string|array
816
+     */
817
+    public function getAction()
818
+    {
819
+        return $this->action;
820
+    }
821 821
 	
822
-	/**
823
-	 * Get the compiled version of the route.
824
-	 * 
825
-	 * @return \Syscodes\Bundles\ApplicationBundle\Routing\CompiledRoute
826
-	 */
827
-	public function getCompiled()
828
-	{
829
-		return $this->compiled;
830
-	}
831
-
832
-	/**
833
-	 * Get the URI associated with the route.
834
-	 *
835
-	 * @return string
836
-	 */
837
-	public function getUri(): string
838
-	{
839
-		return $this->uri;
840
-	}
841
-
842
-	/**
843
-	 * Get the patterns of the current route.
844
-	 *
845
-	 * @return array
846
-	 */
847
-	public function getPatterns(): array
848
-	{
849
-		return $this->wheres;
850
-	}
851
-
852
-	/**
853
-	 * Get the request method of the current route.
854
-	 *
855
-	 * @return array|string
856
-	 */
857
-	public function getMethod(): array|string
858
-	{
859
-		return $this->method;
860
-	}
861
-
862
-	/**
863
-	 * Get the url of the current route.
864
-	 *
865
-	 * @return string|null
866
-	 */
867
-	public function getName(): ?string
868
-	{
869
-		return $this->action['as'] ?? null;
870
-	}
871
-
872
-	/**
873
-	 * Set the container instance on the route.
874
-	 * 
875
-	 * @param  \Syscodes\Components\Container\Container  $container
876
-	 * 
877
-	 * @return static
878
-	 */
879
-	public function setContainer(Container $container): static
880
-	{
881
-		$this->container = $container;
882
-
883
-		return $this;
884
-	}
885
-
886
-	/**
887
-	 * Magic method.
888
-	 * 
889
-	 * Dynamically access route parameters.
890
-	 * 
891
-	 * @param  string  $key
892
-	 * 
893
-	 * @return mixed
894
-	 */
895
-	public function __get($key)
896
-	{
897
-		return $this->parameter($key);
898
-	}
822
+    /**
823
+     * Get the compiled version of the route.
824
+     * 
825
+     * @return \Syscodes\Bundles\ApplicationBundle\Routing\CompiledRoute
826
+     */
827
+    public function getCompiled()
828
+    {
829
+        return $this->compiled;
830
+    }
831
+
832
+    /**
833
+     * Get the URI associated with the route.
834
+     *
835
+     * @return string
836
+     */
837
+    public function getUri(): string
838
+    {
839
+        return $this->uri;
840
+    }
841
+
842
+    /**
843
+     * Get the patterns of the current route.
844
+     *
845
+     * @return array
846
+     */
847
+    public function getPatterns(): array
848
+    {
849
+        return $this->wheres;
850
+    }
851
+
852
+    /**
853
+     * Get the request method of the current route.
854
+     *
855
+     * @return array|string
856
+     */
857
+    public function getMethod(): array|string
858
+    {
859
+        return $this->method;
860
+    }
861
+
862
+    /**
863
+     * Get the url of the current route.
864
+     *
865
+     * @return string|null
866
+     */
867
+    public function getName(): ?string
868
+    {
869
+        return $this->action['as'] ?? null;
870
+    }
871
+
872
+    /**
873
+     * Set the container instance on the route.
874
+     * 
875
+     * @param  \Syscodes\Components\Container\Container  $container
876
+     * 
877
+     * @return static
878
+     */
879
+    public function setContainer(Container $container): static
880
+    {
881
+        $this->container = $container;
882
+
883
+        return $this;
884
+    }
885
+
886
+    /**
887
+     * Magic method.
888
+     * 
889
+     * Dynamically access route parameters.
890
+     * 
891
+     * @param  string  $key
892
+     * 
893
+     * @return mixed
894
+     */
895
+    public function __get($key)
896
+    {
897
+        return $this->parameter($key);
898
+    }
899 899
 }
900 900
\ No newline at end of file
Please login to merge, or discard this patch.
components/Debug/Exceptions/Resources/views/partials/frames/frame_list.php 1 patch
Braces   +5 added lines, -2 removed lines patch added patch discarded remove patch
@@ -8,7 +8,8 @@  discard block
 block discarded – undo
8 8
 			<div class="frame-info-class">
9 9
 			<?php if ($frame->getClass() == '') : ?>
10 10
 				<span class="frame-function"><?= e($frame->getFunction()) ?></span>
11
-			<?php else: ?>
11
+			<?php else {
12
+    : ?>
12 13
 				<span class="frame-class"><?= e($frame->getClass()) ?></span>
13 14
 				<?php if ($frame->getFunction() != '') : ?>
14 15
 				<span class="frame-function"><?= e($frame->getFunction()) ?></span>
@@ -20,5 +21,7 @@  discard block
 block discarded – undo
20 21
 			<span class="frame-line"><?= (int) $frame->getLine() ?></span>
21 22
 		</div>		
22 23
 	</div>		
23
-<?php endforeach; ?>	
24
+<?php endforeach;
25
+}
26
+?>	
24 27
 </aside>
25 28
\ No newline at end of file
Please login to merge, or discard this patch.
src/components/Debug/GDebug.php 1 patch
Indentation   +394 added lines, -394 removed lines patch added patch discarded remove patch
@@ -38,414 +38,414 @@
 block discarded – undo
38 38
  */
39 39
 class GDebug implements DebugContract
40 40
 {
41
-	/**
42
-	 * Allow Handlers to force the script to quit.
43
-	 * 
44
-	 * @var bool $allowQuit
45
-	 */
46
-	protected $allowQuit = true;
41
+    /**
42
+     * Allow Handlers to force the script to quit.
43
+     * 
44
+     * @var bool $allowQuit
45
+     */
46
+    protected $allowQuit = true;
47 47
 	
48
-	/**
49
-	 * Benchmark instance.
50
-	 * 
51
-	 * @var string|object $benchmark
52
-	 */
53
-	protected $benchmark;
54
-
55
-	/**
56
-	 * The handler stack.
57
-	 * 
58
-	 * @var array $handlerStack
59
-	 */
60
-	protected $handlerStack = [];
61
-
62
-	/**
63
-	 * The send Http code by default: 500 Internal Server Error.
64
-	 * 
65
-	 * @var bool $sendHttpCode
66
-	 */
67
-	protected $sendHttpCode = 500;
68
-
69
-	/**
70
-	 * The send output.
71
-	 * 
72
-	 * @var bool $sendOutput
73
-	 */
74
-	protected $sendOutput = true;
75
-
76
-	/**
77
-	 * The functions of system what control errors and exceptions.
78
-	 * 
79
-	 * @var string|object $system
80
-	 */
81
-	protected $system;
82
-
83
-	/**
84
-	 * In certain scenarios, like in shutdown handler, we can not throw exceptions.
85
-	 * 
86
-	 * @var bool $throwExceptions
87
-	 */
88
-	protected $throwExceptions = true;
89
-
90
-	/**
91
-	 * Constructor. The Debug class instance.
92
-	 * 
93
-	 * @param  \Syscodes\Components\Debug\Util\System|null  $system
94
-	 * 
95
-	 * @return void
96
-	 */
97
-	public function __construct(System $system = null)
98
-	{
99
-		$this->system    = $system ?: new System;
100
-		$this->benchmark = new Benchmark;
101
-	}
102
-
103
-	/**
104
-	 * Catches any uncaught errors and exceptions, including most Fatal errors. Will log the 
105
-	 * error, display it if display_errors is on, and fire an event that allows custom actions 
106
-	 * to be taken at this point.
107
-	 *
108
-	 * @param  \Throwable  $exception
109
-	 *
110
-	 * @return string
111
-	 */
112
-	public function handleException(Throwable $exception): string
113
-	{	
114
-		// The start benchmark
115
-		$this->benchmark->start('total_execution', LENEVOR_START);
116
-
117
-		$supervisor = $this->getSupervisor($exception);
118
-
119
-		// Start buffer
120
-		$this->system->startOutputBuferring();
121
-
122
-		$handlerResponse    = null;
123
-		$handlerContentType = null;
48
+    /**
49
+     * Benchmark instance.
50
+     * 
51
+     * @var string|object $benchmark
52
+     */
53
+    protected $benchmark;
54
+
55
+    /**
56
+     * The handler stack.
57
+     * 
58
+     * @var array $handlerStack
59
+     */
60
+    protected $handlerStack = [];
61
+
62
+    /**
63
+     * The send Http code by default: 500 Internal Server Error.
64
+     * 
65
+     * @var bool $sendHttpCode
66
+     */
67
+    protected $sendHttpCode = 500;
68
+
69
+    /**
70
+     * The send output.
71
+     * 
72
+     * @var bool $sendOutput
73
+     */
74
+    protected $sendOutput = true;
75
+
76
+    /**
77
+     * The functions of system what control errors and exceptions.
78
+     * 
79
+     * @var string|object $system
80
+     */
81
+    protected $system;
82
+
83
+    /**
84
+     * In certain scenarios, like in shutdown handler, we can not throw exceptions.
85
+     * 
86
+     * @var bool $throwExceptions
87
+     */
88
+    protected $throwExceptions = true;
89
+
90
+    /**
91
+     * Constructor. The Debug class instance.
92
+     * 
93
+     * @param  \Syscodes\Components\Debug\Util\System|null  $system
94
+     * 
95
+     * @return void
96
+     */
97
+    public function __construct(System $system = null)
98
+    {
99
+        $this->system    = $system ?: new System;
100
+        $this->benchmark = new Benchmark;
101
+    }
102
+
103
+    /**
104
+     * Catches any uncaught errors and exceptions, including most Fatal errors. Will log the 
105
+     * error, display it if display_errors is on, and fire an event that allows custom actions 
106
+     * to be taken at this point.
107
+     *
108
+     * @param  \Throwable  $exception
109
+     *
110
+     * @return string
111
+     */
112
+    public function handleException(Throwable $exception): string
113
+    {	
114
+        // The start benchmark
115
+        $this->benchmark->start('total_execution', LENEVOR_START);
116
+
117
+        $supervisor = $this->getSupervisor($exception);
118
+
119
+        // Start buffer
120
+        $this->system->startOutputBuferring();
121
+
122
+        $handlerResponse    = null;
123
+        $handlerContentType = null;
124 124
 		
125
-		try {
126
-			foreach (array_reverse($this->handlerStack) as $handler) {			
127
-				$handler->setDebug($this);
128
-				$handler->setException($exception);
129
-				$handler->setSupervisor($supervisor);
125
+        try {
126
+            foreach (array_reverse($this->handlerStack) as $handler) {			
127
+                $handler->setDebug($this);
128
+                $handler->setException($exception);
129
+                $handler->setSupervisor($supervisor);
130 130
 				
131
-				$handlerResponse = $handler->handle();
131
+                $handlerResponse = $handler->handle();
132 132
 	
133
-				// Collect the content type for possible sending in the headers
134
-				$handlerContentType = method_exists($handler, 'contentType') ? $handler->contentType() : null;
133
+                // Collect the content type for possible sending in the headers
134
+                $handlerContentType = method_exists($handler, 'contentType') ? $handler->contentType() : null;
135 135
 	
136
-				if (in_array($handlerResponse, [MainHandler::LAST_HANDLER, MainHandler::QUIT])) {
137
-					break;
138
-				}
139
-			}
136
+                if (in_array($handlerResponse, [MainHandler::LAST_HANDLER, MainHandler::QUIT])) {
137
+                    break;
138
+                }
139
+            }
140 140
 	
141
-			$quit = $handlerResponse == MainHandler::QUIT && $this->allowQuit();
142
-		}
143
-		finally {
144
-			// Returns the contents of the output buffer
145
-			$output = $this->system->CleanOutputBuffer();	
146
-		}
147
-
148
-		// Returns the contents of the output buffer for loading time of page
149
-		$totalTime = $this->benchmark->getElapsedTime('total_execution');
150
-		$output    = str_replace('{{ elapsed_time }}', $totalTime, $output);
151
-
152
-		if ($this->writeToOutput()) {
153
-			if ($quit) {
154
-				while ($this->system->getOutputBufferLevel() > 0) {
155
-					// Cleanes the output buffer
156
-					$this->system->endOutputBuffering();
157
-				}
158
-
159
-				if (Misc::sendHeaders() && $handlerContentType)	{
160
-					header("Content-Type: {$handlerContentType}");
161
-				}
162
-			}
163
-
164
-			$this->writeToOutputBuffer($output);
165
-		}
166
-
167
-		if ($quit) {
168
-			$this->system->flushOutputBuffer();
169
-			$this->system->stopException($this->sendHttpCode());
170
-		}
171
-
172
-		return $output;
173
-	}
174
-
175
-	/**
176
-	 * Allow Handlers to force the script to quit.
177
-	 * 
178
-	 * @param  bool|int|null  $exit
179
-	 * 
180
-	 * @return bool
181
-	 */
182
-	public function allowQuit($exit = null): bool
183
-	{
184
-		if (func_num_args() == 0) {
185
-			return $this->allowQuit;
186
-		}
187
-
188
-		return $this->allowQuit = (bool) $exit;
189
-	}
190
-
191
-	/**
192
-	 * Lenevor Exception push output directly to the client it the data  
193
-	 * if they are true, but if it is false, the output will be returned 
194
-	 * by exception.
195
-	 * 
196
-	 * @param  bool|int|null  $send
197
-	 *
198
-	 * @return bool
199
-	 */
200
-	public function writeToOutput($send = null): bool
201
-	{
202
-		if (func_num_args() == 0) {
203
-			return $this->sendOutput;
204
-		}
141
+            $quit = $handlerResponse == MainHandler::QUIT && $this->allowQuit();
142
+        }
143
+        finally {
144
+            // Returns the contents of the output buffer
145
+            $output = $this->system->CleanOutputBuffer();	
146
+        }
147
+
148
+        // Returns the contents of the output buffer for loading time of page
149
+        $totalTime = $this->benchmark->getElapsedTime('total_execution');
150
+        $output    = str_replace('{{ elapsed_time }}', $totalTime, $output);
151
+
152
+        if ($this->writeToOutput()) {
153
+            if ($quit) {
154
+                while ($this->system->getOutputBufferLevel() > 0) {
155
+                    // Cleanes the output buffer
156
+                    $this->system->endOutputBuffering();
157
+                }
158
+
159
+                if (Misc::sendHeaders() && $handlerContentType)	{
160
+                    header("Content-Type: {$handlerContentType}");
161
+                }
162
+            }
163
+
164
+            $this->writeToOutputBuffer($output);
165
+        }
166
+
167
+        if ($quit) {
168
+            $this->system->flushOutputBuffer();
169
+            $this->system->stopException($this->sendHttpCode());
170
+        }
171
+
172
+        return $output;
173
+    }
174
+
175
+    /**
176
+     * Allow Handlers to force the script to quit.
177
+     * 
178
+     * @param  bool|int|null  $exit
179
+     * 
180
+     * @return bool
181
+     */
182
+    public function allowQuit($exit = null): bool
183
+    {
184
+        if (func_num_args() == 0) {
185
+            return $this->allowQuit;
186
+        }
187
+
188
+        return $this->allowQuit = (bool) $exit;
189
+    }
190
+
191
+    /**
192
+     * Lenevor Exception push output directly to the client it the data  
193
+     * if they are true, but if it is false, the output will be returned 
194
+     * by exception.
195
+     * 
196
+     * @param  bool|int|null  $send
197
+     *
198
+     * @return bool
199
+     */
200
+    public function writeToOutput($send = null): bool
201
+    {
202
+        if (func_num_args() == 0) {
203
+            return $this->sendOutput;
204
+        }
205 205
 		
206
-		return $this->sendOutput = (bool) $send;
207
-	}
206
+        return $this->sendOutput = (bool) $send;
207
+    }
208 208
 	
209
-	/**
210
-	 * Generate output to the browser.
211
-	 * 
212
-	 * @param  string  $output
213
-	 * 
214
-	 * @return static
215
-	 */
216
-	protected function writeToOutputBuffer($output): static
217
-	{
218
-		if ($this->sendHttpCode() && Misc::sendHeaders()) {
219
-			$this->system->setHttpResponseCode($this->sendHttpCode());
220
-		}
209
+    /**
210
+     * Generate output to the browser.
211
+     * 
212
+     * @param  string  $output
213
+     * 
214
+     * @return static
215
+     */
216
+    protected function writeToOutputBuffer($output): static
217
+    {
218
+        if ($this->sendHttpCode() && Misc::sendHeaders()) {
219
+            $this->system->setHttpResponseCode($this->sendHttpCode());
220
+        }
221 221
 		
222
-		echo $output;
222
+        echo $output;
223 223
 		
224
-		return $this;
225
-	}
226
-
227
-	/**
228
-	 * Error handler
229
-	 *
230
-	 * This will catch the php native error and treat it as a exception which will 
231
-	 * provide a full back trace on all errors.
232
-	 *
233
-	 * @param  int  $level
234
-	 * @param  string  $message
235
-	 * @param  string|null  $file
236
-	 * @param  int|null  $line
237
-	 * 
238
-	 * @return bool
239
-	 * 
240
-	 * @throws \ErrorException
241
-	 */
242
-	public function handleError(
243
-		int $level, 
244
-		string $message, 
245
-		string $file = null, 
246
-		int $line = null
247
-	): bool {
248
-		if ($level & $this->system->getErrorReportingLevel()) {
249
-			$exception = new ErrorException($message, $level, $level, $file, $line);
250
-
251
-			if ($this->throwExceptions) {
252
-				throw $exception;
253
-			} else {
254
-				$this->handleException($exception);
255
-			}
256
-
257
-			return true;
258
-		}
259
-
260
-		return false;
261
-	}
262
-
263
-	/**
264
-	 * Appends a handler to the end of the stack.
265
-	 * 
266
-	 * @param  \Callable|\Syscodes\Components\Contracts\Debug\Handler  $handler
267
-	 * 
268
-	 * @return static
269
-	 */
270
-	public function appendHandler($handler): static
271
-	{
272
-		array_unshift($this->handlerStack, $this->resolveHandler($handler));
273
-
274
-		return $this;
275
-	}
276
-
277
-	/**
278
-	 * Prepends a handler to the start of the stack.
279
-	 * 
280
-	 * @param  \Callable|\Syscodes\Components\Contracts\Debug\Handler  $handler
281
-	 * 
282
-	 * @return static
283
-	 */
284
-	public function prependHandler($handler): static
285
-	{
286
-		return $this->pushHandler($handler);
287
-	}
288
-
289
-	/**
290
-	 * Pushes a handler to the end of the stack.
291
-	 * 
292
-	 * @param  string|callable  $handler
293
-	 * 
294
-	 * @return static
295
-	 */
296
-	public function pushHandler($handler): static
297
-	{
298
-		$this->handlerStack[] = $this->resolveHandler($handler);
299
-
300
-		return $this;
301
-	}
302
-
303
-	/**
304
-	 * Create a CallbackHandler from callable and throw if handler is invalid.
305
-	 * 
306
-	 * @param  \Callable|\Syscodes\Components\Contracts\Debug\Handler  $handler
307
-	 * 
308
-	 * @return \Syscodes\Components\Contracts\Debug\Handler
309
-	 * 
310
-	 * @throws \InvalidArgumentException If argument is not callable or instance of \Syscodes\Components\Contracts\Debug\Handler
311
-	 */
312
-	protected function resolveHandler($handler)
313
-	{
314
-		if (is_callable($handler)) {
315
-			$handler = new CallbackHandler($handler);
316
-		}
317
-
318
-		if ( ! $handler instanceof MainHandler) {
319
-			throw new InvalidArgumentException(
320
-				"Argument to " . __METHOD__ . " must be a callable, or instance of ".
321
-				"Syscodes\Components\\Contracts\\Debug\\Handler"
322
-			);
323
-		}
324
-
325
-		return $handler;
326
-	}
327
-
328
-	/**
329
-	 * Returns an array with all handlers, in the order they were added to the stack.
330
-	 * 
331
-	 * @return array
332
-	 */
333
-	public function getHandlers(): array
334
-	{
335
-		return $this->handlerStack;
336
-	}
337
-
338
-	/**
339
-	 * Clears all handlers in the handlerStack, including the default PleasingPage handler.
340
-	 * 
341
-	 * @return static
342
-	 */
343
-	public function clearHandlers(): static
344
-	{
345
-		$this->handlerStack = [];
346
-
347
-		return $this;
348
-	}
349
-
350
-	/**
351
-	 * Removes the last handler in the stack and returns it.
352
-	 * 
353
-	 * @return array|null
354
-	 */
355
-	public function popHandler()
356
-	{
357
-		return array_pop($this->handlerStack);
358
-	}
359
-
360
-	/**
361
-	 * Gets supervisor already specified.
362
-	 * 
363
-	 * @param  \Throwable  $exception
364
-	 * 
365
-	 * @return \Syscodes\Components\Debug\Engine\Supervisor
366
-	 */
367
-	protected function getSupervisor(Throwable $exception)
368
-	{
369
-		return new Supervisor($exception);
370
-	}
371
-
372
-	/**
373
-	 * Unregisters all handlers registered by this Debug instance.
374
-	 * 
375
-	 * @return void
376
-	 */
377
-	public function off(): void
378
-	{
379
-		$this->system->restoreExceptionHandler();
380
-		$this->system->restoreErrorHandler();
381
-	}
224
+        return $this;
225
+    }
226
+
227
+    /**
228
+     * Error handler
229
+     *
230
+     * This will catch the php native error and treat it as a exception which will 
231
+     * provide a full back trace on all errors.
232
+     *
233
+     * @param  int  $level
234
+     * @param  string  $message
235
+     * @param  string|null  $file
236
+     * @param  int|null  $line
237
+     * 
238
+     * @return bool
239
+     * 
240
+     * @throws \ErrorException
241
+     */
242
+    public function handleError(
243
+        int $level, 
244
+        string $message, 
245
+        string $file = null, 
246
+        int $line = null
247
+    ): bool {
248
+        if ($level & $this->system->getErrorReportingLevel()) {
249
+            $exception = new ErrorException($message, $level, $level, $file, $line);
250
+
251
+            if ($this->throwExceptions) {
252
+                throw $exception;
253
+            } else {
254
+                $this->handleException($exception);
255
+            }
256
+
257
+            return true;
258
+        }
259
+
260
+        return false;
261
+    }
262
+
263
+    /**
264
+     * Appends a handler to the end of the stack.
265
+     * 
266
+     * @param  \Callable|\Syscodes\Components\Contracts\Debug\Handler  $handler
267
+     * 
268
+     * @return static
269
+     */
270
+    public function appendHandler($handler): static
271
+    {
272
+        array_unshift($this->handlerStack, $this->resolveHandler($handler));
273
+
274
+        return $this;
275
+    }
276
+
277
+    /**
278
+     * Prepends a handler to the start of the stack.
279
+     * 
280
+     * @param  \Callable|\Syscodes\Components\Contracts\Debug\Handler  $handler
281
+     * 
282
+     * @return static
283
+     */
284
+    public function prependHandler($handler): static
285
+    {
286
+        return $this->pushHandler($handler);
287
+    }
288
+
289
+    /**
290
+     * Pushes a handler to the end of the stack.
291
+     * 
292
+     * @param  string|callable  $handler
293
+     * 
294
+     * @return static
295
+     */
296
+    public function pushHandler($handler): static
297
+    {
298
+        $this->handlerStack[] = $this->resolveHandler($handler);
299
+
300
+        return $this;
301
+    }
302
+
303
+    /**
304
+     * Create a CallbackHandler from callable and throw if handler is invalid.
305
+     * 
306
+     * @param  \Callable|\Syscodes\Components\Contracts\Debug\Handler  $handler
307
+     * 
308
+     * @return \Syscodes\Components\Contracts\Debug\Handler
309
+     * 
310
+     * @throws \InvalidArgumentException If argument is not callable or instance of \Syscodes\Components\Contracts\Debug\Handler
311
+     */
312
+    protected function resolveHandler($handler)
313
+    {
314
+        if (is_callable($handler)) {
315
+            $handler = new CallbackHandler($handler);
316
+        }
317
+
318
+        if ( ! $handler instanceof MainHandler) {
319
+            throw new InvalidArgumentException(
320
+                "Argument to " . __METHOD__ . " must be a callable, or instance of ".
321
+                "Syscodes\Components\\Contracts\\Debug\\Handler"
322
+            );
323
+        }
324
+
325
+        return $handler;
326
+    }
327
+
328
+    /**
329
+     * Returns an array with all handlers, in the order they were added to the stack.
330
+     * 
331
+     * @return array
332
+     */
333
+    public function getHandlers(): array
334
+    {
335
+        return $this->handlerStack;
336
+    }
337
+
338
+    /**
339
+     * Clears all handlers in the handlerStack, including the default PleasingPage handler.
340
+     * 
341
+     * @return static
342
+     */
343
+    public function clearHandlers(): static
344
+    {
345
+        $this->handlerStack = [];
346
+
347
+        return $this;
348
+    }
349
+
350
+    /**
351
+     * Removes the last handler in the stack and returns it.
352
+     * 
353
+     * @return array|null
354
+     */
355
+    public function popHandler()
356
+    {
357
+        return array_pop($this->handlerStack);
358
+    }
359
+
360
+    /**
361
+     * Gets supervisor already specified.
362
+     * 
363
+     * @param  \Throwable  $exception
364
+     * 
365
+     * @return \Syscodes\Components\Debug\Engine\Supervisor
366
+     */
367
+    protected function getSupervisor(Throwable $exception)
368
+    {
369
+        return new Supervisor($exception);
370
+    }
371
+
372
+    /**
373
+     * Unregisters all handlers registered by this Debug instance.
374
+     * 
375
+     * @return void
376
+     */
377
+    public function off(): void
378
+    {
379
+        $this->system->restoreExceptionHandler();
380
+        $this->system->restoreErrorHandler();
381
+    }
382 382
 	
383
-	/**
384
-	 * Registers this instance as an error handler.
385
-	 * 
386
-	 * @return void
387
-	 */
388
-	public function on() : void
389
-	{
390
-		// Set the exception handler
391
-		$this->system->setExceptionHandler([$this, self::EXCEPTION_HANDLER]);
392
-		// Set the error handler
393
-		$this->system->setErrorHandler([$this, self::ERROR_HANDLER]);
394
-		// Set the handler for shutdown to catch Parse errors
395
-		$this->system->registerShutdownFunction([$this, self::SHUTDOWN_HANDLER]);
396
-	}
397
-
398
-	/**
399
-	 * Lenevor Exception will by default send HTTP code 500, but you may wish
400
-	 * to use 502, 503, or another 5xx family code.
401
-	 * 
402
-	 * @param  bool|int  $code
403
-	 * 
404
-	 * @return int|false
405
-	 * 
406
-	 * @throws \InvalidArgumentException
407
-	 */
408
-	public function sendHttpCode($code = null)
409
-	{
410
-		if (func_num_args() == 0) {
411
-			return $this->sendHttpCode;
412
-		}
383
+    /**
384
+     * Registers this instance as an error handler.
385
+     * 
386
+     * @return void
387
+     */
388
+    public function on() : void
389
+    {
390
+        // Set the exception handler
391
+        $this->system->setExceptionHandler([$this, self::EXCEPTION_HANDLER]);
392
+        // Set the error handler
393
+        $this->system->setErrorHandler([$this, self::ERROR_HANDLER]);
394
+        // Set the handler for shutdown to catch Parse errors
395
+        $this->system->registerShutdownFunction([$this, self::SHUTDOWN_HANDLER]);
396
+    }
397
+
398
+    /**
399
+     * Lenevor Exception will by default send HTTP code 500, but you may wish
400
+     * to use 502, 503, or another 5xx family code.
401
+     * 
402
+     * @param  bool|int  $code
403
+     * 
404
+     * @return int|false
405
+     * 
406
+     * @throws \InvalidArgumentException
407
+     */
408
+    public function sendHttpCode($code = null)
409
+    {
410
+        if (func_num_args() == 0) {
411
+            return $this->sendHttpCode;
412
+        }
413 413
 		
414
-		if ( ! $code) {
415
-			return $this->sendHttpCode = false;
416
-		}
414
+        if ( ! $code) {
415
+            return $this->sendHttpCode = false;
416
+        }
417 417
 		
418
-		if ($code === true) {
419
-			$code = 500;
420
-		}
418
+        if ($code === true) {
419
+            $code = 500;
420
+        }
421 421
 		
422
-		if ($code < 400 || 600 <= $code) {
423
-			throw new InvalidArgumentException("Invalid status code {$code}, must be 4xx or 5xx");
424
-		}
422
+        if ($code < 400 || 600 <= $code) {
423
+            throw new InvalidArgumentException("Invalid status code {$code}, must be 4xx or 5xx");
424
+        }
425 425
 		
426
-		return $this->sendHttpCode = $code;
427
-	}
428
-
429
-	/**
430
-	 * This will catch errors that are generated at the shutdown level of execution.
431
-	 *
432
-	 * @return void
433
-	 *
434
-	 * @throws \ErrorException
435
-	 */
436
-	public function handleShutdown()
437
-	{
438
-		$this->throwExceptions = false;
439
-
440
-		$error = $this->system->getLastError();
441
-
442
-		// If we've got an error that hasn't been displayed, then convert
443
-		// it to an Exception and use the Exception handler to display it
444
-		// to the user
445
-		if ($error && Misc::isFatalError($error['type'])) {
446
-			$this->allowQuit = false;
426
+        return $this->sendHttpCode = $code;
427
+    }
428
+
429
+    /**
430
+     * This will catch errors that are generated at the shutdown level of execution.
431
+     *
432
+     * @return void
433
+     *
434
+     * @throws \ErrorException
435
+     */
436
+    public function handleShutdown()
437
+    {
438
+        $this->throwExceptions = false;
439
+
440
+        $error = $this->system->getLastError();
441
+
442
+        // If we've got an error that hasn't been displayed, then convert
443
+        // it to an Exception and use the Exception handler to display it
444
+        // to the user
445
+        if ($error && Misc::isFatalError($error['type'])) {
446
+            $this->allowQuit = false;
447 447
 			
448
-			$this->handleError($error['type'], $error['message'], $error['file'], $error['line']);
449
-		}
450
-	}
448
+            $this->handleError($error['type'], $error['message'], $error['file'], $error['line']);
449
+        }
450
+    }
451 451
 }
452 452
\ No newline at end of file
Please login to merge, or discard this patch.
src/components/Stopwatch/Benchmark.php 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -143,7 +143,7 @@
 block discarded – undo
143 143
         $seconds  = (int) ($duration - $hours * 60 * 60 - $minutes * 60); 
144 144
         
145 145
         if ($seconds <= 0) {
146
-           return ' ms';
146
+            return ' ms';
147 147
         } elseif ($seconds > 0) {
148 148
             return ' s';
149 149
         }
Please login to merge, or discard this patch.