ViewFactory::addPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
nc 1
cc 1
eloc 2
nop 1
1
<?php
2
3
namespace Air\View;
4
5
abstract class ViewFactory implements ViewFactoryInterface
6
{
7
    /**
8
     * @var array $viewPaths An array of paths to search for views in.
9
     */
10
    protected $viewPaths = [];
11
12
13
    /**
14
     * @var string $fileExtension The file extension to use when looking for views.
15
     */
16
    protected $fileExtension = 'php';
17
18
19
    /**
20
     * @var string|null $cacheDir A directory to cache rendered templates into (enables caching).
21
     */
22
    protected $cacheDir = null;
23
24
25
    /**
26
     * @var string|null $partialsDir A directory where static partials are stored.
27
     */
28
    protected $partialsDir = null;
29
30
31
    /**
32
     * @param string $fileName The name of the file to load.
33
     * @return ViewInterface A view.
34
     */
35
    abstract public function get($fileName);
36
37
38
    /**
39
     * @param array $viewPaths An array of paths to look for view files in.
40
     */
41
    public function registerPaths(array $viewPaths)
42
    {
43
        $this->viewPaths = $viewPaths;
44
    }
45
46
47
    /**
48
     * @param string $viewPath A view path to check.
49
     */
50
    public function addPath($viewPath)
51
    {
52
        $this->viewPaths[] = $viewPath;
53
    }
54
55
56
    /**
57
     * Locates a view file and returns its full path.
58
     *
59
     * @param string $fileName The file name.
60
     * @return string|false The location of the file, or false if it cannot be found.
61
     * @throws \Exception
62
     */
63
    protected function find($fileName)
64
    {
65
        $fileName = $fileName . '.' . $this->fileExtension;
66
67
        foreach ($this->viewPaths as $path) {
68
            if (file_exists($path . $fileName)) {
69
                return realpath($path . $fileName);
70
            }
71
        }
72
73
        throw new \Exception("View $fileName not found.");
74
    }
75
}
76