似乎很基本:我想在删除之前检查 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”检查文件并获得相同的结果)