Author: caefer
Date: 2010-03-19 14:06:23 +0100 (Fri, 19 Mar 2010)
New Revision: 28619

Added:
   
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceFile.class.php
Modified:
   plugins/sfImageTransformExtraPlugin/trunk/config/routing.yml
   
plugins/sfImageTransformExtraPlugin/trunk/config/sfImageTransformExtraPluginConfiguration.class.php
   plugins/sfImageTransformExtraPlugin/trunk/config/thumbnailing.yml
   
plugins/sfImageTransformExtraPlugin/trunk/lib/routing/sfImageTransformRoute.class.php
   
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceDoctrine.class.php
   
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceHTTP.class.php
   
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceInterface.interface.php
   
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceMock.class.php
   
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageTransformManager.class.php
   
plugins/sfImageTransformExtraPlugin/trunk/modules/sfImageTransformator/lib/BasesfImageTransformatorActions.class.php
Log:

Refactored image sources and routing.

! This is not completely backwards compatible and the tests are not adjusted 
yet !

Now all sfImageSource implementations can have custom routes.
The user of this plugin can now configure any route schema that he choses using 
the routing.yml, sfImageSources will tell him which parameters they expect.

Example routes are in config/routing.yml.

Also added a new sfImageSourceFile class that accepts a local filepath within 
sf_uploads_dir.



Modified: plugins/sfImageTransformExtraPlugin/trunk/config/routing.yml
===================================================================
--- plugins/sfImageTransformExtraPlugin/trunk/config/routing.yml        
2010-03-19 02:52:22 UTC (rev 28618)
+++ plugins/sfImageTransformExtraPlugin/trunk/config/routing.yml        
2010-03-19 13:06:23 UTC (rev 28619)
@@ -1,4 +1,4 @@
-sf_image:
+sf_image_doctrine:
   class: sfImageTransformRoute
   url:   /thumbnails/:type/:format/:path/:slug-:id.:sf_format
   param: { module: sfImageTransformator, action: index }
@@ -7,7 +7,45 @@
     path:      '[\w/]+'
     slug:      '[\w_-]+'
     id:        '\d+(?:,\d+)?'
-    sf_format: gif|png|jpg
+    sf_format: 'gif|png|jpg'
     sf_method: [ get ]
   options:
+    image_source: Doctrine
     segment_separators: [ '/', '.', '-' ]
+
+sf_image_file:
+  class: sfImageTransformRoute
+  url:   /thumbnails/:format/:filepath.:sf_format
+  param: { module: sfImageTransformator, action: index }
+  requirements:
+    format:    '[\w_-]+'
+    filepath:  '[\w/]+'
+    sf_format: 'gif|png|jpg'
+    sf_method: [ get ]
+  options:
+    image_source: File
+
+sf_image_http:
+  class: sfImageTransformRoute
+  url:   /thumbnails/:format/:protocol/:domain/:filepath.:sf_format
+  param: { module: sfImageTransformator, action: index }
+  requirements:
+    format:    '[\w_-]+'
+    sf_format: http|https
+    domain:    '[\w-_.]+'
+    filepath:  '[\w/-_.]+'
+    sf_format: 'gif|png|jpg'
+    sf_method: [ get ]
+  options:
+    image_source: HTTP
+
+sf_image_mock:
+  class: sfImageTransformRoute
+  url:   /thumbnails/:format.:sf_format
+  param: { module: sfImageTransformator, action: index }
+  requirements:
+    format:    '[\w_-]+'
+    sf_format: 'gif|png|jpg'
+    sf_method: [ get ]
+  options:
+    image_source: Mock

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/config/sfImageTransformExtraPluginConfiguration.class.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/config/sfImageTransformExtraPluginConfiguration.class.php
 2010-03-19 02:52:22 UTC (rev 28618)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/config/sfImageTransformExtraPluginConfiguration.class.php
 2010-03-19 13:06:23 UTC (rev 28619)
@@ -33,7 +33,7 @@
       
require_once($this->configuration->getConfigCache()->checkConfig('config/thumbnailing.yml'));
     }
 
-    $this->dispatcher->connect('context.load_factories', 
array('sfImageTransformExtraPluginConfiguration', 'registerStreamWrapper'));
+    //$this->dispatcher->connect('context.load_factories', 
array('sfImageTransformExtraPluginConfiguration', 'registerStreamWrapper'));
     $this->dispatcher->connect('controller.change_action', 
array('sfImageTransformExtraPluginConfiguration', 'setViewCache'));
     $this->dispatcher->connect('sf_image_transform.changed_source', 
array('sfImageTransformExtraPluginConfiguration', 'removeOldThumbnails'));
   }

