Changeset 10092
- Timestamp:
- 07/03/08 15:46:16 (2 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
r6025 r10092 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
r6025 r10092 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 27 29 if ($this->getParameter('javascripts', true)) { 28 30 $this->getCombinedJavascripts(); 29 31 } 30 32 31 33 if ($this->getParameter('stylesheets', true)) { 32 $this->getCombinedStylesheets(); 34 $this->getCombinedStylesheets(); 33 35 } 34 36 } 35 37 36 38 protected function getCombinedJavascripts() 37 39 { 38 $response = $this->getContext()->getResponse(); 40 $response = $this->getContext()->getResponse(); 39 41 $sf_relative_url_root = $this->getContext()->getRequest()->getRelativeUrlRoot(); 40 42 $root_js_only = $this->getParameter('root_js_only', true); 41 43 42 44 $already_seen = array(); 43 45 $combined_sources = array(); 44 46 45 47 foreach (array('first', '', 'last') as $position) 46 48 { … … 57 59 58 60 $already_seen[$file] = 1; 59 61 60 62 if (is_array($options) && $this->isAbsolutePath($options)) 61 63 { 62 64 continue; 63 65 } 64 66 65 67 $path = _compute_public_path($file, 'js', 'js'); 66 68 67 69 if ((!$root_js_only && !strpos($path, '://')) || ($root_js_only && strpos($path, $sf_relative_url_root.'/js/') === 0)) { 68 $combined_sources[] = ($root_js_only ? preg_replace("/^".str_replace('/', '\/', $sf_relative_url_root.'/js/')."/i", '', $path) : $path);70 $combined_sources[] = ($root_js_only ? str_replace($sf_relative_url_root.'/js/', '', $path) : $path); 69 71 $response->getParameterHolder()->remove($file, 'helper/asset/auto/javascript'.($position ? '/'.$position : '')); 70 72 } … … 72 74 } 73 75 } 74 76 75 77 if (count($combined_sources)) { 76 78 $combined_sources_str = $sf_relative_url_root . '/js/packed/' . implode(',', $combined_sources); 77 79 $response->addJavascript($combined_sources_str, ''); 78 80 } 79 81 80 82 } 81 83 82 84 protected function getCombinedStylesheets() 83 85 { 84 $response = $this->getContext()->getResponse(); 86 $response = $this->getContext()->getResponse(); 85 87 $sf_relative_url_root = $this->getContext()->getRequest()->getRelativeUrlRoot(); 86 88 $root_css_only = $this->getParameter('root_css_only', true); 87 89 88 90 $already_seen = array(); 89 91 $combined_sources = array(); 90 92 91 93 foreach (array('first', '', 'last') as $position) 92 94 { … … 103 105 104 106 $already_seen[$file] = 1; 105 107 106 108 if (is_array($options) && ($this->isInvalidMediaType($options) || $this->isAbsolutePath($options))) 107 109 { 108 110 continue; 109 111 } 110 112 111 113 $path = _compute_public_path($file, 'css', 'css'); 112 114 113 115 if ((!$root_css_only && !strpos($path, '://')) || ($root_css_only && strpos($path, $sf_relative_url_root.'/css/') === 0)) { 114 $combined_sources[] = (!$root_css_only ? preg_replace("/^".str_replace('/', '\/', $sf_relative_url_root.'/css/')."/i", '', $path) : $path);116 $combined_sources[] = (!$root_css_only ? str_replace($sf_relative_url_root.'/css/', '', $path) : $path); 115 117 $response->getParameterHolder()->remove($file, 'helper/asset/auto/stylesheet'.($position ? '/'.$position : '')); 116 118 } … … 128 130 return isset($options['media']) && !in_array($options['media'], array('', 'all', 'screen')); 129 131 } 130 132 131 133 protected function isAbsolutePath($options) { 132 134 return isset($options['absolute']) && $options['absolute'] == true; plugins/sfCombineFilterPlugin/trunk/package.xml
r6025 r10092 11 11 <active>yes</active> 12 12 </lead> 13 <date>2007-0 8-01</date>14 <time> 23:50:02</time>13 <date>2007-07-26</date> 14 <time>11:34:37</time> 15 15 <version> 16 <release>0.1. 1</release>17 <api>0.1. 1</api>16 <release>0.1.0</release> 17 <api>0.1.0</api> 18 18 </version> 19 19 <stability> … … 25 25 <contents> 26 26 <dir name="/"> 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" /> 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" /> 31 30 <file md5sum="5a6e3a01bce673b38277f73dc1a8bab5" name="LICENSE" role="data" /> 32 31 </dir> plugins/sfCombineFilterPlugin/trunk/web/combine.php
r6025 r10092 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 33 30 $cache = true; 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'; 31 $cachedir = dirname(__FILE__) . '/../cache'; 32 $cssdir = dirname(__FILE__) . '/css'; 33 $jsdir = dirname(__FILE__) . '/js'; 40 34 41 35 // Determine the directory and type we should use … … 45 39 break; 46 40 case 'javascript': 47 $dir = $jsdir; 41 $dir = $jsdir; 48 42 break; 49 43 default: 50 44 header ("HTTP/1.0 503 Not Implemented"); 51 45 exit; 52 } 46 }; 47 48 $base = realpath($dir); 53 49 54 50 $type = $_GET['type']; … … 57 53 array_shift($elements); 58 54 } 59 60 55 // Determine last modification date of the files 61 56 $lastmodified = 0; 62 57 63 $files = array();64 58 while (list(,$element) = each($elements)) { 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); 59 $path = realpath($element); 60 if (0 === strpos($element, '/')) { 61 $path = realpath(dirname(__FILE__).$element); 72 62 } else { 73 $path = realpath($ dir.DIRECTORY_SEPARATOR.$element);63 $path = realpath($base . '/' . $element); 74 64 } 75 76 if (($type == 'javascript' && substr($path, -3) != '.js') || 65 66 if (($type == 'javascript' && substr($path, -3) != '.js') || 77 67 ($type == 'css' && substr($path, -4) != '.css')) { 78 68 header ("HTTP/1.0 403 Forbidden"); 79 exit; 69 exit; 80 70 } 81 71 … … 84 74 exit; 85 75 } 86 87 $files[] = $path; 76 88 77 $lastmodified = max($lastmodified, filemtime($path)); 89 78 } … … 93 82 header ("Etag: \"" . $hash . "\""); 94 83 95 if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && 96 stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"') 84 if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && 85 stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"') 97 86 { 98 87 // Return visit and no modifications, so do not send anything 99 88 header ("HTTP/1.0 304 Not Modified"); 100 89 header ('Content-Length: 0'); 101 } 102 else 90 } 91 else 103 92 { 104 105 93 // First time visit or files were modified 106 if ($cache) 94 if ($cache) 107 95 { 108 96 // Determine supported compression method … … 114 102 115 103 // Check for buggy versions of Internet Explorer 116 if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') && 104 if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') && 117 105 preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) { 118 106 $version = floatval($matches[1]); 119 107 120 108 if ($version < 6) 121 109 $encoding = 'none'; 122 123 if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1')) 110 111 if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1')) 124 112 $encoding = 'none'; 125 113 } 126 114 127 115 // Try the cache first to see if the combined files were already generated 128 116 129 117 $cachefile = 'cache-' . $hash . '.' . $type . ($encoding != 'none' ? '.' . $encoding : ''); 130 118 131 119 if (file_exists($cachedir . '/' . $cachefile)) { 132 120 if ($fp = fopen($cachedir . '/' . $cachefile, 'rb')) { … … 135 123 header ("Content-Encoding: " . $encoding); 136 124 } 137 125 138 126 header ("Content-Type: text/" . $type); 139 127 header ("Content-Length: " . filesize($cachedir . '/' . $cachefile)); 140 128 141 129 fpassthru($fp); 142 130 fclose($fp); … … 149 137 $contents = ''; 150 138 reset($elements); 151 foreach ($files as $path) { 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 } 152 146 $contents .= "\n\n" . file_get_contents($path); 153 147 } 154 148 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 165 149 // Send Content-Type 166 150 header ("Content-Type: text/" . $type); 167 168 if (isset($encoding) && $encoding != 'none') 151 152 if (isset($encoding) && $encoding != 'none') 169 153 { 170 154 // Send compressed contents … … 174 158 header ('Content-Length: ' . strlen($contents)); 175 159 echo $contents; 176 } 177 else 160 } 161 else 178 162 { 179 163 // Send regular contents … … 184 168 // Store cache 185 169 if ($cache) { 186 file_put_contents($cachedir . '/' . $cachefile, $contents); 170 if ($fp = fopen($cachedir . '/' . $cachefile, 'wb')) { 171 fwrite($fp, $contents); 172 fclose($fp); 173 } 187 174 } 188 175 }