Skip to content

Commit c2e0f4d

Browse files
committed
First stage of working JS code
1 parent f107ea8 commit c2e0f4d

File tree

3 files changed

+48
-26
lines changed

3 files changed

+48
-26
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"require": {
1818
"php": ">=5.4.0",
1919
"illuminate/support": "5.*",
20-
"illuminate/view": "5.*"
20+
"illuminate/view": "5.*",
21+
"nesbot/carbon": "~1.0"
2122
},
2223
"require-dev": {
2324
"phpunit/phpunit": "~4.0"

src/Combiner.php

+44-23
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44
use Illuminate\Http\Request;
55
use Illuminate\Http\Response;
66
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+
*/
819
class Combiner
920
{
1021
protected static $_initialised = false;
@@ -45,9 +56,10 @@ public function viewJS(Route $route, Request $request, Response $response)
4556
return $response;
4657
}
4758

59+
$count = $route->parameter('count', 0);
4860
$files = explode(',', $route->parameter('files', ''));
4961

50-
if ($route->parameter('count', 0) <> count($files))
62+
if ($count <> count($files))
5163
{
5264
$this->app->abort(422, 'Length option incorrect');
5365
}
@@ -56,13 +68,28 @@ public function viewJS(Route $route, Request $request, Response $response)
5668
$files = $this->sanitize_files($files);
5769
$filesstr = implode('_', $files);
5870

59-
$etag = implode('-', $request->getETags());
71+
$etag = preg_replace('#^"(.*)"$#i', '\1', implode('-', $request->getETags()));
6072

6173
if (\Cache::has(sprintf('%s_%s_%d', $etag, $filesstr, $count)))
6274
{
75+
$response->setEtag($etag);
76+
77+
if ($response->isNotModified($request))
78+
{
79+
return $response->send();
80+
}
81+
6382
$data = \Cache::get(sprintf('%s_%s_%d', $etag, $filesstr, $count));
6483

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();
6693
}
6794

6895
$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)
76103
continue;
77104
}
78105

79-
$data .= file_get_contents($fullfile) . PHP_EOL;
106+
$data .= @file_get_contents($fullfile) . PHP_EOL;
80107
}
81108

82-
$this->output($response, $data);
109+
$etag = md5($data);
83110

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+
));
85119

86-
return $response;
120+
\Cache::put(sprintf('%s_%s_%d', $etag, $filesstr, $count), $data, Carbon::now()->addMinutes(60));
121+
122+
return $response->send();
87123
}
88124

89125
/**
@@ -267,21 +303,6 @@ protected function arrayPush(&$array, $value)
267303
return (in_array($value, $array) || array_push($array, $value));
268304
}
269305

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-
285306
protected function sanitize_files($arr)
286307
{
287308
foreach (array_keys($arr) as $idx)

src/CombinerServiceProvider.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected function enableJSCombiner()
8484

8585
$this->app->router->get($uri,
8686
function (Route $route, Request $request, Response $response) {
87-
$this->app->combiner->viewJS($route, $request, $response);
87+
return $this->app->combiner->viewJS($route, $request, $response);
8888
}
8989
);
9090
}
@@ -95,7 +95,7 @@ protected function enableCssCombiner()
9595

9696
$this->app->router->get($uri,
9797
function (Route $route, Request $request, Response $response) {
98-
$this->app->combiner->viewCss($route, $request, $response);
98+
return $this->app->combiner->viewCss($route, $request, $response);
9999
}
100100
);
101101
}

0 commit comments

Comments
 (0)