Completed
Pull Request — master (#58)
by
unknown
09:57
created

Loader::getSourceContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of TwigView.
5
 *
6
 ** (c) 2014 Cees-Jan Kiewiet
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
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
    /**
25
     * Get the file contents of a template.
26
     *
27
     * @param string $name Template.
28
     *
29
     * @return string
30
     */
31 4
    public function getSource($name)
32
    {
33 4
        $name = $this->resolveFileName($name);
34 3
35
        return file_get_contents($name);
36
    }
37
38
    /**
39
     * Returns the source context for a given template logical name.
40
     *
41
     * @param string $name The template logical name.
42
     *
43
     * @return Twig_Source
44 5
     *
45
     * @throws Twig_Error_Loader When $name is not found
46 5
     */
47
    public function getSourceContext($name)
48
    {
49
        $code = $this->getSource($name);
50
        $path = $this->getFilename($name);
51
52
        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 50 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...
53
    }
54
55
    /**
56
     * Get cache key for template.
57 2
     *
58
     * @param string $name Template.
59 2
     *
60 1
     * @return string
61
     */
62
    public function getCacheKey($name)
63
    {
64
        return $this->resolveFileName($name);
65
    }
66
67
    /**
68
     * Check if template is still fresh.
69
     *
70
     * @param string  $name Template.
71
     * @param integer $time Timestamp.
72
     *
73 9
     * @return boolean
74
     */
75
    public function isFresh($name, $time)
76 9
    {
77 3
        $name = $this->resolveFileName($name);
78
79
        return filemtime($name) < $time;
80 6
    }
81
82 6
    /**
83 6
     * Check if we have the source code of a template, given its name.
84
     *
85 6
     * @param string $name The name of the template to check if we can load.
86 6
     *
87 6
     * @return bool If the template source code is handled by this loader or not.
88 6
     */
89 2
    public function exists($name)
90
    {
91
        $filename = $this->getFilename($name);
92 6
        if ($filename === false) {
93 6
            return false;
94 6
        }
95
96
        return true;
97
    }
98
99 3
    /**
100
     * Resolve template name to filename.
101
     *
102
     * @param string $name Template.
103
     *
104
     * @return string
105
     *
106
     * @throws \Twig_Error_Loader Thrown when template file isn't found.
107
     */
108
    protected function resolveFileName($name)
109 6
    {
110
        $filename = $this->getFilename($name);
111 6
        if ($filename === false) {
112 6
            throw new \Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
113
        }
114
115 4
        return $filename;
116
    }
117
118
    /**
119
     * Get template filename
120
     *
121
     * @param string $name Template.
122
     *
123
     * @return string|false
124
     *
125
     */
126
    protected function getFilename($name)
127
    {
128
        if (file_exists($name)) {
129
            return $name;
130
        }
131
132
        list($plugin, $file) = pluginSplit($name);
133
        foreach ([null, $plugin] as $scope) {
134
            $paths = $this->getPaths($scope);
135
            foreach ($paths as $path) {
136
                $filePath = $path . $file;
137
                if (file_exists($filePath)) {
138
                    return $filePath;
139
                }
140
141
                $filePath = $path . $file . TwigView::EXT;
142
                if (file_exists($filePath)) {
143
                    return $filePath;
144
                }
145
            }
146
        }
147
148
        return false;
149
    }
150
151
    /**
152
     * Check if $plugin is active and return it's template paths or return the aps template paths.
153
     *
154
     * @param string|null $plugin The plugin in question.
155
     *
156
     * @return array
157
     */
158
    protected function getPaths($plugin)
159
    {
160
        if ($plugin === null || !Plugin::loaded($plugin)) {
161
            return App::path('Template');
162
        }
163
164
        return App::path('Template', $plugin);
165
    }
166
}
167