Skip to content

Commit

Permalink
Merge pull request #498 from trsteel88/runtime-config-paths
Browse files Browse the repository at this point in the history
check if runtimeconfig path is stored
  • Loading branch information
makasim committed Sep 15, 2014
2 parents 627f52b + dc36f15 commit 334653c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Controller/ImagineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,17 @@ public function filterRuntimeAction(Request $request, $hash, $path, $filter)
throw new NotFoundHttpException(sprintf('Source image could not be found for path "%s" and filter "%s"', $path, $filter), $e);
}

$cachePrefix = 'rc/'.$hash;
$rcPath = $this->cacheManager->getRuntimePath($path, $filters);

$this->cacheManager->store(
$this->filterManager->applyFilter($binary, $filter, array(
'filters' => $filters,
)),
$cachePrefix.'/'.$path,
$rcPath,
$filter
);

return new RedirectResponse($this->cacheManager->resolve($cachePrefix.'/'.$path, $filter), 301);
return new RedirectResponse($this->cacheManager->resolve($rcPath, $filter), 301);
} catch (RuntimeException $e) {
throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $hash.'/'.$path, $filter, $e->getMessage()), 0, $e);
}
Expand Down
18 changes: 17 additions & 1 deletion Imagine/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ protected function getResolver($filter)
public function getBrowserPath($path, $filter, array $runtimeConfig = array())
{
if (!empty($runtimeConfig)) {
return $this->generateUrl($path, $filter, $runtimeConfig);
$rcPath = $this->getRuntimePath($path, $runtimeConfig);

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

return $this->isStored($path, $filter) ?
Expand All @@ -134,6 +139,17 @@ public function getBrowserPath($path, $filter, array $runtimeConfig = array())
;
}

/**
* Get path to runtime config image
*
* @param string $path
* @param array $runtimeConfig
*/
public function getRuntimePath($path, array $runtimeConfig)
{
return 'rc/'.$this->signer->sign($path, $runtimeConfig).'/'.$path;
}

/**
* Returns a web accessible URL.
*
Expand Down
61 changes: 61 additions & 0 deletions Tests/Imagine/Cache/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ public function testGetBrowserPathWithoutResolver()
$cacheManager->getBrowserPath('cats.jpeg', 'thumbnail');
}

public function testGetRuntimePath()
{
$config = $this->createFilterConfigurationMock();
$cacheManager = new CacheManager($config, $this->createRouterMock(), new Signer('secret'), $this->createEventDispatcherMock());

$rcPath = $cacheManager->getRuntimePath('image.jpg', array(
'thumbnail' => array(
'size' => array(180, 180)
)
));

$this->assertEquals('rc/ILfTutxX/image.jpg', $rcPath);
}

public function testDefaultResolverUsedIfNoneSetOnGetBrowserPath()
{
$resolver = $this->createResolverMock();
Expand Down Expand Up @@ -134,6 +148,53 @@ public function testFilterActionUrlGeneratedAndReturnIfResolverReturnNullOnGetBr
$this->assertEquals('/media/cache/thumbnail/cats.jpeg', $actualBrowserPath);
}

public function testFilterActionUrlGeneratedAndReturnIfResolverReturnNullOnGetBrowserPathWithRuntimeConfig()
{
$runtimeConfig = array(
'thumbnail' => array(
'size' => array(100, 100),
)
);

$resolver = $this->createResolverMock();
$resolver
->expects($this->once())
->method('isStored')
->with('rc/VhOzTGRB/cats.jpeg', 'thumbnail')
->will($this->returnValue(false))
;
$resolver
->expects($this->never())
->method('resolve')
;

$config = $this->createFilterConfigurationMock();
$config
->expects($this->atLeastOnce())
->method('get')
->with('thumbnail')
->will($this->returnValue(array(
'size' => array(180, 180),
'mode' => 'outbound',
'cache' => null,
)))
;

$router = $this->createRouterMock();
$router
->expects($this->once())
->method('generate')
->will($this->returnValue('/media/cache/thumbnail/rc/VhOzTGRB/cats.jpeg'))
;

$cacheManager = new CacheManager($config, $router, new Signer('secret'), $this->createEventDispatcherMock());
$cacheManager->addResolver('default', $resolver);

$actualBrowserPath = $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail', $runtimeConfig);

$this->assertEquals('/media/cache/thumbnail/rc/VhOzTGRB/cats.jpeg', $actualBrowserPath);
}

/**
* @dataProvider invalidPathProvider
*/
Expand Down

0 comments on commit 334653c

Please sign in to comment.