Completed
Push — finish-work-from-PR-37 ( 9a2843 )
by Cees-Jan
02:10
created

Loader::resolveFileName()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 28
ccs 18
cts 18
cp 1
rs 8.439
cc 6
eloc 17
nc 6
nop 1
crap 6
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
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
        return file_get_contents($name);
35
    }
36
37
    /**
38
     * Get cache key for template.
39
     *
40
     * @param string $name Template.
41
     *
42
     * @return string
43
     */
44 5
    public function getCacheKey($name)
45
    {
46 5
        return $this->resolveFileName($name);
47
    }
48
49
    /**
50
     * Check if template is still fresh.
51
     *
52
     * @param string  $name Template.
53
     * @param integer $time Timestamp.
54
     *
55
     * @return boolean
56
     */
57 2
    public function isFresh($name, $time)
58
    {
59 2
        $name = $this->resolveFileName($name);
60 1
        return filemtime($name) < $time;
61
    }
62
63
    /**
64
     * Resolve template name to filename.
65
     *
66
     * @param string $name Template.
67
     *
68
     * @return string
69
     *
70
     * @throws \Twig_Error_Loader Thrown when template file isn't found.
71
     */
72
    // @codingStandardsIgnoreStart
73 9
    protected function resolveFileName($name)
74
    {
75
        // @codingStandardsIgnoreEnd
76 9
        if (file_exists($name)) {
77 3
            return $name;
78
        }
79
80 6
        list($plugin, $file) = pluginSplit($name);
81
        foreach ([
82 6
            null,
83 6
            $plugin,
84 6
        ] as $scope) {
85 6
            $paths = $this->getPaths($scope);
86 6
            foreach ($paths as $path) {
87 6
                $filePath = $path . $file;
88 6
                if (file_exists($filePath)) {
89 2
                    return $filePath;
90
                }
91
92 6
                $filePath = $path . $file . TwigView::EXT;
93 6
                if (file_exists($filePath)) {
94 3
                    return $filePath;
95
                }
96 5
            }
97 5
        }
98
99 3
        throw new \Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
100
    }
101
102
    /**
103
     * Check if $plugin is active and return it's template paths or return the aps template paths.
104
     *
105
     * @param string|null $plugin The plugin in question.
106
     *
107
     * @return array
108
     */
109 6
    protected function getPaths($plugin)
110
    {
111 6
        if ($plugin === null || !Plugin::loaded($plugin)) {
112 6
            return App::path('Template');
113
        }
114
115 4
        return App::path('Template', $plugin);
116
    }
117
}
118