Example #1
0
 /**
  * Компилирует медиа файлы
  * @todo сделать возможность встройки картинок base64
  * @param array $files
  * @param type $extension
  * @return boolean|string
  * @throws Kohana_Exception 
  */
 protected function compile_media(array $files, $extension, $no_compress_scripts = array())
 {
     if (count($files) == 0) {
         return false;
     }
     $files_sum = $files;
     sort($files_sum);
     $sum = crc32(implode($files_sum));
     unset($files_sum);
     $uri = "media/{$sum}.{$extension}";
     $minified_file = DOCROOT . $uri;
     if (!file_exists($minified_file)) {
         $files_paths = array();
         foreach ($files as $file) {
             $file_path = Kohana::find_file("media", $file, $extension);
             if ($file_path === false) {
                 if (file_exists($file)) {
                     $file_path = $file;
                 } else {
                     throw new Kohana_Exception("media file '{file}.{extension}' not founded", array("{file}" => $file, "{extension}" => $extension));
                 }
             }
             $files_paths[] = array('file' => $file, 'path' => $file_path);
         }
         $sources = array();
         foreach ($files_paths as $file_path_data) {
             $file_path = $file_path_data['path'];
             $file1 = $file_path_data['file'];
             if (file_exists($file_path)) {
                 if (array_search($file1, $no_compress_scripts) === false) {
                     if ($extension == self::JS_EXT) {
                         $sources[] = "\n\n/*{$file_path}*/\n\n" . JSMin::minify(file_get_contents($file_path));
                     } else {
                         $minified_css = CssMin::minify(file_get_contents($file_path));
                         //Надо заменить относительные пути в css
                         $dir_url = Door::path_to_uri(dirname($file_path));
                         $dir_path = dirname($file_path);
                         /*if(preg_match_all("/url\\(([^\\)]+)\\)/", $minified_css, $results))
                         		{
                         			for($i = 0; $i < count($results[0]); $i++)
                         			{
                         				$url = str_replace(array('"',"'"),"",$results[1][$i]);
                         				if(strpos($url, "data:") === false)
                         				{
                         					$from = $results[0][$i];
                         					$to = "url(/".$dir_url."/".$url.")";
                         					$minified_css = str_replace($from, $to, $minified_css);
                         				}
                         			}
                         		}*/
                         if (preg_match_all("/url\\(([^\\)]+)\\)/", $minified_css, $results)) {
                             for ($i = 0; $i < count($results[0]); $i++) {
                                 $url = str_replace(array('"', "'"), "", $results[1][$i]);
                                 if (strpos($url, "data:") === false && strpos($url, "http:") === false) {
                                     $path_to_media_file = $dir_path . "/" . $url;
                                     if (!file_exists($path_to_media_file)) {
                                         continue;
                                     }
                                     $size = null;
                                     $from = $results[0][$i];
                                     $to = "";
                                     try {
                                         $size = getimagesize($path_to_media_file);
                                     } catch (Exception $e) {
                                     }
                                     if ($size !== false && filesize($path_to_media_file) < 5120) {
                                         $to = "url(\"data:{$size['mime']};base64," . base64_encode(file_get_contents($path_to_media_file)) . "\")";
                                     } else {
                                         $to = "url(/" . $dir_url . "/" . $url . ")";
                                     }
                                     $minified_css = str_replace($from, $to, $minified_css);
                                 }
                             }
                         }
                         while (preg_match_all("#url\\([^\\)]*/([^/]+)/\\.\\./#", $minified_css, $results)) {
                             for ($i = 0; $i < count($results[0]); $i++) {
                                 $minified_css = str_replace("/" . $results[1][$i] . "/../", "/", $minified_css);
                             }
                         }
                         $sources[] = "\n\n" . $minified_css;
                     }
                 } else {
                     $sources[] = file_get_contents($file_path);
                 }
             } else {
                 throw new Kohana_Exception("file '{file}' not founded", array("{file}" => $file_path));
             }
         }
         $minified = implode("", $sources);
         unset($sources);
         file_put_contents($minified_file, $minified);
     }
     return $minified_file;
 }