Skip to content

Commit

Permalink
[resolver] Add ability to force resolver.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Dec 25, 2015
1 parent c2dfddf commit d50bb97
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
10 changes: 7 additions & 3 deletions Controller/ImagineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ public function filterAction(Request $request, $path, $filter)
{
// decoding special characters and whitespaces from path obtained from url
$path = urldecode($path);
$resolver = $request->get('resolver');

try {
if (!$this->cacheManager->isStored($path, $filter)) {
if (!$this->cacheManager->isStored($path, $filter, $resolver)) {
try {
$binary = $this->dataManager->find($filter, $path);
} catch (NotLoadableException $e) {
Expand All @@ -94,11 +95,12 @@ public function filterAction(Request $request, $path, $filter)
$this->cacheManager->store(
$this->filterManager->applyFilter($binary, $filter),
$path,
$filter
$filter,
$resolver
);
}

return new RedirectResponse($this->cacheManager->resolve($path, $filter), 301);
return new RedirectResponse($this->cacheManager->resolve($path, $filter, $resolver), 301);
} catch (NonExistingFilterException $e) {
$message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $path, $e->getMessage());

Expand Down Expand Up @@ -127,6 +129,8 @@ public function filterAction(Request $request, $path, $filter)
*/
public function filterRuntimeAction(Request $request, $hash, $path, $filter)
{
$resolver = $request->get('resolver');

try {
$filters = $request->query->get('filters', array());

Expand Down
53 changes: 35 additions & 18 deletions Imagine/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,22 @@ public function addResolver($filter, ResolverInterface $resolver)
* In case there is no specific resolver, but a default resolver has been configured, the default will be returned.
*
* @param string $filter
* @param string $resolver
*
* @return ResolverInterface
*
* @throws \OutOfBoundsException If neither a specific nor a default resolver is available.
*/
protected function getResolver($filter)
protected function getResolver($filter, $resolver)
{
$config = $this->filterConfig->get($filter);
// BC
if (false == $resolver) {
$config = $this->filterConfig->get($filter);

$resolverName = empty($config['cache']) ? $this->defaultResolver : $config['cache'];
$resolverName = empty($config['cache']) ? $this->defaultResolver : $config['cache'];
} else {
$resolverName = $resolver;
}

if (!isset($this->resolvers[$resolverName])) {
throw new \OutOfBoundsException(sprintf(
Expand All @@ -117,23 +123,24 @@ protected function getResolver($filter)
* @param string $path The path where the resolved file is expected.
* @param string $filter
* @param array $runtimeConfig
* @param string $resolver
*
* @return string
*/
public function getBrowserPath($path, $filter, array $runtimeConfig = array())
public function getBrowserPath($path, $filter, array $runtimeConfig = array(), $resolver = null)
{
if (!empty($runtimeConfig)) {
$rcPath = $this->getRuntimePath($path, $runtimeConfig);

return $this->isStored($rcPath, $filter) ?
$this->resolve($rcPath, $filter) :
$this->generateUrl($path, $filter, $runtimeConfig)
return $this->isStored($rcPath, $filter, $resolver) ?
$this->resolve($rcPath, $filter, $resolver) :
$this->generateUrl($path, $filter, $runtimeConfig, $resolver)
;
}

return $this->isStored($path, $filter) ?
$this->resolve($path, $filter) :
$this->generateUrl($path, $filter)
return $this->isStored($path, $filter, $resolver) ?
$this->resolve($path, $filter, $resolver) :
$this->generateUrl($path, $filter, [], $resolver)
;
}

Expand All @@ -142,6 +149,8 @@ public function getBrowserPath($path, $filter, array $runtimeConfig = array())
*
* @param string $path
* @param array $runtimeConfig
*
* @return string
*/
public function getRuntimePath($path, array $runtimeConfig)
{
Expand All @@ -154,16 +163,21 @@ public function getRuntimePath($path, array $runtimeConfig)
* @param string $path The path where the resolved file is expected.
* @param string $filter The name of the imagine filter in effect.
* @param array $runtimeConfig
* @param string $resolver
*
* @return string
*/
public function generateUrl($path, $filter, array $runtimeConfig = array())
public function generateUrl($path, $filter, array $runtimeConfig = array(), $resolver = null)
{
$params = array(
'path' => ltrim($path, '/'),
'filter' => $filter,
);

if ($resolver) {
$params['resolver'] = $resolver;
}

if (empty($runtimeConfig)) {
$filterUrl = $this->router->generate('liip_imagine_filter', $params, UrlGeneratorInterface::ABSOLUTE_URL);
} else {
Expand All @@ -181,25 +195,27 @@ public function generateUrl($path, $filter, array $runtimeConfig = array())
*
* @param string $path
* @param string $filter
* @param string $resolver
*
* @return bool
*/
public function isStored($path, $filter)
public function isStored($path, $filter, $resolver = null)
{
return $this->getResolver($filter)->isStored($path, $filter);
return $this->getResolver($filter, $resolver)->isStored($path, $filter);
}

/**
* Resolves filtered path for rendering in the browser.
*
* @param string $path
* @param string $filter
* @param string $resolver
*
* @return string The url of resolved image.
*
* @throws NotFoundHttpException if the path can not be resolved
*/
public function resolve($path, $filter)
public function resolve($path, $filter, $resolver = null)
{
if (false !== strpos($path, '/../') || 0 === strpos($path, '../')) {
throw new NotFoundHttpException(sprintf("Source image was searched with '%s' outside of the defined root path", $path));
Expand All @@ -208,7 +224,7 @@ public function resolve($path, $filter)
$preEvent = new CacheResolveEvent($path, $filter);
$this->dispatcher->dispatch(ImagineEvents::PRE_RESOLVE, $preEvent);

$url = $this->getResolver($preEvent->getFilter())->resolve($preEvent->getPath(), $preEvent->getFilter());
$url = $this->getResolver($preEvent->getFilter(), $resolver)->resolve($preEvent->getPath(), $preEvent->getFilter());

$postEvent = new CacheResolveEvent($preEvent->getPath(), $preEvent->getFilter(), $url);
$this->dispatcher->dispatch(ImagineEvents::POST_RESOLVE, $postEvent);
Expand All @@ -222,10 +238,11 @@ public function resolve($path, $filter)
* @param BinaryInterface $binary
* @param string $path
* @param string $filter
* @param string $resolver
*/
public function store(BinaryInterface $binary, $path, $filter)
public function store(BinaryInterface $binary, $path, $filter, $resolver = null)
{
$this->getResolver($filter)->store($binary, $path, $filter);
$this->getResolver($filter, $resolver)->store($binary, $path, $filter);
}

/**
Expand All @@ -249,7 +266,7 @@ public function remove($paths = null, $filters = null)

$mapping = new \SplObjectStorage();
foreach ($filters as $filter) {
$resolver = $this->getResolver($filter);
$resolver = $this->getResolver($filter, null);

$list = isset($mapping[$resolver]) ? $mapping[$resolver] : array();

Expand Down
5 changes: 3 additions & 2 deletions Templating/ImagineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ public function getFilters()
* @param string $path
* @param string $filter
* @param array $runtimeConfig
* @param string $resolver
*
* @return \Twig_Markup
*/
public function filter($path, $filter, array $runtimeConfig = array())
public function filter($path, $filter, array $runtimeConfig = array(), $resolver = null)
{
return $this->cacheManager->getBrowserPath($path, $filter, $runtimeConfig);
return $this->cacheManager->getBrowserPath($path, $filter, $runtimeConfig, $resolver);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion Tests/Binary/Loader/GridFSLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class GridFSLoaderTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
if (!extension_loaded('mongodb')) {
$this->markTestSkipped('ext/mongodb not installed');
$this->markTestSkipped('ext/mongodb is not installed');
}
if (!class_exists('Doctrine\MongoDB\GridFSFile')) {
$this->markTestSkipped('doctrine mongo odm is not installed');
}
$this->repo = $this->getMockBuilder('Doctrine\ODM\MongoDB\DocumentRepository')->disableOriginalConstructor()->getMock();

Expand Down

0 comments on commit d50bb97

Please sign in to comment.