1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Lenevor Framework |
5
|
|
|
* |
6
|
|
|
* LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the new BSD license that is bundled |
9
|
|
|
* with this package in the file license.md. |
10
|
|
|
* It is also available through the world-wide-web at this URL: |
11
|
|
|
* https://lenevor.com/license |
12
|
|
|
* If you did not receive a copy of the license and are unable to |
13
|
|
|
* obtain it through the world-wide-web, please send an email |
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
15
|
|
|
* |
16
|
|
|
* @package Lenevor |
17
|
|
|
* @subpackage Base |
18
|
|
|
* @link https://lenevor.com |
19
|
|
|
* @copyright Copyright (c) 2019 - 2021 Alexander Campo <[email protected]> |
20
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Syscodes\Controller; |
24
|
|
|
|
25
|
|
|
use BadMethodCallException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* This class allows you to manage the middlewares, actions and parameters |
29
|
|
|
* in each controller created by the user. |
30
|
|
|
* |
31
|
|
|
* @author Alexander Campo <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
abstract class Controller |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* The middleware registered on the controller. |
37
|
|
|
* |
38
|
|
|
* @var array $middleware |
39
|
|
|
*/ |
40
|
|
|
protected $middleware = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Register middleware on the controller. |
44
|
|
|
* |
45
|
|
|
* @param \Closure|array|string $middleware |
46
|
|
|
* @param array $options |
47
|
|
|
* |
48
|
|
|
* @return \Syscodes\Controller\ControllerMiddlewareOptions |
49
|
|
|
*/ |
50
|
|
|
public function middleware($middleare, array $options = []) |
51
|
|
|
{ |
52
|
|
|
foreach ((array) $middleware as $m) { |
|
|
|
|
53
|
|
|
$this->middleware[$m] = [ |
54
|
|
|
'options' => &$options |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return new ControllerMiddlewareOptions($options); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the middleware assigned to the controller. |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function getMiddleware() |
67
|
|
|
{ |
68
|
|
|
return $this->middleware; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Execute an action on the controller. |
73
|
|
|
* |
74
|
|
|
* @param string $method |
75
|
|
|
* @param array $parameters |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function callAction($method, $parameters) |
80
|
|
|
{ |
81
|
|
|
return call_user_func_array([$this, $method], $parameters); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Dynamically handle calls to methods on the controller. |
86
|
|
|
* |
87
|
|
|
* @param string $method |
88
|
|
|
* @param array $parameters |
89
|
|
|
* |
90
|
|
|
* @return mixed |
91
|
|
|
* |
92
|
|
|
* @throws \BadMethodCallException |
93
|
|
|
*/ |
94
|
|
|
public function __call($method, $parameters) |
95
|
|
|
{ |
96
|
|
|
throw new BadMethodCallException(sprintf( |
97
|
|
|
'Method %s::%s does not exist', static::class, $method |
98
|
|
|
)); |
99
|
|
|
} |
100
|
|
|
} |