1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Columnis\Service; |
4
|
|
|
|
5
|
|
|
use Columnis\Model\PageBreakpoint; |
6
|
|
|
use \Smarty; |
7
|
|
|
use \CssMin; |
8
|
|
|
|
9
|
|
|
class PageBreakpointService { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var array The templates paths |
13
|
|
|
*/ |
14
|
|
|
protected $templatesPathStack = array(); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array The assets manager paths |
18
|
|
|
*/ |
19
|
|
|
protected $assetsManagerPathStack = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Smarty object |
23
|
|
|
* |
24
|
|
|
* @var Smarty |
25
|
|
|
*/ |
26
|
|
|
protected $smarty; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Retrieve paths to templates |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function getTemplatesPathStack() { |
34
|
|
|
return $this->templatesPathStack; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set the templates paths |
39
|
|
|
* |
40
|
|
|
* @param array $templatesPathStack |
41
|
|
|
*/ |
42
|
|
|
public function setTemplatesPathStack(Array $templatesPathStack) { |
43
|
|
|
$this->templatesPathStack = $templatesPathStack; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Retrieve paths to assets manager |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
function getAssetsManagerPathStack() { |
|
|
|
|
52
|
|
|
return $this->assetsManagerPathStack; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set the assets manager paths |
57
|
|
|
* |
58
|
|
|
* @param array $assetsManagerPathStack |
59
|
|
|
*/ |
60
|
|
|
function setAssetsManagerPathStack($assetsManagerPathStack) { |
|
|
|
|
61
|
|
|
$this->assetsManagerPathStack = $assetsManagerPathStack; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return Smarty Object |
66
|
|
|
* |
67
|
|
|
* @return Smarty |
68
|
|
|
*/ |
69
|
|
|
function getSmarty() { |
|
|
|
|
70
|
|
|
return $this->smarty; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the Smarty Object |
75
|
|
|
* @param Smarty $smarty |
76
|
|
|
*/ |
77
|
|
|
function setSmarty(Smarty $smarty) { |
|
|
|
|
78
|
|
|
$this->smarty = $smarty; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Constructor |
83
|
|
|
* |
84
|
|
|
* @param Smarty $smarty |
85
|
|
|
* @param array $templatesPathStack |
86
|
|
|
* @param array $assetsManagerPathStack |
87
|
|
|
*/ |
88
|
|
|
function __construct(Smarty $smarty, $templatesPathStack, $assetsManagerPathStack) { |
|
|
|
|
89
|
|
|
$this->smarty = $smarty; |
90
|
|
|
$this->templatesPathStack = $templatesPathStack; |
91
|
|
|
$this->assetsManagerPathStack = $assetsManagerPathStack; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Creats a PageBreakpoint file if not exist |
96
|
|
|
* |
97
|
|
|
* @param int $idPage |
98
|
|
|
* @param array $extraData |
99
|
|
|
* @param string $hash |
100
|
|
|
* @param array $images |
101
|
|
|
* @param array $imageSizesGroups |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function createPageBreakpoint($idPage, $extraData, $hash, $images, $imageSizesGroups) { |
105
|
|
|
$pageBreakpoint = new PageBreakpoint(); |
106
|
|
|
$pageBreakpoint->setHash($hash); |
107
|
|
|
$pageBreakpoint->setIdPage($idPage); |
108
|
|
|
$pageBreakpoint->setExtraData($extraData); |
109
|
|
|
$pageBreakpoint->setImages($images); |
110
|
|
|
$pageBreakpoint->setTemplateHash($this->getBreakpointTemplateHash()); |
111
|
|
|
$pageBreakpoint->setImageGroupsSizes($imageSizesGroups); |
112
|
|
|
$this->loadBreakpointsPath($pageBreakpoint); //Sets path |
113
|
|
|
$pathExist = $this->checkBreakpointPath($pageBreakpoint); //Check path exist, if not create it |
114
|
|
|
$currentBreakpointName = $this->getCurrentBreakpointFilename($idPage, $pageBreakpoint); |
115
|
|
|
$breakpointChange = $currentBreakpointName !== $pageBreakpoint->getFileName(); |
116
|
|
|
if(!$pathExist || $breakpointChange) { |
117
|
|
|
//Invalidate last file |
118
|
|
|
$this->invalidateCurrentBreakpointFile($pageBreakpoint, $currentBreakpointName); |
|
|
|
|
119
|
|
|
//Create it if path not exist (css/breakpoint dir) or the current file are diferent with the parameters |
120
|
|
|
$this->createBreakpointFile($pageBreakpoint); |
121
|
|
|
} |
122
|
|
|
return $pageBreakpoint->getFileName(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Sets the path to pageBreakpoint object |
127
|
|
|
* |
128
|
|
|
* @param PageBreakpoint $pageBreakpoint |
129
|
|
|
*/ |
130
|
|
|
protected function loadBreakpointsPath(PageBreakpoint &$pageBreakpoint) { |
131
|
|
|
$assetsPaths = $this->getAssetsManagerPathStack(); |
132
|
|
|
if(is_array($assetsPaths)) { |
133
|
|
|
$pageBreakpoint->setPath($assetsPaths[1]); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Check Breakpoints directory, if not exist create it |
139
|
|
|
* |
140
|
|
|
* @param PageBreakpoint $pageBreakpoint |
141
|
|
|
* @return boolean |
142
|
|
|
*/ |
143
|
|
|
protected function checkBreakpointPath(PageBreakpoint &$pageBreakpoint) { |
144
|
|
|
$path = $pageBreakpoint->getFullPath(); |
145
|
|
|
if(!file_exists($path)) { |
146
|
|
|
mkdir($path); |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Search on breakpoints dir the current breakpoint page file |
154
|
|
|
* |
155
|
|
|
* @param int $idPage |
156
|
|
|
* @param PageBreakpoint $pageBreakpoint |
157
|
|
|
* @return boolean |
158
|
|
|
*/ |
159
|
|
|
protected function getCurrentBreakpointFilename($idPage, PageBreakpoint $pageBreakpoint) { |
160
|
|
|
$breakpointFiles = glob($pageBreakpoint->getFullPath().$idPage."-*.css"); |
161
|
|
|
if(is_array($breakpointFiles) && count($breakpointFiles)) { |
162
|
|
|
$breakpointFile = basename(array_pop($breakpointFiles)); |
163
|
|
|
return $breakpointFile; |
164
|
|
|
} |
165
|
|
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the template file hash |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
protected function getBreakpointTemplateHash() { |
174
|
|
|
$templatesPath = $this->getTemplatesPathStack()[1]; |
175
|
|
|
$templateFile = $templatesPath.DIRECTORY_SEPARATOR.PageBreakpoint::BREAKPOINT_FILE; |
176
|
|
|
if(file_exists($templateFile)) { |
177
|
|
|
$hash = md5_file($templateFile); |
178
|
|
|
} |
179
|
|
|
return $hash; |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Create a new PageBreakpointFile |
184
|
|
|
* |
185
|
|
|
* @param PageBreakpoint $pageBreakpoint |
186
|
|
|
*/ |
187
|
|
|
protected function createBreakpointFile(PageBreakpoint $pageBreakpoint) { |
188
|
|
|
try { |
189
|
|
|
//TemplatesConfig |
190
|
|
|
$templatesPath = $this->getTemplatesPathStack()[1]; |
191
|
|
|
if(file_exists($templatesPath.DIRECTORY_SEPARATOR.PageBreakpoint::BREAKPOINT_FILE)) { |
192
|
|
|
//Get smarty object |
193
|
|
|
$smarty = $this->getSmarty(); |
194
|
|
|
//Set templates path |
195
|
|
|
$smarty->setTemplateDir($templatesPath); |
196
|
|
|
//Assign data |
197
|
|
|
$smarty->assign('data', $pageBreakpoint->getData()); |
198
|
|
|
//Fetch content |
199
|
|
|
$breakpointContent = $smarty->fetch(PageBreakpoint::BREAKPOINT_FILE); |
200
|
|
|
// write to file |
201
|
|
|
file_put_contents($pageBreakpoint->getFullFileName(), CssMin::minify($breakpointContent)); |
202
|
|
|
} |
203
|
|
|
} catch(\Exception $exc) { |
204
|
|
|
|
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Remove the last breakpoint file |
210
|
|
|
* |
211
|
|
|
* @param PageBreakpoint $pageBreakpoint |
212
|
|
|
* @param string $currentFileName |
213
|
|
|
*/ |
214
|
|
|
protected function invalidateCurrentBreakpointFile(PageBreakpoint $pageBreakpoint, $currentFileName) { |
215
|
|
|
try { |
216
|
|
|
//Parse full current breakpoint name |
217
|
|
|
$invalidatePath = $pageBreakpoint->getFullPath().$currentFileName; |
218
|
|
|
//If exist delete it |
219
|
|
|
if(file_exists($invalidatePath) && is_file($invalidatePath)) { |
220
|
|
|
unlink($invalidatePath); |
221
|
|
|
} |
222
|
|
|
} catch(\Exception $exc) { |
|
|
|
|
223
|
|
|
|
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.