Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
34.62% covered (danger)
34.62%
18 / 52
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SetupThemeResources
34.62% covered (danger)
34.62%
18 / 52
60.00% covered (warning)
60.00%
3 / 5
87.56
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 addMetaTags
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 addLinks
13.04% covered (danger)
13.04%
3 / 23
0.00% covered (danger)
0.00%
0 / 1
50.08
 addScripts
12.50% covered (danger)
12.50%
2 / 16
0.00% covered (danger)
0.00%
0 / 1
9.03
1<?php
2
3/**
4 * View helper for loading theme-related resources.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  View_Helpers
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFindTheme\View\Helper;
31
32use function in_array;
33use function is_array;
34
35/**
36 * View helper for loading theme-related resources.
37 *
38 * @category VuFind
39 * @package  View_Helpers
40 * @author   Demian Katz <demian.katz@villanova.edu>
41 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
42 * @link     https://vufind.org/wiki/development Wiki
43 */
44class SetupThemeResources extends \Laminas\View\Helper\AbstractHelper
45{
46    /**
47     * Theme resource container
48     *
49     * @var \VuFindTheme\ResourceContainer
50     */
51    protected $container;
52
53    /**
54     * Constructor
55     *
56     * @param \VuFindTheme\ResourceContainer $container Theme resource container
57     */
58    public function __construct(\VuFindTheme\ResourceContainer $container)
59    {
60        $this->container = $container;
61    }
62
63    /**
64     * Set up items based on contents of theme resource container.
65     *
66     * @param bool $partial Whether rendering an HTML snippet instead of a full page
67     *
68     * @return void
69     */
70    public function __invoke(bool $partial = false)
71    {
72        // meta tags are illegal outside of <head>, so we don't want to render them
73        // in partial mode:
74        if (!$partial) {
75            $this->addMetaTags();
76        }
77        $this->addLinks($partial);
78        $this->addScripts();
79    }
80
81    /**
82     * Add meta tags to header.
83     *
84     * @return void
85     */
86    protected function addMetaTags()
87    {
88        // Set up encoding:
89        $headMeta = $this->getView()->plugin('headMeta');
90        $headMeta()->prependHttpEquiv(
91            'Content-Type',
92            'text/html; charset=' . $this->container->getEncoding()
93        );
94
95        // Set up generator:
96        $generator = $this->container->getGenerator();
97        if (!empty($generator)) {
98            $headMeta()->appendName('Generator', $generator);
99        }
100    }
101
102    /**
103     * Add links to header.
104     *
105     * @param bool $partial Whether rendering an HTML snippet instead of a full page
106     *
107     * @return void
108     */
109    protected function addLinks(bool $partial = false)
110    {
111        // Convenient shortcut to view helper:
112        $headLink = $this->getView()->plugin('headLink');
113
114        // Load CSS (make sure we prepend them in the appropriate order; theme
115        // resources should load before extras added by individual templates):
116        foreach (array_reverse($this->container->getCss()) as $current) {
117            $headLink()->forcePrependStylesheet(
118                $current['file'],
119                empty($current['media']) ? 'all' : $current['media'],
120                $current['conditional'] ?? '',
121                $current['extras'] ?? []
122            );
123        }
124
125        // Insert link elements for favicons specified in the `favicons` property of theme.config.php.
126        // If `favicon` is a string then treat it as a single file path to an .ico icon.
127        // If `favicon` is an array then treat each item as an assoc array of html attributes and render
128        // a link element for each.
129        // Skip favicons in partial mode because they are illegal outside of <head>.
130        if (!$partial && ($favicon = $this->container->getFavicon())) {
131            $imageLink = $this->getView()->plugin('imageLink');
132            if (is_array($favicon)) {
133                foreach ($favicon as $attrs) {
134                    if (isset($attrs['href'])) {
135                        $attrs['href'] = $imageLink($attrs['href']);
136                    }
137                    $attrs['rel'] ??= 'icon';
138                    $headLink($attrs);
139                }
140            } else {
141                $headLink(
142                    [
143                        'href' => $imageLink($favicon),
144                        'type' => 'image/x-icon',
145                        'rel' => 'icon',
146                    ]
147                );
148            }
149        }
150    }
151
152    /**
153     * Add scripts to header or footer.
154     *
155     * @return void
156     */
157    protected function addScripts()
158    {
159        $legalHelpers = ['footScript', 'headScript'];
160
161        // Load Javascript (same ordering considerations as CSS, above):
162        $js = array_reverse($this->container->getJs());
163
164        foreach ($js as $current) {
165            $position = $current['position'] ?? 'header';
166            $helper = substr($position, 0, 4) . 'Script';
167            if (!in_array($helper, $legalHelpers)) {
168                throw new \Exception(
169                    'Invalid script position for '
170                    . $current['file'] . ': ' . $position . '.'
171                );
172            }
173
174            $this->getView()
175                ->plugin($helper)
176                ->forcePrependFile(
177                    $current['file'],
178                    'text/javascript',
179                    $current['attributes'] ?? []
180                );
181        }
182    }
183}