FileArrayLoader::getFileName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace RamRacing\Mustache;
4
5
use Mustache_Exception_UnknownTemplateException;
6
use Mustache_Loader;
7
8
class FileArrayLoader implements Mustache_Loader
9
{
10
    /**
11
     * @property string
12
     */
13
    protected $base_dir;
14
15
    /**
16
     * @property string
17
     */
18
    protected $extension = '.mustache';
19
    
20
    /**
21
     * @property array
22
     */
23
    protected $files;
24
25
    /**
26
     * @param array $files
27
     */
28
    public function __construct(array $files, array $options = array())
29
    {
30
        $this->files = $files;
31
        
32
        if (array_key_exists('base_dir', $options)) {
33
            $this->base_dir = $options['base_dir'];
34
        }
35
        
36
        if (array_key_exists('extension', $options)) {
37
            if (empty($options['extension'])) {
38
                $this->extension = '';
39
            } else {
40
                $this->extension = '.' . ltrim($options['extension'], '.');
41
            }
42
        }
43
    }
44
45
    /**
46
     * Load a Template by name.
47
     *
48
     * @throws Mustache_Exception_UnknownTemplateException If a template file is not found.
49
     *
50
     * @param string $name
51
     *
52
     * @return string Mustache Template source
53
     */
54
    public function load($name)
55
    {
56
        $key = $this->getFileName($name);
57
        
58
        if (!isset($this->files[$key]) || !file_exists($this->files[$key])) {
59
            throw new Mustache_Exception_UnknownTemplateException($name);
60
        }
61
62
        return file_get_contents($this->files[$key]);
63
    }
64
    
65
    /**
66
     * @param string $name
67
     */
68
    protected function getFileName($name)
69
    {
70
        $file = $name . $this->extension;
71
        
72
        if ($this->base_dir) {
73
            $file = $this->base_dir . '/' . $key;
0 ignored issues
show
Bug introduced by
The variable $key does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
74
        }
75
        
76
        return $file;
77
    }
78
}
79