Modified: plugins/sfImageTransformExtraPlugin/trunk/config/thumbnailing.yml
===================================================================
--- plugins/sfImageTransformExtraPlugin/trunk/config/thumbnailing.yml   
2010-03-19 02:52:22 UTC (rev 28618)
+++ plugins/sfImageTransformExtraPlugin/trunk/config/thumbnailing.yml   
2010-03-19 13:06:23 UTC (rev 28619)
@@ -1,19 +1,9 @@
 prod:
 
-test:
-  source_image_stream:
-    class:                          sfImageSourceMock
-
 all:
   static_image_host:                ~
   web_dir:                          /thumbnails
   id_path_depth:                    3
-  source_image_stream:
-  #  class:                          sfImageSourceMock
-    class:                          sfImageSourceDoctrine
-  #  class:                          sfImageSourceHTTP
-  #  param:
-  #    url_schema:                   
http://symfon.ical.ly/source-image/%type/%attribute/%id
 
   .settings:
     formats:

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/lib/routing/sfImageTransformRoute.class.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/lib/routing/sfImageTransformRoute.class.php
       2010-03-19 02:52:22 UTC (rev 28618)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/lib/routing/sfImageTransformRoute.class.php
       2010-03-19 13:06:23 UTC (rev 28619)
@@ -83,18 +83,13 @@
     }
   }
 
-  /**
-   * Binds the current route for a given context and parameters.
-   *
-   * @param array $context    The context
-   * @param array $parameters The parameters
-   */
-  public function bind($context, $parameters)
+  public function getImageSourceStreamWrapper()
   {
-    $idAndAttribute = explode(',', $parameters['id']);
-    $parameters['id'] = $idAndAttribute[0];
-    $parameters['attribute'] = (2 == count($idAndAttribute)) ? 
$idAndAttribute[1] : '0';
+    return 'sfImageSource'.$this->options['image_source'];
+  }
 
-    parent::bind($context, $parameters);
+  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-19 02:52:22 UTC (rev 28618)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceDoctrine.class.php
        2010-03-19 13:06:23 UTC (rev 28619)
@@ -148,4 +148,22 @@
     $attribute = ltrim($url['path'], '/');
     return 
sfConfig::get('sf_upload_dir').'/'.strtolower($url['host']).'/'.$obj->retrieveFilenameForAttribute($attribute);
   }
+
+  /**
+   * Returns an sfImageSource:// URL pointing to a file which path is stored 
on a Doctrine object
+   *
+   * @param  array  $parameters Current request parameters (expected: type, 
attribute, id)
+   * @return string sfImageSource:// URI
+   * @throws InvalidArgumentException
+   */
+  public static function buildURIfromParameters(array $parameters)
+  {
+    // all params must be given
+    if ($diff = array_diff(array('type', 'attribute', 'id'), 
array_keys($parameters)))
+    {
+      throw new InvalidArgumentException(sprintf('The sf_image for 
image_source "Doctrine" route has some missing mandatory parameters (%s).', 
implode(', ', $diff)));
+    }
+
+    return sprintf('sfImageSource://%s/%s#%s', $parameters['type'], 
$parameters['attribute'], $parameters['id']);
+  }
 }

Added: 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceFile.class.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceFile.class.php
                            (rev 0)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceFile.class.php
    2010-03-19 13:06:23 UTC (rev 28619)
