Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
40.00% |
2 / 5 |
CRAP | |
50.00% |
6 / 12 |
| Command | |
0.00% |
0 / 1 |
|
40.00% |
2 / 5 |
13.12 | |
50.00% |
6 / 12 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| getArgs | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
| middleware | |
100.00% |
1 / 1 |
2 | |
100.00% |
2 / 2 |
|||
| execute | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| core | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 2 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\CommandsLaravel\Console\Commands; |
| 4 | |
| 5 | use Closure; |
| 6 | use Illuminate\Console\Command as IlluminateCommand; |
| 7 | use Qmp\Laravel\CommandsLaravel\Middleware\Core\Interfaces\IOConnectorInterface; |
| 8 | use Qmp\Laravel\CommandsLaravel\Middleware\Core\MiddlewareStack; |
| 9 | use Qmp\Laravel\CommandsLaravel\Middleware\Core\Traits\MiddlewareDataAccess; |
| 10 | use Qmp\Laravel\CommandsLaravel\Middleware\Core\Traits\ReportException; |
| 11 | use Symfony\Component\Console\Input\InputInterface; |
| 12 | use Symfony\Component\Console\Output\OutputInterface; |
| 13 | |
| 14 | /** |
| 15 | * Custom command with middleware. |
| 16 | * |
| 17 | * @author David Quartino <dquartino@advertise-me.net> |
| 18 | */ |
| 19 | abstract class Command extends IlluminateCommand implements IOConnectorInterface |
| 20 | { |
| 21 | |
| 22 | use MiddlewareDataAccess, ReportException; |
| 23 | /** |
| 24 | * The current middlewares |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected $middleware = []; |
| 29 | |
| 30 | /** |
| 31 | * The middleware stack |
| 32 | * |
| 33 | * @var MiddlewareStack; |
| 34 | */ |
| 35 | protected $middlewareStack; |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Class constructor |
| 40 | */ |
| 41 | public function __construct() |
| 42 | { |
| 43 | parent::__construct(); |
| 44 | $this->initMiddlewareData(); |
| 45 | $this->middlewareStack = new MiddlewareStack($this, $this->middlewareData); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Undocumented function |
| 50 | * |
| 51 | * @return array |
| 52 | */ |
| 53 | public function getArgs(): array { |
| 54 | return array_merge( |
| 55 | $this->arguments(), |
| 56 | array_diff_key($this->options(), $this->getDefinition()->getOptions()) |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Set middlewares |
| 62 | * |
| 63 | * @param array|string $middlewares |
| 64 | * @return void |
| 65 | */ |
| 66 | protected function middleware($middleware): void |
| 67 | { |
| 68 | $this->middleware = is_string($middleware) ? [$middleware] : $middleware; |
| 69 | } |
| 70 | /** |
| 71 | * Execute the console command. |
| 72 | * |
| 73 | * @param InputInterface $input |
| 74 | * @param OutputInterface $output |
| 75 | * @return int |
| 76 | */ |
| 77 | protected function execute(InputInterface $input, OutputInterface $output): int |
| 78 | { |
| 79 | return (int) $this->middlewareStack->addMiddleware($this->middleware)->process(Closure::fromCallable([$this, 'core'])); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Return core command function call |
| 84 | * |
| 85 | * @return int |
| 86 | */ |
| 87 | protected function core(): int |
| 88 | { |
| 89 | $method = method_exists($this, 'handle') ? 'handle' : '__invoke'; |
| 90 | return (int) $this->laravel->call([$this, $method]); |
| 91 | } |
| 92 | } |