Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

added cache-invalidator-listener service #42

Merged
merged 5 commits into from

2 participants

@dbu
Collaborator

we started with a listener service to go with the image document. whenever the image changes, the imagine cache should be invalidated.

EventListener/ImagineCacheInvalidatorSubscriber.php
((50 lines not shown))
+ *
+ * @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');
@dbu Collaborator
dbu added a note

@havvg @lsmith77 is the phpcr support in LiipImagineBundle incomplete? or what do i need to pass to the cache manager? and is there a way to clear all cached images for a specific document or do i need to know a specific filter name?

@havvg
havvg added a note

On 0.9.x you need to resolve the name first, see liip/LiipImagineBundle#135 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dbu
Collaborator

ok, this would be ready to merge. i will wait until tomorrow if you want to give some feedback /cc @rryter @lsmith77

@dbu dbu commented on the diff
EventListener/ImagineCacheInvalidatorSubscriber.php
((89 lines not shown))
+ */
+ 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));
@dbu Collaborator
dbu added a note

if i get a reply on that issue today, i will clean this up if possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dbu dbu merged commit d12d8d8 into from
@dbu dbu deleted the branch
@dbu
Collaborator

also cleaned up the configuration. you now need to explicitly enable imagine to have this block.

the full config options for imagine are now

doctrine_phpcr:
    ...
    imagine:
        enabled: true
        filter: preview_filter_name
        extra_filters:
            - imagine_filter_name1
            - imagine_filter_name2

guess i have to update the doc as well...

/cc @rryter @elhornair

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 8, 2013
Commits on Mar 12, 2013
  1. @dbu

    starting to implement invalidation

    dbu authored
Commits on Mar 13, 2013
  1. @dbu
  2. @dbu
  3. @dbu
This page is out of date. Refresh to see the latest.
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));
@dbu Collaborator
dbu added a note

if i get a reply on that issue today, i will clean this up if possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+ $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 %}
Something went wrong with that request. Please try again.