Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from liip-forks/imagine-cache-invalidator-list…
…ener added cache-invalidator-listener service
- Loading branch information
Showing
7 changed files
with
195 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\PHPCRBundle\EventListener; | ||
|
||
use Doctrine\Common\Util\Debug; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Doctrine\ODM\PHPCR\Document\Image; | ||
use Doctrine\ODM\PHPCR\Document\File; | ||
use Doctrine\ODM\PHPCR\Document\Resource; | ||
use Doctrine\Common\EventSubscriber; | ||
use Doctrine\ODM\PHPCR\Event\LifecycleEventArgs; | ||
|
||
use Liip\ImagineBundle\Imagine\Cache\CacheManager; | ||
use Liip\ImagineBundle\Imagine\Data\Loader\DoctrinePHPCRLoader; | ||
|
||
/** | ||
* A listener to invalidate the imagine cache when Image documents are modified | ||
*/ | ||
class ImagineCacheInvalidatorSubscriber implements EventSubscriber | ||
{ | ||
/** | ||
* @var CacheManager | ||
*/ | ||
private $manager; | ||
|
||
/** | ||
* Used to get the request from to remove cache | ||
* @var Container | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* Filter names to invalidate | ||
* @var array | ||
*/ | ||
private $filters; | ||
|
||
/** | ||
* @param CacheManager $manager the imagine cache manager | ||
* @param Container $container to get the request from. Need to inject | ||
* this as otherwise we have a scope problem | ||
* @param array $filter list of filter names to invalidate | ||
*/ | ||
public function __construct(CacheManager $manager, Container $container, $filters) | ||
{ | ||
$this->manager = $manager; | ||
$this->container = $container; | ||
$this->filters = $filters; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getSubscribedEvents() | ||
{ | ||
return array( | ||
'postUpdate', | ||
'preRemove', | ||
); | ||
} | ||
|
||
/** | ||
* Invalidate cache after a document was updated. | ||
* | ||
* @param LifecycleEventArgs $args | ||
*/ | ||
public function postUpdate(LifecycleEventArgs $args) | ||
{ | ||
$this->invalidateCache($args); | ||
} | ||
|
||
/** | ||
* Invalidate the cache when removing an image. Do this before the flush to | ||
* still have access to the parent of the document. | ||
* | ||
* @param LifecycleEventArgs $args | ||
*/ | ||
public function preRemove(LifecycleEventArgs $args) | ||
{ | ||
$this->invalidateCache($args); | ||
} | ||
|
||
/** | ||
* Check if this could mean an image document was modified (check resource, | ||
* file and image) | ||
* | ||
* @param LifecycleEventArgs $args | ||
*/ | ||
private function invalidateCache(LifecycleEventArgs $args) | ||
{ | ||
$document = $args->getDocument(); | ||
if ($document instanceof Resource) { | ||
$document = $document->getParent(); | ||
} | ||
if ($document instanceof File) { | ||
$document = $document->getParent(); | ||
} | ||
if ($document instanceof Image) { | ||
foreach ($this->filters as $filter) { | ||
$path = $this->manager->resolve($this->container->get('request'), $document->getId(), 'image_upload_thumbnail')->getTargetUrl(); | ||
// TODO: this might not be needed https://github.com/liip/LiipImagineBundle/issues/162 | ||
if (false !== strpos($path, $filter)) { | ||
$path = substr($path, strpos($path, $filter) + strlen($filter)); | ||
} | ||
$this->manager->remove($path, $filter); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<parameters> | ||
<parameter key="doctrine_phpcr.odm.form.image_class">Doctrine\Bundle\PHPCRBundle\Form\Type\ImageType</parameter> | ||
<parameter key="doctrine_phpcr.odm.subscriber.image_cache_class">Doctrine\Bundle\PHPCRBundle\EventListener\ImagineCacheInvalidatorSubscriber</parameter> | ||
</parameters> | ||
|
||
<services> | ||
|
||
<service id="doctrine_phpcr.odm.form.type.image" class="%doctrine_phpcr.odm.form.image_class%"> | ||
<argument>%doctrine_phpcr.odm.subscriber.image_cache.filter%</argument> | ||
<tag name="form.type" alias="phpcr_odm_image"/> | ||
</service> | ||
|
||
<service id="doctrine_phpcr.odm.subscriber.image_cache" class="%doctrine_phpcr.odm.subscriber.image_cache_class%"> | ||
<tag name="doctrine_phpcr.event_subscriber"/> | ||
<argument type="service" id="liip_imagine.cache.manager"/> | ||
<argument type="service" id="service_container"/> | ||
<argument>%doctrine_phpcr.odm.subscriber.image_cache.all_filters%</argument> | ||
</service> | ||
|
||
</services> | ||
|
||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{% block phpcr_odm_image_widget %} | ||
{{ form_widget(form) }} | ||
{% if form.vars.data %} | ||
<img src="{{ form.vars.data | imagine_filter('image_upload_thumbnail') }}" alt="" /> | ||
<img src="{{ form.vars.data | imagine_filter( imagine_filter ) }}" alt="" /> | ||
{% endif %} | ||
{% endblock %} |