crysalead /
router
| 1 | <?php |
||
| 2 | namespace Lead\Router; |
||
| 3 | |||
| 4 | class Scope |
||
| 5 | { |
||
| 6 | /** |
||
| 7 | * The router instance. |
||
| 8 | * |
||
| 9 | * @var object |
||
| 10 | */ |
||
| 11 | protected $_router = null; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * The parent instance. |
||
| 15 | * |
||
| 16 | * @var object |
||
| 17 | */ |
||
| 18 | protected $_parent = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The middleware array. |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $_middleware = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The scope data. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $_scope = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The constructor. |
||
| 36 | * |
||
| 37 | * @param array $config The config array. |
||
| 38 | */ |
||
| 39 | public function __construct($config = []) |
||
| 40 | { |
||
| 41 | $defaults = [ |
||
| 42 | 'router' => null, |
||
| 43 | 'parent' => null, |
||
| 44 | 'middleware' => [], |
||
| 45 | 'scope' => [] |
||
| 46 | 31 | ]; |
|
| 47 | 31 | $config += $defaults; |
|
| 48 | |||
| 49 | 31 | $this->_router = $config['router']; |
|
| 50 | 31 | $this->_parent = $config['parent']; |
|
| 51 | 31 | $this->_middleware = $config['middleware']; |
|
| 52 | $this->_scope = $config['scope'] + [ |
||
| 53 | 'name' => '', |
||
| 54 | 'scheme' => '*', |
||
| 55 | 'host' => '*', |
||
| 56 | 'methods' => '*', |
||
| 57 | 'prefix' => '', |
||
| 58 | 'namespace' => '', |
||
| 59 | 'persist' => [] |
||
| 60 | 31 | ]; |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Creates a new sub scope based on the instance scope. |
||
| 65 | * |
||
| 66 | * @param array $options The route options to scopify. |
||
| 67 | * @return object The new sub scope. |
||
| 68 | */ |
||
| 69 | public function seed($options) |
||
| 70 | { |
||
| 71 | return new static([ |
||
| 72 | 'router' => $this->_router, |
||
| 73 | 'parent' => $this, |
||
| 74 | 'scope' => $this->scopify($options) |
||
| 75 | 15 | ]); |
|
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Scopes an options array according to the instance scope data. |
||
| 80 | * |
||
| 81 | * @param array $options The options to scope. |
||
| 82 | * @return array The scoped options. |
||
| 83 | */ |
||
| 84 | public function scopify($options) |
||
| 85 | { |
||
| 86 | 25 | $scope = $this->_scope; |
|
| 87 | |||
| 88 | if (!empty($options['name'])) { |
||
| 89 | 9 | $options['name'] = $scope['name'] ? $scope['name'] . '.' . $options['name'] : $options['name']; |
|
| 90 | } |
||
| 91 | |||
| 92 | if (!empty($options['prefix'])) { |
||
| 93 | 15 | $options['prefix'] = $scope['prefix'] . trim($options['prefix'], '/'); |
|
| 94 | 15 | $options['prefix'] = $options['prefix'] ? $options['prefix'] . '/' : ''; |
|
| 95 | } |
||
| 96 | |||
| 97 | if (isset($options['persist'])) { |
||
| 98 | 2 | $options['persist'] = ((array) $options['persist']) + $scope['persist']; |
|
| 99 | } |
||
| 100 | |||
| 101 | if (isset($options['namespace'])) { |
||
| 102 | 2 | $options['namespace'] = $scope['namespace'] . trim($options['namespace'], '\\') . '\\'; |
|
| 103 | } |
||
| 104 | |||
| 105 | 25 | return $options + $scope; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Middleware generator. |
||
| 110 | * |
||
| 111 | * @return callable |
||
| 112 | */ |
||
| 113 | public function middleware() |
||
| 114 | { |
||
| 115 | foreach ($this->_middleware as $middleware) { |
||
| 116 | 4 | yield $middleware; |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 117 | } |
||
| 118 | if ($this->_parent) { |
||
| 119 | foreach ($this->_parent->middleware() as $middleware) { |
||
| 120 | 2 | yield $middleware; |
|
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Adds a middleware to the list of middleware. |
||
| 127 | * |
||
| 128 | * @param object|Closure A callable middleware. |
||
| 129 | */ |
||
| 130 | public function apply($middleware) |
||
| 131 | { |
||
| 132 | foreach (func_get_args() as $mw) { |
||
| 133 | 4 | array_unshift($this->_middleware, $mw); |
|
| 134 | } |
||
| 135 | 4 | return $this; |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Delegates calls to the router instance. |
||
| 140 | * |
||
| 141 | * @param string $name The method name. |
||
| 142 | * @param array $params The parameters. |
||
| 143 | */ |
||
| 144 | public function __call($name, $params) |
||
| 145 | { |
||
| 146 | 1 | $this->_router->pushScope($this); |
|
| 147 | 1 | $result = call_user_func_array([$this->_router, $name], $params); |
|
| 148 | 1 | $this->_router->popScope(); |
|
| 149 | 1 | return $result; |
|
| 150 | } |
||
| 151 | } |