@@ -0,0 +1,171 @@
+<?php
+/**
+ * This file is part of the sfImageTransformExtraPlugin package.
+ * (c) 2010 Christian Schaefer <[email protected]>>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @package    sfImageTransformExtraPlugin
+ * @author     Christian Schaefer <[email protected]>
+ * @version    SVN: $Id: sfRawFileCache.class.php 63 2010-03-09 04:34:28Z 
caefer $
+ */
+
+/**
+ * Maps sfImageSource:// URLs to an image file
+ *
+ * @package    sfImageTransformExtraPlugin
+ * @subpackage source
+ * @author     Christian Schaefer <[email protected]>
+ */
+class sfImageSourceFile implements sfImageSourceInterface
+{
+  /**
+   * resource context
+   *
+   * @var resource
+   */
+  public $context;
+
+  /**
+   * resource handle
+   *
+   * @var resource
+   */
+  private $resource;
+
+  /**
+   * mock image absolute path
+   *
+   * @var string
+   */
+  private $filename;
+
+  /**
+   * Close an resource
+   *
+   * @return void
+   */
+  public function stream_close()
+  {
+    return fclose($this->resource);
+  }
+
+  /**
+   * Tests for end-of-file on a file pointer
+   *
+   * @return bool
+   */
+  public function stream_eof()
+  {
+    return feof($this->resource);
+  }
+
+  /**
+   * Flushes the output
+   *
+   * @return bool
+   */
+  public function stream_flush()
+  {
+    return fflush($this->resource);
+  }
+
+
+  /**
+   * Opens file or URL
+   *
+   * @param string $path
+   * @param string $mode
+   * @param int $options
+   * @param string &$opened_path
+   * @return bool
+   */
+  public function stream_open($path , $mode , $options , &$opened_path)
+  {
+    $this->filename = $this->translatePathToFilename($path);
+    $this->resource = fopen($this->filename, $mode);
+    return false !== $this->resource;
+  }
+
+  /**
+   * Read from stream
+   *
+   * @param int $count
+   * @return string
+   */
+  public function stream_read($count)
+  {
+    return fread($this->resource, $count);
+  }
+
+  /** 
+   * Retrieve information about a file resource
+   * 
+   * @return array 
+   */ 
+  public function stream_stat()
+  {
+    return fstat($this->resource);
+  }
+
+  /**
+   * Retrieve information about a file
+   *
+   * @param string $path
+   * @param int $flags
+   * @return array
+   */
+  public function url_stat($path , $flags)
+  {
+    $this->filename = $this->translatePathToFilename($path);
+    return stat($this->filename);
+  }
+
+  /**
+   * Translates the given stream URL to the abolute path of the source image
+   *
+   * @param  string $path The given stream URL
+   * @return string
+   */
+  private function translatePathToFilename($path)
+  {
+    if(!is_null($this->filename))
+    {
+      return $this->filename;
+    }
+
+    $url  = parse_url($path);
+    $pos  = strrpos($url['path'], '/');
+    $path = substr($url['path'], 0, $pos);
+    $file = substr($url['path'], $pos+1);
+    $files = 
sfFinder::type('file')->name($file.'*')->in(sfConfig::get('sf_upload_dir').$path);
+
+    if(!count($files))
+    {
+      throw new sfError404Exception('Could not find image "'.$url['host'].'"');
+    }
+
+    $this->filename = $files[0];
+
+    return $this->filename;
+  }
+
+  /**
+   * Returns an sfImageSource:// URL pointing to a file on the local filesystem
+   *
+   * @param  array  $parameters Current request parameters (expected: filepath)
+   * @return string sfImageSource:// URI
+   * @throws InvalidArgumentException
+   */
+  public static function buildURIfromParameters(array $parameters)
+  {
+    // all params must be given
+    if (!array_key_exists('filepath', $parameters))
+    {
+      throw new InvalidArgumentException('The sf_image for image_source 
"Doctrine" route has some missing mandatory parameters (filepath).');
+    }
+
+    return sprintf('sfImageSource://file/%s', $parameters['filepath']);
+  }
+}

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceHTTP.class.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceHTTP.class.php
    2010-03-19 02:52:22 UTC (rev 28618)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceHTTP.class.php
    2010-03-19 13:06:23 UTC (rev 28619)
@@ -131,10 +131,24 @@
   {
     $options = sfConfig::get('thumbnailing_source_image_stream_param');
     $path = parse_url($path);
-    return str_replace(
-      array('%type', '%attribute', '%id'),
-      array($path['host'], ltrim($path['path'], '/'), $path['fragment']),
-      $options['url_schema']
-    );
+    return sprintf('%s:/%s/%s', $path['host'], $path['path'], 
$path['fragment']);
   }
+
+  /**
+   * Returns an sfImageSource:// URL pointing to a file read over HTTP
+   *
+   * @param  array  $parameters Current request parameters (expected: 
protocol, domain, filepath)
+   * @return string sfImageSource:// URI
+   * @throws InvalidArgumentException
+   */
+  public static function buildURIfromParameters(array $parameters)
+  {
+    // all params must be given
+    if ($diff = array_diff(array('protocol', 'domain', 'filepath'), 
array_keys($parameters)))
+    {
+      throw new InvalidArgumentException('The sf_image for image_source "HTTP" 
route has some missing mandatory parameters (url).');
+    }
+
+    return sprintf('sfImageSource://%s/%s#%s', $parameters['protocol'], 
$parameters['domain'], $parameters['filepath']);
+  }
 }

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceInterface.interface.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceInterface.interface.php
   2010-03-19 02:52:22 UTC (rev 28618)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceInterface.interface.php
   2010-03-19 13:06:23 UTC (rev 28619)
@@ -84,4 +84,13 @@
    * @return array
    */
   public function url_stat($path , $flags);
+
+  /**
+   * Returns an sfImageSource:// URL specific to the implementing stream 
wrapper
+   *
+   * @param  array  $parameters Current request parameters
+   * @return string sfImageSource:// URI
+   * @throws InvalidArgumentException
+   */
+  public static function buildURIfromParameters(array $parameters);
 }

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceMock.class.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceMock.class.php
    2010-03-19 02:52:22 UTC (rev 28618)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceMock.class.php
    2010-03-19 13:06:23 UTC (rev 28619)
