From 344fededaf78a4c15d88929d086e470265b1f20e Mon Sep 17 00:00:00 2001 From: Josiah Date: Tue, 31 Jan 2012 15:50:29 +0530 Subject: [PATCH 1/4] Added a cache clearer for generated images --- Imagine/Cache/CacheClearer.php | 58 ++++++++++++++++++++++++++++++++++ Resources/config/imagine.xml | 13 ++++++++ 2 files changed, 71 insertions(+) create mode 100644 Imagine/Cache/CacheClearer.php diff --git a/Imagine/Cache/CacheClearer.php b/Imagine/Cache/CacheClearer.php new file mode 100644 index 000000000..8cabf7a7e --- /dev/null +++ b/Imagine/Cache/CacheClearer.php @@ -0,0 +1,58 @@ + + */ +class CacheClearer implements CacheClearerInterface +{ + /** + * The Cache Manager + * + * @var CacheManager + */ + private $cacheManager; + + /** + * The prefix applied to all cached images + * + * @var string + */ + private $cachePrefix; + + /** + * The filesystem utilities + * + * @var Filesystem + */ + private $filesystem; + + /** + * @param CacheManager $cacheManager + */ + public function __construct(CacheManager $cacheManager, $cachePrefix, Filesystem $filesystem) + { + $this->cacheManager = $cacheManager; + $this->cachePrefix = $cachePrefix; + $this->filesystem = $filesystem; + } + + /** + * (non-PHPdoc) + * @see Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface::clear() + */ + public function clear($cacheDir) + { + $cachePath = $this->cacheManager->getWebRoot().DIRECTORY_SEPARATOR.$this->cachePrefix; + + $this->filesystem->remove(Finder::create()->in($cachePath)->depth(0)->directories()); + } +} diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index fb4e9f4a1..73259d619 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -45,6 +45,10 @@ Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver + + + Liip\ImagineBundle\Imagine\Cache\CacheClearer + @@ -140,5 +144,14 @@ + + + + + + %liip_imagine.cache_prefix% + + + From 4c4bc1693ef2e4cb58a204fe6fe3cd8973bba369 Mon Sep 17 00:00:00 2001 From: Daniel Barsotti Date: Wed, 13 Jun 2012 10:47:15 +0200 Subject: [PATCH 2/4] CS fixes --- Imagine/Cache/CacheClearer.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Imagine/Cache/CacheClearer.php b/Imagine/Cache/CacheClearer.php index 8cabf7a7e..ed2e572c9 100644 --- a/Imagine/Cache/CacheClearer.php +++ b/Imagine/Cache/CacheClearer.php @@ -1,11 +1,10 @@ Date: Wed, 13 Jun 2012 14:51:32 +0200 Subject: [PATCH 3/4] Refactored the CacheClearer Original code from @web-dev in pull request #48. Adapted with Lukas comments. --- Imagine/Cache/CacheClearer.php | 19 ++++-------------- Imagine/Cache/CacheManager.php | 7 +++++++ Imagine/Cache/Resolver/AmazonS3Resolver.php | 8 ++++++++ Imagine/Cache/Resolver/ResolverInterface.php | 9 +++++++++ Imagine/Cache/Resolver/WebPathResolver.php | 21 +++++++++++++++++++- Resources/config/imagine.xml | 1 - 6 files changed, 48 insertions(+), 17 deletions(-) diff --git a/Imagine/Cache/CacheClearer.php b/Imagine/Cache/CacheClearer.php index ed2e572c9..41363dc31 100644 --- a/Imagine/Cache/CacheClearer.php +++ b/Imagine/Cache/CacheClearer.php @@ -2,9 +2,7 @@ namespace Liip\ImagineBundle\Imagine\Cache; -use Symfony\Component\Finder\Finder, - Symfony\Component\Filesystem\Filesystem, - Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface; +use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface; /** * Clears the Liip Imagine Bundle cache @@ -27,21 +25,13 @@ class CacheClearer implements CacheClearerInterface */ private $cachePrefix; - /** - * The filesystem utilities - * - * @var Filesystem - */ - private $filesystem; - /** * @param CacheManager $cacheManager */ - public function __construct(CacheManager $cacheManager, $cachePrefix, Filesystem $filesystem) + public function __construct(CacheManager $cacheManager, $cachePrefix) { $this->cacheManager = $cacheManager; $this->cachePrefix = $cachePrefix; - $this->filesystem = $filesystem; } /** @@ -50,8 +40,7 @@ public function __construct(CacheManager $cacheManager, $cachePrefix, Filesystem */ public function clear($cacheDir) { - $cachePath = $this->cacheManager->getWebRoot().DIRECTORY_SEPARATOR.$this->cachePrefix; - - $this->filesystem->remove(Finder::create()->in($cachePath)->depth(0)->directories()); + // $cacheDir contains the application cache, which we don't care about + $this->cacheManager->clearResolversCache($this->cachePrefix); } } diff --git a/Imagine/Cache/CacheManager.php b/Imagine/Cache/CacheManager.php index 769e206e9..07781d607 100644 --- a/Imagine/Cache/CacheManager.php +++ b/Imagine/Cache/CacheManager.php @@ -172,4 +172,11 @@ public function remove($targetPath, $filter) { return $this->getResolver($filter)->remove($targetPath, $filter); } + + public function clearResolversCache($cachePrefix) + { + foreach ($this->resolvers as $resolver) { + $resolver->clear($cachePrefix); + } + } } diff --git a/Imagine/Cache/Resolver/AmazonS3Resolver.php b/Imagine/Cache/Resolver/AmazonS3Resolver.php index 2a9e24f8e..2b5d5ac77 100644 --- a/Imagine/Cache/Resolver/AmazonS3Resolver.php +++ b/Imagine/Cache/Resolver/AmazonS3Resolver.php @@ -169,6 +169,14 @@ public function setObjectUrlOption($key, $value) return $this; } + /** + * {@inheritDoc} + */ + public function clear($cachePrefix) + { + // TODO: implement cache clearing for Amazon S3 service + } + /** * Returns the object path within the bucket. * diff --git a/Imagine/Cache/Resolver/ResolverInterface.php b/Imagine/Cache/Resolver/ResolverInterface.php index 28b9e991b..1de1ea964 100644 --- a/Imagine/Cache/Resolver/ResolverInterface.php +++ b/Imagine/Cache/Resolver/ResolverInterface.php @@ -53,4 +53,13 @@ function getBrowserPath($targetPath, $filter, $absolute = false); * @return bool Whether the file has been removed successfully. */ function remove($targetPath, $filter); + + /** + * Clear the CacheResolver cache + * + * @param string $cachePrefix The cache prefix as defined in the configuration + * + * @return void + */ + function clear($cachePrefix); } diff --git a/Imagine/Cache/Resolver/WebPathResolver.php b/Imagine/Cache/Resolver/WebPathResolver.php index 9beebae7f..fdffe4eec 100644 --- a/Imagine/Cache/Resolver/WebPathResolver.php +++ b/Imagine/Cache/Resolver/WebPathResolver.php @@ -7,7 +7,8 @@ use Symfony\Component\HttpFoundation\Request, Symfony\Component\HttpFoundation\RedirectResponse, - Symfony\Component\HttpKernel\Exception\NotFoundHttpException; + Symfony\Component\HttpKernel\Exception\NotFoundHttpException, + Symfony\Component\Finder\Finder; class WebPathResolver extends AbstractFilesystemResolver implements CacheManagerAwareInterface { @@ -61,6 +62,24 @@ public function getBrowserPath($targetPath, $filter, $absolute = false) ); } + /** + * {@inheritDoc} + */ + public function clear($cachePrefix) + { + // Let's just avoid to remove the web/ directory content if cache prefix is empty + if ($cachePrefix === '') { + throw new \InvalidArgumentException("Cannot clear the Imagine cache because the cache_prefix is empty in your config."); + } + + $cachePath = $this->cacheManager->getWebRoot() . DIRECTORY_SEPARATOR . $cachePrefix; + + // Avoid an exception if the cache path does not exist (i.e. Imagine didn't yet render any image) + if (is_dir($cachePath)) { + $this->filesystem->remove(Finder::create()->in($cachePath)->depth(0)->directories()); + } + } + /** * {@inheritDoc} */ diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index b6cc16d66..ec0fe5ae4 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -162,7 +162,6 @@ %liip_imagine.cache_prefix% - From 84fdb0af8f94e7963599adfc81a1b4628365a700 Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 16 Jun 2012 17:27:24 +0200 Subject: [PATCH 4/4] Updated documentation for the cache resolver --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 5dd804b37..c95eaabec 100644 --- a/README.md +++ b/README.md @@ -500,6 +500,13 @@ liip_imagine: For an example of a cache resolver implementation, refer to `Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver`. +### Cache cleaner + +Custom cache resolver classes must implement the ```clear``` method, at worst doing nothing. + +When the ```console cache:clear``` command is run, the clear method of all the registered cache +resolvers is automatically called. + ### AmazonS3Resolver The AmazonS3Resolver requires the [aws-sdk-php](/~https://github.com/amazonwebservices/aws-sdk-for-php).