Skip to content

Commit

Permalink
Merge pull request #284 from formapro-forks/resolver-get-rid-of-get-b…
Browse files Browse the repository at this point in the history
…rowser-path

[1.0][CacheResolver] Resolver get rid of get browser path
  • Loading branch information
havvg committed Dec 18, 2013
2 parents 14def51 + ff6b7a7 commit 11e889a
Show file tree
Hide file tree
Showing 19 changed files with 293 additions and 301 deletions.
5 changes: 3 additions & 2 deletions Controller/ImagineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Liip\ImagineBundle\Imagine\Data\DataManager;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -51,8 +52,8 @@ public function __construct(DataManager $dataManager, FilterManager $filterManag
*/
public function filterAction(Request $request, $path, $filter)
{
if ($response = $this->cacheManager->resolve($path, $filter)) {
return $response;
if ($this->cacheManager->isStored($path, $filter)) {
return new RedirectResponse($this->cacheManager->resolve($path, $filter), 301);
}

$image = $this->dataManager->find($filter, $path);
Expand Down
8 changes: 8 additions & 0 deletions Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Liip\ImagineBundle\Exception;

interface ExceptionInterface
{

}
10 changes: 10 additions & 0 deletions Exception/Imagine/Cache/Resolver/NotResolvableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Liip\ImagineBundle\Exception\Imagine\Cache\Resolver;

use Liip\ImagineBundle\Exception\ExceptionInterface;

class NotResolvableException extends \RuntimeException implements ExceptionInterface
{

}
24 changes: 21 additions & 3 deletions Imagine/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ protected function getResolver($filter)

/**
* Gets filtered path for rendering in the browser.
*
* @see ResolverInterface::getBrowserPath
* It could be the cached one or an url of filter action.
*
* @param string $path The path where the resolved file is expected.
* @param string $filter
Expand All @@ -120,7 +119,13 @@ protected function getResolver($filter)
*/
public function getBrowserPath($path, $filter, $absolute = false)
{
return $this->getResolver($filter)->getBrowserPath($path, $filter, $absolute);
//call it to make sure the resolver for the give filter exists.
$this->getResolver($filter);

return
$this->resolve($path, $filter) ?:
$this->generateUrl($path, $filter, $absolute)
;
}

/**
Expand Down Expand Up @@ -161,6 +166,19 @@ public function generateUrl($path, $filter, $absolute = false)
);
}

/**
* Checks whether the path is already stored within the respective Resolver.
*
* @param string $path
* @param string $filter
*
* @return bool
*/
public function isStored($path, $filter)
{
return $this->getResolver($filter)->isStored($path, $filter);
}

/**
* Resolves filtered path for rendering in the browser.
*
Expand Down
8 changes: 8 additions & 0 deletions Imagine/Cache/Resolver/AbstractFilesystemResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public function setFolderPermissions ($folderPermissions)
$this->folderPermissions = $folderPermissions;
}

/**
* {@inheritDoc}
*/
public function isStored($path, $filter)
{
return file_exists($this->getFilePath($path, $filter));
}

/**
* {@inheritDoc}
*/
Expand Down
40 changes: 7 additions & 33 deletions Imagine/Cache/Resolver/AmazonS3Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

namespace Liip\ImagineBundle\Imagine\Cache\Resolver;

use \AmazonS3;

use Liip\ImagineBundle\Imagine\Cache\CacheManagerAwareInterface;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

class AmazonS3Resolver implements ResolverInterface, CacheManagerAwareInterface
class AmazonS3Resolver implements ResolverInterface
{
/**
* @var AmazonS3
* @var \AmazonS3
*/
protected $storage;

Expand All @@ -27,11 +22,6 @@ class AmazonS3Resolver implements ResolverInterface, CacheManagerAwareInterface
*/
protected $acl;

/**
* @var CacheManager
*/
protected $cacheManager;

/**
* @var array
*/
Expand All @@ -50,7 +40,7 @@ class AmazonS3Resolver implements ResolverInterface, CacheManagerAwareInterface
* @param string $acl The ACL to use when storing new objects. Default: owner read/write, public read
* @param array $objUrlOptions A list of options to be passed when retrieving the object url from Amazon S3.
*/
public function __construct(AmazonS3 $storage, $bucket, $acl = AmazonS3::ACL_PUBLIC, array $objUrlOptions = array())
public function __construct(\AmazonS3 $storage, $bucket, $acl = \AmazonS3::ACL_PUBLIC, array $objUrlOptions = array())
{
$this->storage = $storage;

Expand All @@ -71,22 +61,19 @@ public function setLogger(LoggerInterface $logger)
}

/**
* @param CacheManager $cacheManager
* {@inheritDoc}
*/
public function setCacheManager(CacheManager $cacheManager)
public function isStored($path, $filter)
{
$this->cacheManager = $cacheManager;
return $this->objectExists($this->getObjectPath($path, $filter));
}

/**
* {@inheritDoc}
*/
public function resolve($path, $filter)
{
$objectPath = $this->getObjectPath($path, $filter);
if ($this->objectExists($objectPath)) {
return new RedirectResponse($this->getObjectUrl($objectPath), 301);
}
return $this->getObjectUrl($this->getObjectPath($path, $filter));
}

/**
Expand Down Expand Up @@ -119,19 +106,6 @@ public function store(Response $response, $path, $filter)
return $response;
}

/**
* {@inheritDoc}
*/
public function getBrowserPath($path, $filter, $absolute = false)
{
$objectPath = $this->getObjectPath($path, $filter);
if ($this->objectExists($objectPath)) {
return $this->getObjectUrl($objectPath);
}

return $this->cacheManager->generateUrl($path, $filter, $absolute);
}

/**
* {@inheritDoc}
*/
Expand Down
34 changes: 5 additions & 29 deletions Imagine/Cache/Resolver/AwsS3Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

use Aws\S3\Enum\CannedAcl;
use Aws\S3\S3Client;
use Liip\ImagineBundle\Imagine\Cache\CacheManagerAwareInterface;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

class AwsS3Resolver implements ResolverInterface, CacheManagerAwareInterface
class AwsS3Resolver implements ResolverInterface
{
/**
* @var S3Client
Expand All @@ -27,11 +24,6 @@ class AwsS3Resolver implements ResolverInterface, CacheManagerAwareInterface
*/
protected $acl;

/**
* @var CacheManager
*/
protected $cacheManager;

/**
* @var array
*/
Expand Down Expand Up @@ -71,22 +63,19 @@ public function setLogger(LoggerInterface $logger)
}

/**
* @param CacheManager $cacheManager
* {@inheritDoc}
*/
public function setCacheManager(CacheManager $cacheManager)
public function isStored($path, $filter)
{
$this->cacheManager = $cacheManager;
return $this->objectExists($this->getObjectPath($path, $filter));
}

/**
* {@inheritDoc}
*/
public function resolve($path, $filter)
{
$objectPath = $this->getObjectPath($path, $filter);
if ($this->objectExists($objectPath)) {
return new RedirectResponse($this->getObjectUrl($objectPath), 301);
}
return $this->getObjectUrl($this->getObjectPath($path, $filter));
}

/**
Expand Down Expand Up @@ -121,19 +110,6 @@ public function store(Response $response, $path, $filter)
return $response;
}

/**
* {@inheritDoc}
*/
public function getBrowserPath($path, $filter, $absolute = false)
{
$objectPath = $this->getObjectPath($path, $filter);
if ($this->objectExists($objectPath)) {
return $this->getObjectUrl($objectPath);
}

return $this->cacheManager->generateUrl($path, $filter, $absolute);
}

/**
* {@inheritDoc}
*/
Expand Down
33 changes: 12 additions & 21 deletions Imagine/Cache/Resolver/CacheResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Liip\ImagineBundle\Imagine\Cache\Resolver;

use Doctrine\Common\Cache\Cache;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
Expand Down Expand Up @@ -51,6 +50,15 @@ public function __construct(Cache $cache, ResolverInterface $cacheResolver, arra
$this->options = $optionsResolver->resolve($options);
}

/**
* {@inheritDoc}
*/
public function isStored($path, $filter)
{
// We do not actually save this operation.
return $this->resolver->isStored($path, $filter);
}

/**
* {@inheritDoc}
*/
Expand All @@ -62,7 +70,9 @@ public function resolve($path, $filter)
}

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

return $resolved;
}
Expand All @@ -75,25 +85,6 @@ public function store(Response $response, $path, $filter)
return $this->resolver->store($response, $path, $filter);
}

/**
* {@inheritDoc}
*/
public function getBrowserPath($path, $filter, $absolute = false)
{
$key = $this->generateCacheKey('getBrowserPath', $path, $filter, array(
$absolute ? 'absolute' : 'relative',
));

if ($this->cache->contains($key)) {
return $this->cache->fetch($key);
}

$result = $this->resolver->getBrowserPath($path, $filter, $absolute);
$this->saveToCache($key, $result);

return $result;
}

/**
* {@inheritDoc}
*/
Expand Down
10 changes: 9 additions & 1 deletion Imagine/Cache/Resolver/NoCacheResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@
*/
class NoCacheResolver extends WebPathResolver
{
/**
* {@inheritDoc}
*/
public function isStored($path, $filter)
{
return true;
}

/**
* {@inheritDoc}
*/
public function resolve($path, $filter)
{
return $this->getRequest()->getSchemeAndHttpHost().'/'.$path;
}

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

/**
Expand Down
Loading

0 comments on commit 11e889a

Please sign in to comment.