diff --git a/src/devtool/src/ConfigProvider.php b/src/devtool/src/ConfigProvider.php index b920b4f8..574a6a3b 100644 --- a/src/devtool/src/ConfigProvider.php +++ b/src/devtool/src/ConfigProvider.php @@ -16,6 +16,7 @@ use Hypervel\Devtool\Generator\JobCommand; use Hypervel\Devtool\Generator\ListenerCommand; use Hypervel\Devtool\Generator\MailCommand; +use Hypervel\Devtool\Generator\MiddlewareCommand; use Hypervel\Devtool\Generator\ModelCommand; use Hypervel\Devtool\Generator\NotificationCommand; use Hypervel\Devtool\Generator\NotificationTableCommand; @@ -64,6 +65,7 @@ public function __invoke(): array NotificationCommand::class, MailCommand::class, PolicyCommand::class, + MiddlewareCommand::class, ], ]; } diff --git a/src/devtool/src/Generator/MiddlewareCommand.php b/src/devtool/src/Generator/MiddlewareCommand.php new file mode 100644 index 00000000..bc2f1ef1 --- /dev/null +++ b/src/devtool/src/Generator/MiddlewareCommand.php @@ -0,0 +1,44 @@ +<?php + +declare(strict_types=1); + +namespace Hypervel\Devtool\Generator; + +use Hyperf\Devtool\Generator\GeneratorCommand; +use Symfony\Component\Console\Input\InputOption; + +class MiddlewareCommand extends GeneratorCommand +{ + public function __construct() + { + parent::__construct('make:middleware'); + } + + public function configure() + { + $this->setDescription('Create a new HTTP middleware class'); + + parent::configure(); + } + + protected function getStub(): string + { + return $this->getConfig()['stub'] ?? __DIR__ . ( + $this->input->getOption('psr15') + ? '/stubs/middleware.psr15.stub' + : '/stubs/middleware.stub' + ); + } + + protected function getDefaultNamespace(): string + { + return $this->getConfig()['namespace'] ?? 'App\Http\Middleware'; + } + + protected function getOptions(): array + { + return array_merge(parent::getOptions(), [ + ['psr15', null, InputOption::VALUE_NONE, 'Create a PSR-15 compatible middleware'], + ]); + } +} diff --git a/src/devtool/src/Generator/stubs/Middleware.stub b/src/devtool/src/Generator/stubs/Middleware.stub new file mode 100644 index 00000000..fbeb8ab1 --- /dev/null +++ b/src/devtool/src/Generator/stubs/Middleware.stub @@ -0,0 +1,21 @@ +<?php + +declare(strict_types=1); + +namespace %NAMESPACE%; + +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; + +class %CLASS% implements MiddlewareInterface +{ + /** + * Process an incoming server request. + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + return $handler->handle($request); + } +} \ No newline at end of file diff --git a/src/devtool/src/Generator/stubs/middleware.psr15.stub b/src/devtool/src/Generator/stubs/middleware.psr15.stub new file mode 100644 index 00000000..fbeb8ab1 --- /dev/null +++ b/src/devtool/src/Generator/stubs/middleware.psr15.stub @@ -0,0 +1,21 @@ +<?php + +declare(strict_types=1); + +namespace %NAMESPACE%; + +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; + +class %CLASS% implements MiddlewareInterface +{ + /** + * Process an incoming server request. + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + return $handler->handle($request); + } +} \ No newline at end of file diff --git a/src/devtool/src/Generator/stubs/middleware.stub b/src/devtool/src/Generator/stubs/middleware.stub new file mode 100644 index 00000000..0c5cc750 --- /dev/null +++ b/src/devtool/src/Generator/stubs/middleware.stub @@ -0,0 +1,18 @@ +<?php + +namespace %NAMESPACE%; + +use Closure; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Message\ResponseInterface; + +class %CLASS% +{ + /** + * Process an incoming server request. + */ + public function handle(ServerRequestInterface $request, Closure $next): ResponseInterface + { + return $next($request); + } +}