Browse files

starting to implement invalidation

  • Loading branch information...
1 parent ae83b43 commit 54ea79e682bee10f6121c5f07a575a198d81044d @dbu dbu committed Mar 12, 2013
View
9 DependencyInjection/DoctrinePHPCRExtension.php
@@ -145,14 +145,14 @@ private function loadJackalopeSession(array $session, ContainerBuilder $containe
// Factory
if (isset($session['backend']['factory'])) {
/**
- * If it is a class, pass the name as is, else assume it is
+ * If it is a class, pass the name as is, else assume it is
* a service id and get a reference to it
*/
if (class_exists($session['backend']['factory'])) {
- $parameters['jackalope.factory'] = $session['backend']['factory'];
+ $parameters['jackalope.factory'] = $session['backend']['factory'];
} else {
$parameters['jackalope.factory'] = new Reference($session['backend']['factory']);
- }
+ }
}
break;
}
@@ -251,6 +251,9 @@ private function odmLoad(array $config, ContainerBuilder $container)
$dm->addMethodCall('setLocaleChooserStrategy', array(new Reference('doctrine_phpcr.odm.locale_chooser')));
}
+ // TODO make optional
+ $this->loader->load('odm_image.xml');
+
$documentManagers = array();
foreach ($config['document_managers'] as $name => $documentManager) {
if (empty($config['default_document_manager'])) {
View
30 EventListener/ImageCacheInvalidatorListener.php
@@ -1,30 +0,0 @@
-<?php
-
-namespace Doctrine\Bundle\PHPCRBundle\EventListener;
-
-use Doctrine\Common\Util\Debug;
-use Doctrine\ODM\PHPCR\Event\LifecycleEventArgs;
-use Liip\ImagineBundle\Imagine\Data\Loader\DoctrinePHPCRLoader;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-use Symfony\Component\HttpKernel\KernelEvents;
-
-/**
- * A listener to tell the locale chooser the request locale.
- *
- * This listener is invoked on every sub-request, keeping the locale up to date.
- */
-class ImageCacheInvalidatorListener
-{
-
- protected $phpcrLoader;
-
- public function __construct(DoctrinePHPCRLoader $phpcrLoader)
- {
-
- }
- public function postUpdate(LifecycleEventArgs $args)
- {
-
- }
-
-}
View
69 EventListener/ImagineCacheInvalidatorSubscriber.php
@@ -0,0 +1,69 @@
+<?php
+
+namespace Doctrine\Bundle\PHPCRBundle\EventListener;
+
+use Doctrine\Common\Util\Debug;
+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
+{
+
+ public function __construct(CacheManager $manager)
+ {
+ $this->manager = $manager;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getSubscribedEvents()
+ {
+ return array(
+ 'postUpdate',
+ 'preRemove', // when removing, do before the flush to still get parents
+ );
+ }
+
+ public function postUpdate(LifecycleEventArgs $args)
+ {
+ $this->invalidateCache($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) {
+ // TODO: this does not work, what do we need to pass to manager->remove?
+ // TODO: can we invalidate all caches? otherwise inject filter name(s)? by config
+ $this->manager->remove($document->getId(), 'image_upload_thumbnail');
+ }
+ }
+
+}
View
13 Resources/config/odm_image.xml
@@ -4,15 +4,20 @@
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="form.type.phpcr_odm.image" class="Doctrine\Bundle\PHPCRBundle\Form\Type\ImageType">
+ <service id="doctrine_phpcr.odm.form.type.image" class="%doctrine_phpcr.odm.form.image_class%">
<tag name="form.type" alias="phpcr_odm_image"/>
</service>
- <service id="doctrine_phpcr.odm.listener.image_cache" class="Doctrine\Bundle\PHPCRBundle\EventListener\ImageCacheInvalidatorListener">
- <tag name="doctrine_phpcr.event_listener" event="postUpdate" />
- <argument type="service" id="liipch_core.imagine.data_loader.phpcr"/>
+ <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"/>
</service>
</services>

0 comments on commit 54ea79e

Please sign in to comment.