/** * 处理压缩资源内容。调用父类APF_Resource_ResourcesController的handle_request方法, * 获取资源内容,在调用压缩服务实现压缩,最终给浏览器返回压缩后的资源内容。 * @see APF_Resource_ResourcesController::handle_request() */ public function handle_request() { // 开启缓冲区 ob_start(); parent::handle_request(); $uri = $_SERVER['REQUEST_URI']; // 获取资源内容 $content = ob_get_contents(); ob_end_clean(); // 读取压缩服务配置 $mpf = MPF::get_instance(); $host = @$mpf->get_config(self::CONFIG_N_YUICOMPRESSOR_HOST, parent::CONFIG_F_RESOURCE); $port = @$mpf->get_config(self::CONFIG_N_YUICOMPRESSOR_PORT, parent::CONFIG_F_RESOURCE); if (!$host || !$port) { // 为配置压缩服务则直接输出原始内容 echo $content; return; } // 调用压缩服务,输出压缩结果 $fp = @fsockopen($host, $port); if (!$fp) { // 连接失败则直接输出内容 echo $content; return; } // 发送要压缩的内容 fwrite($fp, "{$uri}\n"); fwrite($fp, $content); fwrite($fp, "\n\n"); // 读取并显示压缩后的内容 while (!feof($fp)) { echo fread($fp, 8192); } fclose($fp); }
public function handle_request() { // TODO: to handling 304 header by an interceptor? $mpf = MPF::get_instance(); $response = $mpf->get_response(); $cache_control = $this->get_cache_control(); if ($cache_control) { $response->set_header("Cache-Control", $cache_control); } $expires = @$mpf->get_config(self::CONFIG_N_EXPIRES, self::CONFIG_F_RESOURCE); $last_modified = @$mpf->get_config(self::CONFIG_N_LAST_MODIFIED, self::CONFIG_F_RESOURCE); if (isset($last_modified)) { $etag = '"' . dechex($last_modified) . '"'; } if (isset($etag)) { $none_match = @$_SERVER['HTTP_IF_NONE_MATCH']; if ($none_match && $none_match == $etag) { $response->set_header("HTTP/1.1", "304 ETag Matched", "304"); exit; } } if (isset($last_modified) && isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { $tmp = explode(";", $_SERVER['HTTP_IF_MODIFIED_SINCE']); $modified_since = @strtotime($tmp[0]); if ($modified_since && $modified_since >= $last_modified) { $response->set_header("HTTP/1.1", "304 Not Modified", "304"); exit; } } if (isset($expires)) { $response->set_header("Expires", gmdate("D, d M Y H:i:s", $expires) . " GMT"); } if (isset($last_modified)) { $response->set_header("ETag", $etag); $response->set_header("Last-Modified", gmdate("D, d M Y H:i:s", $last_modified) . " GMT"); } return parent::handle_request(); }
public function get_prefetch_javascript_uri($class_name) { return MPF_Resource_ResourcesController::build_boundable_uri($class_name, "js"); }
/** * Returns the uri of boundable resource * * @param string $resource Page name * @param string $ext File extention without prefix dot * @return string */ public static function build_boundable_uri($resource, $ext) { $mpf = MPF::get_instance(); $version = @$mpf->get_config(self::CONFIG_N_VERSION, self::CONFIG_F_RESOURCE); $prefix = @$mpf->get_config(self::CONFIG_N_PREFIX_URI, self::CONFIG_F_RESOURCE); if (!isset($prefix)) { trigger_error("Unable to get config \"" . self::CONFIG_N_PREFIX_URI . "\" from \"" . self::CONFIG_F_RESOURCE . "\"", E_USER_ERROR); } $type = $mpf->get_config(self::CONFIG_N_RESOURCE_TYPE_BOUNDABLE, self::CONFIG_F_RESOURCE); if (!isset($type)) { trigger_error("Unable to get config \"" . self::CONFIG_N_RESOURCE_TYPE_BOUNDABLE . "\" from \"" . self::CONFIG_F_RESOURCE . "\"", E_USER_ERROR); } if (isset($version)) { $uri = "{$prefix}/{$version}/{$type}/{$resource}.{$ext}"; } else { $uri = "{$prefix}/{$type}/{$resource}.{$ext}"; } if (self::is_use_redis()) { mpf_require_class('MPF_Cache_Factory'); $objRedis = MPF_Cache_Factory::get_instance()->get_redis('redis'); $strContentHash = $objRedis->get('url_' . $uri); if (false === $strContentHash) { if (self::URL_FORCE_HASH) { //force hash $o = new MPF_Resource_ResourcesController(); $strContentHash = $o->url2hash($type, $resource, $ext, $uri, true); if ('' == $strContentHash) { return $uri; } $type = $mpf->get_config('resource_type_hash', self::CONFIG_F_RESOURCE); return $prefix . '/' . $type . '/' . $strContentHash . '.' . $ext . '#' . $resource . (isset($version) ? '(' . $version . ')' : ''); } return $uri; } else { $type = $mpf->get_config('resource_type_hash', self::CONFIG_F_RESOURCE); return $prefix . '/' . $type . '/' . $strContentHash . '.' . $ext; } } else { return $uri; } }
public function get_boundable_javascripts_uri() { return MPF_Resource_ResourcesController::build_boundable_uri($this->get_page_class(), "js"); }