Skip to content

Commit

Permalink
ImageTransformationCache: Increase method visibility for subclassing (#…
Browse files Browse the repository at this point in the history
…503)

* Increase visibility, add isCacheHit helper method
* Add protected getter/setter for current base path
* Add boolean check and fix phpdoc in Transf Cache
  • Loading branch information
matslindh authored and christeredvartsen committed Nov 10, 2016
1 parent 65f18a6 commit 27a7d5b
Showing 1 changed file with 45 additions and 9 deletions.
54 changes: 45 additions & 9 deletions src/EventListener/ImageTransformationCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct(array $params) {
);
}

$this->path = $path;
$this->setPath($path);
}

/**
Expand Down Expand Up @@ -129,7 +129,7 @@ public function loadFromCache(EventInterface $event) {
$event->stopPropagation();

// Mark this as a cache hit to prevent us from re-writing the result
$this->cacheHit = true;
$this->setCacheHit(true);

return;
} else {
Expand All @@ -152,7 +152,7 @@ public function storeInCache(EventInterface $event) {
$response = $event->getResponse();
$model = $response->getModel();

if (!$model instanceof Image || $this->cacheHit) {
if (!$model instanceof Image || $this->isCacheHit()) {
// Only store images in the cache, and don't try to rewrite on cache hit
return;
}
Expand Down Expand Up @@ -210,18 +210,54 @@ public function deleteFromCache(EventInterface $event) {
}
}

/**
* Set whether the current request already did a cache hit
*
* @param boolean $cacheHit Whether the request has already triggered a cache hit
*/
protected function setCacheHit($cacheHit) {
$this->cacheHit = (boolean) $cacheHit;
}

/**
* Check whether the current request hit the cache
*
* @return boolean
*/
protected function isCacheHit() {
return $this->cacheHit;
}

/**
* Get the base path for the cache.
*
* @return string
*/
protected function getPath() {
return $this->path;
}

/**
* Set the current base path for the cache.
*
* @param string $path
*/
protected function setPath($path) {
$this->path = $path;
}

/**
* Get the path to the current image cache dir
*
* @param string $user The user which the image belongs to
* @param string $imageIdentifier The image identifier
* @return string Returns the absolute path to the image cache dir
*/
private function getCacheDir($user, $imageIdentifier) {
protected function getCacheDir($user, $imageIdentifier) {
$userPath = str_pad($user, 3, '0', STR_PAD_LEFT);
return sprintf(
'%s/%s/%s/%s/%s/%s/%s/%s/%s',
$this->path,
$this->getPath(),
$userPath[0],
$userPath[1],
$userPath[2],
Expand All @@ -239,7 +275,7 @@ private function getCacheDir($user, $imageIdentifier) {
* @param Request $request The current request instance
* @return string Returns the absolute path to the cache file
*/
private function getCacheFilePath(Request $request) {
protected function getCacheFilePath(Request $request) {
$hash = $this->getCacheKey($request);
$dir = $this->getCacheDir($request->getUser(), $request->getImageIdentifier());

Expand All @@ -259,7 +295,7 @@ private function getCacheFilePath(Request $request) {
* @param Request $request The current request instance
* @return string Returns a string that can be used as a cache key for the current image
*/
private function getCacheKey(Request $request) {
protected function getCacheKey(Request $request) {
$user = $request->getUser();
$imageIdentifier = $request->getImageIdentifier();
$accept = $request->headers->get('Accept', '*/*');
Expand Down Expand Up @@ -299,7 +335,7 @@ private function getCacheKey(Request $request) {
*
* @param string $dir Name of a directory
*/
private function rmdir($dir) {
protected function rmdir($dir) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::CHILD_FIRST
Expand Down Expand Up @@ -331,7 +367,7 @@ private function rmdir($dir) {
* @param string $path The path to check
* @return boolean
*/
private function isWritable($path) {
protected function isWritable($path) {
if (!is_dir($path)) {
// Path does not exist, check parent
return $this->isWritable(dirname($path));
Expand Down

0 comments on commit 27a7d5b

Please sign in to comment.