1
|
|
|
<?php |
2
|
|
|
namespace EngineWorks\Templates; |
3
|
|
|
|
4
|
|
|
class Template |
5
|
|
|
{ |
6
|
|
|
/** @var Callables */ |
7
|
|
|
private $callables; |
8
|
|
|
|
9
|
|
|
/** @var Resolver */ |
10
|
|
|
private $resolver; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Template constructor. |
14
|
|
|
* |
15
|
|
|
* @param Callables|null $callables If NULL then an empty Callables will be created |
16
|
|
|
* @param Resolver|null $resolver If NULL then a basic Resolver will be created |
17
|
|
|
*/ |
18
|
16 |
|
public function __construct(Callables $callables = null, Resolver $resolver = null) |
19
|
|
|
{ |
20
|
16 |
|
$this->callables = ($callables) ? : new Callables(); |
21
|
16 |
|
$this->resolver = ($resolver) ? : new Resolver(); |
22
|
16 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Retrieve the Callables object |
26
|
|
|
* |
27
|
|
|
* @return Callables |
28
|
|
|
*/ |
29
|
4 |
|
public function callables() |
30
|
|
|
{ |
31
|
4 |
|
return $this->callables; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Retrieve the Resolver object |
36
|
|
|
* |
37
|
|
|
* @return Resolver |
38
|
|
|
*/ |
39
|
3 |
|
public function resolver() |
40
|
|
|
{ |
41
|
3 |
|
return $this->resolver; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Magic method to export calls to $this into calls to Callables |
46
|
|
|
* |
47
|
|
|
* @param string $name |
48
|
|
|
* @param mixed $arguments |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
1 |
|
public function __call($name, $arguments) |
52
|
|
|
{ |
53
|
1 |
|
return $this->callables->call($name, $arguments); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Retrieve if a resolved file is a valid template |
58
|
|
|
* If return FALSE then the $errorMessage variable is populated |
59
|
|
|
* |
60
|
|
|
* @param string $templateFilename |
61
|
|
|
* @param string $errorMessage |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
11 |
|
public function isValidTemplateFilename($templateFilename, &$errorMessage = '') |
65
|
|
|
{ |
66
|
11 |
|
if (! file_exists($templateFilename)) { |
67
|
2 |
|
$errorMessage = "Template {$templateFilename} does not exists"; |
68
|
2 |
|
return false; |
69
|
|
|
} |
70
|
9 |
|
if (! is_readable($templateFilename)) { |
71
|
1 |
|
$errorMessage = "Template {$templateFilename} is not readable"; |
72
|
1 |
|
return false; |
73
|
|
|
} |
74
|
8 |
|
if (is_dir($templateFilename)) { |
75
|
1 |
|
$errorMessage = "Template {$templateFilename} is a directory"; |
76
|
1 |
|
return false; |
77
|
|
|
} |
78
|
7 |
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Fetch and return the content of a templates passing the specified variables. |
83
|
|
|
* Inside the template the variable $this refer to exactly this template object |
84
|
|
|
* |
85
|
|
|
* @param string $templateName |
86
|
|
|
* @param array $templateVariables |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
8 |
|
public function fetch($templateName, array $templateVariables = []) |
90
|
|
|
{ |
91
|
8 |
|
$templateName = $this->resolver->resolve($templateName); |
92
|
8 |
|
if (! $this->isValidTemplateFilename($templateName, $errorMessage)) { |
93
|
1 |
|
throw new \InvalidArgumentException($errorMessage); |
94
|
|
|
} |
95
|
7 |
|
if (! ob_start()) { |
96
|
|
|
throw new \RuntimeException('Cannot create a new buffer'); |
97
|
|
|
} |
98
|
|
|
// as we are using EXTR_OVERWRITE lets remove $templateName if set |
99
|
7 |
|
unset($templateVariables['templateName']); |
100
|
7 |
|
extract($templateVariables, EXTR_OVERWRITE); |
101
|
|
|
/** @noinspection PhpIncludeInspection */ |
102
|
7 |
|
require $templateName; |
103
|
7 |
|
return ob_get_clean(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|