/
System.php
126 lines (108 loc) · 3.67 KB
/
System.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Gajus\Brick;
/**
* Responsible for template resolution and scope management.
*
* @link https://github.com/gajus/brick for the canonical source repository
* @license https://github.com/gajus/brick/blob/master/LICENSE BSD 3-Clause
*/
class System {
private
/**
* @var string Path to templates directory.
*/
$directory,
/**
* @var array Variables shared across all templates.
*/
$globals = [],
/**
* @var string Extension of the template files.
*/
$template_extension = '.php';
/**
* @param string $directory Path to the template directory.
* @param array $globals Variables shared across all templates.
*/
public function __construct ($directory, array $globals = []) {
$this->setDirectory($directory);
$this->setGlobals($globals);
}
/**
* Template resolution is restricted to the paths under the template directory.
*
* @param string $directory Absolute path to the template directory.
*/
public function setDirectory ($directory) {
if (strpos($directory, '/') !== 0) {
throw new Exception\InvalidArgumentException('Directory name must be an absolute path.');
}
if (!is_dir($directory)) {
throw new Exception\LogicException('Template directory does not exist.');
}
$this->directory = realpath($directory);
}
/**
* Get the template resolution base path.
*
* @return string
*/
public function getDirectory () {
return $this->directory;
}
/**
* @param array $globals
*/
public function setGlobals (array $globals) {
$this->globals = $globals;
}
/**
* @return array
*/
public function getGlobals () {
return $this->globals;
}
/**
* @param string $template_extension
*/
public function setTemplateExtension ($template_extension) {
$this->template_extension = $template_extension;
}
/**
* @return string
*/
public function getTemplateExtension () {
return $this->template_extension;
}
/**
* @param string $name Template file (excluding the file extension) relative to the templates directory.
* @param array $scope Variables populated in the template scope.
* @return string
*/
public function view ($name, array $scope = []) {
return $this->template($name, $scope)->render();
}
/**
* @param string $name Template file (excluding the file extension) relative to the templates directory.
* @param array $scope Variables populated in the template scope.
* @return Gajus\Brick\Template
*/
public function template ($name, array $scope = []) {
$file = realpath($this->directory . '/' . $name . $this->getTemplateExtension());
if (!$file) {
throw new Exception\LogicException('Template ("' . $name . '") does not exist.');
}
if (mb_strpos($file, $this->directory) !== 0) {
throw new Exception\InvalidArgumentException('Directory traversal attempt.');
}
if (array_key_exists('globals', $scope) && $scope['globals'] !== $this->globals) {
throw new Exception\InvalidArgumentException('"globals" variable name is reserved.');
}
if (array_key_exists('system', $scope) && $scope['system'] !== $this) {
throw new Exception\InvalidArgumentException('"system" variable name is reserved.');
}
$scope['globals'] = $this->getGlobals();
$scope['system'] = new Subsystem($this);
return new Template($file, $scope);
}
}