Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Context
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 apply
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 restore
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 renderInContext
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Context manager (useful for using render() instead of partial() for better
5 * performance -- this allows us to set and roll back variables in the global
6 * scope instead of relying on the overhead of building a whole new scope).
7 *
8 * PHP version 8
9 *
10 * Copyright (C) Villanova University 2010.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2,
14 * as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24 *
25 * @category VuFind
26 * @package  View_Helpers
27 * @author   Demian Katz <demian.katz@villanova.edu>
28 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
29 * @link     https://vufind.org/wiki/development Wiki
30 */
31
32namespace VuFind\View\Helper\Root;
33
34use Laminas\View\Helper\AbstractHelper;
35use Laminas\View\Renderer\RendererInterface;
36
37/**
38 * Context manager (useful for using render() instead of partial() for better
39 * performance -- this allows us to set and roll back variables in the global
40 * scope instead of relying on the overhead of building a whole new scope).
41 *
42 * @category VuFind
43 * @package  View_Helpers
44 * @author   Demian Katz <demian.katz@villanova.edu>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org/wiki/development Wiki
47 */
48class Context extends AbstractHelper
49{
50    /**
51     * Set an array of variables in the view; return the previous values of those
52     * variables so they can be restored.
53     *
54     * @param array $vars Variables to set
55     *
56     * @return array
57     */
58    public function apply($vars)
59    {
60        $view = $this->getView();
61
62        $oldVars = [];
63        foreach ($vars as $k => $v) {
64            $oldVars[$k] = $view->$k ?? null;
65            $view->$k = $v;
66        }
67        return $oldVars;
68    }
69
70    /**
71     * Restore an old context returned by apply().
72     *
73     * @param array $vars Variables to set
74     *
75     * @return void
76     */
77    public function restore($vars)
78    {
79        $view = $this->getView();
80
81        foreach ($vars as $k => $v) {
82            if (null === $v) {
83                unset($view->$k);
84            } else {
85                $view->$k = $v;
86            }
87        }
88    }
89
90    /**
91     * Render a template using a temporary context; restore the view to its
92     * original state when done. This offers the template full access to the
93     * global scope, modified by $context, then puts the global scope back
94     * the way it was.
95     *
96     * @param string $template Template to render
97     * @param array  $context  Array of context variables to set temporarily
98     *
99     * @return string          Rendered template output
100     */
101    public function renderInContext($template, $context)
102    {
103        $oldContext = $this->apply($context);
104        $html = $this->getView()->render($template);
105        $this->restore($oldContext);
106        return $html;
107    }
108
109    /**
110     * Grab the helper object, so we can call methods on it.
111     *
112     * @param ?RendererInterface $view View object to modify.
113     *
114     * @return Context
115     */
116    public function __invoke(RendererInterface $view = null)
117    {
118        if (null !== $view) {
119            $this->setView($view);
120        }
121        return $this;
122    }
123}