From d415a4dae8c5ef92fce230ce212e14e12705fa94 Mon Sep 17 00:00:00 2001 From: Aleksei Date: Sat, 4 Jun 2022 04:47:28 +1000 Subject: [PATCH 1/2] Drop ModuleRouteListener support Signed-off-by: Aleksei --- docs/book/mvc-event.md | 1 - src/Controller/Plugin/Url.php | 11 -- src/ModuleRouteListener.php | 73 ------------ test/Controller/Plugin/UrlTest.php | 45 -------- test/ModuleRouteListenerTest.php | 134 ----------------------- test/View/InjectTemplateListenerTest.php | 101 ----------------- 6 files changed, 365 deletions(-) delete mode 100644 src/ModuleRouteListener.php delete mode 100644 test/ModuleRouteListenerTest.php diff --git a/docs/book/mvc-event.md b/docs/book/mvc-event.md index e452ce8b6..d47d2b64b 100644 --- a/docs/book/mvc-event.md +++ b/docs/book/mvc-event.md @@ -94,7 +94,6 @@ priority): Class | Priority | Method Called | Triggers | Description -------------------------------|---------:|---------------|----------|------------ -`Laminas\Mvc\ModuleRouteListener` | 1 | `onRoute` | none | Determines if the module namespace should be prepended to the controller name. This is the case if the route match contains a parameter key matching the `MODULE_NAMESPACE` constant. `Laminas\Mvc\RouteListener` | 1 | `onRoute` | `MvcEvent::EVENT_DISPATCH_ERROR` (if no route is matched) | Tries to match the request to the router and return a `RouteMatch` object. ### Triggered By diff --git a/src/Controller/Plugin/Url.php b/src/Controller/Plugin/Url.php index f796fbccb..652f19254 100644 --- a/src/Controller/Plugin/Url.php +++ b/src/Controller/Plugin/Url.php @@ -10,7 +10,6 @@ use Laminas\Mvc\Exception\InvalidArgumentException; use Laminas\Mvc\Exception\RuntimeException; use Laminas\Mvc\InjectApplicationEventInterface; -use Laminas\Mvc\ModuleRouteListener; use Laminas\Mvc\MvcEvent; use Laminas\Router\RouteStackInterface; use Traversable; @@ -89,16 +88,6 @@ public function fromRoute($route = null, $params = [], $options = [], $reuseMatc if ($reuseMatchedParams && $matches) { $routeMatchParams = $matches->getParams(); - - if (isset($routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER])) { - $routeMatchParams['controller'] = $routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER]; - unset($routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER]); - } - - if (isset($routeMatchParams[ModuleRouteListener::MODULE_NAMESPACE])) { - unset($routeMatchParams[ModuleRouteListener::MODULE_NAMESPACE]); - } - $params = array_merge($routeMatchParams, $params); } diff --git a/src/ModuleRouteListener.php b/src/ModuleRouteListener.php deleted file mode 100644 index c44eeebce..000000000 --- a/src/ModuleRouteListener.php +++ /dev/null @@ -1,73 +0,0 @@ -listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, [$this, 'onRoute'], $priority); - } - - /** - * Listen to the "route" event and determine if the module namespace should - * be prepended to the controller name. - * - * If the route match contains a parameter key matching the MODULE_NAMESPACE - * constant, that value will be prepended, with a namespace separator, to - * the matched controller parameter. - * - * @return null - */ - public function onRoute(MvcEvent $e) - { - $matches = $e->getRouteMatch(); - if (! $matches instanceof RouteMatch) { - // Can't do anything without a route match - return; - } - - $module = $matches->getParam(self::MODULE_NAMESPACE, false); - if (! $module) { - // No module namespace found; nothing to do - return; - } - - $controller = $matches->getParam('controller', false); - if (! $controller) { - // no controller matched, nothing to do - return; - } - - // Ensure the module namespace has not already been applied - if (0 === strpos($controller, $module)) { - return; - } - - // Keep the originally matched controller name around - $matches->setParam(self::ORIGINAL_CONTROLLER, $controller); - - // Prepend the controllername with the module, and replace it in the - // matches - $controller = $module . '\\' . str_replace(' ', '', ucwords(str_replace('-', ' ', $controller))); - $matches->setParam('controller', $controller); - } -} diff --git a/test/Controller/Plugin/UrlTest.php b/test/Controller/Plugin/UrlTest.php index 498b8e284..99a67a6f0 100644 --- a/test/Controller/Plugin/UrlTest.php +++ b/test/Controller/Plugin/UrlTest.php @@ -8,7 +8,6 @@ use Laminas\Mvc\Controller\Plugin\Url as UrlPlugin; use Laminas\Mvc\Exception\DomainException; use Laminas\Mvc\Exception\RuntimeException; -use Laminas\Mvc\ModuleRouteListener; use Laminas\Mvc\MvcEvent; use Laminas\Router\Http\Literal as LiteralRoute; use Laminas\Router\Http\Segment; @@ -148,48 +147,4 @@ public function testCanPassBooleanValueForThirdArgumentToAllowReusingRouteMatche $url = $this->plugin->fromRoute('replace', ['action' => 'bar'], true); $this->assertEquals('/foo/bar', $url); } - - public function testRemovesModuleRouteListenerParamsWhenReusingMatchedParameters(): void - { - $router = new TreeRouteStack(); - $router->addRoute('default', [ - 'type' => Segment::class, - 'options' => [ - 'route' => '/:controller/:action', - 'defaults' => [ - ModuleRouteListener::MODULE_NAMESPACE => 'LaminasTest\Mvc\Controller\TestAsset', - 'controller' => 'SampleController', - 'action' => 'Dash', - ], - ], - 'child_routes' => [ - 'wildcard' => [ - 'type' => Wildcard::class, - 'options' => [ - 'param_delimiter' => '=', - 'key_value_delimiter' => '%', - ], - ], - ], - ]); - - $routeMatch = new RouteMatch([ - ModuleRouteListener::MODULE_NAMESPACE => 'LaminasTest\Mvc\Controller\TestAsset', - 'controller' => 'Rainbow', - ]); - $routeMatch->setMatchedRouteName('default/wildcard'); - - $event = new MvcEvent(); - $event->setRouter($router) - ->setRouteMatch($routeMatch); - - $moduleRouteListener = new ModuleRouteListener(); - $moduleRouteListener->onRoute($event); - - $controller = new SampleController(); - $controller->setEvent($event); - $url = $controller->plugin('url')->fromRoute('default/wildcard', ['Twenty' => 'Cooler'], true); - - $this->assertEquals('/Rainbow/Dash=Twenty%Cooler', $url); - } } diff --git a/test/ModuleRouteListenerTest.php b/test/ModuleRouteListenerTest.php deleted file mode 100644 index 4e249292e..000000000 --- a/test/ModuleRouteListenerTest.php +++ /dev/null @@ -1,134 +0,0 @@ -request = new Request(); - $this->events = new EventManager(); - $this->router = new Router\Http\TreeRouteStack(); - $this->routeListener = new RouteListener(); - $this->moduleRouteListener = new ModuleRouteListener(); - - $this->routeListener->attach($this->events); - $this->moduleRouteListener->attach($this->events, -1); - } - - public function testRouteReturningModuleNamespaceInRouteMatchTriggersControllerRename(): void - { - $this->router->addRoute('foo', [ - 'type' => 'Literal', - 'options' => [ - 'route' => '/foo', - 'defaults' => [ - ModuleRouteListener::MODULE_NAMESPACE => 'Foo', - 'controller' => 'Index', - ], - ], - ]); - $this->request->setUri('/foo'); - $event = new MvcEvent(); - $event->setName('route'); - $event->setRouter($this->router); - $event->setRequest($this->request); - $this->events->triggerEvent($event); - - $matches = $event->getRouteMatch(); - $this->assertInstanceOf(RouteMatch::class, $matches); - $this->assertEquals('Foo\Index', $matches->getParam('controller')); - $this->assertEquals('Index', $matches->getParam(ModuleRouteListener::ORIGINAL_CONTROLLER)); - } - - public function testRouteNotReturningModuleNamespaceInRouteMatchLeavesControllerUntouched(): void - { - $this->router->addRoute('foo', [ - 'type' => 'Literal', - 'options' => [ - 'route' => '/foo', - 'defaults' => [ - 'controller' => 'Index', - ], - ], - ]); - $this->request->setUri('/foo'); - $event = new MvcEvent(); - $event->setName('route'); - $event->setRouter($this->router); - $event->setRequest($this->request); - $this->events->triggerEvent($event); - - $matches = $event->getRouteMatch(); - $this->assertInstanceOf(RouteMatch::class, $matches); - $this->assertEquals('Index', $matches->getParam('controller')); - } - - public function testMultipleRegistrationShouldNotResultInMultiplePrefixingOfControllerName(): void - { - $moduleListener = new ModuleRouteListener(); - $moduleListener->attach($this->events); - - $this->router->addRoute('foo', [ - 'type' => 'Literal', - 'options' => [ - 'route' => '/foo', - 'defaults' => [ - ModuleRouteListener::MODULE_NAMESPACE => 'Foo', - 'controller' => 'Index', - ], - ], - ]); - $this->request->setUri('/foo'); - $event = new MvcEvent(); - $event->setName('route'); - $event->setRouter($this->router); - $event->setRequest($this->request); - $this->events->triggerEvent($event); - - $matches = $event->getRouteMatch(); - $this->assertInstanceOf(RouteMatch::class, $matches); - $this->assertEquals('Foo\Index', $matches->getParam('controller')); - $this->assertEquals('Index', $matches->getParam(ModuleRouteListener::ORIGINAL_CONTROLLER)); - } - - public function testRouteMatchIsTransformedToProperControllerClassName(): void - { - $moduleListener = new ModuleRouteListener(); - $moduleListener->attach($this->events); - - $this->router->addRoute('foo', [ - 'type' => 'Literal', - 'options' => [ - 'route' => '/foo', - 'defaults' => [ - ModuleRouteListener::MODULE_NAMESPACE => 'Foo', - 'controller' => 'some-index', - ], - ], - ]); - - $this->request->setUri('/foo'); - $event = new MvcEvent(); - $event->setName('route'); - $event->setRouter($this->router); - $event->setRequest($this->request); - $this->events->triggerEvent($event); - - $matches = $event->getRouteMatch(); - $this->assertInstanceOf(RouteMatch::class, $matches); - $this->assertEquals('Foo\SomeIndex', $matches->getParam('controller')); - $this->assertEquals('some-index', $matches->getParam(ModuleRouteListener::ORIGINAL_CONTROLLER)); - } -} diff --git a/test/View/InjectTemplateListenerTest.php b/test/View/InjectTemplateListenerTest.php index 1d7cbebcd..2dcd026fc 100644 --- a/test/View/InjectTemplateListenerTest.php +++ b/test/View/InjectTemplateListenerTest.php @@ -6,7 +6,6 @@ use Laminas\EventManager\EventManager; use Laminas\EventManager\Test\EventListenerIntrospectionTrait; -use Laminas\Mvc\ModuleRouteListener; use Laminas\Mvc\MvcEvent; use Laminas\Mvc\View\Http\InjectTemplateListener; use Laminas\Router\RouteMatch; @@ -113,82 +112,6 @@ public function testMapsSubNamespaceToSubDirectory(): void $this->assertEquals('laminas-test/mvc/test-asset/sample', $myViewModel->getTemplate()); } - public function testMapsSubNamespaceToSubDirectoryWithControllerFromRouteMatch(): void - { - $this->routeMatch->setParam(ModuleRouteListener::MODULE_NAMESPACE, 'Aj\Controller\SweetAppleAcres\Reports'); - $this->routeMatch->setParam('controller', 'CiderSales'); - $this->routeMatch->setParam('action', 'PinkiePieRevenue'); - - $moduleRouteListener = new ModuleRouteListener(); - $moduleRouteListener->onRoute($this->event); - - $model = new ViewModel(); - $this->event->setResult($model); - $this->listener->injectTemplate($this->event); - - $this->assertEquals('aj/sweet-apple-acres/reports/cider-sales/pinkie-pie-revenue', $model->getTemplate()); - } - - public function testMapsSubNamespaceToSubDirectoryWithControllerFromRouteMatchHavingSubNamespace(): void - { - $this->routeMatch->setParam(ModuleRouteListener::MODULE_NAMESPACE, 'Aj\Controller\SweetAppleAcres\Reports'); - $this->routeMatch->setParam('controller', 'Sub\CiderSales'); - $this->routeMatch->setParam('action', 'PinkiePieRevenue'); - - $moduleRouteListener = new ModuleRouteListener(); - $moduleRouteListener->onRoute($this->event); - - $model = new ViewModel(); - $this->event->setResult($model); - $this->listener->injectTemplate($this->event); - - $this->assertEquals('aj/sweet-apple-acres/reports/sub/cider-sales/pinkie-pie-revenue', $model->getTemplate()); - } - - public function testMapsSubNamespaceToSubDirectoryWithControllerFromEventTarget(): void - { - $this->routeMatch->setParam(ModuleRouteListener::MODULE_NAMESPACE, 'LaminasTest\Mvc\Controller\TestAsset'); - $this->routeMatch->setParam('action', 'test'); - - $moduleRouteListener = new ModuleRouteListener(); - $moduleRouteListener->onRoute($this->event); - - $myViewModel = new ViewModel(); - $myController = new SampleController(); - - $this->event->setTarget($myController); - $this->event->setResult($myViewModel); - $this->listener->injectTemplate($this->event); - - $this->assertEquals('laminas-test/mvc/test-asset/sample/test', $myViewModel->getTemplate()); - } - - // @codingStandardsIgnoreLine - public function testMapsSubNamespaceToSubDirectoryWithControllerFromEventTargetShouldMatchControllerFromRouteParam(): void - { - $this->routeMatch->setParam(ModuleRouteListener::MODULE_NAMESPACE, 'LaminasTest\Mvc\Controller'); - $this->routeMatch->setParam('controller', 'TestAsset\SampleController'); - $this->routeMatch->setParam('action', 'test'); - - $moduleRouteListener = new ModuleRouteListener(); - $moduleRouteListener->onRoute($this->event); - - $myViewModel = new ViewModel(); - $this->event->setResult($myViewModel); - $this->listener->injectTemplate($this->event); - - $template1 = $myViewModel->getTemplate(); - - $myViewModel = new ViewModel(); - $myController = new SampleController(); - - $this->event->setTarget($myController); - $this->event->setResult($myViewModel); - $this->listener->injectTemplate($this->event); - - $this->assertEquals($template1, $myViewModel->getTemplate()); - } - public function testControllerMatchedByMapIsInflected(): void { $this->routeMatch->setParam('controller', 'MappedNs\SubNs\Controller\Sample'); @@ -238,30 +161,6 @@ public function testControllerMapMatchedPrefixReplacedByStringValue(): void $this->assertEquals('string-value/index', $template); } - public function testUsingNamespaceRouteParameterGivesSameResultAsFullControllerParameter(): void - { - $this->routeMatch->setParam('controller', 'MappedNs\Foo\Controller\Bar\Baz\Sample'); - $myViewModel = new ViewModel(); - - $this->event->setResult($myViewModel); - $this->listener->injectTemplate($this->event); - - $template1 = $myViewModel->getTemplate(); - - $this->routeMatch->setParam(ModuleRouteListener::MODULE_NAMESPACE, 'MappedNs\Foo\Controller\Bar'); - $this->routeMatch->setParam('controller', 'Baz\Sample'); - - $moduleRouteListener = new ModuleRouteListener(); - $moduleRouteListener->onRoute($this->event); - - $myViewModel = new ViewModel(); - - $this->event->setResult($myViewModel); - $this->listener->injectTemplate($this->event); - - $this->assertEquals($template1, $myViewModel->getTemplate()); - } - public function testControllerMapOnlyFullNamespaceMatches(): void { $this->listener->setControllerMap([ From e197f405699420cb4b4943d378d33171476f06b0 Mon Sep 17 00:00:00 2001 From: Aleksei Khudiakov Date: Sat, 20 Aug 2022 21:22:14 +1000 Subject: [PATCH 2/2] Apply CS fixes Signed-off-by: Aleksei Khudiakov --- src/Controller/Plugin/Url.php | 2 +- test/Controller/Plugin/UrlTest.php | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Controller/Plugin/Url.php b/src/Controller/Plugin/Url.php index 652f19254..c055b5b74 100644 --- a/src/Controller/Plugin/Url.php +++ b/src/Controller/Plugin/Url.php @@ -88,7 +88,7 @@ public function fromRoute($route = null, $params = [], $options = [], $reuseMatc if ($reuseMatchedParams && $matches) { $routeMatchParams = $matches->getParams(); - $params = array_merge($routeMatchParams, $params); + $params = array_merge($routeMatchParams, $params); } $options['name'] = $route; diff --git a/test/Controller/Plugin/UrlTest.php b/test/Controller/Plugin/UrlTest.php index 99a67a6f0..bdaa0945d 100644 --- a/test/Controller/Plugin/UrlTest.php +++ b/test/Controller/Plugin/UrlTest.php @@ -12,8 +12,6 @@ use Laminas\Router\Http\Literal as LiteralRoute; use Laminas\Router\Http\Segment; use Laminas\Router\Http\Segment as SegmentRoute; -use Laminas\Router\Http\TreeRouteStack; -use Laminas\Router\Http\Wildcard; use Laminas\Router\RouteMatch; use Laminas\Router\SimpleRouteStack; use LaminasTest\Mvc\Controller\TestAsset\SampleController;