Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
32.73% |
18 / 55 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
SetupThemeResources | |
32.73% |
18 / 55 |
|
60.00% |
3 / 5 |
128.91 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__invoke | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
addMetaTags | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
addLinks | |
11.54% |
3 / 26 |
|
0.00% |
0 / 1 |
94.76 | |||
addScripts | |
12.50% |
2 / 16 |
|
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 | |
30 | namespace VuFindTheme\View\Helper; |
31 | |
32 | use function count; |
33 | use function in_array; |
34 | use function is_array; |
35 | |
36 | /** |
37 | * View helper for loading theme-related resources. |
38 | * |
39 | * @category VuFind |
40 | * @package View_Helpers |
41 | * @author Demian Katz <demian.katz@villanova.edu> |
42 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
43 | * @link https://vufind.org/wiki/development Wiki |
44 | */ |
45 | class SetupThemeResources extends \Laminas\View\Helper\AbstractHelper |
46 | { |
47 | /** |
48 | * Theme resource container |
49 | * |
50 | * @var \VuFindTheme\ResourceContainer |
51 | */ |
52 | protected $container; |
53 | |
54 | /** |
55 | * Constructor |
56 | * |
57 | * @param \VuFindTheme\ResourceContainer $container Theme resource container |
58 | */ |
59 | public function __construct(\VuFindTheme\ResourceContainer $container) |
60 | { |
61 | $this->container = $container; |
62 | } |
63 | |
64 | /** |
65 | * Set up items based on contents of theme resource container. |
66 | * |
67 | * @param bool $partial Whether rendering an HTML snippet instead of a full page |
68 | * |
69 | * @return void |
70 | */ |
71 | public function __invoke(bool $partial = false) |
72 | { |
73 | // meta tags are illegal outside of <head>, so we don't want to render them |
74 | // in partial mode: |
75 | if (!$partial) { |
76 | $this->addMetaTags(); |
77 | } |
78 | $this->addLinks($partial); |
79 | $this->addScripts(); |
80 | } |
81 | |
82 | /** |
83 | * Add meta tags to header. |
84 | * |
85 | * @return void |
86 | */ |
87 | protected function addMetaTags() |
88 | { |
89 | // Set up encoding: |
90 | $headMeta = $this->getView()->plugin('headMeta'); |
91 | $headMeta()->prependHttpEquiv( |
92 | 'Content-Type', |
93 | 'text/html; charset=' . $this->container->getEncoding() |
94 | ); |
95 | |
96 | // Set up generator: |
97 | $generator = $this->container->getGenerator(); |
98 | if (!empty($generator)) { |
99 | $headMeta()->appendName('Generator', $generator); |
100 | } |
101 | } |
102 | |
103 | /** |
104 | * Add links to header. |
105 | * |
106 | * @param bool $partial Whether rendering an HTML snippet instead of a full page |
107 | * |
108 | * @return void |
109 | */ |
110 | protected function addLinks(bool $partial = false) |
111 | { |
112 | // Convenient shortcut to view helper: |
113 | $headLink = $this->getView()->plugin('headLink'); |
114 | |
115 | // Load CSS (make sure we prepend them in the appropriate order; theme |
116 | // resources should load before extras added by individual templates): |
117 | foreach (array_reverse($this->container->getCss()) as $current) { |
118 | $parts = $this->container->parseSetting($current); |
119 | // Special case for media with parentheses |
120 | // ie. (min-width: 768px) |
121 | if (count($parts) > 1 && str_starts_with($parts[1], '(')) { |
122 | $parts[1] .= ':' . $parts[2]; |
123 | array_splice($parts, 2, 1); |
124 | } |
125 | $headLink()->forcePrependStylesheet( |
126 | trim($parts[0]), |
127 | isset($parts[1]) ? trim($parts[1]) : 'all', |
128 | isset($parts[2]) ? trim($parts[2]) : false |
129 | ); |
130 | } |
131 | |
132 | // Insert link elements for favicons specified in the `favicons` property of theme.config.php. |
133 | // If `favicon` is a string then treat it as a single file path to an .ico icon. |
134 | // If `favicon` is an array then treat each item as an assoc array of html attributes and render |
135 | // a link element for each. |
136 | // Skip favicons in partial mode because they are illegal outside of <head>. |
137 | if (!$partial && ($favicon = $this->container->getFavicon())) { |
138 | $imageLink = $this->getView()->plugin('imageLink'); |
139 | if (is_array($favicon)) { |
140 | foreach ($favicon as $attrs) { |
141 | if (isset($attrs['href'])) { |
142 | $attrs['href'] = $imageLink($attrs['href']); |
143 | } |
144 | $attrs['rel'] ??= 'icon'; |
145 | $headLink($attrs); |
146 | } |
147 | } else { |
148 | $headLink( |
149 | [ |
150 | 'href' => $imageLink($favicon), |
151 | 'type' => 'image/x-icon', |
152 | 'rel' => 'icon', |
153 | ] |
154 | ); |
155 | } |
156 | } |
157 | } |
158 | |
159 | /** |
160 | * Add scripts to header or footer. |
161 | * |
162 | * @return void |
163 | */ |
164 | protected function addScripts() |
165 | { |
166 | $legalHelpers = ['footScript', 'headScript']; |
167 | |
168 | // Load Javascript (same ordering considerations as CSS, above): |
169 | $js = array_reverse($this->container->getJs()); |
170 | |
171 | foreach ($js as $current) { |
172 | $position = $current['position'] ?? 'header'; |
173 | $helper = substr($position, 0, 4) . 'Script'; |
174 | if (!in_array($helper, $legalHelpers)) { |
175 | throw new \Exception( |
176 | 'Invalid script position for ' |
177 | . $current['file'] . ': ' . $position . '.' |
178 | ); |
179 | } |
180 | |
181 | $this->getView() |
182 | ->plugin($helper) |
183 | ->forcePrependFile( |
184 | $current['file'], |
185 | 'text/javascript', |
186 | $current['attributes'] ?? [] |
187 | ); |
188 | } |
189 | } |
190 | } |