Skip to content

Commit

Permalink
Merge pull request #282 from formapro-forks/resolver-do-not-expose-ta…
Browse files Browse the repository at this point in the history
…rget-path

[1.0][resolver] do not expose `targetPath`
  • Loading branch information
havvg committed Dec 15, 2013
2 parents 414fb79 + 42e7395 commit 14def51
Show file tree
Hide file tree
Showing 20 changed files with 313 additions and 243 deletions.
15 changes: 5 additions & 10 deletions Controller/ImagineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,14 @@ public function __construct(DataManager $dataManager, FilterManager $filterManag
*/
public function filterAction(Request $request, $path, $filter)
{
$originalImagePath = $path;
$filteredImagePath = $this->cacheManager->resolve($originalImagePath, $filter);
if ($filteredImagePath instanceof Response) {
return $filteredImagePath;
if ($response = $this->cacheManager->resolve($path, $filter)) {
return $response;
}

$originalImage = $this->dataManager->find($filter, $originalImagePath);
$response = $this->filterManager->get($request, $filter, $originalImage, $originalImagePath);
$image = $this->dataManager->find($filter, $path);

if ($filteredImagePath) {
$response = $this->cacheManager->store($response, $filteredImagePath, $filter);
}
$response = $this->filterManager->get($request, $filter, $image, $path);

return $response;
return $this->cacheManager->store($response, $path, $filter);
}
}
16 changes: 7 additions & 9 deletions Imagine/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,10 @@ public function generateUrl($path, $filter, $absolute = false)
/**
* Resolves filtered path for rendering in the browser.
*
* @param Request $request
* @param string $path
* @param string $filter
*
* @return string|boolean|Response target path or false if filter has no
* resolver or a Response object from the resolver
* @return Response|boolean The response of the respective Resolver or false.
*
* @throws NotFoundHttpException if the path can not be resolved
*/
Expand All @@ -194,15 +192,15 @@ public function resolve($path, $filter)
* @see ResolverInterface::store
*
* @param Response $response
* @param string $targetPath
* @param string $path
* @param string $filter
*
* @return Response
*/
public function store(Response $response, $targetPath, $filter)
public function store(Response $response, $path, $filter)
{
if ($response->isSuccessful()) {
$response = $this->getResolver($filter)->store($response, $targetPath, $filter);
$response = $this->getResolver($filter)->store($response, $path, $filter);
}

return $response;
Expand All @@ -213,14 +211,14 @@ public function store(Response $response, $targetPath, $filter)
*
* @see ResolverInterface::remove
*
* @param string $targetPath
* @param string $path
* @param string $filter
*
* @return bool
*/
public function remove($targetPath, $filter)
public function remove($path, $filter)
{
return $this->getResolver($filter)->remove($targetPath, $filter);
return $this->getResolver($filter)->remove($path, $filter);
}

/**
Expand Down
33 changes: 14 additions & 19 deletions Imagine/Cache/Resolver/AbstractFilesystemResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Kernel;

abstract class AbstractFilesystemResolver implements ResolverInterface, CacheManagerAwareInterface
{
Expand Down Expand Up @@ -83,23 +82,17 @@ public function setFolderPermissions ($folderPermissions)
}

/**
* Stores the content into a static file.
*
* @param Response $response
* @param string $targetPath
* @param string $filter
*
* @return Response
*
* @throws \RuntimeException
* {@inheritDoc}
*/
public function store(Response $response, $targetPath, $filter)
public function store(Response $response, $path, $filter)
{
$dir = pathinfo($targetPath, PATHINFO_DIRNAME);
$filePath = $this->getFilePath($path, $filter);

$dir = pathinfo($filePath, PATHINFO_DIRNAME);

$this->makeFolder($dir);

file_put_contents($targetPath, $response->getContent());
file_put_contents($filePath, $response->getContent());

$response->setStatusCode(201);

Expand All @@ -109,17 +102,19 @@ public function store(Response $response, $targetPath, $filter)
/**
* Removes a stored image resource.
*
* @param string $targetPath The target path provided by the resolve method.
* @param string $path The target path provided by the resolve method.
* @param string $filter The name of the imagine filter in effect.
*
* @return bool Whether the file has been removed successfully.
*/
public function remove($targetPath, $filter)
public function remove($path, $filter)
{
$filename = $this->getFilePath($targetPath, $filter);
$this->filesystem->remove($filename);
$this->basePath = $this->getRequest()->getBaseUrl();
$filePath = $this->getFilePath($path, $filter);

$this->filesystem->remove($filePath);

return !file_exists($filename);
return !file_exists($filePath);
}

/**
Expand All @@ -140,7 +135,7 @@ protected function getRequest()
* @param string $dir
* @throws \RuntimeException
*/
protected function makeFolder ($dir)
protected function makeFolder($dir)
{
if (!is_dir($dir)) {
$parent = dirname($dir);
Expand Down
30 changes: 16 additions & 14 deletions Imagine/Cache/Resolver/AmazonS3Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ public function resolve($path, $filter)
if ($this->objectExists($objectPath)) {
return new RedirectResponse($this->getObjectUrl($objectPath), 301);
}

return $objectPath;
}

/**
* {@inheritDoc}
*/
public function store(Response $response, $targetPath, $filter)
public function store(Response $response, $path, $filter)
{
$storageResponse = $this->storage->create_object($this->bucket, $targetPath, array(
$objectPath = $this->getObjectPath($path, $filter);

$storageResponse = $this->storage->create_object($this->bucket, $objectPath, array(
'body' => $response->getContent(),
'contentType' => $response->headers->get('Content-Type'),
'length' => strlen($response->getContent()),
Expand All @@ -105,11 +105,11 @@ public function store(Response $response, $targetPath, $filter)

if ($storageResponse->isOK()) {
$response->setStatusCode(301);
$response->headers->set('Location', $this->getObjectUrl($targetPath));
$response->headers->set('Location', $this->getObjectUrl($objectPath));
} else {
if ($this->logger) {
$this->logger->warning('The object could not be created on Amazon S3.', array(
'targetPath' => $targetPath,
'objectPath' => $objectPath,
'filter' => $filter,
's3_response' => $storageResponse,
));
Expand All @@ -135,14 +135,16 @@ public function getBrowserPath($path, $filter, $absolute = false)
/**
* {@inheritDoc}
*/
public function remove($targetPath, $filter)
public function remove($path, $filter)
{
if (!$this->objectExists($targetPath)) {
// A non-existing object to delete: done!
return true;
$objectPath = $this->getObjectPath($path, $filter);

if ($this->objectExists($objectPath)) {
return $this->storage->delete_object($this->bucket, $objectPath)->isOK();
}

return $this->storage->delete_object($this->bucket, $targetPath)->isOK();
// A non-existing object to delete: done!
return true;
}

/**
Expand Down Expand Up @@ -188,13 +190,13 @@ protected function getObjectPath($path, $filter)
/**
* Returns the URL for an object saved on Amazon S3.
*
* @param string $targetPath
* @param string $path
*
* @return string
*/
protected function getObjectUrl($targetPath)
protected function getObjectUrl($path)
{
return $this->storage->get_object_url($this->bucket, $targetPath, 0, $this->objUrlOptions);
return $this->storage->get_object_url($this->bucket, $path, 0, $this->objUrlOptions);
}

/**
Expand Down
26 changes: 14 additions & 12 deletions Imagine/Cache/Resolver/AwsS3Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,27 @@ public function resolve($path, $filter)
if ($this->objectExists($objectPath)) {
return new RedirectResponse($this->getObjectUrl($objectPath), 301);
}

return $objectPath;
}

/**
* {@inheritDoc}
*/
public function store(Response $response, $targetPath, $filter)
public function store(Response $response, $path, $filter)
{
$objectPath = $this->getObjectPath($path, $filter);

try {
$storageResponse = $this->storage->putObject(array(
'ACL' => $this->acl,
'Bucket' => $this->bucket,
'Key' => $targetPath,
'Key' => $objectPath,
'Body' => $response->getContent(),
'ContentType' => $response->headers->get('Content-Type')
));
} catch (\Exception $e) {
if ($this->logger) {
$this->logger->warning('The object could not be created on Amazon S3.', array(
'targetPath' => $targetPath,
'objectPath' => $objectPath,
'filter' => $filter,
));
}
Expand Down Expand Up @@ -137,17 +137,19 @@ public function getBrowserPath($path, $filter, $absolute = false)
/**
* {@inheritDoc}
*/
public function remove($targetPath, $filter)
public function remove($path, $filter)
{
if (!$this->objectExists($targetPath)) {
$objectPath = $this->getObjectPath($path, $filter);

if (!$this->objectExists($objectPath)) {
// A non-existing object to delete: done!
return true;
}

try {
$response = $this->storage->deleteObject(array(
$this->storage->deleteObject(array(
'Bucket' => $this->bucket,
'Key' => $targetPath,
'Key' => $objectPath,
));

return true;
Expand Down Expand Up @@ -199,13 +201,13 @@ protected function getObjectPath($path, $filter)
/**
* Returns the URL for an object saved on Amazon S3.
*
* @param string $targetPath
* @param string $path
*
* @return string
*/
protected function getObjectUrl($targetPath)
protected function getObjectUrl($path)
{
return $this->storage->getObjectUrl($this->bucket, $targetPath, 0, $this->objUrlOptions);
return $this->storage->getObjectUrl($this->bucket, $path, 0, $this->objUrlOptions);
}

/**
Expand Down
34 changes: 10 additions & 24 deletions Imagine/Cache/Resolver/CacheResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,18 @@ public function resolve($path, $filter)
return $this->cache->fetch($key);
}

$targetPath = $this->resolver->resolve($path, $filter);
$this->saveToCache($key, $targetPath);
$resolved = $this->resolver->resolve($path, $filter);
$this->saveToCache($key, $resolved);

/*
* The targetPath being a string will be forwarded to the ResolverInterface::store method.
* As there is no way to reverse this operation by the interface, we store this information manually.
*
* If it's not a string, it's a Response it will be returned as it without calling the store method.
*/
if (is_string($targetPath)) {
$reverseKey = $this->generateCacheKey('reverse', $targetPath, $filter);
$this->saveToCache($reverseKey, $path);
}

return $targetPath;
return $resolved;
}

/**
* {@inheritDoc}
*/
public function store(Response $response, $targetPath, $filter)
public function store(Response $response, $path, $filter)
{
return $this->resolver->store($response, $targetPath, $filter);
return $this->resolver->store($response, $path, $filter);
}

/**
Expand All @@ -108,18 +97,16 @@ public function getBrowserPath($path, $filter, $absolute = false)
/**
* {@inheritDoc}
*/
public function remove($targetPath, $filter)
public function remove($path, $filter)
{
$removed = $this->resolver->remove($targetPath, $filter);
$removed = $this->resolver->remove($path, $filter);

// If the resolver did not remove the content, we can leave the cache.
if ($removed) {
$reverseKey = $this->generateCacheKey('reverse', $targetPath, $filter);
if ($this->cache->contains($reverseKey)) {
$path = $this->cache->fetch($reverseKey);

$key = $this->generateCacheKey('resolve', $path, $filter);
if ($this->cache->contains($key)) {
// The indexKey is not utilizing the method so the value is not important.
$indexKey = $this->generateIndexKey($this->generateCacheKey(null, $path, $filter));
$indexKey = $this->generateIndexKey($key);

// Retrieve the index and remove the content from the cache.
$index = $this->cache->fetch($indexKey);
Expand All @@ -129,7 +116,6 @@ public function remove($targetPath, $filter)

// Remove the auxiliary keys.
$this->cache->delete($indexKey);
$this->cache->delete($reverseKey);
}
}

Expand Down
7 changes: 2 additions & 5 deletions Imagine/Cache/Resolver/NoCacheResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,20 @@ class NoCacheResolver extends WebPathResolver
*/
public function resolve($path, $filter)
{
$this->setBasePath($this->getRequest()->getBaseUrl());

return $this->getFilePath($path, $filter);
}

/**
* {@inheritDoc}
*/
public function store(Response $response, $targetPath, $filter)
public function store(Response $response, $path, $filter)
{
return $response;
}

/**
* {@inheritDoc}
*/
public function remove($targetPath, $filter)
public function remove($path, $filter)
{
return true;
}
Expand Down
Loading

0 comments on commit 14def51

Please sign in to comment.