1 | <?php |
||
11 | class PHP implements ViewInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $path; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $ext = 'php'; |
||
22 | |||
23 | /** |
||
24 | * Class constructor |
||
25 | * |
||
26 | * @param array $options |
||
27 | */ |
||
28 | 14 | public function __construct(array $options) |
|
29 | { |
||
30 | 14 | if (!isset($options['path'])) { |
|
31 | 1 | throw new \BadMethodCallException("'path' option is required"); |
|
32 | } |
||
33 | |||
34 | 14 | $this->path = $options['path']; |
|
35 | |||
36 | 14 | if (isset($options['ext'])) { |
|
37 | 2 | $this->ext = $options['ext']; |
|
38 | 2 | } |
|
39 | 14 | } |
|
40 | |||
41 | /** |
||
42 | * Get directory path. |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | 2 | public function getPath() |
|
50 | |||
51 | /** |
||
52 | * Get the default file extension |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | 7 | public function getExt() |
|
60 | |||
61 | |||
62 | /** |
||
63 | * Expose a function to the view. |
||
64 | * |
||
65 | * @param string $name Function name in template |
||
66 | * @param callable|null $function |
||
67 | * @return $this |
||
68 | * @throws \BadMethodCallException if function can't be exposed |
||
69 | */ |
||
70 | 4 | public function expose($name, $function = null) |
|
78 | |||
79 | |||
80 | /** |
||
81 | * Get the path to a view file |
||
82 | * |
||
83 | * @param string $name |
||
84 | * @return string |
||
85 | */ |
||
86 | 6 | protected function getFilePath($name) |
|
87 | { |
||
88 | 6 | if (pathinfo($name, PATHINFO_EXTENSION) === '') { |
|
89 | 5 | $name .= (substr($name, -1) === '/' ? 'index' : '') . '.' . $this->getExt(); |
|
90 | 5 | } |
|
91 | |||
92 | 6 | return rtrim($this->path, '/') . '/' . ltrim($name, '/'); |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * Assert the filename and that the file exists |
||
97 | * |
||
98 | * @param string $name |
||
99 | * @throws \InvalidArgumentException |
||
100 | */ |
||
101 | 7 | protected function assertFile($name) |
|
113 | |||
114 | /** |
||
115 | * Run the PHP script |
||
116 | * |
||
117 | * @param string $name |
||
118 | * @param array $context |
||
119 | */ |
||
120 | 5 | protected function runScript($name, array $context) |
|
133 | |||
134 | /** |
||
135 | * Render and output template |
||
136 | * |
||
137 | * @param ResponseInterface $response |
||
138 | * @param string $name Template name |
||
139 | * @param array $context Template context as associated array |
||
140 | * @return ResponseInterface |
||
141 | */ |
||
142 | 7 | public function render(ResponseInterface $response, $name, array $context = []) |
|
164 | } |
||
165 |