Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #42 from liip-forks/imagine-cache-invalidator-list…

…ener

added cache-invalidator-listener service
  • Loading branch information...
commit d12d8d85c5665d7b57dc3db1c37aad6283a6eccf 2 parents c84f1fe + 79e8e2e
@dbu dbu authored
View
21 DependencyInjection/Configuration.php
@@ -154,6 +154,7 @@ private function addOdmSection(ArrayNodeDefinition $node)
->fixXmlConfig('document_manager')
->append($this->getOdmDocumentManagersNode())
->append($this->getOdmLocaleNode())
+ ->append($this->getOdmImagineNode())
->end()
->end()
;
@@ -245,4 +246,24 @@ private function getOdmCacheDriverNode($name)
return $node;
}
+
+ private function getOdmImagineNode()
+ {
+ $treeBuilder = new TreeBuilder();
+ $node = $treeBuilder->root('imagine');
+
+ $node
+ ->children()
+ ->scalarNode('enabled')->defaultValue(false)->end()
+ ->scalarNode('filter')->defaultValue('image_upload_thumbnail')->end()
+ ->arrayNode('extra_filters')
+ ->requiresAtLeastOneElement()
+ ->prototype('scalar')->end()
+ ->end()
+ ->end()
+ ->end()
+ ;
+
+ return $node;
+ }
}
View
19 DependencyInjection/DoctrinePHPCRExtension.php
@@ -42,6 +42,7 @@ class DoctrinePHPCRExtension extends AbstractDoctrineExtension
private $defaultSession;
private $sessions = array();
private $bundleDirs = array();
+ /** @var XmlFileLoader */
private $loader;
public function load(array $configs, ContainerBuilder $container)
@@ -145,14 +146,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 +252,18 @@ private function odmLoad(array $config, ContainerBuilder $container)
$dm->addMethodCall('setLocaleChooserStrategy', array(new Reference('doctrine_phpcr.odm.locale_chooser')));
}
+ if (isset($config['imagine']['enabled']) && $config['imagine']['enabled']) {
+ $filter = $config['imagine']['filter'];
+ $filters = isset($config['imagine']['extra_filters']) && is_array($config['imagine']['extra_filters'])
+ ? $config['imagine']['extra_filters']
+ : array();
+
+ $filters[] = $filter;
+ $container->setParameter('doctrine_phpcr.odm.subscriber.image_cache.filter', $filter);
+ $container->setParameter('doctrine_phpcr.odm.subscriber.image_cache.all_filters', $filters);
+ $this->loader->load('odm_image.xml');
+ }
+
$documentManagers = array();
foreach ($config['document_managers'] as $name => $documentManager) {
if (empty($config['default_document_manager'])) {
View
111 EventListener/ImagineCacheInvalidatorSubscriber.php
@@ -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);
+ }
+ }
+ }
+
+}
View
19 Form/Type/ImageType.php
@@ -3,12 +3,21 @@
namespace Doctrine\Bundle\PHPCRBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormInterface;
+use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\Bundle\PHPCRBundle\Form\DataTransformer\ModelToFileTransformer;
class ImageType extends AbstractType
{
+ private $defaultFilter;
+
+ public function __construct($defaultFilter)
+ {
+ $this->defaultFilter = $defaultFilter;
+ }
+
public function getParent()
{
return 'file';
@@ -25,8 +34,16 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->addModelTransformer($transformer);
}
+ public function buildView(FormView $view, FormInterface $form, array $options)
+ {
+ $view->vars['imagine_filter'] = $options['imagine_filter'];
+ }
+
public function setDefaultOptions(OptionsResolverInterface $options)
{
- $options->setDefaults(array('data_class' => 'Doctrine\ODM\PHPCR\Document\Image'));
+ $options->setDefaults(array(
+ 'data_class' => 'Doctrine\ODM\PHPCR\Document\Image',
+ 'imagine_filter' => $this->defaultFilter,
+ ));
}
}
View
4 Resources/config/odm.xml
@@ -60,10 +60,6 @@
<argument type="service" id="service_container"/>
</service>
- <service id="form.type.phpcr_odm.image" class="Doctrine\Bundle\PHPCRBundle\Form\Type\ImageType">
- <tag name="form.type" alias="phpcr_odm_image"/>
- </service>
-
<service id="form.type.phpcr_odm.reference_collection" class="Doctrine\Bundle\PHPCRBundle\Form\Type\PHPCRODMReferenceCollectionType">
<tag name="form.type" alias="phpcr_odm_reference_collection"/>
<argument type="service" id="doctrine_phpcr.odm.document_manager"/>
View
28 Resources/config/odm_image.xml
@@ -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>
View
2  Resources/views/Form/fields.html.twig
@@ -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 %}
Please sign in to comment.
Something went wrong with that request. Please try again.