1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Document; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Templating\TemplateBridgeInterface; |
11
|
|
|
use allejo\stakx\Templating\TemplateErrorInterface; |
12
|
|
|
|
13
|
|
|
trait TemplateEngineDependent |
14
|
|
|
{ |
15
|
|
|
protected $importDependencies = []; |
16
|
|
|
|
17
|
|
|
protected $dataDependencies = []; |
18
|
|
|
|
19
|
|
|
/** @var TemplateBridgeInterface */ |
20
|
|
|
protected $templateEngine; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Set the template engine used to parse the body of the document. |
24
|
|
|
* |
25
|
|
|
* @param TemplateBridgeInterface $templateEngine |
26
|
|
|
* |
27
|
|
|
* @return $this |
28
|
|
|
*/ |
29
|
|
|
public function setTemplateEngine(TemplateBridgeInterface $templateEngine) |
30
|
|
|
{ |
31
|
|
|
$this->templateEngine = $templateEngine; |
32
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Pass the document's body through the template engine. |
38
|
|
|
* |
39
|
|
|
* @param string $bodyContent |
40
|
|
|
* |
41
|
|
|
* @throws TemplateErrorInterface |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
7 |
|
protected function parseTemplateLanguage($bodyContent) |
46
|
|
|
{ |
47
|
7 |
|
if ($this->templateEngine !== null) |
48
|
7 |
|
{ |
49
|
|
|
$this->importDependencies = $this->templateEngine->getTemplateImportDependencies($bodyContent); |
50
|
|
|
$this->dataDependencies = [ |
51
|
|
|
'collections' => $this->templateEngine->getAssortmentDependencies('collections', $bodyContent), |
52
|
|
|
'data' => $this->templateEngine->getAssortmentDependencies('data', $bodyContent), |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
$template = $this->templateEngine->createTemplate($bodyContent); |
56
|
|
|
|
57
|
|
|
return $template->render(); |
58
|
|
|
} |
59
|
|
|
|
60
|
7 |
|
return $bodyContent; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check whether this object has a reference to a collection or data item. |
65
|
|
|
* |
66
|
|
|
* @param string $namespace 'collections' or 'data' |
67
|
|
|
* @param string $needle |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function hasDependencyOnCollection($namespace, $needle) |
72
|
|
|
{ |
73
|
|
|
return |
74
|
|
|
in_array($needle, $this->dataDependencies[$namespace]) || |
75
|
|
|
(is_null($needle) && !empty($this->dataDependencies[$namespace])); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Check whether this object has an "import" or "from" reference to a given path. |
80
|
|
|
* |
81
|
|
|
* @param string $filePath |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function hasDependencyOnTemplateImport($filePath) |
86
|
|
|
{ |
87
|
|
|
return in_array($filePath, $this->importDependencies); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string[] |
92
|
|
|
*/ |
93
|
|
|
public function getImportDependencies() |
94
|
|
|
{ |
95
|
|
|
return $this->importDependencies; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string[] |
100
|
|
|
*/ |
101
|
|
|
public function getCollectionDependencies() |
102
|
|
|
{ |
103
|
|
|
return $this->dataDependencies; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|