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');
}