Development

Changeset 6025

You must first sign up to be able to contribute.

Changeset 6025

Show
Ignore:
Timestamp:
11/14/07 16:47:17 (10 months ago)
Author:
superhaggis
Message:

Fixed regular expressions.
Fixed issues when included files were outside of root CSS and JS directories.
Added option to minify the JS using JSMin.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfCombineFilterPlugin/trunk/README

    r6023 r6025  
    7575 
    7676Initial public release. 
     77 
     78=== 2007-08-02 | 0.1.1 beta === 
     79 
     80Changes 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  
    33 * This file is part of the sfCombineFilter package. 
    44 * 
    5  * sfCombineFilter.class.php (c) 2007 Scott Meves.  
     5 * sfCombineFilter.class.php (c) 2007 Scott Meves. 
    66 * Combine.php Copyright (c) 2006 by Niels Leenheer 
    77 * 
     
    1212/** 
    1313 * This filter combines requested js and css files into a single request each. 
    14  *  
     14 * 
    1515 * @package      sfCombineFilter 
    1616 * @subpackage   filter 
    1717 * @author       Scott Meves <scott@stereointeractive.com> 
    18  *  
     18 * 
    1919 */ 
    2020class sfCombineFilter extends sfFilter 
    2121{ 
    22    
     22 
    2323  public function execute ($filterChain) 
    2424  { 
    2525    $filterChain->execute(); 
    2626 
    27     sfLoader::loadHelpers('Asset'); 
    28          
    2927    if ($this->getParameter('javascripts', true)) { 
    3028      $this->getCombinedJavascripts(); 
    3129    } 
    32      
     30 
    3331    if ($this->getParameter('stylesheets', true)) { 
    34       $this->getCombinedStylesheets();   
     32      $this->getCombinedStylesheets(); 
    3533    } 
    3634  } 
    37    
     35 
    3836  protected function getCombinedJavascripts() 
    3937  { 
    40     $response = $this->getContext()->getResponse();     
     38    $response = $this->getContext()->getResponse(); 
    4139    $sf_relative_url_root = $this->getContext()->getRequest()->getRelativeUrlRoot(); 
    4240    $root_js_only = $this->getParameter('root_js_only', true); 
    43      
     41 
    4442    $already_seen = array(); 
    4543    $combined_sources = array(); 
    46      
     44 
    4745    foreach (array('first', '', 'last') as $position) 
    4846    { 
     
    5957 
    6058          $already_seen[$file] = 1; 
    61            
     59 
    6260          if (is_array($options) && $this->isAbsolutePath($options)) 
    6361          { 
    6462            continue; 
    6563          } 
    66            
     64 
    6765          $path = _compute_public_path($file, 'js', 'js'); 
    68                      
     66 
    6967          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); 
    7169            $response->getParameterHolder()->remove($file, 'helper/asset/auto/javascript'.($position ? '/'.$position : '')); 
    7270          } 
     
    7472      } 
    7573    } 
    76      
     74 
    7775    if (count($combined_sources)) { 
    7876      $combined_sources_str = $sf_relative_url_root . '/js/packed/' . implode(',', $combined_sources); 
    7977      $response->addJavascript($combined_sources_str, ''); 
    8078    } 
    81      
     79 
    8280  } 
    83    
     81 
    8482  protected function getCombinedStylesheets() 
    8583  { 
    86     $response = $this->getContext()->getResponse();     
     84    $response = $this->getContext()->getResponse(); 
    8785    $sf_relative_url_root = $this->getContext()->getRequest()->getRelativeUrlRoot(); 
    8886    $root_css_only = $this->getParameter('root_css_only', true); 
    89          
     87 
    9088    $already_seen = array(); 
    9189    $combined_sources = array(); 
    92      
     90 
    9391    foreach (array('first', '', 'last') as $position) 
    9492    { 
     
    105103 
    106104          $already_seen[$file] = 1; 
    107            
     105 
    108106          if (is_array($options) && ($this->isInvalidMediaType($options) || $this->isAbsolutePath($options))) 
    109107          { 
    110108            continue; 
    111109          } 
    112            
     110 
    113111          $path = _compute_public_path($file, 'css', 'css'); 
    114            
     112 
    115113          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); 
    117115            $response->getParameterHolder()->remove($file, 'helper/asset/auto/stylesheet'.($position ? '/'.$position : '')); 
    118116          } 
     
    130128    return isset($options['media']) && !in_array($options['media'], array('', 'all', 'screen')); 
    131129  } 
    132    
     130 
    133131  protected function isAbsolutePath($options) { 
    134132    return isset($options['absolute']) && $options['absolute'] == true; 
  • plugins/sfCombineFilterPlugin/trunk/package.xml

    r6023 r6025  
    1111  <active>yes</active> 
    1212 </lead> 
    13  <date>2007-07-26</date> 
    14  <time>11:34:37</time> 
     13 <date>2007-08-01</date> 
     14 <time>23:50:02</time> 
    1515 <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> 
    1818 </version> 
    1919 <stability> 
     
    2525 <contents> 
    2626  <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" /> 
    3031   <file md5sum="5a6e3a01bce673b38277f73dc1a8bab5" name="LICENSE" role="data" /> 
    3132  </dir> 
  • plugins/sfCombineFilterPlugin/trunk/web/combine.php

    r6023 r6025  
    1313 * permit persons to whom the Software is furnished to do so, subject to 
    1414 * the following conditions: 
    15  *  
     15 * 
    1616 * The above copyright notice and this permission notice shall be 
    1717 * included in all copies or substantial portions of the Software. 
     
    2525 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
    2626 */ 
    27   
     27 
    2828// Minor modifications by Scott Meves for use with sfCombineFilterPlugin. 
    2929 
     30define('SF_ROOT_DIR',    realpath(dirname(__FILE__).'/../../..')); 
     31require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php'); 
     32 
    3033$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'; 
    3440 
    3541// Determine the directory and type we should use 
     
    3945    break; 
    4046  case 'javascript': 
    41     $dir = $jsdir;     
     47    $dir = $jsdir; 
    4248    break; 
    4349  default: 
    4450    header ("HTTP/1.0 503 Not Implemented"); 
    4551    exit; 
    46 }; 
    47  
    48 $base = realpath($dir); 
     52
    4953 
    5054$type = $_GET['type']; 
     
    5357  array_shift($elements); 
    5458} 
     59 
    5560// Determine last modification date of the files 
    5661$lastmodified = 0; 
    5762 
     63$files = array(); 
    5864while (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); 
    6272  } else { 
    63     $path = realpath($base . '/' . $element); 
     73    $path = realpath($dir.DIRECTORY_SEPARATOR.$element); 
    6474  } 
    65    
    66   if (($type == 'javascript' && substr($path, -3) != '.js') ||  
     75 
     76  if (($type == 'javascript' && substr($path, -3) != '.js') || 
    6777    ($type == 'css' && substr($path, -4) != '.css')) { 
    6878    header ("HTTP/1.0 403 Forbidden"); 
    69     exit;   
     79    exit; 
    7080  } 
    7181 
     
    7484    exit; 
    7585  } 
    76    
     86 
     87  $files[] = $path; 
    7788  $lastmodified = max($lastmodified, filemtime($path)); 
    7889} 
     
    8293header ("Etag: \"" . $hash . "\""); 
    8394 
    84 if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&  
    85   stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"')  
     95if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && 
     96  stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"') 
    8697{ 
    8798  // Return visit and no modifications, so do not send anything 
    8899  header ("HTTP/1.0 304 Not Modified"); 
    89100  header ('Content-Length: 0'); 
    90 }  
    91 else  
     101} 
     102else 
    92103{ 
     104 
    93105  // First time visit or files were modified 
    94   if ($cache)  
     106  if ($cache) 
    95107  { 
    96108    // Determine supported compression method 
     
    102114 
    103115    // Check for buggy versions of Internet Explorer 
    104     if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') &&  
     116    if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') && 
    105117      preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) { 
    106118      $version = floatval($matches[1]); 
    107        
     119 
    108120      if ($version < 6) 
    109121        $encoding = 'none'; 
    110          
    111       if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1'))  
     122 
     123      if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1')) 
    112124        $encoding = 'none'; 
    113125    } 
    114      
     126 
    115127    // Try the cache first to see if the combined files were already generated 
    116128 
    117129    $cachefile = 'cache-' . $hash . '.' . $type . ($encoding != 'none' ? '.' . $encoding : ''); 
    118      
     130 
    119131    if (file_exists($cachedir . '/' . $cachefile)) { 
    120132      if ($fp = fopen($cachedir . '/' . $cachefile, 'rb')) { 
     
    123135          header ("Content-Encoding: " . $encoding); 
    124136        } 
    125        
     137 
    126138        header ("Content-Type: text/" . $type); 
    127139        header ("Content-Length: " . filesize($cachedir . '/' . $cachefile)); 
    128    
     140 
    129141        fpassthru($fp); 
    130142        fclose($fp); 
     
    137149  $contents = ''; 
    138150  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) { 
    146152    $contents .= "\n\n" . file_get_contents($path); 
    147153  } 
    148154 
     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 
    149165  // Send Content-Type 
    150166  header ("Content-Type: text/" . $type); 
    151    
    152   if (isset($encoding) && $encoding != 'none')  
     167 
     168  if (isset($encoding) && $encoding != 'none') 
    153169  { 
    154170    // Send compressed contents 
     
    158174    header ('Content-Length: ' . strlen($contents)); 
    159175    echo $contents; 
    160   }  
    161   else  
     176  } 
     177  else 
    162178  { 
    163179    // Send regular contents 
     
    168184  // Store cache 
    169185  if ($cache) { 
    170     if ($fp = fopen($cachedir . '/' . $cachefile, 'wb')) { 
    171       fwrite($fp, $contents); 
    172       fclose($fp); 
    173     } 
     186    file_put_contents($cachedir . '/' . $cachefile, $contents); 
    174187  } 
    175188}