Changeset 6025
- Timestamp:
- 11/14/07 16:47:17 (10 months ago)
- Files:
-
- plugins/sfCombineFilterPlugin/trunk/README (modified) (1 diff)
- plugins/sfCombineFilterPlugin/trunk/lib/sfCombineFilter.class.php (modified) (6 diffs)
- plugins/sfCombineFilterPlugin/trunk/package.xml (modified) (2 diffs)
- plugins/sfCombineFilterPlugin/trunk/web/combine.php (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfCombineFilterPlugin/trunk/README
r6023 r6025 75 75 76 76 Initial public release. 77 78 === 2007-08-02 | 0.1.1 beta === 79 80 Changes by Kiril Angov: 81 - Fixes regular expression to match '^/css/' 82 - Fixes issues when included files outside of root css and js directories. 83 - New: Option to minify the js using JSMin. plugins/sfCombineFilterPlugin/trunk/lib/sfCombineFilter.class.php
r6023 r6025 3 3 * This file is part of the sfCombineFilter package. 4 4 * 5 * sfCombineFilter.class.php (c) 2007 Scott Meves. 5 * sfCombineFilter.class.php (c) 2007 Scott Meves. 6 6 * Combine.php Copyright (c) 2006 by Niels Leenheer 7 7 * … … 12 12 /** 13 13 * This filter combines requested js and css files into a single request each. 14 * 14 * 15 15 * @package sfCombineFilter 16 16 * @subpackage filter 17 17 * @author Scott Meves <scott@stereointeractive.com> 18 * 18 * 19 19 */ 20 20 class sfCombineFilter extends sfFilter 21 21 { 22 22 23 23 public function execute ($filterChain) 24 24 { 25 25 $filterChain->execute(); 26 26 27 sfLoader::loadHelpers('Asset');28 29 27 if ($this->getParameter('javascripts', true)) { 30 28 $this->getCombinedJavascripts(); 31 29 } 32 30 33 31 if ($this->getParameter('stylesheets', true)) { 34 $this->getCombinedStylesheets(); 32 $this->getCombinedStylesheets(); 35 33 } 36 34 } 37 35 38 36 protected function getCombinedJavascripts() 39 37 { 40 $response = $this->getContext()->getResponse(); 38 $response = $this->getContext()->getResponse(); 41 39 $sf_relative_url_root = $this->getContext()->getRequest()->getRelativeUrlRoot(); 42 40 $root_js_only = $this->getParameter('root_js_only', true); 43 41 44 42 $already_seen = array(); 45 43 $combined_sources = array(); 46 44 47 45 foreach (array('first', '', 'last') as $position) 48 46 { … … 59 57 60 58 $already_seen[$file] = 1; 61 59 62 60 if (is_array($options) && $this->isAbsolutePath($options)) 63 61 { 64 62 continue; 65 63 } 66 64 67 65 $path = _compute_public_path($file, 'js', 'js'); 68 66 69 67 if ((!$root_js_only && !strpos($path, '://')) || ($root_js_only && strpos($path, $sf_relative_url_root.'/js/') === 0)) { 70 $combined_sources[] = ($root_js_only ? str_replace($sf_relative_url_root.'/js/', '', $path) : $path);68 $combined_sources[] = ($root_js_only ? preg_replace("/^".str_replace('/', '\/', $sf_relative_url_root.'/js/')."/i", '', $path) : $path); 71 69 $response->getParameterHolder()->remove($file, 'helper/asset/auto/javascript'.($position ? '/'.$position : '')); 72 70 } … … 74 72 } 75 73 } 76 74 77 75 if (count($combined_sources)) { 78 76 $combined_sources_str = $sf_relative_url_root . '/js/packed/' . implode(',', $combined_sources); 79 77 $response->addJavascript($combined_sources_str, ''); 80 78 } 81 79 82 80 } 83 81 84 82 protected function getCombinedStylesheets() 85 83 { 86 $response = $this->getContext()->getResponse(); 84 $response = $this->getContext()->getResponse(); 87 85 $sf_relative_url_root = $this->getContext()->getRequest()->getRelativeUrlRoot(); 88 86 $root_css_only = $this->getParameter('root_css_only', true); 89 87 90 88 $already_seen = array(); 91 89 $combined_sources = array(); 92 90 93 91 foreach (array('first', '', 'last') as $position) 94 92 { … … 105 103 106 104 $already_seen[$file] = 1; 107 105 108 106 if (is_array($options) && ($this->isInvalidMediaType($options) || $this->isAbsolutePath($options))) 109 107 { 110 108 continue; 111 109 } 112 110 113 111 $path = _compute_public_path($file, 'css', 'css'); 114 112 115 113 if ((!$root_css_only && !strpos($path, '://')) || ($root_css_only && strpos($path, $sf_relative_url_root.'/css/') === 0)) { 116 $combined_sources[] = (!$root_css_only ? str_replace($sf_relative_url_root.'/css/', '', $path) : $path);114 $combined_sources[] = (!$root_css_only ? preg_replace("/^".str_replace('/', '\/', $sf_relative_url_root.'/css/')."/i", '', $path) : $path); 117 115 $response->getParameterHolder()->remove($file, 'helper/asset/auto/stylesheet'.($position ? '/'.$position : '')); 118 116 } … … 130 128 return isset($options['media']) && !in_array($options['media'], array('', 'all', 'screen')); 131 129 } 132 130 133 131 protected function isAbsolutePath($options) { 134 132 return isset($options['absolute']) && $options['absolute'] == true; plugins/sfCombineFilterPlugin/trunk/package.xml
r6023 r6025 11 11 <active>yes</active> 12 12 </lead> 13 <date>2007-0 7-26</date>14 <time> 11:34:37</time>13 <date>2007-08-01</date> 14 <time>23:50:02</time> 15 15 <version> 16 <release>0.1. 0</release>17 <api>0.1. 0</api>16 <release>0.1.1</release> 17 <api>0.1.1</api> 18 18 </version> 19 19 <stability> … … 25 25 <contents> 26 26 <dir name="/"> 27 <file md5sum="fd27c01a1eaa51a51f254ac518e0af00" name="lib/sfCombineFilter.class.php" role="data" /> 28 <file md5sum="0dbc029fd2b17de944edd6865effc420" name="web/combine.php" role="data" /> 29 <file md5sum="064987ac72d5a69a474a7fda995d2832" name="README" role="data" /> 27 <file md5sum="86e7cd2d5f0386b575804ed6332e6940" name="lib/sfCombineFilter.class.php" role="data" /> 28 <file md5sum="f3405423799e62528a9ff46a059e1315" name="lib/JSMin.class.php" role="data" /> 29 <file md5sum="05c3b05532b187a2a6e173d36f6aee19" name="web/combine.php" role="data" /> 30 <file md5sum="d870ba4c56e95678233ea66ec1bc3ea0" name="README" role="data" /> 30 31 <file md5sum="5a6e3a01bce673b38277f73dc1a8bab5" name="LICENSE" role="data" /> 31 32 </dir> plugins/sfCombineFilterPlugin/trunk/web/combine.php
r6023 r6025 13 13 * permit persons to whom the Software is furnished to do so, subject to 14 14 * the following conditions: 15 * 15 * 16 16 * The above copyright notice and this permission notice shall be 17 17 * included in all copies or substantial portions of the Software. … … 25 25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 26 */ 27 27 28 28 // Minor modifications by Scott Meves for use with sfCombineFilterPlugin. 29 29 30 define('SF_ROOT_DIR', realpath(dirname(__FILE__).'/../../..')); 31 require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php'); 32 30 33 $cache = true; 31 $cachedir = dirname(__FILE__) . '/../cache'; 32 $cssdir = dirname(__FILE__) . '/css'; 33 $jsdir = dirname(__FILE__) . '/js'; 34 $minify_js = true; 35 36 $cachedir = SF_ROOT_DIR.DIRECTORY_SEPARATOR.'cache'; 37 $webdir = SF_ROOT_DIR.DIRECTORY_SEPARATOR.'web'; 38 $cssdir = $webdir.DIRECTORY_SEPARATOR.'css'; 39 $jsdir = $webdir.DIRECTORY_SEPARATOR.'js'; 34 40 35 41 // Determine the directory and type we should use … … 39 45 break; 40 46 case 'javascript': 41 $dir = $jsdir; 47 $dir = $jsdir; 42 48 break; 43 49 default: 44 50 header ("HTTP/1.0 503 Not Implemented"); 45 51 exit; 46 }; 47 48 $base = realpath($dir); 52 } 49 53 50 54 $type = $_GET['type']; … … 53 57 array_shift($elements); 54 58 } 59 55 60 // Determine last modification date of the files 56 61 $lastmodified = 0; 57 62 63 $files = array(); 58 64 while (list(,$element) = each($elements)) { 59 $path = realpath($element); 60 if (0 === strpos($element, '/')) { 61 $path = realpath(dirname(__FILE__).$element); 65 $path = null; 66 if (substr($element, 0, 4) == '/sf/') { 67 $path = $sf_symfony_data_dir.DIRECTORY_SEPARATOR.'web'.$element; 68 } else if (substr($element, 0, 3) == 'sf/') { 69 $path = $sf_symfony_data_dir.DIRECTORY_SEPARATOR.'web'.DIRECTORY_SEPARATOR.$element; 70 } else if (0 === strpos($element, '/')) { 71 $path = realpath($webdir.$element); 62 72 } else { 63 $path = realpath($ base . '/' .$element);73 $path = realpath($dir.DIRECTORY_SEPARATOR.$element); 64 74 } 65 66 if (($type == 'javascript' && substr($path, -3) != '.js') || 75 76 if (($type == 'javascript' && substr($path, -3) != '.js') || 67 77 ($type == 'css' && substr($path, -4) != '.css')) { 68 78 header ("HTTP/1.0 403 Forbidden"); 69 exit; 79 exit; 70 80 } 71 81 … … 74 84 exit; 75 85 } 76 86 87 $files[] = $path; 77 88 $lastmodified = max($lastmodified, filemtime($path)); 78 89 } … … 82 93 header ("Etag: \"" . $hash . "\""); 83 94 84 if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && 85 stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"') 95 if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && 96 stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"') 86 97 { 87 98 // Return visit and no modifications, so do not send anything 88 99 header ("HTTP/1.0 304 Not Modified"); 89 100 header ('Content-Length: 0'); 90 } 91 else 101 } 102 else 92 103 { 104 93 105 // First time visit or files were modified 94 if ($cache) 106 if ($cache) 95 107 { 96 108 // Determine supported compression method … … 102 114 103 115 // Check for buggy versions of Internet Explorer 104 if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') && 116 if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') && 105 117 preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) { 106 118 $version = floatval($matches[1]); 107 119 108 120 if ($version < 6) 109 121 $encoding = 'none'; 110 111 if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1')) 122 123 if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1')) 112 124 $encoding = 'none'; 113 125 } 114 126 115 127 // Try the cache first to see if the combined files were already generated 116 128 117 129 $cachefile = 'cache-' . $hash . '.' . $type . ($encoding != 'none' ? '.' . $encoding : ''); 118 130 119 131 if (file_exists($cachedir . '/' . $cachefile)) { 120 132 if ($fp = fopen($cachedir . '/' . $cachefile, 'rb')) { … … 123 135 header ("Content-Encoding: " . $encoding); 124 136 } 125 137 126 138 header ("Content-Type: text/" . $type); 127 139 header ("Content-Length: " . filesize($cachedir . '/' . $cachefile)); 128 140 129 141 fpassthru($fp); 130 142 fclose($fp); … … 137 149 $contents = ''; 138 150 reset($elements); 139 while (list(,$element) = each($elements)) { 140 $path = realpath($element); 141 if (0 === strpos($element, '/')) { 142 $path = realpath(dirname(__FILE__).$element); 143 } else { 144 $path = realpath($base . '/' . $element); 145 } 151 foreach ($files as $path) { 146 152 $contents .= "\n\n" . file_get_contents($path); 147 153 } 148 154 155 if ($_GET['type'] == 'javascript' && $minify_js) { 156 include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'JSMin.class.php'; 157 try { 158 $contents = JSMin::minify($contents); 159 } catch (JSMinException $e) { 160 ; 161 } 162 } 163 164 149 165 // Send Content-Type 150 166 header ("Content-Type: text/" . $type); 151 152 if (isset($encoding) && $encoding != 'none') 167 168 if (isset($encoding) && $encoding != 'none') 153 169 { 154 170 // Send compressed contents … … 158 174 header ('Content-Length: ' . strlen($contents)); 159 175 echo $contents; 160 } 161 else 176 } 177 else 162 178 { 163 179 // Send regular contents … … 168 184 // Store cache 169 185 if ($cache) { 170 if ($fp = fopen($cachedir . '/' . $cachefile, 'wb')) { 171 fwrite($fp, $contents); 172 fclose($fp); 173 } 186 file_put_contents($cachedir . '/' . $cachefile, $contents); 174 187 } 175 188 }