|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* MIT License |
|
4
|
|
|
|
|
5
|
|
|
Copyright (c) 2018 Eridan Domoratskiy |
|
6
|
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
in the Software without restriction, including without limitation the rights |
|
10
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
furnished to do so, subject to the following conditions: |
|
13
|
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
copies or substantial portions of the Software. |
|
16
|
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
SOFTWARE. */ |
|
24
|
|
|
|
|
25
|
|
|
namespace ProgMinerUtils\TemplateRenderer; |
|
26
|
|
|
|
|
27
|
|
|
use Symfony\Component\Config\FileLocator; |
|
28
|
|
|
|
|
29
|
|
|
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Template renderer for PHP templates |
|
33
|
|
|
* |
|
34
|
|
|
* @author Eridan Domoratskiy |
|
35
|
|
|
*/ |
|
36
|
|
|
class PhpTemplateRenderer implements ITemplateRenderer { |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var FileLocator File locator for template files |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $fileLocator; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param FileLocator $fileLocator File locator for template files |
|
45
|
|
|
*/ |
|
46
|
8 |
|
public function __construct(FileLocator $fileLocator) { |
|
47
|
8 |
|
$this->fileLocator = $fileLocator; |
|
48
|
8 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
6 |
|
public function render(string $template, array $variables = []): string { |
|
54
|
6 |
|
if (!$this->supports($template)) { |
|
55
|
4 |
|
throw new \UnexpectedValueException("Template \"$template\" is not supported"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
6 |
|
$file = $this->fileLocator->locate($template, null, true); |
|
59
|
|
|
|
|
60
|
6 |
|
ob_start(); |
|
61
|
|
|
|
|
62
|
|
|
(function() use($file, $variables) { |
|
63
|
6 |
|
extract($variables); |
|
64
|
6 |
|
require $file; |
|
65
|
6 |
|
})(); |
|
66
|
|
|
|
|
67
|
6 |
|
$ret = ob_get_contents(); |
|
68
|
6 |
|
ob_end_clean(); |
|
69
|
|
|
|
|
70
|
6 |
|
return $ret; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
8 |
|
public function supports(string $template): bool { |
|
77
|
8 |
|
$ext = pathinfo($template, PATHINFO_EXTENSION); |
|
78
|
|
|
|
|
79
|
|
|
if ( |
|
80
|
8 |
|
'php' !== $ext && |
|
81
|
8 |
|
'phtml' !== $ext |
|
82
|
|
|
) { |
|
83
|
4 |
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
try { |
|
87
|
6 |
|
$this->fileLocator->locate($template, null, true); |
|
88
|
2 |
|
} catch (FileLocatorFileNotFoundException $e) { |
|
89
|
2 |
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
6 |
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|