Imbo\EventListener\ImageTransformationCache::loadFromCache PHP Method

loadFromCache() public method

Load transformed images from the cache
public loadFromCache ( Imbo\EventManager\EventInterface $event )
$event Imbo\EventManager\EventInterface The current event
    public function loadFromCache(EventInterface $event)
    {
        $request = $event->getRequest();
        $response = $event->getResponse();
        // Generate the full file path to the response
        $path = $this->getCacheFilePath($request);
        if (is_file($path)) {
            $data = @unserialize(file_get_contents($path));
            // Make sure the data from the cache is valid
            if (is_array($data) && isset($data['image']) && isset($data['headers']) && $data['image'] instanceof Image && $data['headers'] instanceof ResponseHeaderBag) {
                // Mark as cache hit
                $data['headers']->set('X-Imbo-TransformationCache', 'Hit');
                $data['image']->hasBeenTransformed(false);
                // Replace all headers and set the image model
                $response->headers = $data['headers'];
                $response->setModel($data['image']);
                // Stop other listeners on this event
                $event->stopPropagation();
                // Mark this as a cache hit to prevent us from re-writing the result
                $this->cacheHit = true;
                return;
            } else {
                // Invalid data in the cache, delete the file
                unlink($path);
            }
        }
        // Mark as cache miss
        $response->headers->set('X-Imbo-TransformationCache', 'Miss');
    }

Usage Example

コード例 #1
0
 /**
  * @covers Imbo\EventListener\ImageTransformationCache::loadFromCache
  * @covers Imbo\EventListener\ImageTransformationCache::getCacheDir
  * @covers Imbo\EventListener\ImageTransformationCache::getCacheKey
  * @covers Imbo\EventListener\ImageTransformationCache::getCacheFilePath
  */
 public function testAddsCorrectResponseHeaderOnCacheMiss()
 {
     $this->requestHeaders->expects($this->once())->method('get')->with('Accept', '*/*')->will($this->returnValue('*/*'));
     $this->responseHeaders->expects($this->once())->method('set')->with('X-Imbo-TransformationCache', 'Miss');
     $this->listener->loadFromCache($this->event);
 }