1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EM\CssCompiler\Container; |
4
|
|
|
|
5
|
|
|
use EM\CssCompiler\Exception\FileException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @since 0.1 |
9
|
|
|
*/ |
10
|
|
|
class FileContainer |
11
|
|
|
{ |
12
|
|
|
const TYPE_UNKNOWN = 'unknown'; |
13
|
|
|
const TYPE_COMPASS = 'compass'; |
14
|
|
|
const TYPE_SASS = 'sass'; |
15
|
|
|
const TYPE_SCSS = 'scss'; |
16
|
|
|
const TYPE_LESS = 'less'; |
17
|
|
|
static $supportedTypes = [ |
|
|
|
|
18
|
|
|
self::TYPE_COMPASS, |
19
|
|
|
self::TYPE_SASS, |
20
|
|
|
self::TYPE_SCSS, |
21
|
|
|
self::TYPE_LESS |
22
|
|
|
]; |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $sourcePath; |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $outputPath; |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $sourceContent; |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $parsedContent; |
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $type; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $sourcePath |
46
|
|
|
* @param string $outputPath |
47
|
|
|
*/ |
48
|
|
|
public function __construct($sourcePath, $outputPath) |
49
|
|
|
{ |
50
|
|
|
$this->setSourcePath($sourcePath); |
51
|
|
|
$this->outputPath = $outputPath; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getOutputPath() |
58
|
|
|
{ |
59
|
|
|
return $this->outputPath; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $path |
64
|
|
|
* |
65
|
|
|
* @return File |
66
|
|
|
*/ |
67
|
|
|
public function setOutputPath($path) |
68
|
|
|
{ |
69
|
|
|
$this->outputPath = $path; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getSourceContent() |
78
|
|
|
{ |
79
|
|
|
return $this->sourceContent; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $content |
84
|
|
|
* |
85
|
|
|
* @return File |
86
|
|
|
*/ |
87
|
|
|
public function setSourceContent($content) |
88
|
|
|
{ |
89
|
|
|
$this->sourceContent = $content; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return File |
96
|
|
|
* @throws FileException |
97
|
|
|
*/ |
98
|
|
|
public function setSourceContentFromSourcePath() |
99
|
|
|
{ |
100
|
|
|
$this->sourceContent = $this->readSourceContentByPath(); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string |
107
|
|
|
* @throws FileException |
108
|
|
|
*/ |
109
|
|
|
protected function readSourceContentByPath() |
110
|
|
|
{ |
111
|
|
|
if (!file_exists($this->getSourcePath())) { |
112
|
|
|
throw new FileException("file: {$this->sourcePath} doesn't exists"); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return file_get_contents($this->getSourcePath()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getSourcePath() |
119
|
|
|
{ |
120
|
|
|
return $this->sourcePath; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $path |
125
|
|
|
* |
126
|
|
|
* @return File |
127
|
|
|
*/ |
128
|
|
|
public function setSourcePath($path) |
129
|
|
|
{ |
130
|
|
|
$this->sourcePath = $path; |
131
|
|
|
$this->type = $this->detectSourceTypeFromPath($path); |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function getParsedContent() |
140
|
|
|
{ |
141
|
|
|
return $this->parsedContent; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $content |
146
|
|
|
* |
147
|
|
|
* @return File |
148
|
|
|
*/ |
149
|
|
|
public function setParsedContent($content) |
150
|
|
|
{ |
151
|
|
|
$this->parsedContent = $content; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function getType() |
160
|
|
|
{ |
161
|
|
|
return $this->type; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $type |
166
|
|
|
* |
167
|
|
|
* @return File |
168
|
|
|
*/ |
169
|
|
|
public function setType($type) |
170
|
|
|
{ |
171
|
|
|
$this->type = $type; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $path |
178
|
|
|
* |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
protected function detectSourceTypeFromPath($path) |
182
|
|
|
{ |
183
|
|
|
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); |
184
|
|
|
|
185
|
|
|
return in_array($extension, static::$supportedTypes) |
|
|
|
|
186
|
|
|
? $extension |
187
|
|
|
: static::TYPE_UNKNOWN; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.