Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 43 - Added a cache clearer for generated images #80

Merged
merged 5 commits into from
Jun 16, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Imagine/Cache/CacheClearer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Liip\ImagineBundle\Imagine\Cache;

use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;

/**
* Clears the Liip Imagine Bundle cache
*
* @author Josiah <josiah@web-dev.com.au>
*/
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);
}
}
7 changes: 7 additions & 0 deletions Imagine/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
8 changes: 8 additions & 0 deletions Imagine/Cache/Resolver/AmazonS3Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
9 changes: 9 additions & 0 deletions Imagine/Cache/Resolver/ResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
21 changes: 20 additions & 1 deletion Imagine/Cache/Resolver/WebPathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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}
*/
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
12 changes: 12 additions & 0 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<parameter key="liip_imagine.cache.resolver.web_path.class">Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver</parameter>
<parameter key="liip_imagine.cache.resolver.no_cache.class">Liip\ImagineBundle\Imagine\Cache\Resolver\NoCacheResolver</parameter>

<!-- Cache clearers' classes -->

<parameter key="liip_imagine.cache.clearer.class">Liip\ImagineBundle\Imagine\Cache\CacheClearer</parameter>

</parameters>

<services>
Expand Down Expand Up @@ -152,5 +156,13 @@
<argument type="service" id="filesystem" />
</service>

<!-- Cache Clearer -->

<service id="liip_imagine.cache.clearer" class="%liip_imagine.cache.clearer.class%">
<tag name="kernel.cache_clearer" />
<argument type="service" id="liip_imagine.cache.manager" />
<argument>%liip_imagine.cache_prefix%</argument>
</service>

</services>
</container>