-
Notifications
You must be signed in to change notification settings - Fork 393
Add RouteHelper, wrapping Laminas URL helper #5049
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
73a14ef
Add RouteHelper helper class, wrapping Laminas URL helper
maccabeelevine 1d5b102
Remove unneeded tests
maccabeelevine 2108d58
Apply Demian's suggestions
maccabeelevine b76eb56
Fix styling
maccabeelevine 07ca406
Tweak naming and add typing
maccabeelevine d9c3cc4
Disable normalize_path
maccabeelevine 0c18727
Fix test
maccabeelevine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Route Helper class. | ||
| * | ||
| * PHP version 8 | ||
| * | ||
| * Copyright (C) Villanova University 2025. | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 2, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program; if not, see | ||
| * <https://www.gnu.org/licenses/>. | ||
| * | ||
| * @category VuFind | ||
| * @package Http | ||
| * @author Demian Katz <demian.katz@villanova.edu> | ||
| * @author Maccabee Levine <msl321@lehigh.edu> | ||
| * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
| * @link https://vufind.org | ||
| */ | ||
|
|
||
| namespace VuFind\Http; | ||
|
|
||
| use Closure; | ||
|
|
||
| /** | ||
| * Route Helper class. Wrapper around Laminas UrlHelper. | ||
| * | ||
| * @category VuFind | ||
| * @package Http | ||
| * @author Demian Katz <demian.katz@villanova.edu> | ||
| * @author Maccabee Levine <msl321@lehigh.edu> | ||
| * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
| * @link https://vufind.org | ||
| */ | ||
| class RouteHelper | ||
| { | ||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param Closure $urlHelper URL helper function | ||
| * | ||
| * @return void | ||
| */ | ||
| public function __construct(protected Closure $urlHelper) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * Generates a url given the name of a route. | ||
| * | ||
| * @param string $name Name of the route | ||
| * @param array $linkParams Parameters for the link | ||
| * @param array|\Traversable $routeOptions Options for the route | ||
| * | ||
| * @see \Laminas\Router\RouteInterface::assemble() | ||
| * | ||
| * @throws \Laminas\View\Exception\RuntimeException If no RouteStackInterface was provided | ||
| * @throws \Laminas\View\Exception\RuntimeException If no RouteMatch was provided | ||
| * @throws \Laminas\View\Exception\RuntimeException If RouteMatch didn't contain a matched | ||
| * route name | ||
| * @throws \Laminas\View\Exception\InvalidArgumentException If the params object was not an | ||
| * array or Traversable object. | ||
| * | ||
| * @return self|string Url For the link href attribute | ||
| */ | ||
| public function getUrlFromRoute( | ||
| $name = null, | ||
| $linkParams = [], | ||
| $routeOptions = [] | ||
| ): string { | ||
| return ($this->urlHelper)($name, $linkParams, $routeOptions); | ||
| } | ||
|
maccabeelevine marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Route Helper factory. | ||
| * | ||
| * PHP version 8 | ||
| * | ||
| * Copyright (C) Villanova University 2025. | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 2, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program; if not, see | ||
| * <https://www.gnu.org/licenses/>. | ||
| * | ||
| * @category VuFind | ||
| * @package Http | ||
| * @author Maccabee Levine <msl321@lehigh.edu> | ||
| * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
| * @link https://vufind.org/wiki/development Wiki | ||
| */ | ||
|
|
||
| namespace VuFind\Http; | ||
|
|
||
| use Closure; | ||
| use Laminas\ServiceManager\Exception\ServiceNotCreatedException; | ||
| use Laminas\ServiceManager\Exception\ServiceNotFoundException; | ||
| use Laminas\ServiceManager\Factory\FactoryInterface; | ||
| use Psr\Container\ContainerExceptionInterface as ContainerException; | ||
| use Psr\Container\ContainerInterface; | ||
|
|
||
| /** | ||
| * Route Helper factory. | ||
| * | ||
| * @category VuFind | ||
| * @package Http | ||
| * @author Maccabee Levine <msl321@lehigh.edu> | ||
| * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
| * @link https://vufind.org/wiki/development Wiki | ||
| */ | ||
| class RouteHelperFactory implements FactoryInterface | ||
| { | ||
| /** | ||
| * Create an object | ||
| * | ||
| * @param ContainerInterface $container Service manager | ||
| * @param string $requestedName Service being created | ||
| * @param null|array $options Extra options (optional) | ||
| * | ||
| * @return object | ||
| * | ||
| * @throws ServiceNotFoundException if unable to resolve the service. | ||
| * @throws ServiceNotCreatedException if an exception is raised when | ||
| * creating a service. | ||
| * @throws ContainerException&\Throwable if any other error occurs | ||
| */ | ||
| public function __invoke( | ||
| ContainerInterface $container, | ||
| $requestedName, | ||
| ?array $options = null | ||
| ) { | ||
| if (!empty($options)) { | ||
| throw new \Exception('Unexpected options passed to factory.'); | ||
| } | ||
|
|
||
| $viewRenderer = $container->get('ViewRenderer'); | ||
| return new $requestedName( | ||
| Closure::fromCallable($viewRenderer->plugin('url')) | ||
| ); | ||
| } | ||
| } |
100 changes: 100 additions & 0 deletions
100
module/VuFind/tests/unit-tests/src/VuFindTest/Http/RouteHelperTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * RouteHelper Test Class | ||
| * | ||
| * PHP version 8 | ||
| * | ||
| * Copyright (C) Villanova University 2025. | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 2, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program; if not, see | ||
| * <https://www.gnu.org/licenses/>. | ||
| * | ||
| * @category VuFind | ||
| * @package Tests | ||
| * @author Maccabee Levine <msl321@lehigh.edu> | ||
| * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
| * @link https://vufind.org/wiki/development:testing:unit_tests Wiki | ||
| */ | ||
|
|
||
| namespace VuFindTest\Http; | ||
|
|
||
| use Closure; | ||
| use Laminas\View\Helper\Url as UrlHelper; | ||
| use VuFind\Http\RouteHelper; | ||
|
|
||
| /** | ||
| * RouteHelper Test Class | ||
| * | ||
| * @category VuFind | ||
| * @package Tests | ||
| * @author Maccabee Levine <msl321@lehigh.edu> | ||
| * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
| * @link https://vufind.org/wiki/development:testing:unit_tests Wiki | ||
| */ | ||
| class RouteHelperTest extends \PHPUnit\Framework\TestCase | ||
| { | ||
| /** | ||
| * Shallowly test helper's getUrlFromRoute method | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testShallowGetUrlFromRoute(): void | ||
| { | ||
| $routeName = 'some_route'; | ||
|
|
||
| $urlHelper = $this->createMock(UrlHelper::class); | ||
| $urlHelper->expects($this->once())->method('__invoke')->willReturn($routeName); | ||
| $routeHelper = new RouteHelper( | ||
| Closure::fromCallable($urlHelper) | ||
| ); | ||
|
|
||
| $url = $routeHelper->getUrlFromRoute('', [], []); | ||
| $this->assertSame($routeName, $url); | ||
|
maccabeelevine marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // /** | ||
| // * Data provider for testGetUrlFromRoute tests. | ||
| // * | ||
| // * @return \Iterator<string, array> | ||
| // */ | ||
| // public static function getUrlFromRouteProvider(): \Iterator | ||
| // { | ||
| // yield 'Solr results' => [ 'search-results', [], ['query' => ['lookfor' => 'foo']], 'vufind/search/results' ]; | ||
| // } | ||
|
|
||
| // /** | ||
| // * Test helper's getUrlFromRoute method | ||
| // * | ||
| // * @param string $name Name of the route | ||
| // * @param array $linkParams Parameters for the link | ||
| // * @param array|\Traversable $routeOptions Options for the route | ||
| // * @param string $expected Expected route URL | ||
| // * | ||
| // * @return void | ||
| // */ | ||
| // #[\PHPUnit\Framework\Attributes\DataProvider('getUrlFromRouteProvider')] | ||
| // public function testGetUrlFromRoute( | ||
|
maccabeelevine marked this conversation as resolved.
Outdated
|
||
| // string $name, | ||
| // array $linkParams, | ||
| // array | \Traversable $routeOptions, | ||
| // string $expected | ||
| // ): void { | ||
| // $routeHelper = new RouteHelper( | ||
| // Closure::fromCallable(new UrlHelper()) | ||
| // ); | ||
|
|
||
| // $url = $routeHelper->getUrlFromRoute($name, $linkParams, $routeOptions); | ||
| // $this->assertSame($expected, $url); | ||
| // } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.