|
1
|
|
|
<?php |
|
2
|
|
|
namespace ntentan\honam\helpers; |
|
3
|
|
|
|
|
4
|
|
|
use ntentan\honam\Helper; |
|
5
|
|
|
use ntentan\honam\AssetsLoader; |
|
6
|
|
|
use ntentan\honam\helpers\minifiables\Minifier; |
|
7
|
|
|
|
|
8
|
|
|
abstract class MinifiablesHelper extends Helper |
|
9
|
|
|
{ |
|
10
|
|
|
private $minifiableScripts = array(); |
|
11
|
|
|
private $otherScripts = array(); |
|
12
|
|
|
private $context = 'default'; |
|
13
|
|
|
private $combine = false; |
|
14
|
|
|
|
|
15
|
|
|
protected abstract function getExtension(); |
|
|
|
|
|
|
16
|
|
|
protected abstract function getMinifier(); |
|
|
|
|
|
|
17
|
|
|
protected abstract function getTag($url); |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
7 |
|
private function makePublic($file) |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
7 |
|
return $this->getTag(AssetsLoader::load($this->getExtension() . "/" . basename($file), $file)); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
11 |
|
private function writeTags($files) |
|
25
|
|
|
{ |
|
26
|
11 |
|
$tags = ''; |
|
27
|
11 |
|
foreach($files as $script) |
|
28
|
|
|
{ |
|
29
|
7 |
|
$tags .= $this->makePublic($script); |
|
30
|
|
|
} |
|
31
|
9 |
|
return $tags; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
11 |
|
public function __toString() |
|
35
|
|
|
{ |
|
36
|
11 |
|
$minifiedScript = ''; |
|
37
|
11 |
|
$tags = ''; |
|
38
|
11 |
|
$filename = "/" . $this->getExtension() . "/combined_{$this->context}." . $this->getExtension(); |
|
39
|
11 |
|
if($this->combine === true) |
|
40
|
|
|
{ |
|
41
|
4 |
|
foreach($this->minifiableScripts as $script) |
|
42
|
|
|
{ |
|
43
|
4 |
|
$minifiedScript .= file_get_contents($script); |
|
44
|
|
|
} |
|
45
|
4 |
|
file_put_contents(AssetsLoader::getDestinationDir() . $filename, Minifier::minify($minifiedScript, $this->getMinifier())); |
|
46
|
4 |
|
$tags = $this->getTag(AssetsLoader::getSiteUrl() . $filename); |
|
47
|
|
|
} |
|
48
|
7 |
|
else if($this->combine == false) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
7 |
|
$tags .= $this->writeTags($this->minifiableScripts); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
9 |
|
$tags .= $this->writeTags($this->otherScripts); |
|
54
|
|
|
|
|
55
|
9 |
|
$this->minifiableScripts = array(); |
|
56
|
9 |
|
$this->otherScripts = array(); |
|
57
|
|
|
|
|
58
|
9 |
|
return $tags; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
public function help($arguments) |
|
62
|
|
|
{ |
|
63
|
2 |
|
if(is_array($arguments)) |
|
64
|
|
|
{ |
|
65
|
1 |
|
foreach($arguments as $argument) |
|
66
|
|
|
{ |
|
67
|
1 |
|
if($argument == '') continue; |
|
68
|
1 |
|
$this->otherScripts[]= $argument; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
1 |
|
else if($arguments != '') |
|
72
|
|
|
{ |
|
73
|
1 |
|
$this->otherScripts[]= $arguments; |
|
74
|
|
|
} |
|
75
|
2 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
9 |
|
public function add($script) |
|
79
|
|
|
{ |
|
80
|
9 |
|
if($script != '' && is_string($script)) |
|
81
|
|
|
{ |
|
82
|
7 |
|
$this->minifiableScripts[] = $script; |
|
83
|
|
|
} |
|
84
|
2 |
|
else if(is_array($script)) |
|
85
|
|
|
{ |
|
86
|
2 |
|
foreach($script as $scriptFile) |
|
87
|
|
|
{ |
|
88
|
2 |
|
$this->minifiableScripts[] = $scriptFile; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
9 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function setContext($context) |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->context($context); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function setCombine($combine) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->combine($combine); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
public function context($context) |
|
105
|
|
|
{ |
|
106
|
1 |
|
$this->context = $context; |
|
107
|
1 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
4 |
|
public function combine($combine) |
|
111
|
|
|
{ |
|
112
|
4 |
|
$this->combine = $combine; |
|
113
|
4 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.