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

Commit

Permalink
Merge pull request #163 from karborator/master
Browse files Browse the repository at this point in the history
InjectTemplateListener - returnn api proper view
  • Loading branch information
weierophinney committed Jun 30, 2016
2 parents 4fcaa6d + 3ff4408 commit df63e7e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
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
24 changes: 24 additions & 0 deletions test/View/InjectTemplateListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,28 @@ public function testPrefersRouteMatchController()

$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 \ZendTest\Mvc\Controller\TestAsset\SampleController();

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

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

0 comments on commit df63e7e

Please sign in to comment.