@@ -118,4 +118,16 @@
   {
     return stat(dirname(__FILE__).$this->filename);
   }
+
+  /**
+   * Returns an sfImageSource:// URL pointing to a single mock file within 
this plugin
+   *
+   * @param  array  $parameters Current request parameters (expected: ~)
+   * @return string sfImageSource:// URI
+   * @throws InvalidArgumentException
+   */
+  public static function buildURIfromParameters(array $parameters)
+  {
+    return 'sfImageSource://mock';
+  }
 }

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageTransformManager.class.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageTransformManager.class.php
  2010-03-19 02:52:22 UTC (rev 28618)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/lib/transforms/sfImageTransformManager.class.php
  2010-03-19 13:06:23 UTC (rev 28619)
@@ -50,21 +50,15 @@
    * options configured in the thumbnailing.yml and uses them to call 
sfImageTransformPlugins transformations.
    * Additionally the generated thumbnail can be cached.
    *
+   * @param  string  $uri     Image source URI (sfImageSource://...)
    * @param  array   $options Thumbnail parameters taken from the thumbnail 
URL referencing a format and id
    * @return sfImage
    */
-  public function generate($options = array())
+  public function generate($uri, $format)
   {
-    if (!array_key_exists($options['format'], $this->options['formats']))
-    {
-      sfContext::getInstance()->getLogger()->warning('{' . __CLASS__ . '} [' . 
__FUNCTION__ . '] Format "' . $options['format'] . '" unknown. Using "default" 
instead.');
-      $options['format'] = 'default';
-    }
+    $sourceImage = new sfImage($uri);
+    $settings    = $this->options['formats'][$format];
 
-    $sourceImage    = $this->getSourceImage($options);
-
-    $settings       = $this->options['formats'][$options['format']];
-
     if(array_key_exists('mime_type', $settings))
     {
       $sourceImage->setMIMEType($settings['mime_type']);
@@ -145,18 +139,6 @@
   }
 
   /**
-   * Returns the image source stream for the given options
-   *
-   * @param  array   $options Array of options
-   * @return sfImage
-   */
-  private function getSourceImage($options)
-  {
-    $sourceImageFile = 
'sfImageSource://'.$options['type'].'/'.$options['attribute'].'#'.$options['id'];
-    return  new sfImage($sourceImageFile);
-  }
-
-  /**
    * Returns a adapter class of the specified type
    * @access protected
    * @param  string                          $name Name of the transformation 
to instantiate

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/modules/sfImageTransformator/lib/BasesfImageTransformatorActions.class.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/modules/sfImageTransformator/lib/BasesfImageTransformatorActions.class.php
        2010-03-19 02:52:22 UTC (rev 28618)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/modules/sfImageTransformator/lib/BasesfImageTransformatorActions.class.php
        2010-03-19 13:06:23 UTC (rev 28619)
@@ -30,12 +30,19 @@
    */
   public function executeIndex(sfWebRequest $request) 
   {
-    $options = $this->prepareOptions($request);
+    if(in_array('sfImageSource', stream_get_wrappers()))
+    {
+      stream_wrapper_unregister('sfImageSource');
+    }
+    $streamwrapper = $this->getRoute()->getImageSourceStreamWrapper();
+    stream_wrapper_register('sfImageSource', $streamwrapper) or die('Failed to 
register protocol..');
 
-    $response = $this->getResponse();
     $formats = sfConfig::get('thumbnailing_formats', array());
     $thumbnailer = new sfImageTransformManager($formats);
-    $thumbnail = $thumbnailer->generate($options);
+    $uri = $this->getRoute()->getImageSourceURI();
+    $thumbnail = $thumbnailer->generate($uri, $request->getParameter('format', 
'default'));
+
+    $response = $this->getResponse();
     $response->setContentType($thumbnail->getMIMEType());
     $response->setContent($thumbnail->toString());
 

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