Author: caefer
Date: 2010-03-21 12:16:27 +0100 (Sun, 21 Mar 2010)
New Revision: 28643

Removed:
   plugins/sfImageTransformExtraPlugin/trunk/lib/template/
Modified:
   plugins/sfImageTransformExtraPlugin/trunk/config/routing.yml
   
plugins/sfImageTransformExtraPlugin/trunk/lib/routing/sfImageTransformRoute.class.php
   
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceDoctrine.class.php
   
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageTransformManager.class.php
Log:
Cleaned up routing related stuff.

 * sfImageTransformRoute now excepts objects for generating URLs (url_for())
 * sfImageTransformRoute can now assign a value to the :type parameter from a 
passed object
 * sfImageTransformRoute can now assign a value to the :path parameter from a 
passed :id parameter
 * sfImageTransformRoute can now assign a value to the :sf_format parameter 
from the passed :format parameter
 * sfSourceImageTemplate is now removed as it became obsolete
 * routing.yml now has an attribute for sf_image_doctrine route
 * sfImageTransformManager now holds the parameter prepatation method for the 
overlay transformation



Modified: plugins/sfImageTransformExtraPlugin/trunk/config/routing.yml
===================================================================
--- plugins/sfImageTransformExtraPlugin/trunk/config/routing.yml        
2010-03-21 10:24:08 UTC (rev 28642)
+++ plugins/sfImageTransformExtraPlugin/trunk/config/routing.yml        
2010-03-21 11:16:27 UTC (rev 28643)
@@ -1,7 +1,7 @@
 sf_image_doctrine:
   class: sfImageTransformRoute
   url:   /thumbnails/:type/:format/:path/:slug-:id.:sf_format
-  param: { module: sfImageTransformator, action: index }
+  param: { module: sfImageTransformator, action: index, attribute: file }
   requirements:
     format:    '[\w_-]+(?:,[\w_-]+(?:,[\w_-]+)?)?'
     path:      '[\w/]+'

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/lib/routing/sfImageTransformRoute.class.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/lib/routing/sfImageTransformRoute.class.php
       2010-03-21 10:24:08 UTC (rev 28642)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/lib/routing/sfImageTransformRoute.class.php
       2010-03-21 11:16:27 UTC (rev 28643)
