2011-01-25 18:59:31 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-03-10 00:46:25 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-01-25 18:59:31 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-07-04 20:22:42 +02:00
|
|
|
/**
|
|
|
|
* Delivers CSS and JS resources to the browser. This controller handles all
|
|
|
|
* ##/res/## requests, and manages caching, package construction, and resource
|
|
|
|
* preprocessing.
|
|
|
|
*
|
|
|
|
* @group celerity
|
|
|
|
*/
|
2012-03-10 00:46:25 +01:00
|
|
|
final class CelerityResourceController extends AphrontController {
|
2011-01-31 20:55:26 +01:00
|
|
|
|
2011-01-25 18:59:31 +01:00
|
|
|
private $path;
|
|
|
|
private $hash;
|
2011-01-30 01:10:05 +01:00
|
|
|
private $package;
|
2011-01-31 20:55:26 +01:00
|
|
|
|
2011-01-25 18:59:31 +01:00
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->path = $data['path'];
|
|
|
|
$this->hash = $data['hash'];
|
2011-01-30 01:10:05 +01:00
|
|
|
$this->package = !empty($data['package']);
|
2011-01-25 18:59:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$path = $this->path;
|
|
|
|
|
|
|
|
// Sanity checking to keep this from exposing anything sensitive.
|
|
|
|
$path = preg_replace('@(//|\\.\\.)@', '', $path);
|
|
|
|
$matches = null;
|
|
|
|
if (!preg_match('/\.(css|js)$/', $path, $matches)) {
|
|
|
|
throw new Exception("Only CSS and JS resources may be served.");
|
|
|
|
}
|
2011-01-31 20:55:26 +01:00
|
|
|
|
2011-05-11 12:42:02 +02:00
|
|
|
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
|
|
|
|
!PhabricatorEnv::getEnvConfig('celerity.force-disk-reads')) {
|
2011-05-09 10:10:40 +02:00
|
|
|
// Return a "304 Not Modified". We don't care about the value of this
|
|
|
|
// field since we never change what resource is served by a given URI.
|
|
|
|
return $this->makeResponseCacheable(new Aphront304Response());
|
|
|
|
}
|
|
|
|
|
2011-01-25 18:59:31 +01:00
|
|
|
$type = $matches[1];
|
|
|
|
|
|
|
|
$root = dirname(phutil_get_library_root('phabricator'));
|
|
|
|
|
2011-01-30 01:10:05 +01:00
|
|
|
if ($this->package) {
|
|
|
|
$map = CelerityResourceMap::getInstance();
|
|
|
|
$paths = $map->resolvePackage($this->hash);
|
|
|
|
if (!$paths) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
2011-01-31 20:55:26 +01:00
|
|
|
|
2011-01-30 01:10:05 +01:00
|
|
|
try {
|
|
|
|
$data = array();
|
2012-03-29 22:24:06 +02:00
|
|
|
foreach ($paths as $package_path) {
|
|
|
|
$data[] = Filesystem::readFile($root.'/webroot/'.$package_path);
|
2011-01-30 01:10:05 +01:00
|
|
|
}
|
|
|
|
$data = implode("\n\n", $data);
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$data = Filesystem::readFile($root.'/webroot/'.$path);
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
2011-01-25 18:59:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$response = new AphrontFileResponse();
|
2012-03-28 19:13:53 +02:00
|
|
|
$data = $this->minifyData($data, $type);
|
2011-01-25 18:59:31 +01:00
|
|
|
$response->setContent($data);
|
|
|
|
switch ($type) {
|
|
|
|
case 'css':
|
|
|
|
$response->setMimeType("text/css; charset=utf-8");
|
|
|
|
break;
|
|
|
|
case 'js':
|
|
|
|
$response->setMimeType("text/javascript; charset=utf-8");
|
|
|
|
break;
|
|
|
|
}
|
2011-01-31 20:55:26 +01:00
|
|
|
|
2011-05-09 10:10:40 +02:00
|
|
|
return $this->makeResponseCacheable($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function makeResponseCacheable(AphrontResponse $response) {
|
2011-01-27 20:35:04 +01:00
|
|
|
$response->setCacheDurationInSeconds(60 * 60 * 24 * 30);
|
2011-05-09 10:10:40 +02:00
|
|
|
$response->setLastModified(time());
|
2011-01-25 18:59:31 +01:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2012-03-28 19:13:53 +02:00
|
|
|
private function minifyData($data, $type) {
|
|
|
|
if (!PhabricatorEnv::getEnvConfig('celerity.minify')) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2012-04-02 21:07:59 +02:00
|
|
|
// Some resources won't survive minification (like Raphael.js), and are
|
|
|
|
// marked so as not to be minified.
|
|
|
|
if (strpos($data, '@'.'do-not-minify') !== false) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2012-03-28 19:13:53 +02:00
|
|
|
switch ($type) {
|
|
|
|
case 'css':
|
|
|
|
// Remove comments.
|
|
|
|
$data = preg_replace('@/\*.*?\*/@s', '', $data);
|
|
|
|
// Remove whitespace around symbols.
|
|
|
|
$data = preg_replace('@\s*([{}:;,])\s+@', '\1', $data);
|
|
|
|
// Remove unnecessary semicolons.
|
|
|
|
$data = preg_replace('@;}@', '}', $data);
|
|
|
|
// Replace #rrggbb with #rgb when possible.
|
|
|
|
$data = preg_replace(
|
|
|
|
'@#([a-f0-9])\1([a-f0-9])\2([a-f0-9])\3@i',
|
|
|
|
'#\1\2\3',
|
|
|
|
$data);
|
|
|
|
$data = trim($data);
|
|
|
|
break;
|
|
|
|
case 'js':
|
|
|
|
$root = dirname(phutil_get_library_root('phabricator'));
|
|
|
|
$bin = $root.'/externals/javelin/support/jsxmin/jsxmin';
|
|
|
|
|
|
|
|
if (@file_exists($bin)) {
|
|
|
|
$future = new ExecFuture("{$bin} __DEV__:0");
|
|
|
|
$future->write($data);
|
|
|
|
list($err, $result) = $future->resolve();
|
|
|
|
if (!$err) {
|
|
|
|
$data = $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2011-01-25 18:59:31 +01:00
|
|
|
}
|