Возникла идея реализовать подстановку таймштампов для лучшего кеширования браузером (как это сделано в RoR: style.css?123123708). Сразу говорю - в регулярках я полный ноль, вот что получилось у меня.
<?php
class Controller extends CController
{
public $layout='application.views.layouts.main';
public $menu=array();
public $breadcrumbs=array();
public function processOutput($output)
{
$output = parent::processOutput($output);
$output = self::setCacheTimestamps($output);
return $output;
}
# private
/*
Set timestamps for browser caching
*/
private function setCacheTimestamps($output)
{
$stylesheets = array();
$javascripts = array();
$images = array();
preg_match_all('/(link|href)\=(\"|\')[^\"\'\>]+/i', $output, $media);
$css_buffer = preg_replace('/(link|href)(\"|\'|\=\"|\=\')(.*)/i',"$3",$media[0]);
preg_match_all('/(script|src)\=(\"|\')[^\"\'\>]+/i', $output, $media);
$js_buffer = preg_replace('/(script|src)(\"|\'|\=\"|\=\')(.*)/i',"$3",$media[0]);
preg_match_all('/(script|src)\=(\"|\')[^\"\'\>]+/i', $output, $media);
$img_buffer = preg_replace('/(img|src)(\"|\'|\=\"|\=\')(.*)/i',"$3",$media[0]);
foreach($css_buffer as $url) {
$info = pathinfo($url);
if (isset($info['extension']) && $info['extension'] == "css" && !preg_match('((https?|ftp|gopher|telnet|file|notes|ms-help)<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />(//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)', $url)) {
$_url = $url;
$_url = preg_replace('/\//i',"\/", $_url);
array_push($stylesheets, array( "path" => $url, "re" => $_url , "timestamp" => filemtime(Yii::app()->basePath . "/.." . $url)));
}
}
foreach($js_buffer as $url) {
$info = pathinfo($url);
if (isset($info['extension']) && $info['extension'] == "js" && !preg_match('((https?|ftp|gopher|telnet|file|notes|ms-help)<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />(//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)', $url) ) {
$_url = $url;
$_url = preg_replace('/\//i',"\/", $_url);
array_push($javascripts, array( "path" => $url, "re" => $_url , "timestamp" => filemtime(Yii::app()->basePath . "/.." . $url)));
}
}
foreach($img_buffer as $url) {
$info = pathinfo($url);
if (isset($info['extension']) && !preg_match('((https?|ftp|gopher|telnet|file|notes|ms-help)<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />(//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)', $url) ) {
$_url = $url;
$_url = preg_replace('/\//i',"\/", $_url);
array_push($images, array( "path" => Yii::app()->baseUrl . $url, "re" => $_url , "timestamp" => filemtime(Yii::app()->basePath . "/.." . $url)));
}
}
foreach($stylesheets as $css) {
$regexp = "/(link|href)\=(\"|\')".$css['re']."[^\'\>]+/i";
$output = preg_replace($regexp, 'href="'. $css["path"] .'?' . $css["timestamp"] . '"', $output);
}
foreach($javascripts as $js) {
$regexp = "/(script|src)\=(\"|\')".$js['re']."[^\'\>]+/i";
$output = preg_replace($regexp, 'src="'. $js["path"] .'?' . $js["timestamp"] . '"', $output);
}
foreach($images as $img) {
$regexp = "/(img|src)\=(\"|\')".$img['re']."[^\'\>]+/i";
$output = preg_replace($regexp, 'src="'. $img["path"] .'?' . $img["timestamp"] . '"', $output);
}
return $output;
}
}
Все работает. Только возник вот такой вопрос - как я понимаю, при каждом рендере страницы эта манипуляция будет отъедать некоторое кол-во времени для этого. Подскажите, можно ли как-то с помощью кеширования это дело ускорить? Тему об этом в доках я почитал, но так и не нашел способа для себя.