Update and remove cached image using Liipimaginebundle

寵の児 提交于 2019-12-05 17:27:45

Here is a solution that works fine

services:
    project.cacheimage_listener:
        class: Project\DashboardBundle\Listener\CacheImageListener
        arguments: ["@liip_imagine.cache.manager"]
        tags:
            - { name: doctrine.event_listener, event: postUpdate }
            - { name: doctrine.event_listener, event: preRemove }

create the listener

namespace Project\DashboardBundle\Listener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Project\DashboardBundle\Entity\Image;

class CacheImageListener
{
protected $cacheManager;

public function __construct($cacheManager)
{
    $this->cacheManager = $cacheManager;
}

public function postUpdate(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();

    if ($entity instanceof Image) {
        // clear cache of thumbnail
        $this->cacheManager->remove($entity->getUploadDir());
    }
}

// when delete entity so remove all thumbnails related 
public function preRemove(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();

    if ($entity instanceof Image) {

        $this->cacheManager->remove($entity->getWebPath());
    }
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!