Author: caefer
Date: 2010-03-31 08:51:12 +0200 (Wed, 31 Mar 2010)
New Revision: 28904

Modified:
   plugins/sfImageTransformExtraPlugin/trunk/lib/cache/sfRawFileCache.class.php
   
plugins/sfImageTransformExtraPlugin/trunk/lib/routing/sfImageTransformRoute.class.php
   
plugins/sfImageTransformExtraPlugin/trunk/test/unit/lib/cache/sfRawFileCacheTest.php
Log:

Implemented the basis for narrowed thumbnail removal.



Modified: 
plugins/sfImageTransformExtraPlugin/trunk/lib/cache/sfRawFileCache.class.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/lib/cache/sfRawFileCache.class.php    
    2010-03-30 21:00:43 UTC (rev 28903)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/lib/cache/sfRawFileCache.class.php    
    2010-03-31 06:51:12 UTC (rev 28904)
@@ -98,8 +98,19 @@
    */
   public function removePattern($pattern)
   {
-    if (false !== strpos($pattern, '**'))
+    if($pattern instanceof sfRoute)
     {
+      $paths = array();
+      foreach (new RecursiveIteratorIterator(new 
RecursiveDirectoryIterator($this->getOption('cache_dir'))) as $path)
+      {
+        if(false !== 
$pattern->matchesUrl('/'.str_replace($this->getOption('cache_dir').DIRECTORY_SEPARATOR,
 '', $path), array('method' => 'get')))
+        {
+          $paths[] = $path;
+        }
+      }
+    }
+    else if (false !== strpos($pattern, '**'))
+    {
       $pattern = str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, 
$pattern);
 
       $regexp = self::patternToRegexp($pattern);

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/lib/routing/sfImageTransformRoute.class.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/lib/routing/sfImageTransformRoute.class.php
       2010-03-30 21:00:43 UTC (rev 28903)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/lib/routing/sfImageTransformRoute.class.php
       2010-03-31 06:51:12 UTC (rev 28904)
@@ -145,4 +145,22 @@
   {
     return call_user_func(array($this->getImageSourceStreamWrapper(), 
'buildURIfromParameters'), $this->parameters);
   }
+
+  /**
+   * Preassembles pattern with passed parameters
+   *
+   * This is used to limit matches when removing generated images
+   *
+   * @param  array $params Parameters to be encoded in the pattern
+   * @return void
+   */
+  public function preassemblePattern($params = array())
+  {
+    foreach($params as $key => $value)
+    {
+      $this->pattern = str_replace(':'.$key, $value, $this->pattern);
+    }
+
+    $this->compiled = false;
+  }
 }

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/test/unit/lib/cache/sfRawFileCacheTest.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/test/unit/lib/cache/sfRawFileCacheTest.php
        2010-03-30 21:00:43 UTC (rev 28903)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/test/unit/lib/cache/sfRawFileCacheTest.php
        2010-03-31 06:51:12 UTC (rev 28904)
@@ -44,11 +44,65 @@
     $this->assertEquals(true, $this->cache->set('any/key', 
serialize($response)));
   }
 
-  public function testRemovePattern()
+  public function testRemovePatternForDoctrineSources()
   {
-    $this->markTestIncomplete('This test has not been implemented yet.');
+    $route = $this->getRoute('sf_image_doctrine');
+    $route->preassemblePattern(array('type' => 'testrecord'));
+    $finder = new sfFinder();
+    $starting_count = 
count($finder->type('file')->in($this->cache->getOption('cache_dir')));
+    $this->cache->removePattern($route);
+    $files = $finder->type('file')->in($this->cache->getOption('cache_dir'));
+    
$this->assertNotContains($this->cache->getOption('cache_dir').'/thumbnails/testrecord/default/01/00/00/test-record-1.gif',
 $files);
+    
$this->assertNotContains($this->cache->getOption('cache_dir').'/thumbnails/testrecord/original/01/00/00/test-record-1.jpg',
 $files);
+    
$this->assertNotContains($this->cache->getOption('cache_dir').'/thumbnails/testrecord/default/02/00/00/test-record-2.gif',
 $files);
+    
$this->assertNotContains($this->cache->getOption('cache_dir').'/thumbnails/testrecord/original/02/00/00/test-record-2.jpg',
 $files);
+    $this->assertEquals($starting_count - 4, count($files));
   }
 
