11<?php namespace CeesVanEgmond \Minify ;
22
3+ use CeesVanEgmond \Minify \Exceptions \InvalidArgumentException ;
34use CeesVanEgmond \Minify \Providers \JavaScript ;
45use CeesVanEgmond \Minify \Providers \StyleSheet ;
56
@@ -10,68 +11,116 @@ class Minify
1011 */
1112 protected $ config ;
1213
14+ /**
15+ * @var array
16+ */
17+ protected $ attributes = array ();
18+
1319 /**
1420 * @var string
1521 */
1622 private $ environment ;
1723
24+ /**
25+ * @var
26+ */
27+ private $ provider ;
28+
29+ /**
30+ * @var
31+ */
32+ private $ buildPath ;
33+
1834 /**
1935 * @param array $config
20- * @param $environment
36+ * @param string $environment
2137 */
2238 public function __construct (array $ config , $ environment )
2339 {
40+ $ this ->checkConfiguration ($ config );
41+
2442 $ this ->config = $ config ;
2543 $ this ->environment = $ environment ;
2644 }
2745
2846 /**
2947 * @param $file
48+ * @param array $attributes
3049 * @return string
3150 */
32- public function javascript ($ file )
51+ public function javascript ($ file, $ attributes = array () )
3352 {
34- $ provider = new JavaScript (public_path ());
35- $ buildPath = $ this ->config ['js_build_path ' ];
53+ $ this ->provider = new JavaScript (public_path ());
54+ $ this ->buildPath = $ this ->config ['js_build_path ' ];
55+ $ this ->attributes = $ attributes ;
56+
57+ $ this ->process ($ file );
3658
37- return $ this -> process ( $ file , $ provider , $ buildPath ) ;
59+ return $ this ;
3860 }
3961
4062 /**
4163 * @param $file
64+ * @param array $attributes
4265 * @return string
4366 */
44- public function stylesheet ($ file )
67+ public function stylesheet ($ file, $ attributes = array () )
4568 {
46- $ provider = new StyleSheet (public_path ());
47- $ buildPath = $ this ->config ['css_build_path ' ];
69+ $ this ->provider = new StyleSheet (public_path ());
70+ $ this ->buildPath = $ this ->config ['css_build_path ' ];
71+ $ this ->attributes = $ attributes ;
4872
49- return $ this ->process ($ file , $ provider , $ buildPath );
73+ $ this ->process ($ file );
74+
75+ return $ this ;
5076 }
5177
5278 /**
5379 * @param $file
54- * @param $provider
55- * @param $buildPath
56- * @return string
5780 */
58- private function process ($ file, $ provider , $ buildPath )
81+ private function process ($ file )
5982 {
60- $ provider ->add ($ file );
83+ $ this -> provider ->add ($ file );
6184
62- if ( in_array ( $ this ->environment , $ this ->config [ ' ignore_envionments ' ] ))
85+ if ( $ this ->provider -> make ( $ this ->buildPath ))
6386 {
64- return $ provider ->tags ();
87+ $ this -> provider ->minify ();
6588 }
89+ }
6690
67- //Return when minified file already exists
68- if (!$ provider ->make ($ buildPath ))
91+ /**
92+ * @return mixed
93+ */
94+ public function render ()
95+ {
96+ if (in_array ($ this ->environment , $ this ->config ['ignore_environments ' ]))
6997 {
70- return $ provider ->tag ( $ buildPath . $ provider -> getFilename () );
98+ return $ this -> provider ->tags ( $ this -> attributes );
7199 }
72100
73- $ provider ->minify ();
101+ return $ this ->provider ->tag ($ this ->buildPath . $ this ->provider ->getFilename (), $ this ->attributes );
102+ }
103+
104+ /**
105+ * @return string
106+ */
107+ public function __toString ()
108+ {
109+ return $ this ->render ();
110+ }
74111
75- return $ provider ->tag ($ buildPath . $ provider ->getFilename ());
112+ /**
113+ * @param array $config
114+ * @throws Exceptions\InvalidArgumentException
115+ * @return array
116+ */
117+ private function checkConfiguration (array $ config )
118+ {
119+ if (!isset ($ config ['css_build_path ' ]) || !is_string ($ config ['css_build_path ' ]))
120+ throw new InvalidArgumentException ("Missing css_build_path field " );
121+ if (!isset ($ config ['js_build_path ' ]) || !is_string ($ config ['js_build_path ' ]))
122+ throw new InvalidArgumentException ("Missing js_build_path field " );
123+ if (!isset ($ config ['ignore_environments ' ]) || !is_array ($ config ['ignore_environments ' ]))
124+ throw new InvalidArgumentException ("Missing ignore_environments field " );
76125 }
77- }
126+ }
0 commit comments