Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Fix uri generation for custom parser #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TaylorSasser
Copy link

@TaylorSasser TaylorSasser commented May 22, 2019

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 TaylorSasser changed the title Fix uri generation for custom parse Fix uri generation for custom parser May 22, 2019
@geerteltink
Copy link
Member

The problem is this line:

$routeParser = new RouteParser();

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

@weierophinney
Copy link
Member

This repository has been closed and moved to mezzio/mezzio-fastroute; a new issue has been opened at mezzio/mezzio-fastroute#1.

@weierophinney
Copy link
Member

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants