1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Columnis\Model; |
4
|
|
|
|
5
|
|
|
use Columnis\Utils\Directory as DirectoryUtils; |
6
|
|
|
use Columnis\Exception\Templates\PathNotFoundException; |
7
|
|
|
|
8
|
|
|
class Template |
9
|
|
|
{ |
10
|
|
|
const DEFINITION_FILE = 'def.json'; |
11
|
|
|
const MAIN_FILE = 'main.tpl'; |
12
|
|
|
|
13
|
|
|
protected $name; |
14
|
|
|
protected $path; |
15
|
|
|
protected $parsedDefinition; |
16
|
|
|
|
17
|
|
|
/* |
18
|
|
|
* Returns the name of the template |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
|
23
|
3 |
|
public function getName() |
24
|
|
|
{ |
25
|
3 |
|
return $this->name; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Sets the name of the template |
30
|
|
|
* |
31
|
|
|
* @param string $name |
32
|
|
|
*/ |
33
|
16 |
|
public function setName($name) |
34
|
|
|
{ |
35
|
16 |
|
$this->name = $name; |
36
|
16 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the absolute path for the template |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
17 |
|
public function getPath() |
44
|
|
|
{ |
45
|
17 |
|
return $this->path; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Sets the path of the template |
50
|
|
|
* |
51
|
|
|
* @param string $path |
52
|
|
|
* @throws PathNotFoundException |
53
|
|
|
*/ |
54
|
19 |
|
public function setPath($path) |
55
|
|
|
{ |
56
|
19 |
|
if ($path === null) { |
57
|
1 |
|
throw new PathNotFoundException('Path can not be null.'); |
58
|
|
|
} |
59
|
18 |
|
$absPath = realpath($path); |
60
|
18 |
|
if (!$absPath) { |
61
|
2 |
|
throw new PathNotFoundException('Path: '.$path.' does not exist.'); |
62
|
|
|
} |
63
|
16 |
|
$this->path = $absPath; |
64
|
16 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the definition of the template. If it is not parsed yet, it will call parseDefinition to parse it. |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
2 |
|
public function getParsedDefinition() |
72
|
|
|
{ |
73
|
2 |
|
if (!$this->parsedDefinition) { |
74
|
2 |
|
$this->parsedDefinition = $this->parseDefinition(); |
75
|
2 |
|
} |
76
|
2 |
|
return $this->parsedDefinition; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Constructor with no parameters needed |
81
|
|
|
*/ |
82
|
22 |
|
public function __construct() |
83
|
|
|
{ |
84
|
|
|
|
85
|
22 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the path to the definition file |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
13 |
|
public function getDefinitionFile() |
93
|
|
|
{ |
94
|
13 |
|
return $this->getPath().DIRECTORY_SEPARATOR.self::DEFINITION_FILE; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the path to the main file |
99
|
|
|
* |
100
|
|
|
* @param boolean $withPath if false, will return just the main file |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
10 |
|
public function getMainFile($withPath = true) |
104
|
|
|
{ |
105
|
10 |
|
return ($withPath ? $this->getPath() : $this->getName()).DIRECTORY_SEPARATOR.self::MAIN_FILE; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns true if it is a valid template |
110
|
|
|
* |
111
|
|
|
* @return boolean |
112
|
|
|
*/ |
113
|
13 |
|
public function isValid() |
114
|
|
|
{ |
115
|
13 |
|
$templatePath = $this->getPath(); |
116
|
13 |
|
if (!is_dir($templatePath)) { |
117
|
2 |
|
return false; |
118
|
|
|
} |
119
|
11 |
|
$definitionFile = $this->getDefinitionFile(); |
120
|
11 |
|
if (!is_file($definitionFile)) { |
121
|
2 |
|
return false; |
122
|
|
|
} |
123
|
9 |
|
$mainFile = $this->getMainFile(); |
124
|
9 |
|
if (!is_file($mainFile)) { |
125
|
1 |
|
return false; |
126
|
|
|
} |
127
|
8 |
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns the defined assets + assets in the template folder |
132
|
|
|
* |
133
|
|
|
* @param string $extension |
134
|
|
|
* @return Array |
135
|
|
|
*/ |
136
|
2 |
|
public function getAssets($extension, Array $excludes = null) |
137
|
|
|
{ |
138
|
2 |
|
$defined = $this->getDefinedAssets($extension); |
139
|
2 |
|
$search = $this->searchAssets($extension, $excludes); |
140
|
2 |
|
sort($search); |
141
|
2 |
|
$ret = array_merge($defined, $search); |
142
|
2 |
|
return $ret; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Searchs for assets inside the template path |
147
|
|
|
* |
148
|
|
|
* @param string $extension |
149
|
|
|
* @return Array |
150
|
|
|
*/ |
151
|
3 |
|
public function searchAssets($extension, Array $excludes = null) |
152
|
|
|
{ |
153
|
3 |
|
$path = $this->getPath(); |
154
|
3 |
|
return DirectoryUtils::recursiveSearchByExtension($path, $extension, $excludes); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns an array with the defined assets given an extension |
159
|
|
|
* |
160
|
|
|
* @param string $extension |
161
|
|
|
* @return Array |
162
|
|
|
*/ |
163
|
3 |
|
public function getDefinedAssets($extension) |
164
|
|
|
{ |
165
|
3 |
|
$ret = array(); |
166
|
3 |
|
$data = $this->getParsedDefinition(); |
167
|
3 |
|
if (is_array($data[$extension])) { |
168
|
3 |
|
foreach($data[$extension] as $asset) { |
169
|
3 |
|
$assetRealpath = realpath($this->getPath() . DIRECTORY_SEPARATOR . $asset); |
170
|
3 |
|
if (!empty($assetRealpath)) { |
171
|
3 |
|
$ret[] = $assetRealpath; |
172
|
3 |
|
} |
173
|
3 |
|
} |
174
|
3 |
|
} |
175
|
3 |
|
return $ret; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Parses the definition file |
180
|
|
|
* |
181
|
|
|
* @return Array |
182
|
|
|
*/ |
183
|
4 |
|
protected function parseDefinition() |
184
|
|
|
{ |
185
|
4 |
|
$definitionFile = $this->getDefinitionFile(); |
186
|
4 |
|
return json_decode(file_get_contents($definitionFile), true); |
187
|
|
|
} |
188
|
|
|
} |