Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions module/VuFind/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@
'VuFind\Http\CachingDownloader' => 'VuFind\Http\CachingDownloaderFactory',
'VuFind\Http\GuzzleService' => 'VuFind\Http\GuzzleServiceFactory',
'VuFind\Http\PhpEnvironment\Request' => 'Laminas\ServiceManager\Factory\InvokableFactory',
'VuFind\Http\RouteHelper' => 'VuFind\Http\RouteHelperFactory',
'VuFind\Http\ServerUrlHelper' => 'VuFind\Http\ServerUrlHelperFactory',
'VuFind\I18n\Locale\LocaleSettings' => 'VuFind\Service\ServiceWithConfigIniFactory',
'VuFind\I18n\Sorter' => 'VuFind\I18n\SorterFactory',
Expand Down
83 changes: 83 additions & 0 deletions module/VuFind/src/VuFind/Http/RouteHelper.php
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
Comment thread
demiankatz marked this conversation as resolved.
Outdated
* @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);
}
Comment thread
maccabeelevine marked this conversation as resolved.
}
78 changes: 78 additions & 0 deletions module/VuFind/src/VuFind/Http/RouteHelperFactory.php
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 module/VuFind/tests/unit-tests/src/VuFindTest/Http/RouteHelperTest.php
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);
Comment thread
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(
Comment thread
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);
// }
}
Loading