-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Url helper to preserve BC with legacy
Laminas\View\Helper\Url
- Loading branch information
Showing
10 changed files
with
1,333 additions
and
293 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<files psalm-version="v4.25.0@d7cd84c4ebca74ba3419b9601f81d177bcbe2aac"> | ||
<file src="src/Helper/Url.php"> | ||
<RedundantCastGivenDocblockType occurrences="1"> | ||
<code>(string) $this->routeMatch->getMatchedRouteName()</code> | ||
</RedundantCastGivenDocblockType> | ||
</file> | ||
</files> |
This file contains 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 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 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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Mvc\View\Exception; | ||
|
||
use RuntimeException; | ||
|
||
final class RouteNotMatchedException extends RuntimeException implements ExceptionInterface | ||
{ | ||
public static function withEmptyRouteName(): self | ||
{ | ||
return new self('A route name was not provided or RouteMatch does not contain a matched route name'); | ||
} | ||
} |
This file contains 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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Mvc\View\Helper\Factory; | ||
|
||
use Laminas\Mvc\Application; | ||
use Laminas\Mvc\View\Assert; | ||
use Laminas\Mvc\View\Helper\Url; | ||
use Laminas\Router\RouteMatch; | ||
use Laminas\Router\RouteStackInterface; | ||
use Psr\Container\ContainerInterface; | ||
|
||
use function assert; | ||
|
||
final class UrlFactory | ||
{ | ||
public function __invoke(ContainerInterface $container): Url | ||
{ | ||
/** | ||
* This is traditionally the alias we use in MVC to fetch the router in use | ||
*/ | ||
$router = $container->get('HttpRouter'); | ||
Assert::isInstanceOf($router, RouteStackInterface::class); | ||
|
||
/** | ||
* The RouteMatch instance must be retrieved from the MVC Event | ||
*/ | ||
$mvcApplication = $container->get('Application'); | ||
assert($mvcApplication instanceof Application); | ||
$routeMatch = $mvcApplication->getMvcEvent()->getRouteMatch(); | ||
Assert::isInstanceOf($routeMatch, RouteMatch::class); | ||
|
||
return new Url( | ||
$routeMatch, | ||
$router | ||
); | ||
} | ||
} |
This file contains 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,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Mvc\View\Helper; | ||
|
||
use Laminas\Mvc\ModuleRouteListener; | ||
use Laminas\Mvc\View\Exception\RouteNotMatchedException; | ||
use Laminas\Router\RouteInterface; | ||
use Laminas\Router\RouteMatch; | ||
use Laminas\Router\RouteStackInterface; | ||
use Laminas\Stdlib\ArrayUtils; | ||
|
||
use function array_merge; | ||
use function assert; | ||
use function func_num_args; | ||
use function is_array; | ||
use function is_bool; | ||
use function is_object; | ||
use function is_string; | ||
|
||
final class Url | ||
{ | ||
private RouteMatch $routeMatch; | ||
private RouteStackInterface $router; | ||
|
||
public function __construct( | ||
RouteMatch $routeMatch, | ||
RouteStackInterface $router | ||
) { | ||
$this->routeMatch = $routeMatch; | ||
$this->router = $router; | ||
} | ||
|
||
/** | ||
* Generates an url given the name of a route. | ||
* | ||
* @see RouteInterface::assemble() | ||
* | ||
* @param string|null $name Name of the route | ||
* @param iterable<string, mixed> $params Parameters for the link | ||
* @param iterable<mixed>|bool $options Options for the route, or bool $reuseMatchedParams to skip the 4th argument | ||
* @param bool $reuseMatchedParams Whether to reuse matched parameters | ||
* @return string Url For the link href attribute | ||
* @throws RouteNotMatchedException If RouteMatch didn't contain a matched route name. | ||
*/ | ||
public function __invoke( | ||
?string $name = null, | ||
iterable $params = [], | ||
$options = [], | ||
bool $reuseMatchedParams = false | ||
): string { | ||
$name = $name ?? (string) $this->routeMatch->getMatchedRouteName(); | ||
if ($name === '') { | ||
throw RouteNotMatchedException::withEmptyRouteName(); | ||
} | ||
|
||
if (is_object($params)) { | ||
$params = ArrayUtils::iteratorToArray($params); | ||
} | ||
|
||
if (is_object($options)) { | ||
$options = ArrayUtils::iteratorToArray($options); | ||
} | ||
|
||
if (func_num_args() === 3 && is_bool($options)) { | ||
$reuseMatchedParams = $options; | ||
$options = []; | ||
} | ||
|
||
assert(is_array($options)); | ||
|
||
if ($reuseMatchedParams) { | ||
$routeMatchParams = $this->routeMatch->getParams(); | ||
|
||
/** @var mixed $controller */ | ||
$controller = $routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER] ?? null; | ||
|
||
if (is_string($controller)) { | ||
$routeMatchParams['controller'] = $controller; | ||
unset($routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER]); | ||
} | ||
|
||
if (isset($routeMatchParams[ModuleRouteListener::MODULE_NAMESPACE])) { | ||
unset($routeMatchParams[ModuleRouteListener::MODULE_NAMESPACE]); | ||
} | ||
|
||
$params = array_merge($routeMatchParams, $params); | ||
} | ||
|
||
$options['name'] = $name; | ||
|
||
return (string) $this->router->assemble($params, $options); | ||
} | ||
} |
This file contains 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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\Mvc\View\Helper\Factory; | ||
|
||
use Laminas\Mvc\Application; | ||
use Laminas\Mvc\MvcEvent; | ||
use Laminas\Mvc\View\Helper\Factory\UrlFactory; | ||
use Laminas\Router\Http\RouteMatch; | ||
use Laminas\Router\Http\TreeRouteStack; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class UrlFactoryTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
public function testTheFactoryWillComposeTheCorrectDependencies(): void | ||
{ | ||
$router = new TreeRouteStack(); | ||
$match = new RouteMatch([]); | ||
$event = new MvcEvent(); | ||
$event->setRouteMatch($match); | ||
|
||
$application = $this->createMock(Application::class); | ||
$application->expects(self::once()) | ||
->method('getMvcEvent') | ||
->willReturn($event); | ||
|
||
$container = $this->createMock(ContainerInterface::class); | ||
$container->expects(self::exactly(2)) | ||
->method('get') | ||
->willReturnMap([ | ||
['HttpRouter', $router], | ||
['Application', $application], | ||
]); | ||
|
||
(new UrlFactory())->__invoke($container); | ||
} | ||
} |
Oops, something went wrong.