1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Todd Burry <[email protected]> |
4
|
|
|
* @copyright 2009-2017 Vanilla Forums Inc. |
5
|
|
|
* @license MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Ebi; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class FilesystemLoader implements TemplateLoaderInterface { |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $basePath; |
16
|
|
|
|
17
|
26 |
|
public function __construct($basePath) { |
18
|
26 |
|
$this->basePath = $basePath; |
19
|
26 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Return the cache key of a component. |
23
|
|
|
* |
24
|
|
|
* @param string $component The name of the component. |
25
|
|
|
* @return string Returns the unique key of the component. |
26
|
|
|
*/ |
27
|
26 |
|
public function cacheKey($component) { |
28
|
26 |
|
$subpath = $this->componentPath($component, false); |
29
|
|
|
|
30
|
26 |
|
if (empty($subpath)) { |
31
|
|
|
return null; |
32
|
|
|
} else { |
33
|
26 |
|
return str_replace(DIRECTORY_SEPARATOR, '.', $subpath); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Return the template source of a component. |
39
|
|
|
* |
40
|
|
|
* @param string $component The name of the component. |
41
|
|
|
* @return string Returns the template source of the component. |
|
|
|
|
42
|
|
|
*/ |
43
|
25 |
|
public function load($component) { |
44
|
25 |
|
$path = $this->componentPath($component, true); |
45
|
|
|
|
46
|
25 |
|
if (empty($path)) { |
47
|
|
|
return null; |
48
|
|
|
} else { |
49
|
25 |
|
return file_get_contents($path); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
26 |
|
private function componentPath($component, $full = true) { |
|
|
|
|
54
|
26 |
|
$subpath = str_replace('.', DIRECTORY_SEPARATOR, $component); |
55
|
|
|
|
56
|
|
|
do { |
57
|
26 |
|
$path = "{$this->basePath}/$subpath.html"; |
58
|
26 |
|
if (file_exists($path)) { |
59
|
26 |
|
return $full ? $path : $subpath; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($pos = strrpos($subpath, DIRECTORY_SEPARATOR)) { |
63
|
|
|
$subpath = substr($subpath, 0, $pos - 1); |
64
|
|
|
} else { |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
} while (!empty($subpath)); |
69
|
|
|
|
70
|
|
|
return ''; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the basePath. |
75
|
|
|
* |
76
|
|
|
* @return string Returns the basePath. |
77
|
|
|
*/ |
78
|
|
|
public function getBasePath() { |
79
|
|
|
return $this->basePath; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set the basePath. |
84
|
|
|
* |
85
|
|
|
* @param string $basePath |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function setBasePath($basePath) { |
89
|
|
|
$this->basePath = $basePath; |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.