diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 94e9a36..cbdafc3 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -35,7 +35,7 @@ jobs: echo "::set-output name=dir::$(composer config cache-files-dir)" - name: composer-cache - uses: actions/cache@v4.0.2 + uses: actions/cache@v5.0.1 with: path: ${{ steps.composercache.outputs.dir }} key: composer-${{ hashFiles('**/composer.json') }}-${{ matrix.install-args }} @@ -53,7 +53,7 @@ jobs: vendor/bin/simple-phpunit --no-coverage - name: phpstan-cache - uses: actions/cache@v4.0.2 + uses: actions/cache@v5.0.1 with: key: phpstan-${{ matrix.php-version }}-${{ matrix.install-args }}-${{ github.ref }}-${{ github.sha }} path: .phpstan-cache diff --git a/Controller/GraphQL/InvalidUserPasswordException.php b/Controller/GraphQL/InvalidUserPasswordException.php index 48bdbbc..bee2b57 100644 --- a/Controller/GraphQL/InvalidUserPasswordException.php +++ b/Controller/GraphQL/InvalidUserPasswordException.php @@ -8,7 +8,7 @@ class InvalidUserPasswordException extends GraphQLException { - public static function create(Exception $previous = null): self + public static function create(?Exception $previous = null): self { return new self('The provided user / password is incorrect.', 401, $previous, 'Security'); } diff --git a/Controller/GraphQLiteController.php b/Controller/GraphQLiteController.php index b359eef..d4dd037 100644 --- a/Controller/GraphQLiteController.php +++ b/Controller/GraphQLiteController.php @@ -8,16 +8,16 @@ use Laminas\Diactoros\ServerRequestFactory; use Laminas\Diactoros\StreamFactory; use Laminas\Diactoros\UploadedFileFactory; +use LogicException; use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; +use TheCodingMachine\GraphQLite\Bundle\UploadMiddlewareUtils\DummyResponseWithRequest; +use TheCodingMachine\GraphQLite\Bundle\UploadMiddlewareUtils\RequestExtractorMiddleware; use TheCodingMachine\GraphQLite\Http\HttpCodeDecider; use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface; -use function array_map; use GraphQL\Executor\ExecutionResult; use GraphQL\Server\ServerConfig; use GraphQL\Server\StandardServer; use GraphQL\Upload\UploadMiddleware; -use function class_exists; -use function json_decode; use Psr\Http\Message\ServerRequestInterface; use RuntimeException; use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface; @@ -28,25 +28,20 @@ use Symfony\Component\Routing\RouteCollection; use TheCodingMachine\GraphQLite\Bundle\Context\SymfonyGraphQLContext; +use function array_map; +use function class_exists; +use function get_class; +use function json_decode; + /** - * Listens to every single request and forward Graphql requests to Graphql Webonix standardServer. + * Listens to every single request and forwards GraphQL requests to Webonyx's {@see \GraphQL\Server\StandardServer}. */ class GraphQLiteController { - /** - * @var HttpMessageFactoryInterface - */ - private $httpMessageFactory; - /** @var int */ - private $debug; - /** - * @var ServerConfig - */ - private $serverConfig; - /** - * @var HttpCodeDeciderInterface - */ - private $httpCodeDecider; + private HttpMessageFactoryInterface $httpMessageFactory; + private int $debug; + private ServerConfig $serverConfig; + private HttpCodeDeciderInterface $httpCodeDecider; public function __construct(ServerConfig $serverConfig, ?HttpMessageFactoryInterface $httpMessageFactory = null, ?int $debug = null, ?HttpCodeDeciderInterface $httpCodeDecider = null) { @@ -90,10 +85,14 @@ public function handleRequest(Request $request): Response $psr7Request = $psr7Request->withParsedBody($parsedBody); } - // Let's parse the request and adapt it for file uploads. + // Let's parse the request and adapt it for file uploads by extracting it from the middleware. if (class_exists(UploadMiddleware::class)) { $uploadMiddleware = new UploadMiddleware(); - $psr7Request = $uploadMiddleware->processRequest($psr7Request); + $dummyResponseWithRequest = $uploadMiddleware->process($psr7Request, new RequestExtractorMiddleware()); + if (! $dummyResponseWithRequest instanceof DummyResponseWithRequest) { + throw new LogicException(DummyResponseWithRequest::class . ' expect, got ' . get_class($dummyResponseWithRequest)); + } + $psr7Request = $dummyResponseWithRequest->getRequest(); } return $this->handlePsr7Request($psr7Request, $request); diff --git a/UploadMiddlewareUtils/DummyResponseWithRequest.php b/UploadMiddlewareUtils/DummyResponseWithRequest.php new file mode 100644 index 0000000..a5a389b --- /dev/null +++ b/UploadMiddlewareUtils/DummyResponseWithRequest.php @@ -0,0 +1,27 @@ +request = $request; + } + + public function getRequest(): ServerRequestInterface + { + return $this->request; + } +} diff --git a/UploadMiddlewareUtils/RequestExtractorMiddleware.php b/UploadMiddlewareUtils/RequestExtractorMiddleware.php new file mode 100644 index 0000000..ce17ba5 --- /dev/null +++ b/UploadMiddlewareUtils/RequestExtractorMiddleware.php @@ -0,0 +1,21 @@ +