66
77class File
88{
9- const TYPE_SCSS = 'scss ' ;
10- const TYPE_SASS = 'sass ' ;
11- const TYPE_COMPASS = 'compass ' ;
12- const TYPE_LESS = 'less ' ;
13- const TYPE_UNKNOWN = 'unknown ' ;
14- /**
15- * @var string[]
16- */
17- private static $ extensions = [
18- self ::TYPE_SCSS ,
19- self ::TYPE_SASS ,
9+ const TYPE_UNKNOWN = 'unknown ' ;
10+ const TYPE_COMPASS = 'compass ' ;
11+ const TYPE_SASS = 'sass ' ;
12+ const TYPE_SCSS = 'scss ' ;
13+ const TYPE_LESS = 'less ' ;
14+ static $ supportedTypes = [
2015 self ::TYPE_COMPASS ,
21- self ::TYPE_LESS ,
16+ self ::TYPE_SASS ,
17+ self ::TYPE_SCSS ,
18+ self ::TYPE_LESS
2219 ];
2320 /**
2421 * @var string
@@ -51,28 +48,10 @@ public function __construct($sourcePath, $outputPath)
5148 $ this ->outputPath = $ outputPath ;
5249 }
5350
54- public function getSourcePath ()
55- {
56- return $ this ->sourcePath ;
57- }
58-
59- /**
60- * @param string $path
61- *
62- * @return File
63- */
64- public function setSourcePath ($ path )
65- {
66- $ this ->sourcePath = $ path ;
67- $ this ->detectSourceTypeFromPath ($ path );
68-
69- return $ this ;
70- }
71-
7251 /**
7352 * @return string
7453 */
75- public function getOutputPath ()
54+ public function getOutputPath ()
7655 {
7756 return $ this ->outputPath ;
7857 }
@@ -113,13 +92,44 @@ public function setSourceContent($content)
11392 * @return File
11493 * @throws FileException
11594 */
116- public function setSourceContentFromSourcePath ()
95+ public function setSourceContentFromSourcePath ()
11796 {
11897 $ this ->sourceContent = $ this ->readSourceContentByPath ();
11998
12099 return $ this ;
121100 }
122101
102+ /**
103+ * @return string
104+ * @throws FileException
105+ */
106+ protected function readSourceContentByPath ()
107+ {
108+ if (!file_exists ($ this ->getSourcePath ())) {
109+ throw new FileException ("file: {$ this ->sourcePath } doesn't exists " );
110+ }
111+
112+ return file_get_contents ($ this ->getSourcePath ());
113+ }
114+
115+ public function getSourcePath ()
116+ {
117+ return $ this ->sourcePath ;
118+ }
119+
120+ /**
121+ * @param string $path
122+ *
123+ * @return File
124+ */
125+ public function setSourcePath ($ path )
126+ {
127+ $ this ->sourcePath = $ path ;
128+ $ this ->type = $ this ->detectSourceTypeFromPath ($ path );
129+
130+ return $ this ;
131+ }
132+
123133 /**
124134 * @return string
125135 */
@@ -133,7 +143,7 @@ public function getParsedContent()
133143 *
134144 * @return File
135145 */
136- public function setParsedContent ($ content )
146+ public function setParsedContent ($ content )
137147 {
138148 $ this ->parsedContent = $ content ;
139149
@@ -143,7 +153,7 @@ public function setParsedContent($content)
143153 /**
144154 * @return string
145155 */
146- public function getType ()
156+ public function getType ()
147157 {
148158 return $ this ->type ;
149159 }
@@ -162,29 +172,15 @@ public function setType($type)
162172
163173 /**
164174 * @param string $path
165- *
166- * @return void
167- */
168- private function detectSourceTypeFromPath ($ path )
169- {
170- $ extension = strtolower (pathinfo ($ path , PATHINFO_EXTENSION ));
171- if (in_array ($ extension , static ::$ extensions )) {
172- $ this ->type = $ extension ;
173- } else {
174- $ this ->type = static ::TYPE_UNKNOWN ;
175- }
176- }
177-
178- /**
175+ *
179176 * @return string
180- * @throws FileException
181177 */
182- private function readSourceContentByPath ( )
178+ protected function detectSourceTypeFromPath ( $ path )
183179 {
184- if (!file_exists ($ this ->getSourcePath ())) {
185- throw new FileException ("file: {$ this ->sourcePath } doesn't exists " );
186- }
180+ $ extension = strtolower (pathinfo ($ path , PATHINFO_EXTENSION ));
187181
188- return file_get_contents ($ this ->getSourcePath ());
182+ return in_array ($ extension , static ::$ supportedTypes )
183+ ? $ extension
184+ : static ::TYPE_UNKNOWN ;
189185 }
190186}
0 commit comments