Skip to content

Commit 686c561

Browse files
authored
Refactor Context View Helper to use Autowiring (#5096)
1 parent 29989db commit 686c561

4 files changed

Lines changed: 31 additions & 21 deletions

File tree

module/VuFind/src/VuFind/View/Helper/Root/Context.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131

3232
namespace VuFind\View\Helper\Root;
3333

34-
use Laminas\View\Helper\AbstractHelper;
3534
use Laminas\View\Renderer\RendererInterface;
35+
use VuFind\ServiceManager\Factory\Autowire;
3636

3737
/**
3838
* Context manager (useful for using render() instead of partial() for better
@@ -45,8 +45,19 @@
4545
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
4646
* @link https://vufind.org/wiki/development Wiki
4747
*/
48-
class Context extends AbstractHelper
48+
class Context
4949
{
50+
/**
51+
* Constructor
52+
*
53+
* @param RendererInterface $view View renderer
54+
*/
55+
#[Autowire]
56+
public function __construct(
57+
protected RendererInterface $view
58+
) {
59+
}
60+
5061
/**
5162
* Set an array of variables in the view; return the previous values of those
5263
* variables so they can be restored.
@@ -57,12 +68,10 @@ class Context extends AbstractHelper
5768
*/
5869
public function apply($vars)
5970
{
60-
$view = $this->getView();
61-
6271
$oldVars = [];
6372
foreach ($vars as $k => $v) {
64-
$oldVars[$k] = $view->$k ?? null;
65-
$view->$k = $v;
73+
$oldVars[$k] = $this->view->$k ?? null;
74+
$this->view->$k = $v;
6675
}
6776
return $oldVars;
6877
}
@@ -76,13 +85,11 @@ public function apply($vars)
7685
*/
7786
public function restore($vars)
7887
{
79-
$view = $this->getView();
80-
8188
foreach ($vars as $k => $v) {
8289
if (null === $v) {
83-
unset($view->$k);
90+
unset($this->view->$k);
8491
} else {
85-
$view->$k = $v;
92+
$this->view->$k = $v;
8693
}
8794
}
8895
}
@@ -101,22 +108,22 @@ public function restore($vars)
101108
public function renderInContext($template, $context)
102109
{
103110
$oldContext = $this->apply($context);
104-
$html = $this->getView()->render($template);
111+
$html = $this->view->render($template);
105112
$this->restore($oldContext);
106113
return $html;
107114
}
108115

109116
/**
110-
* Grab the helper object, so we can call methods on it.
117+
* Return this helper instance.
111118
*
112-
* @param ?RendererInterface $view View object to modify.
119+
* @param RendererInterface $view View Renderer
113120
*
114121
* @return Context
115122
*/
116123
public function __invoke(?RendererInterface $view = null)
117124
{
118125
if (null !== $view) {
119-
$this->setView($view);
126+
$this->view = $view;
120127
}
121128
return $this;
122129
}

module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/PermissionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,11 @@ protected function getMockView()
222222
$escapehtml = new \Laminas\View\Helper\EscapeHtml();
223223
$translate = new \VuFind\View\Helper\Root\Translate();
224224
$transEsc = new \VuFind\View\Helper\Root\TransEsc($translate, $escapehtml);
225-
$context = new \VuFind\View\Helper\Root\Context();
226225
$realView = $this->getPhpRenderer(
227-
compact('translate', 'transEsc', 'context', 'escapehtml')
226+
compact('translate', 'transEsc', 'escapehtml')
228227
);
229-
$context->setView($realView);
228+
$context = new \VuFind\View\Helper\Root\Context($realView);
229+
$realView->getHelperPluginManager()->setService('context', $context);
230230
return $realView;
231231
}
232232
}

module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/RecordDataFormatterTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,18 @@ protected function getMockRecordRouter(): MockObject&\VuFind\Record\Router
8181
*/
8282
protected function getViewHelpers(ContainerInterface $container, SchemaOrg $schemaOrgHelper): array
8383
{
84-
$context = new \VuFind\View\Helper\Root\Context();
84+
$translate = new \VuFind\View\Helper\Root\Translate();
85+
$escapeHtml = new \Laminas\View\Helper\EscapeHtml();
86+
$transEsc = new \VuFind\View\Helper\Root\TransEsc($translate, $escapeHtml);
87+
$helperArray = compact('translate', 'transEsc', 'escapeHtml');
88+
$view = $this->getPhpRenderer($helperArray);
89+
$context = new \VuFind\View\Helper\Root\Context($view);
8590
$record = new \VuFind\View\Helper\Root\Record($this->createMock(TagsService::class));
8691
$serviceManager = $this->createMock(\VuFind\Db\Service\PluginManager::class);
8792
$serviceManager->method('get')->willReturnCallback(function ($service) {
8893
return $this->createMock($service);
8994
});
9095
$record->setDbServiceManager($serviceManager);
91-
$translate = new \VuFind\View\Helper\Root\Translate();
9296
return [
9397
'auth' => new \VuFind\View\Helper\Root\Auth(
9498
$this->createMock(\VuFind\Auth\Manager::class),
@@ -119,7 +123,7 @@ protected function getViewHelpers(ContainerInterface $container, SchemaOrg $sche
119123
new \VuFind\Search\Options\PluginManager($container)
120124
),
121125
'searchTabs' => $this->createMock(\VuFind\View\Helper\Root\SearchTabs::class),
122-
'transEsc' => new \VuFind\View\Helper\Root\TransEsc($translate, new \Laminas\View\Helper\EscapeHtml()),
126+
'transEsc' => $transEsc,
123127
'translate' => $translate,
124128
'usertags' => new \VuFind\View\Helper\Root\UserTags(),
125129
];

themes/root/theme.config.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
'VuFind\View\Helper\Root\CookieConsent' => 'VuFind\View\Helper\Root\CookieConsentFactory',
2525
'VuFind\View\Helper\Root\CookieManager' => 'VuFind\View\Helper\Root\CookieManagerFactory',
2626
'VuFind\View\Helper\Root\ContentBlock' => 'Laminas\ServiceManager\Factory\InvokableFactory',
27-
'VuFind\View\Helper\Root\Context' => 'Laminas\ServiceManager\Factory\InvokableFactory',
2827
'VuFind\View\Helper\Root\Csp' => 'VuFind\View\Helper\Root\CspFactory',
2928
'VuFind\View\Helper\Root\CurrentPath' => 'Laminas\ServiceManager\Factory\InvokableFactory',
3029
'VuFind\View\Helper\Root\DateTime' => 'VuFind\View\Helper\Root\DateTimeFactory',

0 commit comments

Comments
 (0)