Author: Jonathan.Wage
Date: 2010-01-11 15:49:05 +0100 (Mon, 11 Jan 2010)
New Revision: 26476

Added:
   plugins/sfSympalPlugin/trunk/lib/minify/
   plugins/sfSympalPlugin/trunk/lib/minify/sfSympalMinifier.class.php
Modified:
   plugins/sfSympalPlugin/trunk/config/app.yml
   plugins/sfSympalPlugin/trunk/lib/helper/SympalHelper.php
   plugins/sfSympalPlugin/trunk/lib/plugins/
   
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAdminPlugin/modules/sympal_dashboard/templates/indexSuccess.php
   
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAdminPlugin/templates/admin.php
   plugins/sfSympalPlugin/trunk/templates/default.php
   plugins/sfSympalPlugin/trunk/templates/sympal.php
   plugins/sfSympalPlugin/trunk/templates/wordpress.php
Log:
[1.4][sfSympalPlugin][1.0] Initial entry of very simple js and css minifier


Modified: plugins/sfSympalPlugin/trunk/config/app.yml
===================================================================
--- plugins/sfSympalPlugin/trunk/config/app.yml 2010-01-11 14:26:04 UTC (rev 
26475)
+++ plugins/sfSympalPlugin/trunk/config/app.yml 2010-01-11 14:49:05 UTC (rev 
26476)
@@ -1,5 +1,9 @@
 all:
   sympal_config:
+    # Minify css and javascript
+    minifier:
+      enabled: true
+
     # Configure some defaults for inline editing
     inline_editing:
       default_column_form: sfSympalInlineEditContentColumnForm

Modified: plugins/sfSympalPlugin/trunk/lib/helper/SympalHelper.php
===================================================================
--- plugins/sfSympalPlugin/trunk/lib/helper/SympalHelper.php    2010-01-11 
14:26:04 UTC (rev 26475)
+++ plugins/sfSympalPlugin/trunk/lib/helper/SympalHelper.php    2010-01-11 
14:49:05 UTC (rev 26476)
@@ -1,5 +1,17 @@
 <?php
 
