prooph / http-middleware

PSR compatible middleware to integrate prooph with a middleware dispatcher
http://getprooph.org/
BSD 3-Clause "New" or "Revised" License
11 stars 4 forks source link

FastRoute workaound #18

Closed bweston92 closed 6 years ago

bweston92 commented 6 years ago

FastRoute doesn't add the options to the request attributes. Below is a workaround middleware.

<?php

namespace App\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Expressive\Router\Route;

final class RouterOptionValuesToRequestAttributes implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $attributes = $request->getAttributes();

        if (array_key_exists('Zend\Expressive\Router\RouteResult', $attributes)) {
            /** @var \Zend\Expressive\Router\RouteResult $result */
            $result = $attributes['Zend\Expressive\Router\RouteResult'];
            $route = $result->getMatchedRoute();
            if ($route instanceof Route) {
                $options = $route->getOptions();
                if (array_key_exists('values', $options)) {
                    $values = $options['values'];
                    $request = array_reduce(array_keys($values), function(ServerRequestInterface $request, $key) use ($values) {
                        return $request->withAttribute($key, $values[$key]);
                    }, $request);
                }
            }
        }
        return $handler->handle($request);
    }
}
prolic commented 6 years ago

Why closed immediately?

On Mon, Sep 10, 2018, 16:57 Bradley Weston notifications@github.com wrote:

Closed #18 https://github.com/prooph/http-middleware/issues/18.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/prooph/http-middleware/issues/18#event-1835422468, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYEvHOfq4MSeHQ4Pe60FBrYJeHSVj32ks5uZil-gaJpZM4WODLT .

bweston92 commented 6 years ago

It is just there for is someone searches "FastRoute" in the issues.