Completed
Pull Request — master (#58)
by
unknown
08:40
created

Loader::resolveFileName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 1
b 1
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of TwigView.
4
 *
5
 ** (c) 2014 Cees-Jan Kiewiet
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace WyriHaximus\TwigView\Lib\Twig;
12
13
use Cake\Core\App;
14
use Cake\Core\Plugin;
15
use WyriHaximus\TwigView\View\TwigView;
16
17
/**
18
 * Class Loader.
19
 * @package WyriHaximus\TwigView\Lib\Twig
20
 */
21
class Loader implements \Twig_LoaderInterface, \Twig_SourceContextLoaderInterface
22
{
23
    /**
24
     * Get the file contents of a template.
25
     *
26
     * @param string $name Template.
27
     *
28
     * @return string
29
     */
30
    public function getSource($name)
31 4
    {
32
        $name = $this->resolveFileName($name);
33 4
34 3
        return file_get_contents($name);
35
    }
36
37
    /**
38
     * Returns the source context for a given template logical name.
39
     *
40
     * @param string $name The template logical name.
41
     *
42
     * @throws Twig_Error_Loader When $name is not found
43
     * @return Twig_Source
44 5
     *
45
     */
46 5
    public function getSourceContext($name)
47
    {
48
        $code = $this->getSource($name);
49
        $path = $this->getFilename($name);
50
51
        return new \Twig_Source($code, $name, $path);
0 ignored issues
show
Security Bug introduced by
It seems like $path defined by $this->getFilename($name) on line 49 can also be of type false; however, Twig_Source::__construct() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
52
    }
53
54
    /**
55
     * Get cache key for template.
56
     *
57 2
     * @param string $name Template.
58
     *
59 2
     * @return string
60 1
     */
61
    public function getCacheKey($name)
62
    {
63
        return $this->resolveFileName($name);
64
    }
65
66
    /**
67
     * Check if template is still fresh.
68
     *
69
     * @param string $name Template.
70
     * @param int    $time Timestamp.
71
     *
72
     * @return bool
73 9
     */
74
    public function isFresh($name, $time)
75
    {
76 9
        $name = $this->resolveFileName($name);
77 3
78
        return filemtime($name) < $time;
79
    }
80 6
81
    /**
82 6
     * Check if we have the source code of a template, given its name.
83 6
     *
84
     * @param string $name The name of the template to check if we can load.
85 6
     *
86 6
     * @return bool If the template source code is handled by this loader or not.
87 6
     */
88 6
    public function exists($name)
89 2
    {
90
        $filename = $this->getFilename($name);
91
        if ($filename === false) {
92 6
            return false;
93 6
        }
94 6
95
        return true;
96
    }
97
98
    /**
99 3
     * Resolve template name to filename.
100
     *
101
     * @param string $name Template.
102
     *
103
     * @throws \Twig_Error_Loader Thrown when template file isn't found.
104
     * @return string
105
     *
106
     */
107
    protected function resolveFileName($name)
108
    {
109 6
        $filename = $this->getFilename($name);
110
        if ($filename === false) {
111 6
            throw new \Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
112 6
        }
113
114
        return $filename;
115 4
    }
116
117
    /**
118
     * Get template filename.
119
     *
120
     * @param string $name Template.
121
     *
122
     * @return string|false
123
     *
124
     */
125
    protected function getFilename($name)
126
    {
127
        if (file_exists($name)) {
128
            return $name;
129
        }
130
131
        list($plugin, $file) = pluginSplit($name);
132
        foreach ([null, $plugin] as $scope) {
133
            $paths = $this->getPaths($scope);
134
            foreach ($paths as $path) {
135
                $filePath = $path . $file;
136
                if (file_exists($filePath)) {
137
                    return $filePath;
138
                }
139
140
                $filePath = $path . $file . TwigView::EXT;
141
                if (file_exists($filePath)) {
142
                    return $filePath;
143
                }
144
            }
145
        }
146
147
        return false;
148
    }
149
150
    /**
151
     * Check if $plugin is active and return it's template paths or return the aps template paths.
152
     *
153
     * @param string|null $plugin The plugin in question.
154
     *
155
     * @return array
156
     */
157
    protected function getPaths($plugin)
158
    {
159
        if ($plugin === null || !Plugin::loaded($plugin)) {
160
            return App::path('Template');
161
        }
162
163
        return App::path('Template', $plugin);
164
    }
165
}
166