diff --git a/Controller/ImagineController.php b/Controller/ImagineController.php index 4b2988f1c..3dfaa9e59 100644 --- a/Controller/ImagineController.php +++ b/Controller/ImagineController.php @@ -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) { @@ -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()); @@ -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()); @@ -160,10 +164,11 @@ public function filterRuntimeAction(Request $request, $hash, $path, $filter) 'filters' => $filters, )), $rcPath, - $filter + $filter, + $resolver ); - return new RedirectResponse($this->cacheManager->resolve($rcPath, $filter), 301); + return new RedirectResponse($this->cacheManager->resolve($rcPath, $filter, $resolver), 301); } catch (NonExistingFilterException $e) { $message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $hash.'/'.$path, $e->getMessage()); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 426c914bf..9fdc77ea4 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -43,6 +43,7 @@ public function getConfigTreeBuilder() ->arrayNode('resolvers') ->useAttributeAsKey('name') ->prototype('array') + ->performNoDeepMerging() ; $this->addResolversSections($resolversPrototypeNode); diff --git a/Imagine/Cache/CacheManager.php b/Imagine/Cache/CacheManager.php index 588978151..e8d89ad1f 100644 --- a/Imagine/Cache/CacheManager.php +++ b/Imagine/Cache/CacheManager.php @@ -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( @@ -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, array(), $resolver) ; } @@ -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) { @@ -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 { @@ -181,12 +195,13 @@ 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); } /** @@ -194,12 +209,13 @@ public function isStored($path, $filter) * * @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)); @@ -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); @@ -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); } /** @@ -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(); diff --git a/Templating/ImagineExtension.php b/Templating/ImagineExtension.php index 321c703ac..9fb4ae96d 100644 --- a/Templating/ImagineExtension.php +++ b/Templating/ImagineExtension.php @@ -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); } /** diff --git a/Tests/Binary/Loader/GridFSLoaderTest.php b/Tests/Binary/Loader/GridFSLoaderTest.php index 8dd80450b..c43926a0a 100644 --- a/Tests/Binary/Loader/GridFSLoaderTest.php +++ b/Tests/Binary/Loader/GridFSLoaderTest.php @@ -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();