4
4
use Illuminate \Http \Request ;
5
5
use Illuminate \Http \Response ;
6
6
use Illuminate \Foundation \Application ;
7
-
7
+ use Carbon \Carbon ;
8
+
9
+ /**
10
+ * Main Combiner class
11
+ *
12
+ * @todo functionize the view class
13
+ * @todo better function naming
14
+ * @todo output complete html tags on (JS/Css)URL
15
+ * @todo write documentation
16
+ * @todo move import in css files to top of document (RFC)
17
+ *
18
+ */
8
19
class Combiner
9
20
{
10
21
protected static $ _initialised = false ;
@@ -45,9 +56,10 @@ public function viewJS(Route $route, Request $request, Response $response)
45
56
return $ response ;
46
57
}
47
58
59
+ $ count = $ route ->parameter ('count ' , 0 );
48
60
$ files = explode (', ' , $ route ->parameter ('files ' , '' ));
49
61
50
- if ($ route -> parameter ( ' count ' , 0 ) <> count ($ files ))
62
+ if ($ count <> count ($ files ))
51
63
{
52
64
$ this ->app ->abort (422 , 'Length option incorrect ' );
53
65
}
@@ -56,13 +68,28 @@ public function viewJS(Route $route, Request $request, Response $response)
56
68
$ files = $ this ->sanitize_files ($ files );
57
69
$ filesstr = implode ('_ ' , $ files );
58
70
59
- $ etag = implode ('- ' , $ request ->getETags ());
71
+ $ etag = preg_replace ( ' #^"(.*)"$#i ' , ' \1 ' , implode ('- ' , $ request ->getETags () ));
60
72
61
73
if (\Cache::has (sprintf ('%s_%s_%d ' , $ etag , $ filesstr , $ count )))
62
74
{
75
+ $ response ->setEtag ($ etag );
76
+
77
+ if ($ response ->isNotModified ($ request ))
78
+ {
79
+ return $ response ->send ();
80
+ }
81
+
63
82
$ data = \Cache::get (sprintf ('%s_%s_%d ' , $ etag , $ filesstr , $ count ));
64
83
65
- return $ this ->output ($ response , $ data );
84
+ $ response ->setPublic ();
85
+ $ response ->setContent ($ data );
86
+ $ response ->setVary ('etag ' , true );
87
+ $ response ->header ('Content-Type ' , 'text/javascript ' , true );
88
+ $ response ->setExpires (Carbon::now ()->addSeconds (
89
+ $ this ->app ->config ->get ('combiner.javascript.expires ' )
90
+ ));
91
+
92
+ return $ response ->send ();
66
93
}
67
94
68
95
$ basedir = $ this ->app ->basePath () . DIRECTORY_SEPARATOR . $ this ->app ->config ->get ('combiner.javascript.path ' );
@@ -76,14 +103,23 @@ public function viewJS(Route $route, Request $request, Response $response)
76
103
continue ;
77
104
}
78
105
79
- $ data .= file_get_contents ($ fullfile ) . PHP_EOL ;
106
+ $ data .= @ file_get_contents ($ fullfile ) . PHP_EOL ;
80
107
}
81
108
82
- $ this -> output ( $ response , $ data );
109
+ $ etag = md5 ( $ data );
83
110
84
- \Cache::put (sprintf ('%s_%s_%d ' , $ response ->getEtag (), $ filesstr , $ count ), $ data , \Carbin::Carbon ()->addMinutes (60 ));
111
+ $ response ->setPublic ();
112
+ $ response ->setEtag ($ etag );
113
+ $ response ->setContent ($ data );
114
+ $ response ->setVary ('etag ' , true );
115
+ $ response ->header ('Content-Type ' , 'text/javascript ' , true );
116
+ $ response ->setExpires (Carbon::now ()->addSeconds (
117
+ $ this ->app ->config ->get ('combiner.javascript.expires ' )
118
+ ));
85
119
86
- return $ response ;
120
+ \Cache::put (sprintf ('%s_%s_%d ' , $ etag , $ filesstr , $ count ), $ data , Carbon::now ()->addMinutes (60 ));
121
+
122
+ return $ response ->send ();
87
123
}
88
124
89
125
/**
@@ -267,21 +303,6 @@ protected function arrayPush(&$array, $value)
267
303
return (in_array ($ value , $ array ) || array_push ($ array , $ value ));
268
304
}
269
305
270
- protected function output (Response &$ response , $ data ='' )
271
- {
272
- $ etag = \Crypt::hash ('js ' , $ data );
273
-
274
- $ response ->setPublic ();
275
- $ response ->setEtag ($ etag );
276
- $ response ->setExpires (\Carbon::Carbon ()->addSeconds (
277
- $ this ->app ->config ->get ('combiner.javascript.expires ' )
278
- ));
279
-
280
- $ response ->setContent ($ data );
281
-
282
- return $ response ;
283
- }
284
-
285
306
protected function sanitize_files ($ arr )
286
307
{
287
308
foreach (array_keys ($ arr ) as $ idx )
0 commit comments