+function sympal_minify()
+{
+  if (sfSympalConfig::get('minifier', 'enabled', true))
+  {
+    $minifier = new sfSympalMinifier(
+      sfContext::getInstance()->getResponse(),
+      sfContext::getInstance()->getRequest()
+    );
+    $minifier->minify();
+  }
+}
+
 function sympal_use_jquery($plugins = array())
 {
   sfSympalToolkit::useJQuery($plugins);

Added: plugins/sfSympalPlugin/trunk/lib/minify/sfSympalMinifier.class.php
===================================================================
--- plugins/sfSympalPlugin/trunk/lib/minify/sfSympalMinifier.class.php          
                (rev 0)
+++ plugins/sfSympalPlugin/trunk/lib/minify/sfSympalMinifier.class.php  
2010-01-11 14:49:05 UTC (rev 26476)
@@ -0,0 +1,97 @@
+<?php
+
+class sfSympalMinifier
+{
+  private
+    $_response,
+    $_request;
+
+  public function __construct(sfWebResponse $response, sfWebRequest $request)
+  {
+    $this->_response = $response;
+    $this->_request = $request;
+  }
+
+  public function minify()
+  {
+    $this->_minifyFiles($this->_response->getJavascripts(), 'js');
+    $this->_minifyFiles($this->_response->getStylesheets(), 'css');
+  }
+
+  private function _minifyFiles(array $files, $type)
+  {
+    if ($files)
+    {
+      $typeName = $type == 'js' ? 'Javascript' : 'Stylesheet';
+      $filename = md5(serialize($files)).'.'.$type;
+      $webPath = '/cache/'.$type.'/'.$filename;
+      $cachedPath = sfConfig::get('sf_web_dir').$webPath;
+      if (!file_exists($cachedPath))
+      {
+        $minified = '';
+        foreach ($files as $file => $options)
+        {
+          $path = sfConfig::get('sf_web_dir').'/'.$file;
+          $minified .= 
"\n\n".$this->{'_minify'.$typeName}(file_get_contents($path), 
$this->_request->getUriPrefix().$this->_request->getRelativeUrlRoot().$file);
+        }
+
+        if (!is_dir($dir = dirname($cachedPath)))
+        {
+          mkdir($dir, 0777, true);
+        }
+        file_put_contents($cachedPath, $minified);
+      }
+    
+      foreach ($this->_response->{'get'.$typeName.'s'}() as $file => $options)
+      {
+        $this->_response->{'remove'.$typeName}($file);
+      }
+      $this->_response->{'add'.$typeName}($webPath);
+    }
+  }
+
+  private function _minifyJavascript($javascript, $path)
+  {
+    return $javascript;
+  }
+
+  private function _minifyStylesheet($stylesheet, $path)
+  {
+    $stylesheet = $this->_fixCssPaths($stylesheet, $path);
+    return str_replace("\n", null,
+      preg_replace(array("/\\;\s/", "/\s+\{\\s+/", "/\\:\s+\\#/", "/,\s+/i", 
"/\\:\s+\\\'/i", "/\\:\s+([0-9]+|[A-F]+)/i"), array(';', '{', ':#', ',', ":\'", 
":$1"),
+        preg_replace(array("/\/\*[\d\D]*?\*\/|\t+/", "/\s+/", "/\}\s+/"), 
array(null, ' ', "}\n"),
+          str_replace("\r\n", "\n", trim($stylesheet))
+        )
+      )
+    );
+  }
+
+  private function _fixCssPaths($content, $path)
+  {
+    if (preg_match_all("/url\(\s?[\'|\"]?(.+)[\'|\"]?\s?\)/ix", $content, 
$urlMatches))
+    {
+      $urlMatches = array_unique( $urlMatches[1] );
+      $cssPathArray = explode('/', $path);
+      
+      // pop the css file name
+      array_pop( $cssPathArray );
+      $cssPathCount   = count( $cssPathArray );
+
+      foreach( $urlMatches as $match )
+      {
+        $match = str_replace( array('"', "'"), '', $match );
+        // replace path if it is relative
+        if ( $match[0] !== '/' && strpos( $match, 'http:' ) === false )
+        {
+          $relativeCount = substr_count( $match, '../' );
+          $cssPathSlice = $relativeCount === 0 ? $cssPathArray : 
array_slice($cssPathArray  , 0, $cssPathCount - $relativeCount);
+          $newMatchPath = implode('/', $cssPathSlice) . '/' . 
str_replace('../', '', $match);
+          $content = str_replace($match, $newMatchPath, $content);
+        }
+      }
+    }
+    
+    return $content;
+  }
+}
\ No newline at end of file


Property changes on: 
plugins/sfSympalPlugin/trunk/lib/minify/sfSympalMinifier.class.php
___________________________________________________________________
Added: svn:executable
   + *


Property changes on: plugins/sfSympalPlugin/trunk/lib/plugins
___________________________________________________________________
Modified: svn:externals
   - sfImageTransformPlugin 
http://svn.symfony-project.com/plugins/sfImageTransformPlugin/trunk/
sfThumbnailPlugin http://svn.symfony-project.com/plugins/sfThumbnailPlugin/trunk
sfJqueryReloadedPlugin 
http://svn.symfony-project.com/plugins/sfJqueryReloadedPlugin/1.2/trunk
sfDoctrineGuardPlugin 
http://svn.symfony-project.com/plugins/sfDoctrineGuardPlugin/trunk
sfFormExtraPlugin 
http://svn.symfony-project.com/plugins/sfFormExtraPlugin/branches/1.3
sfTaskExtraPlugin http://svn.symfony-project.com/plugins/sfTaskExtraPlugin/trunk
sfFeed2Plugin http://svn.symfony-project.com/plugins/sfFeed2Plugin/branches/1.2
sfWebBrowserPlugin 
http://svn.symfony-project.com/plugins/sfWebBrowserPlugin/trunk

   + sfImageTransformPlugin 
http://svn.symfony-project.com/plugins/sfImageTransformPlugin/trunk/
sfThumbnailPlugin http://svn.symfony-project.com/plugins/sfThumbnailPlugin/trunk
sfJqueryReloadedPlugin 
http://svn.symfony-project.com/plugins/sfJqueryReloadedPlugin/1.2/trunk
sfDoctrineGuardPlugin 
http://svn.symfony-project.com/plugins/sfDoctrineGuardPlugin/trunk
sfFormExtraPlugin 
http://svn.symfony-project.com/plugins/sfFormExtraPlugin/branches/1.3
sfTaskExtraPlugin 
http://svn.symfony-project.com/plugins/sfTaskExtraPlugin/branches/1.3
sfFeed2Plugin http://svn.symfony-project.com/plugins/sfFeed2Plugin/branches/1.2
sfWebBrowserPlugin 
http://svn.symfony-project.com/plugins/sfWebBrowserPlugin/trunk


Modified: 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAdminPlugin/modules/sympal_dashboard/templates/indexSuccess.php
===================================================================
--- 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAdminPlugin/modules/sympal_dashboard/templates/indexSuccess.php
    2010-01-11 14:26:04 UTC (rev 26475)
+++ 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAdminPlugin/modules/sympal_dashboard/templates/indexSuccess.php
    2010-01-11 14:49:05 UTC (rev 26476)
@@ -1,9 +1,9 @@
 <?php if ($isAjax): ?>
-  <link rel="stylesheet" type="text/css" media="screen" href="<?php echo 
stylesheet_path('/sfSympalAdminPlugin/css/dashboard') ?>" />
-  <link rel="stylesheet" type="text/css" media="screen" href="<?php echo 
stylesheet_path('/sfSympalUpgradePlugin/css/upgrade') ?>" />
+  <link rel="stylesheet" type="text/css" media="screen" href="<?php echo 
stylesheet_path('/sfSympalAdminPlugin/css/dashboard.css') ?>" />
+  <link rel="stylesheet" type="text/css" media="screen" href="<?php echo 
stylesheet_path('/sfSympalUpgradePlugin/css/upgrade.css') ?>" />
 <?php else: ?>
-  <?php sympal_use_stylesheet('/sfSympalAdminPlugin/css/dashboard') ?>
-  <?php sympal_use_stylesheet('/sfSympalUpgradePlugin/css/upgrade') ?>
+  <?php sympal_use_stylesheet('/sfSympalAdminPlugin/css/dashboard.css') ?>
+  <?php sympal_use_stylesheet('/sfSympalUpgradePlugin/css/upgrade.css') ?>
 <?php endif; ?>
 
 <?php if ($hasNewVersion): ?>

Modified: 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAdminPlugin/templates/admin.php
===================================================================
--- 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAdminPlugin/templates/admin.php
    2010-01-11 14:26:04 UTC (rev 26475)
+++ 
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAdminPlugin/templates/admin.php
    2010-01-11 14:49:05 UTC (rev 26476)
@@ -5,6 +5,7 @@
   <?php include_http_metas() ?>
   <?php include_metas() ?>
   <?php include_title() ?>
+  <?php sympal_minify() ?>
   <?php include_stylesheets() ?>
   <?php include_javascripts() ?>
 </head>

Modified: plugins/sfSympalPlugin/trunk/templates/default.php
===================================================================
--- plugins/sfSympalPlugin/trunk/templates/default.php  2010-01-11 14:26:04 UTC 
(rev 26475)
+++ plugins/sfSympalPlugin/trunk/templates/default.php  2010-01-11 14:49:05 UTC 
(rev 26476)
@@ -5,6 +5,7 @@
   <?php include_http_metas() ?>
   <?php include_metas() ?>
   <?php include_title() ?>
+  <?php sympal_minify() ?>
   <?php include_stylesheets() ?>
   <?php include_javascripts() ?>
 </head>

Modified: plugins/sfSympalPlugin/trunk/templates/sympal.php
===================================================================
--- plugins/sfSympalPlugin/trunk/templates/sympal.php   2010-01-11 14:26:04 UTC 
(rev 26475)
+++ plugins/sfSympalPlugin/trunk/templates/sympal.php   2010-01-11 14:49:05 UTC 
(rev 26476)
@@ -20,6 +20,7 @@
   <?php include_http_metas() ?>
   <?php include_metas() ?>
   <?php include_title() ?>
+  <?php sympal_minify() ?>
   <?php include_stylesheets() ?>
   <?php include_javascripts() ?>
 </head>

Modified: plugins/sfSympalPlugin/trunk/templates/wordpress.php
===================================================================
--- plugins/sfSympalPlugin/trunk/templates/wordpress.php        2010-01-11 
14:26:04 UTC (rev 26475)
+++ plugins/sfSympalPlugin/trunk/templates/wordpress.php        2010-01-11 
14:49:05 UTC (rev 26476)
@@ -5,6 +5,7 @@
   <?php include_http_metas() ?>
   <?php include_metas() ?>
   <?php include_title() ?>
+  <?php sympal_minify() ?>
   <?php include_stylesheets() ?>
   <?php include_javascripts() ?>
 </head>

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.


Reply via email to