@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | |
| 263 | 263 | $uri = $this->trimUrl($root, $this->replaceRouteParameters($route->getRoute(), $parameters)); |
| 264 | 264 | |
| 265 | - return $forced ? $uri : '/' .ltrim(str_replace($root, '', $uri), '/'); |
|
| 265 | + return $forced ? $uri : '/'.ltrim(str_replace($root, '', $uri), '/'); |
|
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | /** |
@@ -324,7 +324,7 @@ discard block |
||
| 324 | 324 | */ |
| 325 | 325 | protected function replaceNamedParameters($path, &$parameters) |
| 326 | 326 | { |
| 327 | - return preg_replace_callback('/\{(.*?)\??\}/', function ($match) use (&$parameters) { |
|
| 327 | + return preg_replace_callback('/\{(.*?)\??\}/', function($match) use (&$parameters) { |
|
| 328 | 328 | return isset($parameters[$match[1]]) ? Arr::pull($parameters, $match[1]) : $match[0]; |
| 329 | 329 | }, $path); |
| 330 | 330 | } |
@@ -339,7 +339,7 @@ |
||
| 339 | 339 | */ |
| 340 | 340 | protected function getNestedResourceUri(array $segments) |
| 341 | 341 | { |
| 342 | - return implode('/', array_map(function ($segment) { |
|
| 342 | + return implode('/', array_map(function($segment) { |
|
| 343 | 343 | return $segment.'/{'.$this->getResourceWilcard($segment).'}'; |
| 344 | 344 | }, $segments)); |
| 345 | 345 | } |
@@ -39,581 +39,581 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | class Router implements Routable |
| 41 | 41 | { |
| 42 | - use Concerns\RouteMap, |
|
| 43 | - Concerns\RouteResolver; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The registered route value binders. |
|
| 47 | - * |
|
| 48 | - * @var array $binders |
|
| 49 | - */ |
|
| 50 | - protected $binders = []; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The container instance used by the router. |
|
| 54 | - * |
|
| 55 | - * @var \Syscodes\Contracts\Container\Container $container |
|
| 56 | - */ |
|
| 57 | - protected $container; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Variable of group route. |
|
| 61 | - * |
|
| 62 | - * @var array $groupStack |
|
| 63 | - */ |
|
| 64 | - protected $groupStack = []; |
|
| 42 | + use Concerns\RouteMap, |
|
| 43 | + Concerns\RouteResolver; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The registered route value binders. |
|
| 47 | + * |
|
| 48 | + * @var array $binders |
|
| 49 | + */ |
|
| 50 | + protected $binders = []; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The container instance used by the router. |
|
| 54 | + * |
|
| 55 | + * @var \Syscodes\Contracts\Container\Container $container |
|
| 56 | + */ |
|
| 57 | + protected $container; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Variable of group route. |
|
| 61 | + * |
|
| 62 | + * @var array $groupStack |
|
| 63 | + */ |
|
| 64 | + protected $groupStack = []; |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * The registered string macros. |
|
| 68 | - * |
|
| 69 | - * @var array $macros |
|
| 70 | - */ |
|
| 71 | - protected $macros = []; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Middleware for function of filters |
|
| 75 | - * |
|
| 76 | - * @var string[] $middleware |
|
| 77 | - */ |
|
| 78 | - protected $middleware = []; |
|
| 66 | + /** |
|
| 67 | + * The registered string macros. |
|
| 68 | + * |
|
| 69 | + * @var array $macros |
|
| 70 | + */ |
|
| 71 | + protected $macros = []; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Middleware for function of filters |
|
| 75 | + * |
|
| 76 | + * @var string[] $middleware |
|
| 77 | + */ |
|
| 78 | + protected $middleware = []; |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * The globally available parameter patterns. |
|
| 82 | - * |
|
| 83 | - * @var array $patterns |
|
| 84 | - */ |
|
| 85 | - protected $patterns = []; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * The route collection instance. |
|
| 89 | - * |
|
| 90 | - * @var \Syscodes\Routing\RouteCollection $routes |
|
| 91 | - */ |
|
| 92 | - protected $routes; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * The Resource instance. |
|
| 96 | - * |
|
| 97 | - * @var \Syscodes\Routing\ResourceRegister $resources |
|
| 98 | - */ |
|
| 99 | - protected $resources; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Constructor. Create a new Router instance. |
|
| 103 | - * |
|
| 104 | - * @param \Syscodes\Contracts\Container\Container|null $container (null by default) |
|
| 105 | - * |
|
| 106 | - * @return void |
|
| 107 | - */ |
|
| 108 | - public function __construct(Container $container = null) |
|
| 109 | - { |
|
| 110 | - $this->routes = new RouteCollection(); |
|
| 111 | - |
|
| 112 | - $this->container = $container ?: new Container; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Get the prefix from the group on the stack. |
|
| 117 | - * |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - public function getGroupPrefix() |
|
| 121 | - { |
|
| 122 | - if ( ! empty($this->groupStack)) { |
|
| 123 | - $last = end($this->groupStack); |
|
| 124 | - |
|
| 125 | - return $last['prefix'] ?? ''; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - return ''; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Group a series of routes under a single URL segment. This is handy |
|
| 133 | - * for grouping items into an admin area, like: |
|
| 134 | - * |
|
| 135 | - * Example: |
|
| 136 | - * // Creates route: /admin show the word 'User' |
|
| 137 | - * Route::group(['prefix' => 'admin'], function() { |
|
| 138 | - * |
|
| 139 | - * Route::get('/user', function() { |
|
| 140 | - * echo 'Hello world..!'; |
|
| 141 | - * }); |
|
| 142 | - * |
|
| 143 | - * }); /admin/user |
|
| 144 | - * |
|
| 145 | - * @param array $attributes |
|
| 146 | - * @param \Closure|string $callback |
|
| 147 | - * |
|
| 148 | - * @return void |
|
| 149 | - */ |
|
| 150 | - public function group(array $attributes, $callback) |
|
| 151 | - { |
|
| 152 | - $this->updateGroupStack($attributes); |
|
| 153 | - |
|
| 154 | - $this->loadRoutes($callback); |
|
| 155 | - |
|
| 156 | - array_pop($this->groupStack); |
|
| 157 | - |
|
| 158 | - return $this; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Update the group stack with the given attributes. |
|
| 163 | - * |
|
| 164 | - * @param array $attributes |
|
| 165 | - * |
|
| 166 | - * @return void |
|
| 167 | - */ |
|
| 168 | - protected function updateGroupStack(array $attributes) |
|
| 169 | - { |
|
| 170 | - if ( ! empty($this->groupStack)) { |
|
| 171 | - $attributes = $this->mergeGroup($attributes); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - $this->groupStack[] = $attributes; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Merge the given group attributes. |
|
| 179 | - * |
|
| 180 | - * @param array $new |
|
| 181 | - * |
|
| 182 | - * @return array |
|
| 183 | - */ |
|
| 184 | - protected function mergeGroup($new) |
|
| 185 | - { |
|
| 186 | - return RouteGroup::mergeGroup($new, end($this->groupStack)); |
|
| 187 | - } |
|
| 80 | + /** |
|
| 81 | + * The globally available parameter patterns. |
|
| 82 | + * |
|
| 83 | + * @var array $patterns |
|
| 84 | + */ |
|
| 85 | + protected $patterns = []; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * The route collection instance. |
|
| 89 | + * |
|
| 90 | + * @var \Syscodes\Routing\RouteCollection $routes |
|
| 91 | + */ |
|
| 92 | + protected $routes; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * The Resource instance. |
|
| 96 | + * |
|
| 97 | + * @var \Syscodes\Routing\ResourceRegister $resources |
|
| 98 | + */ |
|
| 99 | + protected $resources; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Constructor. Create a new Router instance. |
|
| 103 | + * |
|
| 104 | + * @param \Syscodes\Contracts\Container\Container|null $container (null by default) |
|
| 105 | + * |
|
| 106 | + * @return void |
|
| 107 | + */ |
|
| 108 | + public function __construct(Container $container = null) |
|
| 109 | + { |
|
| 110 | + $this->routes = new RouteCollection(); |
|
| 111 | + |
|
| 112 | + $this->container = $container ?: new Container; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Get the prefix from the group on the stack. |
|
| 117 | + * |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + public function getGroupPrefix() |
|
| 121 | + { |
|
| 122 | + if ( ! empty($this->groupStack)) { |
|
| 123 | + $last = end($this->groupStack); |
|
| 124 | + |
|
| 125 | + return $last['prefix'] ?? ''; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + return ''; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Group a series of routes under a single URL segment. This is handy |
|
| 133 | + * for grouping items into an admin area, like: |
|
| 134 | + * |
|
| 135 | + * Example: |
|
| 136 | + * // Creates route: /admin show the word 'User' |
|
| 137 | + * Route::group(['prefix' => 'admin'], function() { |
|
| 138 | + * |
|
| 139 | + * Route::get('/user', function() { |
|
| 140 | + * echo 'Hello world..!'; |
|
| 141 | + * }); |
|
| 142 | + * |
|
| 143 | + * }); /admin/user |
|
| 144 | + * |
|
| 145 | + * @param array $attributes |
|
| 146 | + * @param \Closure|string $callback |
|
| 147 | + * |
|
| 148 | + * @return void |
|
| 149 | + */ |
|
| 150 | + public function group(array $attributes, $callback) |
|
| 151 | + { |
|
| 152 | + $this->updateGroupStack($attributes); |
|
| 153 | + |
|
| 154 | + $this->loadRoutes($callback); |
|
| 155 | + |
|
| 156 | + array_pop($this->groupStack); |
|
| 157 | + |
|
| 158 | + return $this; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Update the group stack with the given attributes. |
|
| 163 | + * |
|
| 164 | + * @param array $attributes |
|
| 165 | + * |
|
| 166 | + * @return void |
|
| 167 | + */ |
|
| 168 | + protected function updateGroupStack(array $attributes) |
|
| 169 | + { |
|
| 170 | + if ( ! empty($this->groupStack)) { |
|
| 171 | + $attributes = $this->mergeGroup($attributes); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + $this->groupStack[] = $attributes; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Merge the given group attributes. |
|
| 179 | + * |
|
| 180 | + * @param array $new |
|
| 181 | + * |
|
| 182 | + * @return array |
|
| 183 | + */ |
|
| 184 | + protected function mergeGroup($new) |
|
| 185 | + { |
|
| 186 | + return RouteGroup::mergeGroup($new, end($this->groupStack)); |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - /** |
|
| 190 | - * Load the provided routes. |
|
| 191 | - * |
|
| 192 | - * @param \Closure|string $callback |
|
| 193 | - * |
|
| 194 | - * @return void |
|
| 195 | - */ |
|
| 196 | - protected function loadRoutes($callback) |
|
| 197 | - { |
|
| 198 | - if ($callback instanceof Closure) { |
|
| 199 | - $callback($this); |
|
| 200 | - } else { |
|
| 201 | - (new RouteFileRegister($this))->register($callback); |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Add a route to the underlying route collection. |
|
| 207 | - * |
|
| 208 | - * @param array|string $method |
|
| 209 | - * @param string $route |
|
| 210 | - * @param mixed $action |
|
| 211 | - * |
|
| 212 | - * @return \Syscodes\Routing\Route |
|
| 213 | - */ |
|
| 214 | - public function addRoute($method, $route, $action) |
|
| 215 | - { |
|
| 216 | - return $this->routes->add($this->map($method, $route, $action)); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Create a redirect from one URI to another. |
|
| 221 | - * |
|
| 222 | - * @param string $uri |
|
| 223 | - * @param string $destination |
|
| 224 | - * @param int $status (302 by default) |
|
| 225 | - * |
|
| 226 | - * @return \Syscodes\Routing\Route |
|
| 227 | - */ |
|
| 228 | - public function redirect($uri, $destination, $status = 302) |
|
| 229 | - { |
|
| 230 | - return $this->any($uri, function () use ($destination, $status) { |
|
| 231 | - return new RedirectResponse($destination, $status); |
|
| 232 | - }); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Register a new route that returns a view. |
|
| 237 | - * |
|
| 238 | - * @param string $uri |
|
| 239 | - * @param string $view |
|
| 240 | - * @param array $data |
|
| 241 | - * |
|
| 242 | - * @return \Syscodes\Routing\Route |
|
| 243 | - */ |
|
| 244 | - public function view($uri, $view, $data = []) |
|
| 245 | - { |
|
| 246 | - return $this->match(['GET', 'HEAD'], $uri, function () use ($view, $data) { |
|
| 247 | - return $this->container->make('view')->make($view, $data); |
|
| 248 | - }); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Add new route to routes array. |
|
| 253 | - * |
|
| 254 | - * @param array|string $method |
|
| 255 | - * @param string $route |
|
| 256 | - * @param mixed $action |
|
| 257 | - * |
|
| 258 | - * @return void |
|
| 259 | - * |
|
| 260 | - * @throws \InvalidArgumentException |
|
| 261 | - */ |
|
| 262 | - public function map($method, $route, $action) |
|
| 263 | - { |
|
| 264 | - if ($this->actionReferencesController($action)) { |
|
| 265 | - $action = $this->convertToControllerAction($action); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $route = $this->newRoute( |
|
| 269 | - array_map('strtoupper', (array) $method), |
|
| 270 | - $this->prefix($route), |
|
| 271 | - $action |
|
| 272 | - ); |
|
| 273 | - |
|
| 274 | - if ($this->hasGroupStack()) { |
|
| 275 | - $this->mergeGroupAttributesIntoRoute($route); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - $this->addWhereClausesToRoute($route); |
|
| 189 | + /** |
|
| 190 | + * Load the provided routes. |
|
| 191 | + * |
|
| 192 | + * @param \Closure|string $callback |
|
| 193 | + * |
|
| 194 | + * @return void |
|
| 195 | + */ |
|
| 196 | + protected function loadRoutes($callback) |
|
| 197 | + { |
|
| 198 | + if ($callback instanceof Closure) { |
|
| 199 | + $callback($this); |
|
| 200 | + } else { |
|
| 201 | + (new RouteFileRegister($this))->register($callback); |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Add a route to the underlying route collection. |
|
| 207 | + * |
|
| 208 | + * @param array|string $method |
|
| 209 | + * @param string $route |
|
| 210 | + * @param mixed $action |
|
| 211 | + * |
|
| 212 | + * @return \Syscodes\Routing\Route |
|
| 213 | + */ |
|
| 214 | + public function addRoute($method, $route, $action) |
|
| 215 | + { |
|
| 216 | + return $this->routes->add($this->map($method, $route, $action)); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Create a redirect from one URI to another. |
|
| 221 | + * |
|
| 222 | + * @param string $uri |
|
| 223 | + * @param string $destination |
|
| 224 | + * @param int $status (302 by default) |
|
| 225 | + * |
|
| 226 | + * @return \Syscodes\Routing\Route |
|
| 227 | + */ |
|
| 228 | + public function redirect($uri, $destination, $status = 302) |
|
| 229 | + { |
|
| 230 | + return $this->any($uri, function () use ($destination, $status) { |
|
| 231 | + return new RedirectResponse($destination, $status); |
|
| 232 | + }); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Register a new route that returns a view. |
|
| 237 | + * |
|
| 238 | + * @param string $uri |
|
| 239 | + * @param string $view |
|
| 240 | + * @param array $data |
|
| 241 | + * |
|
| 242 | + * @return \Syscodes\Routing\Route |
|
| 243 | + */ |
|
| 244 | + public function view($uri, $view, $data = []) |
|
| 245 | + { |
|
| 246 | + return $this->match(['GET', 'HEAD'], $uri, function () use ($view, $data) { |
|
| 247 | + return $this->container->make('view')->make($view, $data); |
|
| 248 | + }); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Add new route to routes array. |
|
| 253 | + * |
|
| 254 | + * @param array|string $method |
|
| 255 | + * @param string $route |
|
| 256 | + * @param mixed $action |
|
| 257 | + * |
|
| 258 | + * @return void |
|
| 259 | + * |
|
| 260 | + * @throws \InvalidArgumentException |
|
| 261 | + */ |
|
| 262 | + public function map($method, $route, $action) |
|
| 263 | + { |
|
| 264 | + if ($this->actionReferencesController($action)) { |
|
| 265 | + $action = $this->convertToControllerAction($action); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $route = $this->newRoute( |
|
| 269 | + array_map('strtoupper', (array) $method), |
|
| 270 | + $this->prefix($route), |
|
| 271 | + $action |
|
| 272 | + ); |
|
| 273 | + |
|
| 274 | + if ($this->hasGroupStack()) { |
|
| 275 | + $this->mergeGroupAttributesIntoRoute($route); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + $this->addWhereClausesToRoute($route); |
|
| 279 | 279 | |
| 280 | - return $route; |
|
| 281 | - } |
|
| 280 | + return $route; |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - /** |
|
| 284 | - * Determine if the action is routing to a controller. |
|
| 285 | - * |
|
| 286 | - * @param array $action |
|
| 287 | - * |
|
| 288 | - * @return bool |
|
| 289 | - */ |
|
| 290 | - protected function actionReferencesController($action) |
|
| 291 | - { |
|
| 292 | - if ($action instanceof Closure) { |
|
| 293 | - return false; |
|
| 294 | - } |
|
| 283 | + /** |
|
| 284 | + * Determine if the action is routing to a controller. |
|
| 285 | + * |
|
| 286 | + * @param array $action |
|
| 287 | + * |
|
| 288 | + * @return bool |
|
| 289 | + */ |
|
| 290 | + protected function actionReferencesController($action) |
|
| 291 | + { |
|
| 292 | + if ($action instanceof Closure) { |
|
| 293 | + return false; |
|
| 294 | + } |
|
| 295 | 295 | |
| 296 | - return is_string($action) || (isset($action['uses']) && is_string($action['uses'])); |
|
| 297 | - } |
|
| 296 | + return is_string($action) || (isset($action['uses']) && is_string($action['uses'])); |
|
| 297 | + } |
|
| 298 | 298 | |
| 299 | - /** |
|
| 300 | - * Add a controller based route action to the action array. |
|
| 301 | - * |
|
| 302 | - * @param array|string $action |
|
| 303 | - * |
|
| 304 | - * @return array |
|
| 305 | - */ |
|
| 306 | - protected function convertToControllerAction($action) |
|
| 307 | - { |
|
| 308 | - if (is_string($action)) { |
|
| 309 | - $action = ['uses' => $action]; |
|
| 310 | - } |
|
| 299 | + /** |
|
| 300 | + * Add a controller based route action to the action array. |
|
| 301 | + * |
|
| 302 | + * @param array|string $action |
|
| 303 | + * |
|
| 304 | + * @return array |
|
| 305 | + */ |
|
| 306 | + protected function convertToControllerAction($action) |
|
| 307 | + { |
|
| 308 | + if (is_string($action)) { |
|
| 309 | + $action = ['uses' => $action]; |
|
| 310 | + } |
|
| 311 | 311 | |
| 312 | - if ( ! empty($this->groupStack)) { |
|
| 313 | - $action['uses'] = $this->prependGroupUses($action['uses']); |
|
| 314 | - } |
|
| 312 | + if ( ! empty($this->groupStack)) { |
|
| 313 | + $action['uses'] = $this->prependGroupUses($action['uses']); |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | - $action['controller'] = $action['uses']; |
|
| 316 | + $action['controller'] = $action['uses']; |
|
| 317 | 317 | |
| 318 | - return $action; |
|
| 319 | - } |
|
| 318 | + return $action; |
|
| 319 | + } |
|
| 320 | 320 | |
| 321 | - /** |
|
| 322 | - * Prepend the last group uses onto the use clause. |
|
| 323 | - * |
|
| 324 | - * @param string $uses |
|
| 325 | - * |
|
| 326 | - * @return string |
|
| 327 | - */ |
|
| 328 | - protected function prependGroupUses($uses) |
|
| 329 | - { |
|
| 330 | - $group = end($this->groupStack); |
|
| 321 | + /** |
|
| 322 | + * Prepend the last group uses onto the use clause. |
|
| 323 | + * |
|
| 324 | + * @param string $uses |
|
| 325 | + * |
|
| 326 | + * @return string |
|
| 327 | + */ |
|
| 328 | + protected function prependGroupUses($uses) |
|
| 329 | + { |
|
| 330 | + $group = end($this->groupStack); |
|
| 331 | 331 | |
| 332 | - return isset($group['namespace']) ? $group['namespace'].'\\'.$uses : $uses; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Create a new Route object. |
|
| 337 | - * |
|
| 338 | - * @param array|string $method |
|
| 339 | - * @param string $uri |
|
| 340 | - * @param mixed $action |
|
| 341 | - * |
|
| 342 | - * @return \Syscodes\Routing\Route |
|
| 343 | - */ |
|
| 344 | - public function newRoute($method, $uri, $action) |
|
| 345 | - { |
|
| 346 | - return take(new Route($method, $uri, $action)) |
|
| 347 | - ->setContainer($this->container); |
|
| 348 | - } |
|
| 332 | + return isset($group['namespace']) ? $group['namespace'].'\\'.$uses : $uses; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Create a new Route object. |
|
| 337 | + * |
|
| 338 | + * @param array|string $method |
|
| 339 | + * @param string $uri |
|
| 340 | + * @param mixed $action |
|
| 341 | + * |
|
| 342 | + * @return \Syscodes\Routing\Route |
|
| 343 | + */ |
|
| 344 | + public function newRoute($method, $uri, $action) |
|
| 345 | + { |
|
| 346 | + return take(new Route($method, $uri, $action)) |
|
| 347 | + ->setContainer($this->container); |
|
| 348 | + } |
|
| 349 | 349 | |
| 350 | - /** |
|
| 351 | - * Determine if the router currently has a group stack. |
|
| 352 | - * |
|
| 353 | - * @return bool |
|
| 354 | - */ |
|
| 355 | - public function hasGroupStack() |
|
| 356 | - { |
|
| 357 | - return ! empty($this->groupStack); |
|
| 358 | - } |
|
| 350 | + /** |
|
| 351 | + * Determine if the router currently has a group stack. |
|
| 352 | + * |
|
| 353 | + * @return bool |
|
| 354 | + */ |
|
| 355 | + public function hasGroupStack() |
|
| 356 | + { |
|
| 357 | + return ! empty($this->groupStack); |
|
| 358 | + } |
|
| 359 | 359 | |
| 360 | - /** |
|
| 361 | - * Merge the group stack with the controller action. |
|
| 362 | - * |
|
| 363 | - * @param \Syscpde\Routing\Route $route |
|
| 364 | - * |
|
| 365 | - * @return void |
|
| 366 | - */ |
|
| 367 | - protected function mergeGroupAttributesIntoRoute($route) |
|
| 368 | - { |
|
| 369 | - $action = static::mergeGroup($route->getAction(), end($this->groupStack)); |
|
| 360 | + /** |
|
| 361 | + * Merge the group stack with the controller action. |
|
| 362 | + * |
|
| 363 | + * @param \Syscpde\Routing\Route $route |
|
| 364 | + * |
|
| 365 | + * @return void |
|
| 366 | + */ |
|
| 367 | + protected function mergeGroupAttributesIntoRoute($route) |
|
| 368 | + { |
|
| 369 | + $action = static::mergeGroup($route->getAction(), end($this->groupStack)); |
|
| 370 | 370 | |
| 371 | - $route->setAction($action); |
|
| 372 | - } |
|
| 371 | + $route->setAction($action); |
|
| 372 | + } |
|
| 373 | 373 | |
| 374 | - /** |
|
| 375 | - * Add the necessary where clauses to the route based on its initial registration. |
|
| 376 | - * |
|
| 377 | - * @param \Syscodes\Routing\Route $route |
|
| 378 | - * |
|
| 379 | - * @return \Syscodes\Routing\Route |
|
| 380 | - */ |
|
| 381 | - protected function addWhereClausesToRoute($route) |
|
| 382 | - { |
|
| 383 | - return $route->where(array_merge( |
|
| 384 | - $this->patterns, Arr::get($route->getAction(), 'where', []) |
|
| 385 | - )); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Add a prefix to the route URI. |
|
| 390 | - * |
|
| 391 | - * @param string $uri |
|
| 392 | - * |
|
| 393 | - * @return string |
|
| 394 | - */ |
|
| 395 | - protected function prefix($uri) |
|
| 396 | - { |
|
| 397 | - $uri = is_null($uri) ? '' : trim($uri, '/').'/'; |
|
| 398 | - |
|
| 399 | - $uri = filter_var($uri, FILTER_SANITIZE_STRING); |
|
| 400 | - |
|
| 401 | - // While we want to add a route within a group of '/', |
|
| 402 | - // it doens't work with matching, so remove them... |
|
| 403 | - if ($uri != '/') { |
|
| 404 | - $uri = ltrim($uri, '/'); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - return trim(trim($this->getGroupPrefix(), '/').'/'.trim($uri, '/'), '/') ?: '/'; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Set a global where pattern on all routes. |
|
| 412 | - * |
|
| 413 | - * @param string $name |
|
| 414 | - * @param string $pattern |
|
| 415 | - * |
|
| 416 | - * @return void |
|
| 417 | - */ |
|
| 418 | - public function pattern($name, $pattern) |
|
| 419 | - { |
|
| 420 | - return $this->patterns[$name] = $pattern; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Set a group of global where patterns on all routes. |
|
| 425 | - * |
|
| 426 | - * @param array $patterns |
|
| 427 | - * |
|
| 428 | - * @return void |
|
| 429 | - */ |
|
| 430 | - public function patterns($patterns) |
|
| 431 | - { |
|
| 432 | - foreach ($patterns as $key => $pattern) { |
|
| 433 | - $this->patterns[$key] = $pattern; |
|
| 434 | - } |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Get a Resource instance. |
|
| 439 | - * |
|
| 440 | - * @return \Syscodes\Routing\ResourceRegister |
|
| 441 | - */ |
|
| 442 | - public function getResource() |
|
| 443 | - { |
|
| 444 | - if (isset($this->resources)) { |
|
| 445 | - return $this->resources; |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - return $this->resources = new ResourceRegister($this); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Dispatches the given url and call the method that belongs to the route. |
|
| 453 | - * |
|
| 454 | - * @param \Syscodes\Http\Request $request |
|
| 455 | - * |
|
| 456 | - * @return mixed |
|
| 457 | - */ |
|
| 458 | - public function dispatch(Request $request) |
|
| 459 | - { |
|
| 460 | - return $this->resolve($request, $this->routes); |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * Check if a route with the given name exists. |
|
| 465 | - * |
|
| 466 | - * @param string $name |
|
| 467 | - * |
|
| 468 | - * @return bool |
|
| 469 | - */ |
|
| 470 | - public function has($name) |
|
| 471 | - { |
|
| 472 | - $names = is_array($name) ? $name : func_get_args(); |
|
| 473 | - |
|
| 474 | - foreach ($names as $value) { |
|
| 475 | - if ( ! $this->routes->hasNamedRoute($value)) { |
|
| 476 | - return false; |
|
| 477 | - } |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - return true; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * Get the currently dispatched route instance. |
|
| 485 | - * |
|
| 486 | - * @return \Syscodes\Routing\Route|null |
|
| 487 | - */ |
|
| 488 | - public function current() |
|
| 489 | - { |
|
| 490 | - return $this->current; |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - /** |
|
| 494 | - * Determine if the current route matches a pattern. |
|
| 495 | - * |
|
| 496 | - * @param mixed ...$patterns |
|
| 497 | - * |
|
| 498 | - * @return bool |
|
| 499 | - */ |
|
| 500 | - public function is(...$patterns) |
|
| 501 | - { |
|
| 502 | - return $this->currentRouteNamed(...$patterns); |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * Determine if the current route matches a pattern. |
|
| 507 | - * |
|
| 508 | - * @param mixed ...$patterns |
|
| 509 | - * |
|
| 510 | - * @return bool |
|
| 511 | - */ |
|
| 512 | - public function currentRouteNamed(...$patterns) |
|
| 513 | - { |
|
| 514 | - return $this->current() && $this->current()->named(...$patterns); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - /** |
|
| 518 | - * Register an array of resource controllers. |
|
| 519 | - * |
|
| 520 | - * @param array $resources |
|
| 521 | - * @param array $options |
|
| 522 | - * |
|
| 523 | - * @return void |
|
| 524 | - */ |
|
| 525 | - public function resources(array $resources, array $options = []) |
|
| 526 | - { |
|
| 527 | - foreach ($resources as $name => $controller) { |
|
| 528 | - $this->resource($name, $controller, $options); |
|
| 529 | - } |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - /** |
|
| 533 | - * Route a resource to a controller. |
|
| 534 | - * |
|
| 535 | - * @param string $name |
|
| 536 | - * @param string $controller |
|
| 537 | - * @param array $options |
|
| 538 | - * |
|
| 539 | - * @return \Syscodes\Routing\AwaitingResourceRegistration |
|
| 540 | - */ |
|
| 541 | - public function resource($name, $controller, array $options = []) |
|
| 542 | - { |
|
| 543 | - if ($this->container) { |
|
| 544 | - $register = $this->container->make(ResourceRegister::class); |
|
| 545 | - } else { |
|
| 546 | - $register = new ResourceRegister($this); |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - return new AwaitingResourceRegistration( |
|
| 550 | - $register, $name, $controller, $options |
|
| 551 | - ); |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * Get the route collection. |
|
| 556 | - * |
|
| 557 | - * @return array |
|
| 558 | - */ |
|
| 559 | - public function getRoutes() |
|
| 560 | - { |
|
| 561 | - return $this->routes; |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - /** |
|
| 565 | - * Get or set the verbs used in the resource URIs. |
|
| 566 | - * |
|
| 567 | - * @param array $verbs |
|
| 568 | - * |
|
| 569 | - * @return array|null |
|
| 570 | - */ |
|
| 571 | - public function resourceVerbs(array $verbs = []) |
|
| 572 | - { |
|
| 573 | - ResourceRegister::verbs($verbs); |
|
| 574 | - } |
|
| 374 | + /** |
|
| 375 | + * Add the necessary where clauses to the route based on its initial registration. |
|
| 376 | + * |
|
| 377 | + * @param \Syscodes\Routing\Route $route |
|
| 378 | + * |
|
| 379 | + * @return \Syscodes\Routing\Route |
|
| 380 | + */ |
|
| 381 | + protected function addWhereClausesToRoute($route) |
|
| 382 | + { |
|
| 383 | + return $route->where(array_merge( |
|
| 384 | + $this->patterns, Arr::get($route->getAction(), 'where', []) |
|
| 385 | + )); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Add a prefix to the route URI. |
|
| 390 | + * |
|
| 391 | + * @param string $uri |
|
| 392 | + * |
|
| 393 | + * @return string |
|
| 394 | + */ |
|
| 395 | + protected function prefix($uri) |
|
| 396 | + { |
|
| 397 | + $uri = is_null($uri) ? '' : trim($uri, '/').'/'; |
|
| 398 | + |
|
| 399 | + $uri = filter_var($uri, FILTER_SANITIZE_STRING); |
|
| 400 | + |
|
| 401 | + // While we want to add a route within a group of '/', |
|
| 402 | + // it doens't work with matching, so remove them... |
|
| 403 | + if ($uri != '/') { |
|
| 404 | + $uri = ltrim($uri, '/'); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + return trim(trim($this->getGroupPrefix(), '/').'/'.trim($uri, '/'), '/') ?: '/'; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Set a global where pattern on all routes. |
|
| 412 | + * |
|
| 413 | + * @param string $name |
|
| 414 | + * @param string $pattern |
|
| 415 | + * |
|
| 416 | + * @return void |
|
| 417 | + */ |
|
| 418 | + public function pattern($name, $pattern) |
|
| 419 | + { |
|
| 420 | + return $this->patterns[$name] = $pattern; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Set a group of global where patterns on all routes. |
|
| 425 | + * |
|
| 426 | + * @param array $patterns |
|
| 427 | + * |
|
| 428 | + * @return void |
|
| 429 | + */ |
|
| 430 | + public function patterns($patterns) |
|
| 431 | + { |
|
| 432 | + foreach ($patterns as $key => $pattern) { |
|
| 433 | + $this->patterns[$key] = $pattern; |
|
| 434 | + } |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Get a Resource instance. |
|
| 439 | + * |
|
| 440 | + * @return \Syscodes\Routing\ResourceRegister |
|
| 441 | + */ |
|
| 442 | + public function getResource() |
|
| 443 | + { |
|
| 444 | + if (isset($this->resources)) { |
|
| 445 | + return $this->resources; |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + return $this->resources = new ResourceRegister($this); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Dispatches the given url and call the method that belongs to the route. |
|
| 453 | + * |
|
| 454 | + * @param \Syscodes\Http\Request $request |
|
| 455 | + * |
|
| 456 | + * @return mixed |
|
| 457 | + */ |
|
| 458 | + public function dispatch(Request $request) |
|
| 459 | + { |
|
| 460 | + return $this->resolve($request, $this->routes); |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * Check if a route with the given name exists. |
|
| 465 | + * |
|
| 466 | + * @param string $name |
|
| 467 | + * |
|
| 468 | + * @return bool |
|
| 469 | + */ |
|
| 470 | + public function has($name) |
|
| 471 | + { |
|
| 472 | + $names = is_array($name) ? $name : func_get_args(); |
|
| 473 | + |
|
| 474 | + foreach ($names as $value) { |
|
| 475 | + if ( ! $this->routes->hasNamedRoute($value)) { |
|
| 476 | + return false; |
|
| 477 | + } |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + return true; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * Get the currently dispatched route instance. |
|
| 485 | + * |
|
| 486 | + * @return \Syscodes\Routing\Route|null |
|
| 487 | + */ |
|
| 488 | + public function current() |
|
| 489 | + { |
|
| 490 | + return $this->current; |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + /** |
|
| 494 | + * Determine if the current route matches a pattern. |
|
| 495 | + * |
|
| 496 | + * @param mixed ...$patterns |
|
| 497 | + * |
|
| 498 | + * @return bool |
|
| 499 | + */ |
|
| 500 | + public function is(...$patterns) |
|
| 501 | + { |
|
| 502 | + return $this->currentRouteNamed(...$patterns); |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * Determine if the current route matches a pattern. |
|
| 507 | + * |
|
| 508 | + * @param mixed ...$patterns |
|
| 509 | + * |
|
| 510 | + * @return bool |
|
| 511 | + */ |
|
| 512 | + public function currentRouteNamed(...$patterns) |
|
| 513 | + { |
|
| 514 | + return $this->current() && $this->current()->named(...$patterns); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + /** |
|
| 518 | + * Register an array of resource controllers. |
|
| 519 | + * |
|
| 520 | + * @param array $resources |
|
| 521 | + * @param array $options |
|
| 522 | + * |
|
| 523 | + * @return void |
|
| 524 | + */ |
|
| 525 | + public function resources(array $resources, array $options = []) |
|
| 526 | + { |
|
| 527 | + foreach ($resources as $name => $controller) { |
|
| 528 | + $this->resource($name, $controller, $options); |
|
| 529 | + } |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + /** |
|
| 533 | + * Route a resource to a controller. |
|
| 534 | + * |
|
| 535 | + * @param string $name |
|
| 536 | + * @param string $controller |
|
| 537 | + * @param array $options |
|
| 538 | + * |
|
| 539 | + * @return \Syscodes\Routing\AwaitingResourceRegistration |
|
| 540 | + */ |
|
| 541 | + public function resource($name, $controller, array $options = []) |
|
| 542 | + { |
|
| 543 | + if ($this->container) { |
|
| 544 | + $register = $this->container->make(ResourceRegister::class); |
|
| 545 | + } else { |
|
| 546 | + $register = new ResourceRegister($this); |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + return new AwaitingResourceRegistration( |
|
| 550 | + $register, $name, $controller, $options |
|
| 551 | + ); |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * Get the route collection. |
|
| 556 | + * |
|
| 557 | + * @return array |
|
| 558 | + */ |
|
| 559 | + public function getRoutes() |
|
| 560 | + { |
|
| 561 | + return $this->routes; |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + /** |
|
| 565 | + * Get or set the verbs used in the resource URIs. |
|
| 566 | + * |
|
| 567 | + * @param array $verbs |
|
| 568 | + * |
|
| 569 | + * @return array|null |
|
| 570 | + */ |
|
| 571 | + public function resourceVerbs(array $verbs = []) |
|
| 572 | + { |
|
| 573 | + ResourceRegister::verbs($verbs); |
|
| 574 | + } |
|
| 575 | 575 | |
| 576 | - /** |
|
| 577 | - * Register a custom macro. |
|
| 578 | - * |
|
| 579 | - * @param string $name |
|
| 580 | - * @param callable $callback |
|
| 581 | - * |
|
| 582 | - * @return void |
|
| 583 | - */ |
|
| 584 | - public function macro($name, callable $callback) |
|
| 585 | - { |
|
| 586 | - $this->macros[$name] = $callback; |
|
| 587 | - } |
|
| 576 | + /** |
|
| 577 | + * Register a custom macro. |
|
| 578 | + * |
|
| 579 | + * @param string $name |
|
| 580 | + * @param callable $callback |
|
| 581 | + * |
|
| 582 | + * @return void |
|
| 583 | + */ |
|
| 584 | + public function macro($name, callable $callback) |
|
| 585 | + { |
|
| 586 | + $this->macros[$name] = $callback; |
|
| 587 | + } |
|
| 588 | 588 | |
| 589 | - /** |
|
| 590 | - * Checks if macro is registered. |
|
| 591 | - * |
|
| 592 | - * @param string $name |
|
| 593 | - * |
|
| 594 | - * @return boolean |
|
| 595 | - */ |
|
| 596 | - public function hasMacro($name) |
|
| 597 | - { |
|
| 598 | - return isset($this->macros[$name]); |
|
| 599 | - } |
|
| 589 | + /** |
|
| 590 | + * Checks if macro is registered. |
|
| 591 | + * |
|
| 592 | + * @param string $name |
|
| 593 | + * |
|
| 594 | + * @return boolean |
|
| 595 | + */ |
|
| 596 | + public function hasMacro($name) |
|
| 597 | + { |
|
| 598 | + return isset($this->macros[$name]); |
|
| 599 | + } |
|
| 600 | 600 | |
| 601 | - /** |
|
| 602 | - * Dynamically handle calls into the router instance. |
|
| 603 | - * |
|
| 604 | - * @param string $method |
|
| 605 | - * @param array $parameters |
|
| 606 | - * |
|
| 607 | - * @return mixed |
|
| 608 | - */ |
|
| 609 | - public function __call($method, $parameters) |
|
| 610 | - { |
|
| 611 | - if (isset($this->macros[$method])) { |
|
| 612 | - $callback = $this->macros[$method]; |
|
| 613 | - |
|
| 614 | - return call_user_func_array($callback, $parameters); |
|
| 615 | - } |
|
| 601 | + /** |
|
| 602 | + * Dynamically handle calls into the router instance. |
|
| 603 | + * |
|
| 604 | + * @param string $method |
|
| 605 | + * @param array $parameters |
|
| 606 | + * |
|
| 607 | + * @return mixed |
|
| 608 | + */ |
|
| 609 | + public function __call($method, $parameters) |
|
| 610 | + { |
|
| 611 | + if (isset($this->macros[$method])) { |
|
| 612 | + $callback = $this->macros[$method]; |
|
| 613 | + |
|
| 614 | + return call_user_func_array($callback, $parameters); |
|
| 615 | + } |
|
| 616 | 616 | |
| 617 | - return (new RouteRegister($this))->attribute($method, $parameters[0]); |
|
| 618 | - } |
|
| 617 | + return (new RouteRegister($this))->attribute($method, $parameters[0]); |
|
| 618 | + } |
|
| 619 | 619 | } |
| 620 | 620 | \ No newline at end of file |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | */ |
| 228 | 228 | public function redirect($uri, $destination, $status = 302) |
| 229 | 229 | { |
| 230 | - return $this->any($uri, function () use ($destination, $status) { |
|
| 230 | + return $this->any($uri, function() use ($destination, $status) { |
|
| 231 | 231 | return new RedirectResponse($destination, $status); |
| 232 | 232 | }); |
| 233 | 233 | } |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | */ |
| 244 | 244 | public function view($uri, $view, $data = []) |
| 245 | 245 | { |
| 246 | - return $this->match(['GET', 'HEAD'], $uri, function () use ($view, $data) { |
|
| 246 | + return $this->match(['GET', 'HEAD'], $uri, function() use ($view, $data) { |
|
| 247 | 247 | return $this->container->make('view')->make($view, $data); |
| 248 | 248 | }); |
| 249 | 249 | } |
@@ -31,97 +31,97 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class RouteGroup |
| 33 | 33 | { |
| 34 | - /** |
|
| 35 | - * Merge the given group attributes. |
|
| 36 | - * |
|
| 37 | - * @param array $new |
|
| 38 | - * @param array $old |
|
| 39 | - * |
|
| 40 | - * @return array |
|
| 41 | - */ |
|
| 42 | - public static function mergeGroup($new, $old) |
|
| 43 | - { |
|
| 44 | - if (isset($new['domain'])) { |
|
| 45 | - unset($old['domain']); |
|
| 46 | - } |
|
| 34 | + /** |
|
| 35 | + * Merge the given group attributes. |
|
| 36 | + * |
|
| 37 | + * @param array $new |
|
| 38 | + * @param array $old |
|
| 39 | + * |
|
| 40 | + * @return array |
|
| 41 | + */ |
|
| 42 | + public static function mergeGroup($new, $old) |
|
| 43 | + { |
|
| 44 | + if (isset($new['domain'])) { |
|
| 45 | + unset($old['domain']); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - $new = array_merge(static::formatUseAs($new, $old), [ |
|
| 48 | + $new = array_merge(static::formatUseAs($new, $old), [ |
|
| 49 | 49 | 'namespace' => static::formatUseNamespace($new, $old), |
| 50 | 50 | 'prefix' => static::formatUsePrefix($new, $old), |
| 51 | 51 | 'where' => static::formatUseWhere($new, $old) |
| 52 | 52 | ]); |
| 53 | 53 | |
| 54 | - return array_merge_recursive( |
|
| 55 | - Arr::except($old, array('namespace', 'prefix', 'where', 'as')), $new |
|
| 56 | - ); |
|
| 57 | - } |
|
| 54 | + return array_merge_recursive( |
|
| 55 | + Arr::except($old, array('namespace', 'prefix', 'where', 'as')), $new |
|
| 56 | + ); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Format the uses namespace for the new group attributes. |
|
| 61 | - * |
|
| 62 | - * @param array $new |
|
| 63 | - * @param array $old |
|
| 64 | - * |
|
| 65 | - * @return string|null |
|
| 66 | - */ |
|
| 67 | - protected static function formatUseNamespace($new, $old) |
|
| 68 | - { |
|
| 69 | - if (isset($new['namespace'])) { |
|
| 70 | - return isset($old['namespace']) |
|
| 71 | - ? trim($old['namespace'], '\\').'\\'.trim($new['namespace'], '\\') |
|
| 72 | - : trim($new['namespace'], '\\'); |
|
| 73 | - } |
|
| 59 | + /** |
|
| 60 | + * Format the uses namespace for the new group attributes. |
|
| 61 | + * |
|
| 62 | + * @param array $new |
|
| 63 | + * @param array $old |
|
| 64 | + * |
|
| 65 | + * @return string|null |
|
| 66 | + */ |
|
| 67 | + protected static function formatUseNamespace($new, $old) |
|
| 68 | + { |
|
| 69 | + if (isset($new['namespace'])) { |
|
| 70 | + return isset($old['namespace']) |
|
| 71 | + ? trim($old['namespace'], '\\').'\\'.trim($new['namespace'], '\\') |
|
| 72 | + : trim($new['namespace'], '\\'); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - return $old['namespace'] ?? null; |
|
| 76 | - } |
|
| 75 | + return $old['namespace'] ?? null; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Format the prefix for the new group attributes. |
|
| 80 | - * |
|
| 81 | - * @param array $new |
|
| 82 | - * @param array $old |
|
| 83 | - * |
|
| 84 | - * @return string|null |
|
| 85 | - */ |
|
| 86 | - protected static function formatUsePrefix($new, $old) |
|
| 87 | - { |
|
| 88 | - $old = $old['prefix'] ?? null; |
|
| 78 | + /** |
|
| 79 | + * Format the prefix for the new group attributes. |
|
| 80 | + * |
|
| 81 | + * @param array $new |
|
| 82 | + * @param array $old |
|
| 83 | + * |
|
| 84 | + * @return string|null |
|
| 85 | + */ |
|
| 86 | + protected static function formatUsePrefix($new, $old) |
|
| 87 | + { |
|
| 88 | + $old = $old['prefix'] ?? null; |
|
| 89 | 89 | |
| 90 | - return isset($new['prefix']) |
|
| 91 | - ? trim($old, '/').'/'.trim($new['prefix'], '/') |
|
| 92 | - : $old; |
|
| 93 | - } |
|
| 90 | + return isset($new['prefix']) |
|
| 91 | + ? trim($old, '/').'/'.trim($new['prefix'], '/') |
|
| 92 | + : $old; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * Format the "wheres" for the new group attributes. |
|
| 97 | - * |
|
| 98 | - * @param array $new |
|
| 99 | - * @param array $old |
|
| 100 | - * |
|
| 101 | - * @return array |
|
| 102 | - */ |
|
| 103 | - protected static function formatUseWhere($new, $old) |
|
| 104 | - { |
|
| 105 | - return array_merge( |
|
| 106 | - $old['where'] ?? [], |
|
| 107 | - $new['where'] ?? [] |
|
| 108 | - ); |
|
| 109 | - } |
|
| 95 | + /** |
|
| 96 | + * Format the "wheres" for the new group attributes. |
|
| 97 | + * |
|
| 98 | + * @param array $new |
|
| 99 | + * @param array $old |
|
| 100 | + * |
|
| 101 | + * @return array |
|
| 102 | + */ |
|
| 103 | + protected static function formatUseWhere($new, $old) |
|
| 104 | + { |
|
| 105 | + return array_merge( |
|
| 106 | + $old['where'] ?? [], |
|
| 107 | + $new['where'] ?? [] |
|
| 108 | + ); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Format the "as" clause of the new group attributes. |
|
| 113 | - * |
|
| 114 | - * @param array $new |
|
| 115 | - * @param array $old |
|
| 116 | - * |
|
| 117 | - * @return array |
|
| 118 | - */ |
|
| 119 | - protected static function formatUseAs($new, $old) |
|
| 120 | - { |
|
| 121 | - if (isset($old['as'])) { |
|
| 111 | + /** |
|
| 112 | + * Format the "as" clause of the new group attributes. |
|
| 113 | + * |
|
| 114 | + * @param array $new |
|
| 115 | + * @param array $old |
|
| 116 | + * |
|
| 117 | + * @return array |
|
| 118 | + */ |
|
| 119 | + protected static function formatUseAs($new, $old) |
|
| 120 | + { |
|
| 121 | + if (isset($old['as'])) { |
|
| 122 | 122 | $new['as'] = $old['as'].($new['as'] ?? ''); |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - return $new; |
|
| 126 | - } |
|
| 125 | + return $new; |
|
| 126 | + } |
|
| 127 | 127 | } |
| 128 | 128 | \ No newline at end of file |
@@ -95,7 +95,7 @@ |
||
| 95 | 95 | |
| 96 | 96 | $parameters = array_intersect_key($matches, array_values($parameterNames)); |
| 97 | 97 | |
| 98 | - return array_filter($parameters, function ($value) { |
|
| 98 | + return array_filter($parameters, function($value) { |
|
| 99 | 99 | return (is_string($value) && strlen($value) > 0); |
| 100 | 100 | }); |
| 101 | 101 | } |
@@ -33,721 +33,721 @@ |
||
| 33 | 33 | */ |
| 34 | 34 | class Cli |
| 35 | 35 | { |
| 36 | - /** |
|
| 37 | - * Background color identifier. |
|
| 38 | - * |
|
| 39 | - * @var array $backgroundColors |
|
| 40 | - */ |
|
| 41 | - protected static $backgroundColors = [ |
|
| 42 | - 'black' => '40', |
|
| 43 | - 'red' => '41', |
|
| 44 | - 'green' => '42', |
|
| 45 | - 'yellow' => '43', |
|
| 46 | - 'blue' => '44', |
|
| 47 | - 'magenta' => '45', |
|
| 48 | - 'cyan' => '46', |
|
| 49 | - 'light_gray' => '47' |
|
| 50 | - ]; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Foreground color identifier. |
|
| 54 | - * |
|
| 55 | - * @var array $foregroundColors |
|
| 56 | - */ |
|
| 57 | - protected static $foregroundColors = [ |
|
| 58 | - 'black' => '0;30', |
|
| 59 | - 'dark_gray' => '1;30', |
|
| 60 | - 'blue' => '0;34', |
|
| 61 | - 'dark_blue' => '1;34', |
|
| 62 | - 'light_blue' => '1;34', |
|
| 63 | - 'green' => '0;32', |
|
| 64 | - 'light_green' => '1;32', |
|
| 65 | - 'cyan' => '0;36', |
|
| 66 | - 'light_cyan' => '1;36', |
|
| 67 | - 'red' => '0;31', |
|
| 68 | - 'light_red' => '1;31', |
|
| 69 | - 'purple' => '0;35', |
|
| 70 | - 'light_purple' => '1;35', |
|
| 71 | - 'light_yellow' => '0;33', |
|
| 72 | - 'yellow' => '1;33', |
|
| 73 | - 'light_gray' => '0;37', |
|
| 74 | - 'white' => '1;37' |
|
| 75 | - ]; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Indicates that you do not use any color for foreground or background. |
|
| 79 | - * |
|
| 80 | - * @var bool $noColor |
|
| 81 | - */ |
|
| 82 | - public static $noColor = false; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * String of arguments to be used in console. |
|
| 86 | - * |
|
| 87 | - * @var array $options |
|
| 88 | - */ |
|
| 89 | - protected static $options = []; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Readline Support for command line. |
|
| 93 | - * |
|
| 94 | - * @var bool $readlineSupport |
|
| 95 | - */ |
|
| 96 | - public static $readlineSupport = false; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * List of array segments. |
|
| 100 | - * |
|
| 101 | - * @var array $segments |
|
| 102 | - */ |
|
| 103 | - protected static $segments = []; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * The standar STDERR is where the application writes its error messages. |
|
| 107 | - * |
|
| 108 | - * @var string $stderr |
|
| 109 | - */ |
|
| 110 | - protected static $stderr; |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * The estandar STDOUT is where the application records its output messages. |
|
| 114 | - * |
|
| 115 | - * @var resource $stdout |
|
| 116 | - */ |
|
| 117 | - protected static $stdout; |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Message that tells the user that he is waiting to receive an order. |
|
| 121 | - * |
|
| 122 | - * @var string $waitMsg |
|
| 123 | - */ |
|
| 124 | - public static $waitMsg = 'Press any key to continue...'; |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Static constructor. Parses all the CLI params. |
|
| 128 | - * |
|
| 129 | - * @return \Syscodes\Contracts\Core\Lenevor $core |
|
| 130 | - * |
|
| 131 | - * @throws \Exception |
|
| 132 | - */ |
|
| 133 | - public static function initialize() |
|
| 134 | - { |
|
| 135 | - // Readline is an extension for PHP that makes interactive the command console |
|
| 136 | - static::$readlineSupport = extension_loaded('readline'); |
|
| 137 | - |
|
| 138 | - // clear segments & options to keep testing clean |
|
| 139 | - static::$options = []; |
|
| 140 | - static::$segments = []; |
|
| 141 | - |
|
| 142 | - static::parseCommandLine(); |
|
| 143 | - |
|
| 144 | - // Writes its error messages |
|
| 145 | - static::$stderr = STDERR; |
|
| 146 | - |
|
| 147 | - // Records its output messages |
|
| 148 | - static::$stdout = STDOUT; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Beeps a certain number of times. |
|
| 153 | - * |
|
| 154 | - * @param int $num The number of times to beep |
|
| 155 | - * |
|
| 156 | - * @return int |
|
| 157 | - */ |
|
| 158 | - public static function bell(int $num = 1) |
|
| 159 | - { |
|
| 160 | - echo str_repeat("\x07", $num); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Clears the screen of output. |
|
| 165 | - * |
|
| 166 | - * @return void |
|
| 167 | - */ |
|
| 168 | - public static function clearScreen() |
|
| 169 | - { |
|
| 170 | - static::isWindows() |
|
| 171 | - // Windows doesn't work for this, but their terminal is tiny so shove this in |
|
| 172 | - ? static::newLine(40) |
|
| 173 | - // Anything with a flair of Unix will handle these magic characters |
|
| 174 | - : fwrite(static::$stdout, chr(27)."[H".chr(27)."[2J"); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Returns the given text with the correct color codes for a foreground and |
|
| 179 | - * optionally a background color. |
|
| 180 | - * |
|
| 181 | - * @param string $text The text to color |
|
| 182 | - * @param string $foreground The foreground color |
|
| 183 | - * @param string $background The background color |
|
| 184 | - * @param string $format Other formatting to apply. Currently only 'underline' is understood |
|
| 185 | - * |
|
| 186 | - * @return string The color coded string |
|
| 187 | - * |
|
| 188 | - * @throws \Syscodes\Core\Exceptions\LenevorException |
|
| 189 | - */ |
|
| 190 | - public static function color(string $text, string $foreground, string $background = null, string $format = null) |
|
| 191 | - { |
|
| 192 | - if (static::$noColor) |
|
| 193 | - { |
|
| 194 | - return $text; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - if ( ! Arr::exists(static::$foregroundColors, $foreground)) { |
|
| 198 | - throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
| 202 | - throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - $string = "\033[".static::$foregroundColors[$foreground]."m"; |
|
| 206 | - |
|
| 207 | - if ($background !== null) { |
|
| 208 | - $string .= "\033[".static::$backgroundColors[$background]."m"; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - if ($format === 'underline') { |
|
| 212 | - $string .= "\033[4m"; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - $string .= $text."\033[0m"; |
|
| 216 | - |
|
| 217 | - return $string; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Get the number of characters in a string. |
|
| 222 | - * |
|
| 223 | - * @param string $string |
|
| 224 | - * |
|
| 225 | - * @return int |
|
| 226 | - */ |
|
| 227 | - public static function strlen(?string $string) |
|
| 228 | - { |
|
| 229 | - if (is_null($string)) { |
|
| 230 | - return 0; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - foreach (static::$foregroundColors as $color) { |
|
| 234 | - $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - foreach (static::$backgroundColors as $color) { |
|
| 238 | - $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $string = strtr($string, ["\033[4m" => '', "\033[0m" => '']); |
|
| 242 | - |
|
| 243 | - return mb_strlen($string); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Outputs an error to the CLI using STDERR instead of STDOUT. |
|
| 248 | - * |
|
| 249 | - * @param string|array $text The text to output, or array of errors |
|
| 250 | - * @param string $foreground The foreground color |
|
| 251 | - * @param string|null $background the background color |
|
| 252 | - * |
|
| 253 | - * @return string |
|
| 254 | - */ |
|
| 255 | - public static function error(string $text = '', string $foreground = 'light_red', string $background = null) |
|
| 256 | - { |
|
| 257 | - if (is_array($text)) { |
|
| 258 | - $text = implode(PHP_EOL, $text); |
|
| 259 | - } |
|
| 36 | + /** |
|
| 37 | + * Background color identifier. |
|
| 38 | + * |
|
| 39 | + * @var array $backgroundColors |
|
| 40 | + */ |
|
| 41 | + protected static $backgroundColors = [ |
|
| 42 | + 'black' => '40', |
|
| 43 | + 'red' => '41', |
|
| 44 | + 'green' => '42', |
|
| 45 | + 'yellow' => '43', |
|
| 46 | + 'blue' => '44', |
|
| 47 | + 'magenta' => '45', |
|
| 48 | + 'cyan' => '46', |
|
| 49 | + 'light_gray' => '47' |
|
| 50 | + ]; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Foreground color identifier. |
|
| 54 | + * |
|
| 55 | + * @var array $foregroundColors |
|
| 56 | + */ |
|
| 57 | + protected static $foregroundColors = [ |
|
| 58 | + 'black' => '0;30', |
|
| 59 | + 'dark_gray' => '1;30', |
|
| 60 | + 'blue' => '0;34', |
|
| 61 | + 'dark_blue' => '1;34', |
|
| 62 | + 'light_blue' => '1;34', |
|
| 63 | + 'green' => '0;32', |
|
| 64 | + 'light_green' => '1;32', |
|
| 65 | + 'cyan' => '0;36', |
|
| 66 | + 'light_cyan' => '1;36', |
|
| 67 | + 'red' => '0;31', |
|
| 68 | + 'light_red' => '1;31', |
|
| 69 | + 'purple' => '0;35', |
|
| 70 | + 'light_purple' => '1;35', |
|
| 71 | + 'light_yellow' => '0;33', |
|
| 72 | + 'yellow' => '1;33', |
|
| 73 | + 'light_gray' => '0;37', |
|
| 74 | + 'white' => '1;37' |
|
| 75 | + ]; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Indicates that you do not use any color for foreground or background. |
|
| 79 | + * |
|
| 80 | + * @var bool $noColor |
|
| 81 | + */ |
|
| 82 | + public static $noColor = false; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * String of arguments to be used in console. |
|
| 86 | + * |
|
| 87 | + * @var array $options |
|
| 88 | + */ |
|
| 89 | + protected static $options = []; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Readline Support for command line. |
|
| 93 | + * |
|
| 94 | + * @var bool $readlineSupport |
|
| 95 | + */ |
|
| 96 | + public static $readlineSupport = false; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * List of array segments. |
|
| 100 | + * |
|
| 101 | + * @var array $segments |
|
| 102 | + */ |
|
| 103 | + protected static $segments = []; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * The standar STDERR is where the application writes its error messages. |
|
| 107 | + * |
|
| 108 | + * @var string $stderr |
|
| 109 | + */ |
|
| 110 | + protected static $stderr; |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * The estandar STDOUT is where the application records its output messages. |
|
| 114 | + * |
|
| 115 | + * @var resource $stdout |
|
| 116 | + */ |
|
| 117 | + protected static $stdout; |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Message that tells the user that he is waiting to receive an order. |
|
| 121 | + * |
|
| 122 | + * @var string $waitMsg |
|
| 123 | + */ |
|
| 124 | + public static $waitMsg = 'Press any key to continue...'; |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Static constructor. Parses all the CLI params. |
|
| 128 | + * |
|
| 129 | + * @return \Syscodes\Contracts\Core\Lenevor $core |
|
| 130 | + * |
|
| 131 | + * @throws \Exception |
|
| 132 | + */ |
|
| 133 | + public static function initialize() |
|
| 134 | + { |
|
| 135 | + // Readline is an extension for PHP that makes interactive the command console |
|
| 136 | + static::$readlineSupport = extension_loaded('readline'); |
|
| 137 | + |
|
| 138 | + // clear segments & options to keep testing clean |
|
| 139 | + static::$options = []; |
|
| 140 | + static::$segments = []; |
|
| 141 | + |
|
| 142 | + static::parseCommandLine(); |
|
| 143 | + |
|
| 144 | + // Writes its error messages |
|
| 145 | + static::$stderr = STDERR; |
|
| 146 | + |
|
| 147 | + // Records its output messages |
|
| 148 | + static::$stdout = STDOUT; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Beeps a certain number of times. |
|
| 153 | + * |
|
| 154 | + * @param int $num The number of times to beep |
|
| 155 | + * |
|
| 156 | + * @return int |
|
| 157 | + */ |
|
| 158 | + public static function bell(int $num = 1) |
|
| 159 | + { |
|
| 160 | + echo str_repeat("\x07", $num); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Clears the screen of output. |
|
| 165 | + * |
|
| 166 | + * @return void |
|
| 167 | + */ |
|
| 168 | + public static function clearScreen() |
|
| 169 | + { |
|
| 170 | + static::isWindows() |
|
| 171 | + // Windows doesn't work for this, but their terminal is tiny so shove this in |
|
| 172 | + ? static::newLine(40) |
|
| 173 | + // Anything with a flair of Unix will handle these magic characters |
|
| 174 | + : fwrite(static::$stdout, chr(27)."[H".chr(27)."[2J"); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Returns the given text with the correct color codes for a foreground and |
|
| 179 | + * optionally a background color. |
|
| 180 | + * |
|
| 181 | + * @param string $text The text to color |
|
| 182 | + * @param string $foreground The foreground color |
|
| 183 | + * @param string $background The background color |
|
| 184 | + * @param string $format Other formatting to apply. Currently only 'underline' is understood |
|
| 185 | + * |
|
| 186 | + * @return string The color coded string |
|
| 187 | + * |
|
| 188 | + * @throws \Syscodes\Core\Exceptions\LenevorException |
|
| 189 | + */ |
|
| 190 | + public static function color(string $text, string $foreground, string $background = null, string $format = null) |
|
| 191 | + { |
|
| 192 | + if (static::$noColor) |
|
| 193 | + { |
|
| 194 | + return $text; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + if ( ! Arr::exists(static::$foregroundColors, $foreground)) { |
|
| 198 | + throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
| 202 | + throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + $string = "\033[".static::$foregroundColors[$foreground]."m"; |
|
| 206 | + |
|
| 207 | + if ($background !== null) { |
|
| 208 | + $string .= "\033[".static::$backgroundColors[$background]."m"; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + if ($format === 'underline') { |
|
| 212 | + $string .= "\033[4m"; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + $string .= $text."\033[0m"; |
|
| 216 | + |
|
| 217 | + return $string; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Get the number of characters in a string. |
|
| 222 | + * |
|
| 223 | + * @param string $string |
|
| 224 | + * |
|
| 225 | + * @return int |
|
| 226 | + */ |
|
| 227 | + public static function strlen(?string $string) |
|
| 228 | + { |
|
| 229 | + if (is_null($string)) { |
|
| 230 | + return 0; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + foreach (static::$foregroundColors as $color) { |
|
| 234 | + $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + foreach (static::$backgroundColors as $color) { |
|
| 238 | + $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $string = strtr($string, ["\033[4m" => '', "\033[0m" => '']); |
|
| 242 | + |
|
| 243 | + return mb_strlen($string); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Outputs an error to the CLI using STDERR instead of STDOUT. |
|
| 248 | + * |
|
| 249 | + * @param string|array $text The text to output, or array of errors |
|
| 250 | + * @param string $foreground The foreground color |
|
| 251 | + * @param string|null $background the background color |
|
| 252 | + * |
|
| 253 | + * @return string |
|
| 254 | + */ |
|
| 255 | + public static function error(string $text = '', string $foreground = 'light_red', string $background = null) |
|
| 256 | + { |
|
| 257 | + if (is_array($text)) { |
|
| 258 | + $text = implode(PHP_EOL, $text); |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - if ($foreground || $background) { |
|
| 262 | - $text = static::color($text, $foreground, $background); |
|
| 263 | - } |
|
| 261 | + if ($foreground || $background) { |
|
| 262 | + $text = static::color($text, $foreground, $background); |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - static::fwrite(static::$stderr, $text.PHP_EOL); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Attempts to determine the width of the viewable CLI window. |
|
| 270 | - * |
|
| 271 | - * @param int $default |
|
| 272 | - * |
|
| 273 | - * @return int |
|
| 274 | - */ |
|
| 275 | - public static function getWidth(int $default = 80) |
|
| 276 | - { |
|
| 277 | - if (static::isWindows() || (int) shell_exec('tput cols') === 0) { |
|
| 278 | - return $default; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - return (int) shell_exec('tput cols'); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Attempts to determine the height of the viewable CLI window. |
|
| 286 | - * |
|
| 287 | - * @param int $default |
|
| 288 | - * |
|
| 289 | - * @return int |
|
| 290 | - */ |
|
| 291 | - public static function getHeight(int $default = 32) |
|
| 292 | - { |
|
| 293 | - if (static::isWindows()) { |
|
| 294 | - return $default; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - return (int) shell_exec('tput lines'); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Takes a string and writes it to the command line, wrapping to a maximum width. |
|
| 302 | - * If no maximum width is specified, will wrap to the window's max. |
|
| 303 | - * |
|
| 304 | - * @param string $string |
|
| 305 | - * @param int $max |
|
| 306 | - * @param int $padLeft |
|
| 307 | - * |
|
| 308 | - * @return string |
|
| 309 | - */ |
|
| 310 | - public static function wrap(string $string = null, int $max = 0, int $padLeft = 0) |
|
| 311 | - { |
|
| 312 | - if (empty($string)) { |
|
| 313 | - return ''; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - if ($max === 0) { |
|
| 317 | - $max = static::getWidth(); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - if (static::getWidth() < $max) { |
|
| 321 | - $max = static::getWidth(); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - $max = $max - $padLeft; |
|
| 325 | - |
|
| 326 | - $lines = wordwrap($string, $max); |
|
| 327 | - |
|
| 328 | - if ($pad_left > 0) { |
|
| 329 | - $lines = explode(PHP_EOL, $lines); |
|
| 330 | - |
|
| 331 | - $first = true; |
|
| 332 | - |
|
| 333 | - array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
| 334 | - if ( ! $first) { |
|
| 335 | - $line = str_repeat(' ', $pad_left) . $line; |
|
| 336 | - } else { |
|
| 337 | - $first = false; |
|
| 338 | - } |
|
| 339 | - }); |
|
| 340 | - |
|
| 341 | - $lines = implode(PHP_EOL, $lines); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - return $lines; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Get input from the shell, using readline or the standard STDIN. |
|
| 349 | - * |
|
| 350 | - * @param string|int $prefix The name of the option (int if unnamed) |
|
| 351 | - * |
|
| 352 | - * @return string |
|
| 353 | - */ |
|
| 354 | - public static function input($prefix = '') |
|
| 355 | - { |
|
| 356 | - if (static::$readlineSupport) { |
|
| 357 | - return readline($prefix); |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - echo $prefix; |
|
| 361 | - |
|
| 362 | - return fgets(STDIN); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * If operating system === windows. |
|
| 367 | - * |
|
| 368 | - * @return string |
|
| 369 | - */ |
|
| 370 | - public static function isWindows() |
|
| 371 | - { |
|
| 372 | - return stripos(PHP_OS, 'WIN') === 0; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Enter a number of empty lines. |
|
| 377 | - * |
|
| 378 | - * @param int $num Number of lines to output |
|
| 379 | - * |
|
| 380 | - * @return void |
|
| 381 | - */ |
|
| 382 | - public static function newLine(int $num = 1) |
|
| 383 | - { |
|
| 384 | - for ($i = 0; $i < $num; $i++) { |
|
| 385 | - static::write(); |
|
| 386 | - } |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Returns the option with the given name. You can also give the option number. |
|
| 391 | - * |
|
| 392 | - * @param string|int $name The name of the option (int if unnamed) |
|
| 393 | - * @param mixed $default The value to return if the option is not defined |
|
| 394 | - * |
|
| 395 | - * @return mixed |
|
| 396 | - * |
|
| 397 | - * @uses \Syscodes\Contract\Core\Lenevor |
|
| 398 | - */ |
|
| 399 | - public static function option($name, $default = null) |
|
| 400 | - { |
|
| 401 | - if ( ! isset(static::$options[$name])) { |
|
| 402 | - return Lenevor::value($default); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - return static::$options[$name]; |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Parses the command line it was called from and collects all |
|
| 410 | - * options and valid segments. |
|
| 411 | - * |
|
| 412 | - * @return bool |
|
| 413 | - */ |
|
| 414 | - protected static function parseCommandLine() |
|
| 415 | - { |
|
| 416 | - $options = false; |
|
| 417 | - |
|
| 418 | - for ($i = 1; $i < $_SERVER['argc']; $i++) { |
|
| 419 | - if ( ! $options && mb_strpos($_SERVER['argv'][$i], '-') === false) { |
|
| 420 | - static::$segments[] = $_SERVER['argv'][$i]; |
|
| 421 | - |
|
| 422 | - continue; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - $options = true; |
|
| 426 | - |
|
| 427 | - $args = str_replace('-', '', $_SERVER['argv'][$i]); |
|
| 428 | - $value = null; |
|
| 429 | - |
|
| 430 | - if (isset($_SERVER['argv'][$i + 1]) && mb_strpos($_SERVER['argv'][$i + 1], '-') !== 0) { |
|
| 431 | - $value = $_SERVER['argv'][$i + 1]; |
|
| 432 | - $i++; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - static::$options[$args] = $value; |
|
| 436 | - |
|
| 437 | - $options = false; |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Returns the command line string portions of the arguments, minus |
|
| 443 | - * any options, as a string. |
|
| 444 | - * |
|
| 445 | - * @return string |
|
| 446 | - */ |
|
| 447 | - public static function getURI() |
|
| 448 | - { |
|
| 449 | - return implode('/', static::$segments); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Returns an individual segment. |
|
| 454 | - * |
|
| 455 | - * @param int $index |
|
| 456 | - * |
|
| 457 | - * @return mixed|null |
|
| 458 | - */ |
|
| 459 | - public static function getSegment(int $index) |
|
| 460 | - { |
|
| 461 | - if ( ! isset(static::$segments[$index - 1])) { |
|
| 462 | - return null; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - return static::$segments[$index - 1]; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * Returns the raw array of segments found. |
|
| 470 | - * |
|
| 471 | - * @return array |
|
| 472 | - */ |
|
| 473 | - public static function getSegments() |
|
| 474 | - { |
|
| 475 | - return static::$segments; |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Asks the user for input. This can have either 1 or 2 arguments. |
|
| 480 | - * |
|
| 481 | - * Usage: |
|
| 482 | - * |
|
| 483 | - * // Waits for any key press |
|
| 484 | - * Cli::prompt(); |
|
| 485 | - * |
|
| 486 | - * // Takes any input |
|
| 487 | - * $color = Cli::prompt('What is your favorite color?'); |
|
| 488 | - * |
|
| 489 | - * // Takes any input, but offers default |
|
| 490 | - * $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 491 | - * |
|
| 492 | - * // Will only accept the options in the array |
|
| 493 | - * $ready = Cli::prompt('Are you ready?', array('y','n')); |
|
| 494 | - * |
|
| 495 | - * @return string The user input |
|
| 496 | - */ |
|
| 497 | - public static function prompt() |
|
| 498 | - { |
|
| 499 | - $args = func_get_args(); |
|
| 500 | - |
|
| 501 | - $options = []; |
|
| 502 | - $output = ''; |
|
| 503 | - $default = null; |
|
| 504 | - |
|
| 505 | - // How many we got |
|
| 506 | - $arg_count = count($args); |
|
| 507 | - |
|
| 508 | - // Is the last argument a boolean? True means required |
|
| 509 | - $required = end($args) === true; |
|
| 510 | - |
|
| 511 | - // Reduce the argument count if required was passed, we don't care about that anymore |
|
| 512 | - $required === true and --$arg_count; |
|
| 513 | - |
|
| 514 | - // This method can take a few crazy combinations of arguments, so lets work it out |
|
| 515 | - switch ($arg_count) { |
|
| 516 | - case 2: |
|
| 517 | - |
|
| 518 | - // E.g: $ready = Cli::prompt('Are you ready?', ['y','n']); |
|
| 519 | - if (is_array($args[1])) { |
|
| 520 | - list($output, $options) = $args; |
|
| 521 | - } |
|
| 522 | - // E.g: $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 523 | - elseif (is_string($args[1])) { |
|
| 524 | - list($output, $default) = $args; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - break; |
|
| 528 | - |
|
| 529 | - case 1: |
|
| 530 | - |
|
| 531 | - // No question (probably been asked already) so just show options |
|
| 532 | - // E.g: $ready = Cli::prompt(array('y','n')); |
|
| 533 | - if (is_array($args[0])) { |
|
| 534 | - $options = $args[0]; |
|
| 535 | - } |
|
| 536 | - // Question without options |
|
| 537 | - // E.g: $ready = Cli::prompt('What did you do today?'); |
|
| 538 | - elseif (is_string($args[0])) { |
|
| 539 | - $output = $args[0]; |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - break; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - // If a question has been asked with the read |
|
| 546 | - if ($output !== '') { |
|
| 547 | - $extra_output = ''; |
|
| 548 | - |
|
| 549 | - if ($default !== null) { |
|
| 550 | - $extra_output = ' [ Default: "'.$default.'" ]'; |
|
| 551 | - } elseif ($options !== []) { |
|
| 552 | - $extra_output = ' [ '.implode(' | ', $options).' ]'; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - static::fwrite(static::$stdout, $output.$extra_output.': '); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - // Read the input from keyboard. |
|
| 559 | - $input = trim(static::input()) ?: $default; |
|
| 560 | - |
|
| 561 | - // No input provided and we require one (default will stop this being called) |
|
| 562 | - if (empty($input) and $required === true) { |
|
| 563 | - static::write('This is required.'); |
|
| 564 | - static::newLine(); |
|
| 565 | - |
|
| 566 | - $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - // If options are provided and the choice is not in the array, tell them to try again |
|
| 570 | - if ( ! empty($options) and ! in_array($input, $options)) { |
|
| 571 | - static::write('This is not a valid option. Please try again.'); |
|
| 572 | - static::newLine(); |
|
| 573 | - |
|
| 574 | - $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - return $input; |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * Allows you to set a commandline option from code. |
|
| 582 | - * |
|
| 583 | - * @param string|int $name The name of the option (int if unnamed) |
|
| 584 | - * @param mixed|null $value The value to set, or null to delete the option |
|
| 585 | - * |
|
| 586 | - * @return mixed |
|
| 587 | - */ |
|
| 588 | - public static function setOption($name, $value = null) |
|
| 589 | - { |
|
| 590 | - if ($value == null) { |
|
| 591 | - if (isset(static::$options[$name])) { |
|
| 592 | - unset(static::$options[$name]); |
|
| 593 | - } |
|
| 594 | - } else { |
|
| 595 | - static::$options[$name] = $value; |
|
| 596 | - } |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - /** |
|
| 600 | - * Waits a certain number of seconds, optionally showing a wait message and |
|
| 601 | - * waiting for a key press. |
|
| 602 | - * |
|
| 603 | - * @param int $seconds Number of seconds |
|
| 604 | - * @param bool $countdown Show a countdown or not |
|
| 605 | - * |
|
| 606 | - * @return string |
|
| 607 | - */ |
|
| 608 | - public static function wait(int $seconds = 0, bool $countdown = false) |
|
| 609 | - { |
|
| 610 | - if ($countdown === true) { |
|
| 611 | - $time = $seconds; |
|
| 612 | - |
|
| 613 | - while ($time > 0) { |
|
| 614 | - fwrite(static::$stdout, $time.'... '); |
|
| 615 | - sleep(1); |
|
| 616 | - $time--; |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - static::write(); |
|
| 620 | - } else { |
|
| 621 | - if ($seconds = 0) { |
|
| 622 | - sleep($seconds); |
|
| 623 | - } else { |
|
| 624 | - static::write(static::$waitMsg); |
|
| 625 | - static::input(); |
|
| 626 | - } |
|
| 627 | - } |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - /** |
|
| 631 | - * Outputs a string to the cli. If you send an array it will implode them |
|
| 632 | - * with a line break. |
|
| 633 | - * |
|
| 634 | - * @param string|array $text The text to output, or array of lines |
|
| 635 | - * @param string|null $foreground The foreground color |
|
| 636 | - * @param string|null $background The background color |
|
| 637 | - * |
|
| 638 | - * @return string |
|
| 639 | - */ |
|
| 640 | - public static function write(string $text = '', string $foreground = null, string $background = null) |
|
| 641 | - { |
|
| 642 | - if (is_array($text)) { |
|
| 643 | - $text = implode(PHP_EOL, $text); |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - if ($foreground OR $background) { |
|
| 647 | - $text = static::color($text, $foreground, $background); |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - static::fwrite(static::$stdout, $text.PHP_EOL); |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - /** |
|
| 654 | - * Returns a well formatted table. |
|
| 655 | - * |
|
| 656 | - * @param array $tbody List of rows |
|
| 657 | - * @param array $thead List of columns |
|
| 658 | - * |
|
| 659 | - * @return void |
|
| 660 | - */ |
|
| 661 | - public static function table(array $tbody, array $thead = []) |
|
| 662 | - { |
|
| 663 | - $rows = []; |
|
| 664 | - |
|
| 665 | - if ( ! empty($thead)) { |
|
| 666 | - $rows[] = array_values($thead); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - foreach ($tbody as $tr) { |
|
| 670 | - $rows[] = count($rows); |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - $totalRows = count($rows); |
|
| 674 | - |
|
| 675 | - // Store all columns lengths |
|
| 676 | - $allColsLengths = []; |
|
| 677 | - |
|
| 678 | - // Store maximum lengths by column |
|
| 679 | - $maxColsLengths = []; |
|
| 680 | - |
|
| 681 | - for ($row = 0; $row < $totalRows; $row++) { |
|
| 682 | - $column = 0; |
|
| 683 | - |
|
| 684 | - foreach ($rows[$row] as $col) { |
|
| 685 | - $allColsLengths[$row][$column] = static::strlen($col); |
|
| 686 | - |
|
| 687 | - if ( ! isset($maxColsLengths[$column]) || $allColsLengths[$row][$column] > $maxColsLengths[$column]) { |
|
| 688 | - $maxColsLengths[$column] = $allColsLengths[$row][$column]; |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - $column++; |
|
| 692 | - } |
|
| 693 | - } |
|
| 694 | - |
|
| 695 | - for ($row = 0; $row < $totalRows; $row++) { |
|
| 696 | - $column = 0; |
|
| 697 | - |
|
| 698 | - foreach ($rows[$row] as $col) |
|
| 699 | - { |
|
| 700 | - $diverse = $maxColsLengths[$column] - static::strlen($col); |
|
| 265 | + static::fwrite(static::$stderr, $text.PHP_EOL); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Attempts to determine the width of the viewable CLI window. |
|
| 270 | + * |
|
| 271 | + * @param int $default |
|
| 272 | + * |
|
| 273 | + * @return int |
|
| 274 | + */ |
|
| 275 | + public static function getWidth(int $default = 80) |
|
| 276 | + { |
|
| 277 | + if (static::isWindows() || (int) shell_exec('tput cols') === 0) { |
|
| 278 | + return $default; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + return (int) shell_exec('tput cols'); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Attempts to determine the height of the viewable CLI window. |
|
| 286 | + * |
|
| 287 | + * @param int $default |
|
| 288 | + * |
|
| 289 | + * @return int |
|
| 290 | + */ |
|
| 291 | + public static function getHeight(int $default = 32) |
|
| 292 | + { |
|
| 293 | + if (static::isWindows()) { |
|
| 294 | + return $default; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + return (int) shell_exec('tput lines'); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Takes a string and writes it to the command line, wrapping to a maximum width. |
|
| 302 | + * If no maximum width is specified, will wrap to the window's max. |
|
| 303 | + * |
|
| 304 | + * @param string $string |
|
| 305 | + * @param int $max |
|
| 306 | + * @param int $padLeft |
|
| 307 | + * |
|
| 308 | + * @return string |
|
| 309 | + */ |
|
| 310 | + public static function wrap(string $string = null, int $max = 0, int $padLeft = 0) |
|
| 311 | + { |
|
| 312 | + if (empty($string)) { |
|
| 313 | + return ''; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + if ($max === 0) { |
|
| 317 | + $max = static::getWidth(); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + if (static::getWidth() < $max) { |
|
| 321 | + $max = static::getWidth(); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + $max = $max - $padLeft; |
|
| 325 | + |
|
| 326 | + $lines = wordwrap($string, $max); |
|
| 327 | + |
|
| 328 | + if ($pad_left > 0) { |
|
| 329 | + $lines = explode(PHP_EOL, $lines); |
|
| 330 | + |
|
| 331 | + $first = true; |
|
| 332 | + |
|
| 333 | + array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
| 334 | + if ( ! $first) { |
|
| 335 | + $line = str_repeat(' ', $pad_left) . $line; |
|
| 336 | + } else { |
|
| 337 | + $first = false; |
|
| 338 | + } |
|
| 339 | + }); |
|
| 340 | + |
|
| 341 | + $lines = implode(PHP_EOL, $lines); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + return $lines; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Get input from the shell, using readline or the standard STDIN. |
|
| 349 | + * |
|
| 350 | + * @param string|int $prefix The name of the option (int if unnamed) |
|
| 351 | + * |
|
| 352 | + * @return string |
|
| 353 | + */ |
|
| 354 | + public static function input($prefix = '') |
|
| 355 | + { |
|
| 356 | + if (static::$readlineSupport) { |
|
| 357 | + return readline($prefix); |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + echo $prefix; |
|
| 361 | + |
|
| 362 | + return fgets(STDIN); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * If operating system === windows. |
|
| 367 | + * |
|
| 368 | + * @return string |
|
| 369 | + */ |
|
| 370 | + public static function isWindows() |
|
| 371 | + { |
|
| 372 | + return stripos(PHP_OS, 'WIN') === 0; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Enter a number of empty lines. |
|
| 377 | + * |
|
| 378 | + * @param int $num Number of lines to output |
|
| 379 | + * |
|
| 380 | + * @return void |
|
| 381 | + */ |
|
| 382 | + public static function newLine(int $num = 1) |
|
| 383 | + { |
|
| 384 | + for ($i = 0; $i < $num; $i++) { |
|
| 385 | + static::write(); |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Returns the option with the given name. You can also give the option number. |
|
| 391 | + * |
|
| 392 | + * @param string|int $name The name of the option (int if unnamed) |
|
| 393 | + * @param mixed $default The value to return if the option is not defined |
|
| 394 | + * |
|
| 395 | + * @return mixed |
|
| 396 | + * |
|
| 397 | + * @uses \Syscodes\Contract\Core\Lenevor |
|
| 398 | + */ |
|
| 399 | + public static function option($name, $default = null) |
|
| 400 | + { |
|
| 401 | + if ( ! isset(static::$options[$name])) { |
|
| 402 | + return Lenevor::value($default); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + return static::$options[$name]; |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Parses the command line it was called from and collects all |
|
| 410 | + * options and valid segments. |
|
| 411 | + * |
|
| 412 | + * @return bool |
|
| 413 | + */ |
|
| 414 | + protected static function parseCommandLine() |
|
| 415 | + { |
|
| 416 | + $options = false; |
|
| 417 | + |
|
| 418 | + for ($i = 1; $i < $_SERVER['argc']; $i++) { |
|
| 419 | + if ( ! $options && mb_strpos($_SERVER['argv'][$i], '-') === false) { |
|
| 420 | + static::$segments[] = $_SERVER['argv'][$i]; |
|
| 421 | + |
|
| 422 | + continue; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + $options = true; |
|
| 426 | + |
|
| 427 | + $args = str_replace('-', '', $_SERVER['argv'][$i]); |
|
| 428 | + $value = null; |
|
| 429 | + |
|
| 430 | + if (isset($_SERVER['argv'][$i + 1]) && mb_strpos($_SERVER['argv'][$i + 1], '-') !== 0) { |
|
| 431 | + $value = $_SERVER['argv'][$i + 1]; |
|
| 432 | + $i++; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + static::$options[$args] = $value; |
|
| 436 | + |
|
| 437 | + $options = false; |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Returns the command line string portions of the arguments, minus |
|
| 443 | + * any options, as a string. |
|
| 444 | + * |
|
| 445 | + * @return string |
|
| 446 | + */ |
|
| 447 | + public static function getURI() |
|
| 448 | + { |
|
| 449 | + return implode('/', static::$segments); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Returns an individual segment. |
|
| 454 | + * |
|
| 455 | + * @param int $index |
|
| 456 | + * |
|
| 457 | + * @return mixed|null |
|
| 458 | + */ |
|
| 459 | + public static function getSegment(int $index) |
|
| 460 | + { |
|
| 461 | + if ( ! isset(static::$segments[$index - 1])) { |
|
| 462 | + return null; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + return static::$segments[$index - 1]; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * Returns the raw array of segments found. |
|
| 470 | + * |
|
| 471 | + * @return array |
|
| 472 | + */ |
|
| 473 | + public static function getSegments() |
|
| 474 | + { |
|
| 475 | + return static::$segments; |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Asks the user for input. This can have either 1 or 2 arguments. |
|
| 480 | + * |
|
| 481 | + * Usage: |
|
| 482 | + * |
|
| 483 | + * // Waits for any key press |
|
| 484 | + * Cli::prompt(); |
|
| 485 | + * |
|
| 486 | + * // Takes any input |
|
| 487 | + * $color = Cli::prompt('What is your favorite color?'); |
|
| 488 | + * |
|
| 489 | + * // Takes any input, but offers default |
|
| 490 | + * $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 491 | + * |
|
| 492 | + * // Will only accept the options in the array |
|
| 493 | + * $ready = Cli::prompt('Are you ready?', array('y','n')); |
|
| 494 | + * |
|
| 495 | + * @return string The user input |
|
| 496 | + */ |
|
| 497 | + public static function prompt() |
|
| 498 | + { |
|
| 499 | + $args = func_get_args(); |
|
| 500 | + |
|
| 501 | + $options = []; |
|
| 502 | + $output = ''; |
|
| 503 | + $default = null; |
|
| 504 | + |
|
| 505 | + // How many we got |
|
| 506 | + $arg_count = count($args); |
|
| 507 | + |
|
| 508 | + // Is the last argument a boolean? True means required |
|
| 509 | + $required = end($args) === true; |
|
| 510 | + |
|
| 511 | + // Reduce the argument count if required was passed, we don't care about that anymore |
|
| 512 | + $required === true and --$arg_count; |
|
| 513 | + |
|
| 514 | + // This method can take a few crazy combinations of arguments, so lets work it out |
|
| 515 | + switch ($arg_count) { |
|
| 516 | + case 2: |
|
| 517 | + |
|
| 518 | + // E.g: $ready = Cli::prompt('Are you ready?', ['y','n']); |
|
| 519 | + if (is_array($args[1])) { |
|
| 520 | + list($output, $options) = $args; |
|
| 521 | + } |
|
| 522 | + // E.g: $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 523 | + elseif (is_string($args[1])) { |
|
| 524 | + list($output, $default) = $args; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + break; |
|
| 528 | + |
|
| 529 | + case 1: |
|
| 530 | + |
|
| 531 | + // No question (probably been asked already) so just show options |
|
| 532 | + // E.g: $ready = Cli::prompt(array('y','n')); |
|
| 533 | + if (is_array($args[0])) { |
|
| 534 | + $options = $args[0]; |
|
| 535 | + } |
|
| 536 | + // Question without options |
|
| 537 | + // E.g: $ready = Cli::prompt('What did you do today?'); |
|
| 538 | + elseif (is_string($args[0])) { |
|
| 539 | + $output = $args[0]; |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + break; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + // If a question has been asked with the read |
|
| 546 | + if ($output !== '') { |
|
| 547 | + $extra_output = ''; |
|
| 548 | + |
|
| 549 | + if ($default !== null) { |
|
| 550 | + $extra_output = ' [ Default: "'.$default.'" ]'; |
|
| 551 | + } elseif ($options !== []) { |
|
| 552 | + $extra_output = ' [ '.implode(' | ', $options).' ]'; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + static::fwrite(static::$stdout, $output.$extra_output.': '); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + // Read the input from keyboard. |
|
| 559 | + $input = trim(static::input()) ?: $default; |
|
| 560 | + |
|
| 561 | + // No input provided and we require one (default will stop this being called) |
|
| 562 | + if (empty($input) and $required === true) { |
|
| 563 | + static::write('This is required.'); |
|
| 564 | + static::newLine(); |
|
| 565 | + |
|
| 566 | + $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + // If options are provided and the choice is not in the array, tell them to try again |
|
| 570 | + if ( ! empty($options) and ! in_array($input, $options)) { |
|
| 571 | + static::write('This is not a valid option. Please try again.'); |
|
| 572 | + static::newLine(); |
|
| 573 | + |
|
| 574 | + $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + return $input; |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Allows you to set a commandline option from code. |
|
| 582 | + * |
|
| 583 | + * @param string|int $name The name of the option (int if unnamed) |
|
| 584 | + * @param mixed|null $value The value to set, or null to delete the option |
|
| 585 | + * |
|
| 586 | + * @return mixed |
|
| 587 | + */ |
|
| 588 | + public static function setOption($name, $value = null) |
|
| 589 | + { |
|
| 590 | + if ($value == null) { |
|
| 591 | + if (isset(static::$options[$name])) { |
|
| 592 | + unset(static::$options[$name]); |
|
| 593 | + } |
|
| 594 | + } else { |
|
| 595 | + static::$options[$name] = $value; |
|
| 596 | + } |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + /** |
|
| 600 | + * Waits a certain number of seconds, optionally showing a wait message and |
|
| 601 | + * waiting for a key press. |
|
| 602 | + * |
|
| 603 | + * @param int $seconds Number of seconds |
|
| 604 | + * @param bool $countdown Show a countdown or not |
|
| 605 | + * |
|
| 606 | + * @return string |
|
| 607 | + */ |
|
| 608 | + public static function wait(int $seconds = 0, bool $countdown = false) |
|
| 609 | + { |
|
| 610 | + if ($countdown === true) { |
|
| 611 | + $time = $seconds; |
|
| 612 | + |
|
| 613 | + while ($time > 0) { |
|
| 614 | + fwrite(static::$stdout, $time.'... '); |
|
| 615 | + sleep(1); |
|
| 616 | + $time--; |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + static::write(); |
|
| 620 | + } else { |
|
| 621 | + if ($seconds = 0) { |
|
| 622 | + sleep($seconds); |
|
| 623 | + } else { |
|
| 624 | + static::write(static::$waitMsg); |
|
| 625 | + static::input(); |
|
| 626 | + } |
|
| 627 | + } |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + /** |
|
| 631 | + * Outputs a string to the cli. If you send an array it will implode them |
|
| 632 | + * with a line break. |
|
| 633 | + * |
|
| 634 | + * @param string|array $text The text to output, or array of lines |
|
| 635 | + * @param string|null $foreground The foreground color |
|
| 636 | + * @param string|null $background The background color |
|
| 637 | + * |
|
| 638 | + * @return string |
|
| 639 | + */ |
|
| 640 | + public static function write(string $text = '', string $foreground = null, string $background = null) |
|
| 641 | + { |
|
| 642 | + if (is_array($text)) { |
|
| 643 | + $text = implode(PHP_EOL, $text); |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + if ($foreground OR $background) { |
|
| 647 | + $text = static::color($text, $foreground, $background); |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + static::fwrite(static::$stdout, $text.PHP_EOL); |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + /** |
|
| 654 | + * Returns a well formatted table. |
|
| 655 | + * |
|
| 656 | + * @param array $tbody List of rows |
|
| 657 | + * @param array $thead List of columns |
|
| 658 | + * |
|
| 659 | + * @return void |
|
| 660 | + */ |
|
| 661 | + public static function table(array $tbody, array $thead = []) |
|
| 662 | + { |
|
| 663 | + $rows = []; |
|
| 664 | + |
|
| 665 | + if ( ! empty($thead)) { |
|
| 666 | + $rows[] = array_values($thead); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + foreach ($tbody as $tr) { |
|
| 670 | + $rows[] = count($rows); |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + $totalRows = count($rows); |
|
| 674 | + |
|
| 675 | + // Store all columns lengths |
|
| 676 | + $allColsLengths = []; |
|
| 677 | + |
|
| 678 | + // Store maximum lengths by column |
|
| 679 | + $maxColsLengths = []; |
|
| 680 | + |
|
| 681 | + for ($row = 0; $row < $totalRows; $row++) { |
|
| 682 | + $column = 0; |
|
| 683 | + |
|
| 684 | + foreach ($rows[$row] as $col) { |
|
| 685 | + $allColsLengths[$row][$column] = static::strlen($col); |
|
| 686 | + |
|
| 687 | + if ( ! isset($maxColsLengths[$column]) || $allColsLengths[$row][$column] > $maxColsLengths[$column]) { |
|
| 688 | + $maxColsLengths[$column] = $allColsLengths[$row][$column]; |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + $column++; |
|
| 692 | + } |
|
| 693 | + } |
|
| 694 | + |
|
| 695 | + for ($row = 0; $row < $totalRows; $row++) { |
|
| 696 | + $column = 0; |
|
| 697 | + |
|
| 698 | + foreach ($rows[$row] as $col) |
|
| 699 | + { |
|
| 700 | + $diverse = $maxColsLengths[$column] - static::strlen($col); |
|
| 701 | 701 | |
| 702 | - if ($diverse) { |
|
| 703 | - $rows[$row][$column] = $rows[$row][$column].str_repeat(' ', $diverse); |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - $column++; |
|
| 707 | - } |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - $table = ''; |
|
| 711 | - $cols = ''; |
|
| 712 | - |
|
| 713 | - for ($row = 0; $row < $rows; $row++) { |
|
| 714 | - if (0 === $row) { |
|
| 715 | - $cols = '+'; |
|
| 716 | - |
|
| 717 | - foreach ($rows[$row] as $col) { |
|
| 718 | - $cols .= str_repeat('-', static::strlen($col) + 2).'+'; |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - $table .= $cols.PHP_EOL; |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - $table .= '| '.implode('-', $rows[$row]).' |'.PHP_EOL; |
|
| 725 | - |
|
| 726 | - if (0 === $row && ( ! empty($thead)) || ($row + 1) === $rows) { |
|
| 727 | - $table .= $cols.PHP_EOL; |
|
| 728 | - } |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - static::write($table); |
|
| 732 | - } |
|
| 733 | - |
|
| 734 | - /** |
|
| 735 | - * The library is intended for used on Cli commanda, |
|
| 736 | - * this commands can be called from controllers and |
|
| 737 | - * elsewhere of framework. |
|
| 738 | - * |
|
| 739 | - * @param resource $handle |
|
| 740 | - * @param string $text |
|
| 741 | - * |
|
| 742 | - * @return string |
|
| 743 | - */ |
|
| 744 | - protected static function fwrite($handle, string $text) |
|
| 745 | - { |
|
| 746 | - if (isCli()) { |
|
| 747 | - fwrite($handle, $text); |
|
| 748 | - return; |
|
| 749 | - } |
|
| 750 | - |
|
| 751 | - echo $text; |
|
| 752 | - } |
|
| 702 | + if ($diverse) { |
|
| 703 | + $rows[$row][$column] = $rows[$row][$column].str_repeat(' ', $diverse); |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + $column++; |
|
| 707 | + } |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + $table = ''; |
|
| 711 | + $cols = ''; |
|
| 712 | + |
|
| 713 | + for ($row = 0; $row < $rows; $row++) { |
|
| 714 | + if (0 === $row) { |
|
| 715 | + $cols = '+'; |
|
| 716 | + |
|
| 717 | + foreach ($rows[$row] as $col) { |
|
| 718 | + $cols .= str_repeat('-', static::strlen($col) + 2).'+'; |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + $table .= $cols.PHP_EOL; |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + $table .= '| '.implode('-', $rows[$row]).' |'.PHP_EOL; |
|
| 725 | + |
|
| 726 | + if (0 === $row && ( ! empty($thead)) || ($row + 1) === $rows) { |
|
| 727 | + $table .= $cols.PHP_EOL; |
|
| 728 | + } |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + static::write($table); |
|
| 732 | + } |
|
| 733 | + |
|
| 734 | + /** |
|
| 735 | + * The library is intended for used on Cli commanda, |
|
| 736 | + * this commands can be called from controllers and |
|
| 737 | + * elsewhere of framework. |
|
| 738 | + * |
|
| 739 | + * @param resource $handle |
|
| 740 | + * @param string $text |
|
| 741 | + * |
|
| 742 | + * @return string |
|
| 743 | + */ |
|
| 744 | + protected static function fwrite($handle, string $text) |
|
| 745 | + { |
|
| 746 | + if (isCli()) { |
|
| 747 | + fwrite($handle, $text); |
|
| 748 | + return; |
|
| 749 | + } |
|
| 750 | + |
|
| 751 | + echo $text; |
|
| 752 | + } |
|
| 753 | 753 | } |
| 754 | 754 | \ No newline at end of file |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
| 201 | + if ($background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
| 202 | 202 | throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
| 203 | 203 | } |
| 204 | 204 | |
@@ -330,9 +330,9 @@ discard block |
||
| 330 | 330 | |
| 331 | 331 | $first = true; |
| 332 | 332 | |
| 333 | - array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
| 333 | + array_walk($lines, function(&$line, $index) use ($pad_left, &$first) { |
|
| 334 | 334 | if ( ! $first) { |
| 335 | - $line = str_repeat(' ', $pad_left) . $line; |
|
| 335 | + $line = str_repeat(' ', $pad_left).$line; |
|
| 336 | 336 | } else { |
| 337 | 337 | $first = false; |
| 338 | 338 | } |
@@ -708,7 +708,7 @@ discard block |
||
| 708 | 708 | } |
| 709 | 709 | |
| 710 | 710 | $table = ''; |
| 711 | - $cols = ''; |
|
| 711 | + $cols = ''; |
|
| 712 | 712 | |
| 713 | 713 | for ($row = 0; $row < $rows; $row++) { |
| 714 | 714 | if (0 === $row) { |
@@ -34,58 +34,58 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class Application implements ApplicationContracts |
| 36 | 36 | { |
| 37 | - /** |
|
| 38 | - * Get the Lenevor class instance. |
|
| 39 | - * |
|
| 40 | - * @var \Syscodes\Contracts\Core\Lenevor $core |
|
| 41 | - */ |
|
| 42 | - protected $core; |
|
| 37 | + /** |
|
| 38 | + * Get the Lenevor class instance. |
|
| 39 | + * |
|
| 40 | + * @var \Syscodes\Contracts\Core\Lenevor $core |
|
| 41 | + */ |
|
| 42 | + protected $core; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Console constructor. Initialize the console of Lenevor. |
|
| 46 | - * |
|
| 47 | - * @param \Syscodes\Contracts\Core\Lenevor $core |
|
| 48 | - * |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function __construct(Lenevor $core) |
|
| 52 | - { |
|
| 53 | - // Initialize the Cli |
|
| 54 | - if (isCli()) { |
|
| 55 | - Cli::initialize(); |
|
| 56 | - } |
|
| 44 | + /** |
|
| 45 | + * Console constructor. Initialize the console of Lenevor. |
|
| 46 | + * |
|
| 47 | + * @param \Syscodes\Contracts\Core\Lenevor $core |
|
| 48 | + * |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function __construct(Lenevor $core) |
|
| 52 | + { |
|
| 53 | + // Initialize the Cli |
|
| 54 | + if (isCli()) { |
|
| 55 | + Cli::initialize(); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - $this->core = $core; |
|
| 59 | - } |
|
| 58 | + $this->core = $core; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Runs the current command discovered on the CLI. |
|
| 63 | - * |
|
| 64 | - * @return void |
|
| 65 | - */ |
|
| 66 | - public function run() |
|
| 67 | - { |
|
| 68 | - Cli::getURI(); |
|
| 61 | + /** |
|
| 62 | + * Runs the current command discovered on the CLI. |
|
| 63 | + * |
|
| 64 | + * @return void |
|
| 65 | + */ |
|
| 66 | + public function run() |
|
| 67 | + { |
|
| 68 | + Cli::getURI(); |
|
| 69 | 69 | |
| 70 | 70 | |
| 71 | - } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Displays basic information about the Console. |
|
| 75 | - * |
|
| 76 | - * @return void |
|
| 77 | - * |
|
| 78 | - * @uses Version::PRODUCT |
|
| 79 | - * @uses Version::RELEASE |
|
| 80 | - */ |
|
| 81 | - public function showHeader() |
|
| 82 | - { |
|
| 83 | - Cli::write(Version::PRODUCT.' ' |
|
| 84 | - .Cli::color(Version::RELEASE, 'cyan').' | ' |
|
| 85 | - .'Server Time: '.Cli::color(date('Y/m/d H:i:sa'), 'light_yellow').' | ' |
|
| 86 | - .cli::color('['.PHP_OS.']', 'light_purple') |
|
| 87 | - ); |
|
| 73 | + /** |
|
| 74 | + * Displays basic information about the Console. |
|
| 75 | + * |
|
| 76 | + * @return void |
|
| 77 | + * |
|
| 78 | + * @uses Version::PRODUCT |
|
| 79 | + * @uses Version::RELEASE |
|
| 80 | + */ |
|
| 81 | + public function showHeader() |
|
| 82 | + { |
|
| 83 | + Cli::write(Version::PRODUCT.' ' |
|
| 84 | + .Cli::color(Version::RELEASE, 'cyan').' | ' |
|
| 85 | + .'Server Time: '.Cli::color(date('Y/m/d H:i:sa'), 'light_yellow').' | ' |
|
| 86 | + .cli::color('['.PHP_OS.']', 'light_purple') |
|
| 87 | + ); |
|
| 88 | 88 | |
| 89 | - Cli::newLine(1); |
|
| 90 | - } |
|
| 89 | + Cli::newLine(1); |
|
| 90 | + } |
|
| 91 | 91 | } |
| 92 | 92 | \ No newline at end of file |
@@ -53,7 +53,7 @@ |
||
| 53 | 53 | */ |
| 54 | 54 | public function register() |
| 55 | 55 | { |
| 56 | - $this->booting(function () { |
|
| 56 | + $this->booting(function() { |
|
| 57 | 57 | $events = $this->listens(); |
| 58 | 58 | |
| 59 | 59 | foreach ((array) $events as $event => $listeners) { |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | return $this->createClassListener($listener, $wildcard); |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - return function ($event, $payload) use ($listener, $wildcard) { |
|
| 136 | + return function($event, $payload) use ($listener, $wildcard) { |
|
| 137 | 137 | if ($wildcard) { |
| 138 | 138 | return $listener($event, $payload); |
| 139 | 139 | } |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | public function createClassListener($listener, $wildcard = false) |
| 154 | 154 | { |
| 155 | - return function ($event, $payload) use ($listener, $wildcard) { |
|
| 155 | + return function($event, $payload) use ($listener, $wildcard) { |
|
| 156 | 156 | if ($wildcard) { |
| 157 | 157 | return call_user_func($this->createClassClosure($listener), $event, $payload); |
| 158 | 158 | } |