2

似乎很基本:我想在删除之前检查 Liip Imagine Bundle 缓存中是否存在文件。一个例子:在照片更新后,所有的缓存都被删除了,只有一些缩略图被重新生成(比如 175px 而不是 250px 的),我想删除相应的照片。正如我看到其他人所做的那样,我在 Symfony 中使用侦听器来执行此操作。这是它的外观:

<?php
namespace AppBundle\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use AppBundle\Entity\Photo;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Description of CachePhotoListener
 *
 * @author Norman
 */
class CachePhotoListener 
{
    protected $cacheManager;
    protected $request;

public function __construct($cacheManager, RequestStack $request_stack) 
{
    $this->cacheManager = $cacheManager;
    $this->request = $request_stack->getCurrentRequest();
}

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

    if ($entity instanceof Photo) {
        $this->cacheManager->remove($entity->userPath());
    }
}

// Case : remove photo
public function preRemove(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();

    $filters = array('thumb_prospect_250', 'thumb_prospect_175');

    foreach($filters as $filter){
        if ($entity instanceof Photo) {                
            $expectedCachePath = $this->cacheManager->getBrowserPath($entity->getPath(), $filter);            

            if (file_exists($expectedCachePath)) {
                $this->cacheManager->resolve($this->request, $entity->getPath(), $filter);
                $this->cacheManager->remove($entity->getPath());
            }
        }
    }
}

问题:即使缩略图存在,file_exists 也总是返回“false”。这是 $expectedCachePath 变量的示例:

'http://dev.playermanager/media/cache/thumb_prospect_250/uploads/photos/11/4d176797ca5c7dd753b23ca17b77630eeff0ba8d.jpg' (length=118)

boolean false

'http://dev.playermanager/media/cache/thumb_prospect_175/uploads/photos/11/4d176797ca5c7dd753b23ca17b77630eeff0ba8d.jpg' (length=118)

boolean false

我究竟做错了什么 ?(我还尝试使用“is_readable”检查文件并获得相同的结果)

4

2 回答 2

1

好的,我设法解决了我的问题。我修改了两件事。首先,我使用 isStored 方法检查了 Liip 缓存中照片的存在。其次,我的 cacheManager->resolve 参数中有一个错误,$this->request 参数不应该在那里。

这在侦听器中为我提供了以下 preRemove 函数:

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

        $filters = array('thumb_prospect_250', 'thumb_prospect_175');

        foreach($filters as $filter){
            if ($entity instanceof Photo) {                         
                $cacheExists = $this->cacheManager->isStored($entity->getPath(), $filter);        

                if ($cacheExists) {
                    $this->cacheManager->resolve($entity->getPath(), $filter);
                    $this->cacheManager->remove($entity->getPath());
                }
            }
        }
    }
于 2016-05-29T11:41:35.037 回答
0

我认为在您需要目录路径时调用会$this->cacheManager->getBrowserPath 返回 URL 。

你存储在什么地方$entity->getPath()?也许您可以自己检查该文件是否存在于"%kernel.cache_dir%"目录中。

于 2016-05-28T06:20:12.210 回答