Skip to content

Commit 4887fc1

Browse files
authored
Defer creation of URL helper in RouteHelper to avoid early plugin invokation. (#5062)
1 parent b1584d4 commit 4887fc1

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

module/VuFind/src/VuFind/Http/RouteHelper.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
namespace VuFind\Http;
3232

3333
use Closure;
34+
use Laminas\View\Helper\Url;
3435

3536
/**
3637
* Route Helper class. Wrapper around Laminas UrlHelper.
@@ -44,14 +45,21 @@
4445
*/
4546
class RouteHelper
4647
{
48+
/**
49+
* URL helper
50+
*
51+
* @var ?Url
52+
*/
53+
protected ?Url $urlHelper = null;
54+
4755
/**
4856
* Constructor.
4957
*
50-
* @param Closure $urlHelper URL helper function
58+
* @param Closure $urlHelperFactory URL helper factory callback
5159
*
5260
* @return void
5361
*/
54-
public function __construct(protected Closure $urlHelper)
62+
public function __construct(protected Closure $urlHelperFactory)
5563
{
5664
}
5765

@@ -71,7 +79,7 @@ public function __construct(protected Closure $urlHelper)
7179
* @throws \Laminas\View\Exception\InvalidArgumentException If the params object was not an
7280
* array or Traversable object.
7381
*
74-
* @return self|string Url For the link href attribute
82+
* @return string Url For the link href attribute
7583
*/
7684
public function getUrlFromRoute(
7785
string $name,
@@ -80,6 +88,19 @@ public function getUrlFromRoute(
8088
): string {
8189
// Path normalization can cause problems with IDs containing escaped slashes, so let's always disable it:
8290
$routeOptions = ['normalize_path' => false] + ($queryParams ? ['query' => $queryParams] : []);
83-
return ($this->urlHelper)($name, $routeParams, $routeOptions);
91+
return ($this->getUrlHelper())($name, $routeParams, $routeOptions);
92+
}
93+
94+
/**
95+
* Get URL helper.
96+
*
97+
* @return Url
98+
*/
99+
protected function getUrlHelper(): Url
100+
{
101+
if (null === $this->urlHelper) {
102+
$this->urlHelper = ($this->urlHelperFactory)();
103+
}
104+
return $this->urlHelper;
84105
}
85106
}

module/VuFind/src/VuFind/Http/RouteHelperFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public function __invoke(
7272

7373
$viewRenderer = $container->get('ViewRenderer');
7474
return new $requestedName(
75-
Closure::fromCallable($viewRenderer->plugin('url'))
75+
// Defer fetching of the plugin until it's actually needed to allow for Laminas MvcEvent to be dispatched
76+
// first:
77+
Closure::fromCallable(fn () => $viewRenderer->plugin('url'))
7678
);
7779
}
7880
}

module/VuFind/tests/unit-tests/src/VuFindTest/Http/RouteHelperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testShallowGetUrlFromRoute(): void
6262
->with($routeName, $routeParams, ['query' => $queryParams, 'normalize_path' => false])
6363
->willReturn($routeResult);
6464
$routeHelper = new RouteHelper(
65-
Closure::fromCallable($urlHelper)
65+
Closure::fromCallable(fn () => $urlHelper)
6666
);
6767

6868
$url = $routeHelper->getUrlFromRoute($routeName, $routeParams, $queryParams);

0 commit comments

Comments
 (0)