Completed
Push — 4.x ( 63a5bc...f9be3b )
by Cees-Jan
12:23
created

Loader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 111
ccs 30
cts 33
cp 0.9091
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSource() 0 5 1
A getCacheKey() 0 4 1
A isFresh() 0 5 1
B resolveFileName() 0 28 6
A getPaths() 0 8 3
A exists() 0 6 1
A getSourceContext() 0 6 1
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 Twig_Error_Loader;
16
use Twig_Source;
17
use WyriHaximus\TwigView\View\TwigView;
18
19
/**
20
 * Class Loader
21
 * @package WyriHaximus\TwigView\Lib\Twig
22
 */
23
class Loader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface, \Twig_SourceContextLoaderInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Twig_ExistsLoaderInterface has been deprecated with message: since 1.12 (to be removed in 3.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
Deprecated Code introduced by
The interface Twig_SourceContextLoaderInterface has been deprecated with message: since 1.27 (to be removed in 3.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
24
{
25
26
    /**
27
     * Get the file contents of a template.
28
     *
29
     * @param string $name Template.
30
     *
31
     * @return string
32
     */
33 2
    public function getSource($name)
34
    {
35 2
        $name = $this->resolveFileName($name);
36 1
        return file_get_contents($name);
37
    }
38
39
    /**
40
     * Get cache key for template.
41
     *
42
     * @param string $name Template.
43
     *
44
     * @return string
45
     */
46 13
    public function getCacheKey($name)
47
    {
48 13
        return $this->resolveFileName($name);
49
    }
50
51
    /**
52
     * Check if template is still fresh.
53
     *
54
     * @param string  $name Template.
55
     * @param integer $time Timestamp.
56
     *
57
     * @return boolean
58
     */
59 2
    public function isFresh($name, $time)
60
    {
61 2
        $name = $this->resolveFileName($name);
62 1
        return filemtime($name) < $time;
63
    }
64
65
    /**
66
     * Resolve template name to filename.
67
     *
68
     * @param string $name Template.
69
     *
70
     * @return string
71
     *
72
     * @throws \Twig_Error_Loader Thrown when template file isn't found.
73
     */
74
    // @codingStandardsIgnoreStart
75 17
    protected function resolveFileName($name)
76
    {
77
        // @codingStandardsIgnoreEnd
78 17
        if (file_exists($name)) {
79 11
            return $name;
80
        }
81
82 6
        list($plugin, $file) = pluginSplit($name);
83
        foreach ([
84 6
            null,
85 6
            $plugin,
86
        ] as $scope) {
87 6
            $paths = $this->getPaths($scope);
88 6
            foreach ($paths as $path) {
89 6
                $filePath = $path . $file;
90 6
                if (file_exists($filePath)) {
91 2
                    return $filePath;
92
                }
93
94 6
                $filePath = $path . $file . TwigView::EXT;
95 6
                if (file_exists($filePath)) {
96 6
                    return $filePath;
97
                }
98
            }
99
        }
100
101 3
        throw new \Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
102
    }
103
104
    /**
105
     * Check if $plugin is active and return it's template paths or return the aps template paths.
106
     *
107
     * @param string|null $plugin The plugin in question.
108
     *
109
     * @return array
110
     */
111 6
    protected function getPaths($plugin)
112
    {
113 6
        if ($plugin === null || !Plugin::loaded($plugin)) {
114 6
            return App::path('Template');
115
        }
116
117 4
        return App::path('Template', $plugin);
118
    }
119
120
    public function exists($name)
121
    {
122
        $name = $this->resolveFileName($name);
123
124
        return file_exists($name);
125
    }
126
127 6
    public function getSourceContext($name)
128
    {
129 6
        $path = $this->resolveFileName($name);
130
131 6
        return new Twig_Source(file_get_contents($path), $name, $path);
132
    }
133
}
134