diff --git a/Imagine/Cache/CacheClearer.php b/Imagine/Cache/CacheClearer.php new file mode 100644 index 000000000..41363dc31 --- /dev/null +++ b/Imagine/Cache/CacheClearer.php @@ -0,0 +1,46 @@ + + */ +class CacheClearer implements CacheClearerInterface +{ + /** + * The Cache Manager + * + * @var CacheManager + */ + private $cacheManager; + + /** + * The prefix applied to all cached images + * + * @var string + */ + private $cachePrefix; + + /** + * @param CacheManager $cacheManager + */ + public function __construct(CacheManager $cacheManager, $cachePrefix) + { + $this->cacheManager = $cacheManager; + $this->cachePrefix = $cachePrefix; + } + + /** + * (non-PHPdoc) + * @see Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface::clear() + */ + public function clear($cacheDir) + { + // $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/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). diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index 5765f0eb2..ec0fe5ae4 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -48,6 +48,10 @@ Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver Liip\ImagineBundle\Imagine\Cache\Resolver\NoCacheResolver + + + Liip\ImagineBundle\Imagine\Cache\CacheClearer + @@ -152,5 +156,13 @@ + + + + + + %liip_imagine.cache_prefix% + +