Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
TemplatePath
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Helper to get path to a template from another theme (for including)
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2019.
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   Chris Hallberg <challber@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 Laminas\View\Resolver\TemplatePathStack;
33
34/**
35 * Helper to get path to a template from another theme (for including)
36 *
37 * @category VuFind
38 * @package  View_Helpers
39 * @author   Chris Hallberg <challber@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development Wiki
42 */
43class TemplatePath extends \Laminas\View\Helper\AbstractHelper
44{
45    /**
46     * Absolute path up to the theme name
47     *
48     * @var string
49     */
50    protected $pathPre;
51
52    /**
53     * Absolute path after the theme name
54     *
55     * @var string
56     */
57    protected $pathPost;
58
59    /**
60     * Template path stack
61     *
62     * @var TemplatePathStack
63     */
64    protected $templatePathStack;
65
66    /**
67     * Constructor
68     *
69     * @param TemplatePathStack $templateStack Inheritance stack of template paths
70     */
71    public function __construct($templateStack)
72    {
73        $this->templatePathStack = $templateStack;
74        // get current theme path
75        $paths = $this->templatePathStack->getPaths();
76        // split for easy replacement later
77        preg_match('/\/themes\/([^\/]+)/', $paths->current(), $matches);
78        [$this->pathPre, $this->pathPost]
79            = explode($matches[1], $paths->current());
80    }
81
82    /**
83     * Returns an template path according the configured theme
84     *
85     * @param string $template    template name like 'footer.phtml'
86     * @param string $targetTheme template to pull the template from
87     *
88     * @return string path, null if image not found
89     * @throws \Exception if no file exists at path
90     */
91    public function __invoke($template, $targetTheme)
92    {
93        $path = $this->pathPre . $targetTheme . $this->pathPost . $template;
94        if (!file_exists($path)) {
95            throw new \Exception(
96                'Template not found in ' . $targetTheme . ': ' . $template
97            );
98        }
99        return $path;
100    }
101}