mezzio / mezzio-fastroute

FastRoute integration for Mezzio
https://docs.mezzio.dev/mezzio/features/router/fast-route/
BSD 3-Clause "New" or "Revised" License
16 stars 11 forks source link

Fix uri generation for custom parser #1

Open weierophinney opened 4 years ago

weierophinney commented 4 years ago

Added Unit test to show difference in output between a user defined parser inside of the RouteCollector and the one generated by $router->generateUri


Originally posted by @TaylorSasser at https://github.com/zendframework/zend-expressive-fastroute/pull/63

weierophinney commented 4 years ago

The problem is this line:

https://github.com/zendframework/zend-expressive-fastroute/blob/abaa993a899b91d4fd717829029fe0a2676402ae/src/FastRouteRouter.php#L261

A new RouteParser is created and the existing one is not used. You can't access the routeparser because there is no getter for it on the RouteCollector. Without BC breaks reflection or a PR to FastRoute is needed.

        $refRouter = new \ReflectionClass(get_class($this->router));
        $refParser = $refRouter->getProperty('routeParser');
        $refParser->setAccessible(true);
        $parser = $refParser->getValue($this->router);
        $routes            = array_reverse($parser->parse($route->getPath()));
        $missingParameters = [];

It would be better to hide that code into a method and cache it so it is needed only once. Another option is to change the constructor, but that would mean a BC break and rewriting all tests:

    public function __construct(
        RouteParser $routeParser = null,
        DataGenerator $dataGenerator = null,
        callable $dispatcherFactory = null,
        array $config = null
    ) {
        if (null === $routeParser) {
            $routeParser = new RouteParser();
        }

        if (null === $dataGenerator) {
            $dataGenerator = new RouteGenerator();
        }

        $this->router             = new RouteCollector($routeParser, $dataGenerator);
        $this->routeParser        = $routeParser;
        $this->dataGenerator      = $dataGenerator;
        $this->dispatcherCallback = $dispatcherFactory;

        $this->loadConfig($config);
    }

Originally posted by @geerteltink at https://github.com/zendframework/zend-expressive-fastroute/pull/63#issuecomment-504010798