Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uri generation for custom parser #1

Open
weierophinney opened this issue Dec 31, 2019 · 1 comment
Open

Fix uri generation for custom parser #1

weierophinney opened this issue Dec 31, 2019 · 1 comment

Comments

@weierophinney
Copy link
Contributor

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 zendframework/zend-expressive-fastroute#63

@weierophinney
Copy link
Contributor Author

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 zendframework/zend-expressive-fastroute#63 (comment)

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

No branches or pull requests

1 participant