zend-expressive-fastroute icon indicating copy to clipboard operation
zend-expressive-fastroute copied to clipboard

Fix uri generation for custom parser

Open TaylorSasser opened this issue 5 years ago • 3 comments

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

TaylorSasser avatar May 22 '19 18:05 TaylorSasser

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);
    }

geerteltink avatar Jun 20 '19 12:06 geerteltink

This repository has been closed and moved to mezzio/mezzio-fastroute; a new issue has been opened at https://github.com/mezzio/mezzio-fastroute/issues/1.

weierophinney avatar Dec 31 '19 20:12 weierophinney

This repository has been moved to mezzio/mezzio-fastroute. If you feel that this patch is still relevant, please re-open against that repository, and reference this issue. To re-open, we suggest the following workflow:

  • Squash all commits in your branch (git rebase -i origin/{branch})
  • Make a note of all changed files (`git diff --name-only origin/{branch}...HEAD
  • Run the laminas/laminas-migration tool on the code.
  • Clone mezzio/mezzio-fastroute to another directory.
  • Copy the files from the second bullet point to the clone of mezzio/mezzio-fastroute.
  • In your clone of mezzio/mezzio-fastroute, commit the files, push to your fork, and open the new PR. We will be providing tooling via laminas/laminas-migration soon to help automate the process.

weierophinney avatar Dec 31 '19 20:12 weierophinney