/** * Call closure * * Call the given closure * and register the result * in cache * * @param \Closure $closure - the closure * @param array $closureArguments - the closure arguments * @param string $id - the cache id * @param string $key - the cache key * * @return string - the result of the closure * @throws \Exception - throw an exception if the result of the closure is not a string */ protected function callClosure(\Closure $closure, $closureArguments, $id, $key) { $result = call_user_func_array($closure, $closureArguments); $cacheCollection = $this->getProvider()->get($id); if ($cacheCollection === null) { $cacheCollection = new CacheCollection($id); } if ($cacheCollection->has($key)) { $cacheCollection->get($key)->setContent($result); } else { $cacheCollection->create($key, $result); } try { $this->getProvider()->save($cacheCollection); } catch (\Exception $e) { throw new \Exception(sprintf("The closure must return a string value to be stored into the cache. %s returned", gettype($result)), 500); } return $result; }
/** * Save * * Save a cache collection * * @param CacheCollection $collection - the cache collection to save * * @return CacheProviderInterface * * @see \Cscfa\Bundle\CacheSystemBundle\Object\provider\CacheProviderInterface::save() */ public function save(CacheCollection $collection) { $directory = $this->cacheDir . $this->prefix; $dir = is_dir($directory); $read = is_readable($directory); $write = is_writable($directory); if ($dir && $read && $write) { if ($this->exist($collection->getId()) && !$this->accessible($collection->getId())) { $read = $this->readable($collection->getId()); $write = $this->writable($collection->getId()); if ($read && !$write) { throw new AccessDeniedException(sprintf("The file %s is on read only state", $this->getPath($collection->getId())), 403); } else { if (!$read && !$write) { throw new AccessDeniedException(sprintf("The file %s cannot be read or write", $this->getPath($collection->getId())), 403); } } } $collection->setTimestampOffset($this->timestamp); $data = $collection->serialize(); $result = file_put_contents($this->getPath($collection->getId()), $data); if ($result === false) { throw new FileException(sprintf("A failure occured during writing file %s", $this->getPath($collection->getId())), 500); } else { if ($result !== strlen($data)) { throw new FileException(sprintf("An error occured during writing file %s. Writed %d bytes, expect %d", $this->getPath($collection->getId()), $result, strlen($data)), 500); } } } else { if (!$dir) { if (!mkdir($directory, 0775, true)) { throw new DirectoryException(sprintf("The %s directory does not exist", $directory), 500); } else { if (!chmod($directory, 0775)) { throw new DirectoryException(sprintf("The %s directory present permission error", $directory), 500); } $this->save($collection); } } else { if ($read && !$write) { throw new AccessDeniedException(sprintf("The directory %s is on read only state", $directory), 403); } else { if (!$read && !$write) { throw new AccessDeniedException(sprintf("The directory %s cannot be read or write", $directory), 403); } else { throw new AccessDeniedException(sprintf("The directory %s cannot be read", $directory), 403); } } } } }