@@ -12,7 +12,7 @@
  */
 
 /**
- * sfImageTransformRoutee represents a route that is bound to a generated 
(transformed) image resource.
+ * sfImageTransformRoute represents a route that is bound to a generated 
(transformed) image resource.
  *
  * @package    sfImageTransformExtraPlugin
  * @subpackage routing
@@ -20,46 +20,89 @@
  */
 class sfImageTransformRoute extends sfRequestRoute
 {
-  protected function mergeArrays($arr1, $arr2)
+  /**
+   * Generates a URL from the given parameters.
+   *
+   * @param  mixed   $params    The parameter values
+   * @param  array   $context   The context
+   * @param  Boolean $absolute  Whether to generate an absolute URL
+   *
+   * @return string The generated URL
+   */
+  public function generate($params, $context = array(), $absolute = false)
   {
-    if(array_key_exists('sf_subject', $arr2) && is_object($arr2['sf_subject']))
+    return urldecode(parent::generate($this->convertObjectToArray($params), 
$context, $absolute));
+  }
+
+  /**
+   * Reads parameters from a passed object and assignes values to type, path 
and sf_format parameters if necessary
+   *
+   * @param  array Parameters as passed to the current route
+   * @return array
+   */
+  protected function convertObjectToArray($object)
+  {
+    if (!$this->compiled)
     {
-      $obj = $arr2['sf_subject'];
-      $arr1['path'] = $obj->getPath();
-      $arr1['type'] = $obj->getType();
-      foreach ($this->variables as $key => $value)
+      $this->compile();
+    }
+
+    if (is_array($object))
+    {
+      if (!isset($object['sf_subject']))
       {
-        if(isset($obj[$key]))
-        {
-          $arr1[$key] = $obj->get($key);
-        }
+        return $object;
       }
 
-      unset($arr2['sf_subject']);
+      $parameters = $object;
+      $object = $parameters['sf_subject'];
+      unset($parameters['sf_subject']);
     }
+    else
+    {
+      $parameters = array();
+    }
 
-    if(array_key_exists('format', $arr2))
+    if(array_key_exists('type', $this->variables))
     {
-      $formats = sfConfig::get('thumbnailing_formats', array());
-      $arr1['sf_format'] = 
$this->get_suffix_for_mime_type($formats[$arr2['format']]['mime_type']);
+      $parameters['type'] = get_class($object instanceof sfOutputEscaper ? 
$object->getRawValue() : $object);
     }
 
-    return parent::mergeArrays($arr1, $arr2);
+    $parameters = array_merge($parameters, 
$this->doConvertObjectToArray($object));
+
+    if(array_key_exists('path', $this->variables) && !array_key_exists('path', 
$parameters) && array_key_exists('id', $parameters))
+    {
+      $parameters['path'] = implode('/', 
array_reverse(str_split(str_pad($parameters['id'], 6, '0', STR_PAD_LEFT) , 2)));
+    }
+
+    if(!array_key_exists('sf_format', $parameters) && 
array_key_exists('format', $parameters))
+    {
+      $formats = sfConfig::get('thumbnailing_formats');
+      $parameters['sf_format'] = 
$this->get_suffix_for_mime_type($formats[$parameters['format']]['mime_type']);
+    }
+
+    return $parameters;
   }
 
   /**
-   * Generates a URL from the given parameters.
+   * Attempts to read all variables for the current route from object 
attributes
    *
-   * @param  mixed   $params    The parameter values
-   * @param  array   $context   The context
-   * @param  Boolean $absolute  Whether to generate an absolute URL
-   *
-   * @return string The generated URL
+   * @param  object $object Object that was passed as sf_subject
+   * @return array
    */
-  public function generate($params, $context = array(), $absolute = false)
+  protected function doConvertObjectToArray($object)
   {
-    $url = parent:: generate($params, $context = array(), $absolute = false);
-    return urldecode($url);
+    $parameters = array();
+    foreach($this->variables as $variable => $token)
+    {
+      try
+      {
+        $parameter = $object->get($variable);
+        $parameters[$variable] = $parameter;
+      }
+      catch(Exception $e){/* do nothing */}
+    }
+    return $parameters;
   }
 
   /**
@@ -83,11 +126,21 @@
     }
   }
 
+  /**
+   * Returns the sfImageSource class name for the currently requested URL
+   *
+   * @return string
+   */
   public function getImageSourceStreamWrapper()
   {
     return 'sfImageSource'.$this->options['image_source'];
   }
 
+  /**
+   * Returns the sfImageSource:// URI for the currently requested URL
+   *
+   * @return string
+   */
   public function getImageSourceURI()
   {
     return call_user_func(array($this->getImageSourceStreamWrapper(), 
'buildURIfromParameters'), $this->parameters);

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceDoctrine.class.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceDoctrine.class.php
        2010-03-21 10:24:08 UTC (rev 28642)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceDoctrine.class.php
        2010-03-21 11:16:27 UTC (rev 28643)
@@ -146,7 +146,8 @@
       throw new sfError404Exception('Could not find "'.$url['host'].'" 
#'.$url['fragment'].'!');
     }
     $attribute = ltrim($url['path'], '/');
-    return 
sfConfig::get('sf_upload_dir').'/'.strtolower($url['host']).'/'.$obj->retrieveFilenameForAttribute($attribute);
+    $files = 
sfFinder::type('file')->name($obj->$attribute.'*')->in(sfConfig::get('sf_upload_dir'));
+    return $files[0];
   }
 
   /**

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageTransformManager.class.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageTransformManager.class.php
  2010-03-21 10:24:08 UTC (rev 28642)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageTransformManager.class.php
  2010-03-21 11:16:27 UTC (rev 28643)
@@ -134,6 +134,11 @@
     {
       $parameters = call_user_func(array($class_generic, 'prepareParameters'), 
$sourceImage, $parameters);
     }
+    else if(method_exists($this, 'prepareParametersFor'.ucfirst($method)))
+    {
+      $prepare = 'prepareParametersFor'.ucfirst($method);
+      $parameters = $this->$prepare($sourceImage, $parameters);
+    }
 
     return $parameters;
   }
@@ -160,4 +165,60 @@
 
     return $adapter;
   }
+
+  /**
+   * Callback function to extend/alter parameters as given in your 
thumbnailing.yml.
+   *
+   * This callback adds the resources path to an overlay image
+   *
+   * @param  sfImage $sourceImage The original image
+   * @param  array   $parameters  Configured parameters for this transformation
+   * @return array   $parameters  Extended/altered parameters
+   */
+  private function prepareParametersForOverlay($sourceImage, $parameters)
+  {
+    if (!array_key_exists('overlay', $parameters))
+    {
+      return $parameters;
+    }
+
+    $filename = $parameters['overlay'];
+
+    if('/' == $filename[0])
+    {
+      if(!file_exists($filename))
+      {
+        throw new InvalidArgumentException('Could not find resource 
"'.$parameters['overlay'].'"!');
+      }
+    }
+    else
+    {
+      $filepath = '';
+      if(0 <= ($pos = strrpos($filename, '/')))
+      {
+        $filepath = substr($filename, 0, $pos);
+        $filename = substr($filename, $pos+1);
+      }
+
+      $files = sfFinder::type('file')
+        ->name($filename)
+        ->maxdepth(1)
+        ->in(array(
+          sfConfig::get('sf_data_dir') . '/resources/'.$filepath,
+          sfConfig::get('sf_plugins_dir') . 
'/sfImageTransformExtraPlugin/data/example-resources/'.$filepath,
+        ));
+
+      if(0 == count($files))
+      {
+        throw new InvalidArgumentException('Could not find resource 
"'.$parameters['overlay'].'"!');
+      }
+
+      $filename = $files[0];
+    }
+
+
+    $parameters['overlay'] = new sfImage($filename);
+
+    return $parameters;
+  }
 }

-- 
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