Skip to content

Commit c71c924

Browse files
committed
Fix isRootTemplate
1 parent 21e6550 commit c71c924

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 0.4.3
4+
5+
* `isRootTemplate` now work even if you have called the `renderResource` helper inside a view
6+
37
## 0.4.2
48

59
* Does not modify status code response if an exception is contained into the MvcEvent

src/View/Renderer/ResourceRenderer.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,24 @@ public function isRootTemplate()
121121
*/
122122
public function render($nameOrModel, $values = null)
123123
{
124-
// We set the currently rendered view model into the viewModel helper. This allows to render additional
125-
// properties in the view by comparing the root and nested view model
126-
$this->viewModel()->setCurrent($nameOrModel);
124+
/** @var \Zend\View\Helper\ViewModel $viewModelHelper */
125+
$viewModelHelper = $this->helperPluginManager->get('viewModel');
126+
127+
// Because templates can be rendered recursively, we need to save the current context
128+
$previousViewModel = $viewModelHelper->getCurrent();
127129

128130
$template = $this->resolver->resolve($nameOrModel->getTemplate());
129131

130132
// We need to save and restore the previous variables, because the same renderer can be used inside
131133
// multiple contexts
132-
$previousTemplateVariables = $this->templateVariables;
133-
$this->templateVariables = $nameOrModel->getVariables();
134+
$viewModelHelper->setCurrent($nameOrModel);
135+
$this->templateVariables = $nameOrModel->getVariables();
134136

135137
$result = include $template;
136138

137-
$this->templateVariables = $previousTemplateVariables;
139+
// Restore the previous context
140+
$this->templateVariables = $previousViewModel->getVariables();
141+
$viewModelHelper->setCurrent($previousViewModel);
138142

139143
return $result;
140144
}

src/View/Strategy/ResourceStrategy.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,12 @@ public function selectRenderer(ViewEvent $event)
9898

9999
// If we have a ResourceViewModel, we set it as the "root" view model in the view model helper. This allows
100100
// to differentiate between a nested context or not, in the view
101-
/** @var \Zend\View\Helper\ViewModel $viewModel */
102-
$viewModel = $this->renderer->viewModel();
103-
$viewModel->setRoot($event->getModel());
101+
/** @var \Zend\View\Helper\ViewModel $viewModelHelper */
102+
$viewModelHelper = $helperPluginManager->get('viewModel');
103+
$viewModel = $event->getModel();
104+
105+
$viewModelHelper->setRoot($viewModel);
106+
$viewModelHelper->setCurrent($viewModel);
104107

105108
return $this->renderer;
106109
}

tests/ZfrRestTest/View/Strategy/ResourceStrategyTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Zend\EventManager\SharedEventManagerInterface;
2424
use Zend\Mvc\MvcEvent;
2525
use Zend\Stdlib\DispatchableInterface;
26+
use Zend\View\Helper\ViewModel;
27+
use Zend\View\HelperPluginManager;
2628
use Zend\View\Model\JsonModel;
2729
use Zend\View\Model\ModelInterface;
2830
use Zend\View\ViewEvent;
@@ -81,4 +83,34 @@ public function testDoNotSetTemplateIfNotResourceViewModel()
8183

8284
$this->resourceStrategy->setTemplate($mvcEvent);
8385
}
86+
87+
public function testDoNotSelectRenderIfNotResourceViewModel()
88+
{
89+
$model = $this->getMock(ModelInterface::class);
90+
91+
$viewEvent = new ViewEvent();
92+
$viewEvent->setModel($model);
93+
94+
$this->assertNull($this->resourceStrategy->selectRenderer($viewEvent));
95+
}
96+
97+
public function testProperlyFillViewModelHelperIfRendererIsSelected()
98+
{
99+
$model = new ResourceViewModel();
100+
101+
$viewEvent = new ViewEvent();
102+
$viewEvent->setModel($model);
103+
104+
$viewModelHelper = $this->getMock(ViewModel::class, [], [], '', false);
105+
$viewModelHelper->expects($this->once())->method('setRoot')->with($model);
106+
$viewModelHelper->expects($this->once())->method('setCurrent')->with($model);
107+
108+
$helperManager = $this->getMock(HelperPluginManager::class, [], [], '', false);
109+
$helperManager->expects($this->once())->method('setRenderer')->with($this->resourceRenderer);
110+
$helperManager->expects($this->once())->method('get')->with('viewModel')->willReturn($viewModelHelper);
111+
112+
$this->resourceRenderer->expects($this->once())->method('getHelperPluginManager')->willReturn($helperManager);
113+
114+
$this->assertSame($this->resourceRenderer, $this->resourceStrategy->selectRenderer($viewEvent));
115+
}
84116
}

0 commit comments

Comments
 (0)