1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Columnis\Service; |
4
|
|
|
|
5
|
|
|
use Columnis\Model\Template; |
6
|
|
|
use Columnis\Exception\Templates\TemplateNameNotSetException; |
7
|
|
|
|
8
|
|
|
class TemplateService |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var array The templates paths |
13
|
|
|
*/ |
14
|
|
|
protected $templatesPathStack = array(); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Retrieve paths to templates |
18
|
|
|
* |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
7 |
|
public function getTemplatesPathStack() |
22
|
|
|
{ |
23
|
7 |
|
return $this->templatesPathStack; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Set the templates paths |
28
|
|
|
* |
29
|
|
|
* @param array $templatesPathStack |
30
|
|
|
*/ |
31
|
16 |
|
public function setTemplatesPathStack(Array $templatesPathStack) |
32
|
|
|
{ |
33
|
16 |
|
$this->templatesPathStack = $templatesPathStack; |
34
|
16 |
|
} |
35
|
|
|
|
36
|
15 |
|
public function __construct(Array $templatesPathStack) |
37
|
|
|
{ |
38
|
15 |
|
$this->setTemplatesPathStack($templatesPathStack); |
39
|
15 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Return the FIRST paths that contain a template with the specified name |
43
|
|
|
* (There should not be more than one posible template path) |
44
|
|
|
* |
45
|
|
|
* @param string $templateName |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
6 |
|
public function getExistantTemplatePath($templateName) |
49
|
|
|
{ |
50
|
6 |
|
$paths = $this->getTemplatesPathStack(); |
51
|
6 |
|
foreach ($paths as $path) { |
52
|
6 |
|
$templatePath = $path . DIRECTORY_SEPARATOR . $templateName; |
53
|
6 |
|
if ($this->validTemplate($templatePath)) { |
54
|
3 |
|
return $templatePath; |
55
|
|
|
} |
56
|
6 |
|
} |
57
|
3 |
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns true if it is a valid template |
62
|
|
|
* |
63
|
|
|
* @param string $templatePath |
64
|
|
|
* @return boolean |
65
|
|
|
*/ |
66
|
9 |
|
public function validTemplate($templatePath) |
67
|
|
|
{ |
68
|
9 |
|
if (!is_dir($templatePath)) { |
69
|
7 |
|
return false; |
70
|
|
|
} |
71
|
5 |
|
$template = new Template(); |
72
|
5 |
|
$template->setPath($templatePath); |
73
|
5 |
|
return $template->isValid(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Creats a Template instance from an array with page Data. |
78
|
|
|
* |
79
|
|
|
* @param array $data |
80
|
|
|
* @throws TemplateNameNotSetException |
81
|
|
|
* @return Template |
82
|
|
|
*/ |
83
|
8 |
|
public function createFromData(Array $data) |
84
|
|
|
{ |
85
|
8 |
|
if (isset($data['template']) && !empty($data['template'])) { |
86
|
5 |
|
$templateName = $data['template']; |
87
|
5 |
|
} else { |
88
|
3 |
|
throw new TemplateNameNotSetException("Template not set in page response."); |
89
|
|
|
} |
90
|
5 |
|
if (isset($data['template_path']) && !empty($data['template_path'])) { |
91
|
2 |
|
$path = $data['template_path']; |
92
|
2 |
|
} else { |
93
|
3 |
|
$path = $this->getExistantTemplatePath($templateName); |
94
|
|
|
} |
95
|
5 |
|
$template = new Template(); |
96
|
5 |
|
$template->setName($templateName); |
97
|
5 |
|
$template->setPath($path); |
98
|
3 |
|
return $template; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|