8 public static function grabCssToSingleFile($html, $folder, $expirationTime = 3600)
10 $css = self::findCss($html);
12 $cssContentFile = $folder .
'/' . md5(serialize($css)) .
'.css';
13 if (!file_exists($cssContentFile) || time() - filemtime($cssContentFile) > $expirationTime) {
15 foreach ($css as $cssUrl) {
16 $content = self::downloadCss($cssUrl);
17 $cssContent .= $content;
19 file_put_contents($cssContentFile, $cssContent);
21 return $cssContentFile;
27 public static function findCss($html)
30 preg_match_all(
'/(<link.*\/>)/', $html, $links);
31 if (!empty($links[0])) {
32 foreach ($links[0] as $l) {
33 preg_match_all(
'/\s+(?<name>.*?)=(?<value>".*?")/', $l, $rawAttributes);
34 $attributes = array();
35 for ($i = 0; $i < count($rawAttributes[
'name']); $i++) {
36 $attributes[$rawAttributes[
'name'][$i]] = trim($rawAttributes[
'value'][$i],
' "');
38 if (isset($attributes[
'rel'], $attributes[
'href']) && $attributes[
'rel'] ==
'stylesheet') {
39 $css[] = $attributes[
'href'];
46 private static function downloadCss($file)
48 if (!preg_match(
'/^(http|https):/', $file)) {
49 $file = Yii::app()->createAbsoluteUrl($file);
51 return file_get_contents($file);