Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/163'
Browse files Browse the repository at this point in the history
Close #163
  • Loading branch information
weierophinney committed Jun 30, 2016
2 parents 4fcaa6d + bdf5b04 commit ada9ca1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
32 changes: 31 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,37 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- Nothing.
- [#163](https://github.com/zendframework/zend-mvc/pull/163) adds support to the
`AcceptableViewModelSelector` plugin for controller maps in the `view_manager`
configuration in the format:

```php
[
'ControllerClassName' => 'view/name',
]
```

This fixes an issue observed when running with Apigility.

- [#163](https://github.com/zendframework/zend-mvc/pull/163) adds support to the
`InjectTemplateListener` for specifying whether or not to prefer the
controller matched during routing via routing configuration:

```php
'route-name' => [
/* ... */
'options' => [
/* ... */
'defaults' => [
/* ... */
'prefer_route_match_controller' => true,
],
],
],
```

This allows actions that might otherwise skip injection of the template
to force the injection.

### Deprecated

Expand Down
3 changes: 3 additions & 0 deletions src/Controller/Plugin/AcceptableViewModelSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ public function getDefaultMatchAgainst()
protected function injectViewModelName($modelAcceptString, $modelName)
{
$modelName = str_replace('\\', '|', $modelName);
$modelAcceptString = (is_array($modelAcceptString))
? $modelAcceptString[key($modelAcceptString)]
: $modelAcceptString;
return $modelAcceptString . '; ' . self::INJECT_VIEWMODEL_NAME . '="' . $modelName . '", ';
}

Expand Down
4 changes: 4 additions & 0 deletions src/View/Http/InjectTemplateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function injectTemplate(MvcEvent $e)
}

$routeMatch = $e->getRouteMatch();
if ($preferRouteMatchController = $routeMatch->getParam('prefer_route_match_controller', false)) {
$this->setPreferRouteMatchController($preferRouteMatchController);
}

$controller = $e->getTarget();
if (is_object($controller)) {
$controller = get_class($controller);
Expand Down
35 changes: 30 additions & 5 deletions test/View/InjectTemplateListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Zend\Router\RouteMatch;
use Zend\Mvc\View\Http\InjectTemplateListener;
use Zend\View\Model\ViewModel;
use ZendTest\Mvc\Controller\TestAsset\SampleController;

class InjectTemplateListenerTest extends TestCase
{
Expand Down Expand Up @@ -108,7 +109,7 @@ public function testBypassesTemplateInjectionIfResultViewModelAlreadyHasATemplat
public function testMapsSubNamespaceToSubDirectory()
{
$myViewModel = new ViewModel();
$myController = new \ZendTest\Mvc\Controller\TestAsset\SampleController();
$myController = new SampleController();
$this->event->setTarget($myController);
$this->event->setResult($myViewModel);

Expand Down Expand Up @@ -158,7 +159,7 @@ public function testMapsSubNamespaceToSubDirectoryWithControllerFromEventTarget(
$moduleRouteListener->onRoute($this->event);

$myViewModel = new ViewModel();
$myController = new \ZendTest\Mvc\Controller\TestAsset\SampleController();
$myController = new SampleController();

$this->event->setTarget($myController);
$this->event->setResult($myViewModel);
Expand All @@ -183,7 +184,7 @@ public function testMapsSubNamespaceToSubDirectoryWithControllerFromEventTargetS
$template1 = $myViewModel->getTemplate();

$myViewModel = new ViewModel();
$myController = new \ZendTest\Mvc\Controller\TestAsset\SampleController();
$myController = new SampleController();

$this->event->setTarget($myController);
$this->event->setResult($myViewModel);
Expand All @@ -204,7 +205,7 @@ public function testControllerMatchedByMapIsInflected()

$this->listener->setControllerMap(['ZendTest' => true]);
$myViewModel = new ViewModel();
$myController = new \ZendTest\Mvc\Controller\TestAsset\SampleController();
$myController = new SampleController();
$this->event->setTarget($myController);
$this->event->setResult($myViewModel);

Expand Down Expand Up @@ -332,12 +333,36 @@ public function testPrefersRouteMatchController()
$this->listener->setPreferRouteMatchController(true);
$this->routeMatch->setParam('controller', 'Some\Other\Service\Namespace\Controller\Sample');
$myViewModel = new ViewModel();
$myController = new \ZendTest\Mvc\Controller\TestAsset\SampleController();
$myController = new SampleController();

$this->event->setTarget($myController);
$this->event->setResult($myViewModel);
$this->listener->injectTemplate($this->event);

$this->assertEquals('some/other/service/namespace/sample', $myViewModel->getTemplate());
}

public function testPrefersRouteMatchControllerWithRouteMatchAndControllerMap()
{
$this->assertFalse($this->listener->isPreferRouteMatchController());
$controllerMap = [
'Some\Other\Service\Namespace\Controller\Sample' => 'another/sample'
];

$this->routeMatch->setParam('prefer_route_match_controller', true);
$this->routeMatch->setParam('controller', 'Some\Other\Service\Namespace\Controller\Sample');

$preferRouteMatchControllerRouteMatchConfig = $this->routeMatch->getParam('prefer_route_match_controller', false);
$this->listener->setPreferRouteMatchController($preferRouteMatchControllerRouteMatchConfig);
$this->listener->setControllerMap($controllerMap);

$myViewModel = new ViewModel();
$myController = new SampleController();

$this->event->setTarget($myController);
$this->event->setResult($myViewModel);
$this->listener->injectTemplate($this->event);

$this->assertEquals('another/sample', $myViewModel->getTemplate());
}
}

0 comments on commit ada9ca1

Please sign in to comment.