+  public function testRemovePatternForFileSources()
+  {
+    $route = $this->getRoute('sf_image_file');
+    $route->preassemblePattern(array('filepath' => 'path/to/file/filename'));
+    $finder = new sfFinder();
+    $starting_count = 
count($finder->type('file')->in($this->cache->getOption('cache_dir')));
+    $this->cache->removePattern($route);
+    $files = $finder->type('file')->in($this->cache->getOption('cache_dir'));
+    
$this->assertNotContains($this->cache->getOption('cache_dir').'/thumbnails/default/path/to/file/filename.gif',
 $files);
+    
$this->assertNotContains($this->cache->getOption('cache_dir').'/thumbnails/original/path/to/file/filename.jpg',
 $files);
+    $this->assertEquals($starting_count - 2, count($files));
+  }
+
+  public function testRemovePatternForHTTPSources()
+  {
+    $route = $this->getRoute('sf_image_http');
+    $route->preassemblePattern(array('format' => 'default'));
+    $finder = new sfFinder();
+    $starting_count = 
count($finder->type('file')->in($this->cache->getOption('cache_dir')));
+    $this->cache->removePattern($route);
+    $files = $finder->type('file')->in($this->cache->getOption('cache_dir'));
+    
$this->assertNotContains($this->cache->getOption('cache_dir').'/thumbnails/site/default/path/to/file/filename.gif',
 $files);
+    
$this->assertContains($this->cache->getOption('cache_dir').'/thumbnails/site/original/path/to/file/filename.jpg',
 $files);
+    $this->assertEquals($starting_count - 1, count($files));
+  }
+
+  public function testRemovePatternForPropelSources()
+  {
+    $this->markTestSkipped('Propel is not tested yet');
+  }
+
+  public function testRemovePatternForMockSources()
+  {
+    $route = $this->getRoute('sf_image_mock');
+    $route->preassemblePattern(array());
+    $finder = new sfFinder();
+    $starting_count = 
count($finder->type('file')->in($this->cache->getOption('cache_dir')));
+    $this->cache->removePattern($route);
+    $files = $finder->type('file')->in($this->cache->getOption('cache_dir'));
+    
$this->assertNotContains($this->cache->getOption('cache_dir').'/thumbnails/default.gif',
 $files);
+    
$this->assertNotContains($this->cache->getOption('cache_dir').'/thumbnails/original.jpg',
 $files);
+    $this->assertEquals($starting_count - 2, count($files));
+  }
+
   public function testGetLastModified()
   {
     $this->assertEquals(0, $this->cache->getLastModified('any_key'));
@@ -75,9 +129,22 @@
     ));
   }
 
+  private function getRoute($routeName)
+  {
+    $routing = sfContext::getInstance()->getRouting();
+    $route = $routing->getRoutes();
+    return $route[$routeName];
+  }
+
   protected function setUp()
   {
+    $appConfig = ProjectConfiguration::getApplicationConfiguration('frontend', 
'test', true);
+    sfContext::createInstance($appConfig);
     $this->cache = 
$this->getCache(sfConfig::get('sf_cache_dir').'/sfImageTransformExtraPluginUnitTests');
+    $fs = new sfFilesystem();
+    $src_dir = dirname(__FILE__).'/../../../fixtures/thumbnails';
+    $dst_dir = $this->cache->getOption('cache_dir').'/thumbnails';
+    $fs->mirror($src_dir, $dst_dir, new sfFinder(), array('override'=>true));
   }
 
   protected function tearDown()

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