Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.0][resolver] remove request parameter #278

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Controller/ImagineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,17 @@ public function __construct(DataManager $dataManager, FilterManager $filterManag
*/
public function filterAction(Request $request, $path, $filter)
{
$targetPath = $this->cacheManager->resolve($request, $path, $filter);
if ($targetPath instanceof Response) {
return $targetPath;
$originalImagePath = $path;
$filteredImagePath = $this->cacheManager->resolve($originalImagePath, $filter);
if ($filteredImagePath instanceof Response) {
return $filteredImagePath;
}

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

if ($targetPath) {
$response = $this->cacheManager->store($response, $targetPath, $filter);
if ($filteredImagePath) {
$response = $this->cacheManager->store($response, $filteredImagePath, $filter);
}

return $response;
Expand Down
4 changes: 2 additions & 2 deletions Imagine/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function generateUrl($path, $filter, $absolute = false)
*
* @throws NotFoundHttpException if the path can not be resolved
*/
public function resolve(Request $request, $path, $filter)
public function resolve($path, $filter)
{
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 @@ -185,7 +185,7 @@ public function resolve(Request $request, $path, $filter)
return false;
}

return $resolver->resolve($request, $path, $filter);
return $resolver->resolve($path, $filter);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Imagine/Cache/Resolver/AmazonS3Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function setCacheManager(CacheManager $cacheManager)
/**
* {@inheritDoc}
*/
public function resolve(Request $request, $path, $filter)
public function resolve($path, $filter)
{
$objectPath = $this->getObjectPath($path, $filter);
if ($this->objectExists($objectPath)) {
Expand Down
2 changes: 1 addition & 1 deletion Imagine/Cache/Resolver/AwsS3Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function setCacheManager(CacheManager $cacheManager)
/**
* {@inheritDoc}
*/
public function resolve(Request $request, $path, $filter)
public function resolve($path, $filter)
{
$objectPath = $this->getObjectPath($path, $filter);
if ($this->objectExists($objectPath)) {
Expand Down
6 changes: 3 additions & 3 deletions Imagine/Cache/Resolver/CacheResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Doctrine\Common\Cache\Cache;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
Expand Down Expand Up @@ -55,14 +55,14 @@ public function __construct(Cache $cache, ResolverInterface $cacheResolver, arra
/**
* {@inheritDoc}
*/
public function resolve(Request $request, $path, $filter)
public function resolve($path, $filter)
{
$key = $this->generateCacheKey('resolve', $path, $filter);
if ($this->cache->contains($key)) {
return $this->cache->fetch($key);
}

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

/*
Expand Down
5 changes: 2 additions & 3 deletions Imagine/Cache/Resolver/NoCacheResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Liip\ImagineBundle\Imagine\Cache\Resolver;

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

/**
Expand All @@ -13,9 +12,9 @@ class NoCacheResolver extends WebPathResolver
/**
* {@inheritDoc}
*/
public function resolve(Request $request, $path, $filter)
public function resolve($path, $filter)
{
$this->setBasePath($request->getBaseUrl());
$this->setBasePath($this->container->get('request')->getBaseUrl());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really bad, imho. I would suggest a RequestAwareInterface and inject the Request if accordingly. There is no reason to inject the container just to receive the base url.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RequestAwareInterface what is this? Is shipped with symfony. Does symfony 2.0 have it?

P.S. I know that this is really bad but as far as I know this how it could be done for symfony 2.0.


return $this->getFilePath($path, $filter);
}
Expand Down
4 changes: 1 addition & 3 deletions Imagine/Cache/Resolver/ResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

namespace Liip\ImagineBundle\Imagine\Cache\Resolver;

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

interface ResolverInterface
{
/**
* Resolves filtered path for rendering in the browser.
*
* @param Request $request The request made against a _imagine_* filter route.
* @param string $path The path where the resolved file is expected.
* @param string $filter The name of the imagine filter in effect.
*
Expand All @@ -19,7 +17,7 @@ interface ResolverInterface
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException In case the path can not be resolved.
*/
function resolve(Request $request, $path, $filter);
function resolve($path, $filter);

/**
* Stores the content of the given Response.
Expand Down
22 changes: 19 additions & 3 deletions Imagine/Cache/Resolver/WebPathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,31 @@

use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class WebPathResolver extends AbstractFilesystemResolver
class WebPathResolver extends AbstractFilesystemResolver implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
protected $container;

/**
* {@inheritDoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

/**
* {@inheritDoc}
*/
public function resolve(Request $request, $path, $filter)
public function resolve($path, $filter)
{
$request = $this->container->get('request');

$browserPath = $this->decodeBrowserPath($this->getBrowserPath($path, $filter));
$this->basePath = $request->getBaseUrl();
$targetPath = $this->getFilePath($path, $filter);
Expand Down
6 changes: 6 additions & 0 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,17 @@
<call method="setFolderPermissions">
<argument type="string">%liip_imagine.cache_mkdir_mode%</argument>
</call>
<call method="setContainer">
<argument type="service">service_container</argument>
</call>
</service>

<service id="liip_imagine.cache.resolver.no_cache" class="%liip_imagine.cache.resolver.no_cache.class%">
<tag name="liip_imagine.cache.resolver" resolver="no_cache" />
<argument type="service" id="filesystem" />
<call method="setContainer">
<argument type="service">service_container</argument>
</call>
</service>

<!-- Form types -->
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ but it illustrates the core idea.
``` php
public function filterAction(Request $request, $path, $filter)
{
$targetPath = $this->cacheManager->resolve($request, $path, $filter);
$targetPath = $this->cacheManager->resolve($path, $filter);
if ($targetPath instanceof Response) {
return $targetPath;
}
Expand Down
6 changes: 6 additions & 0 deletions Tests/Controller/ImagineControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use Liip\ImagineBundle\Tests\AbstractTest;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;

/**
Expand Down Expand Up @@ -100,6 +101,11 @@ public function testFilterActionLive()
$controller = new ImagineController($dataManager, $filterManager, $cacheManager);

$request = Request::create('/media/cache/thumbnail/cats.jpeg');

$container = new Container();
$container->set('request', $request);
$webPathResolver->setContainer($container);

$response = $controller->filterAction($request, 'cats.jpeg', 'thumbnail');

$targetPath = realpath($this->webRoot).'/media/cache/thumbnail/cats.jpeg';
Expand Down
9 changes: 4 additions & 5 deletions Tests/Imagine/Cache/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,25 @@ public function testResolveInvalidPath($path)
$cacheManager = new CacheManager($this->getMockFilterConfiguration(), $this->getMockRouter(), $this->fixturesDir.'/assets');

$this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
$cacheManager->resolve(new Request(), $path, 'thumbnail');
$cacheManager->resolve($path, 'thumbnail');
}

public function testResolveWithoutResolver()
{
$cacheManager = new CacheManager($this->getMockFilterConfiguration(), $this->getMockRouter(), $this->fixturesDir.'/assets');

$this->assertFalse($cacheManager->resolve(new Request(), 'cats.jpeg', 'thumbnail'));
$this->assertFalse($cacheManager->resolve('cats.jpeg', 'thumbnail'));
}

public function testFallbackToDefaultResolver()
{
$response = new Response('', 200);
$request = new Request();

$resolver = $this->getMockResolver();
$resolver
->expects($this->once())
->method('resolve')
->with($request, 'cats.jpeg', 'thumbnail')
->with('cats.jpeg', 'thumbnail')
->will($this->returnValue('/thumbs/cats.jpeg'))
;
$resolver
Expand Down Expand Up @@ -140,7 +139,7 @@ public function testFallbackToDefaultResolver()
$cacheManager->addResolver('default', $resolver);

// Resolve fallback to default resolver
$this->assertEquals('/thumbs/cats.jpeg', $cacheManager->resolve($request, 'cats.jpeg', 'thumbnail'));
$this->assertEquals('/thumbs/cats.jpeg', $cacheManager->resolve('cats.jpeg', 'thumbnail'));

// Store fallback to default resolver
$this->assertEquals($response, $cacheManager->store($response, '/thumbs/cats.jpeg', 'thumbnail'));
Expand Down
4 changes: 2 additions & 2 deletions Tests/Imagine/Cache/Resolver/AmazonS3ResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function testResolveNewObject()
;

$resolver = new AmazonS3Resolver($s3, 'images.example.com');
$targetPath = $resolver->resolve(new Request(), '/some-folder/targetpath.jpg', 'thumb');
$targetPath = $resolver->resolve('/some-folder/targetpath.jpg', 'thumb');

$this->assertEquals('thumb/some-folder/targetpath.jpg', $targetPath);
}
Expand All @@ -170,7 +170,7 @@ public function testResolveRedirectsOnExisting()
;

$resolver = new AmazonS3Resolver($s3, 'images.example.com');
$response = $resolver->resolve(new Request(), '/some-folder/targetpath.jpg', 'thumb');
$response = $resolver->resolve('/some-folder/targetpath.jpg', 'thumb');

$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$this->assertEquals(301, $response->getStatusCode());
Expand Down
4 changes: 2 additions & 2 deletions Tests/Imagine/Cache/Resolver/AwsS3ResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function testResolveNewObject()
;

$resolver = new AwsS3Resolver($s3, 'images.example.com');
$targetPath = $resolver->resolve(new Request(), '/some-folder/targetpath.jpg', 'thumb');
$targetPath = $resolver->resolve('/some-folder/targetpath.jpg', 'thumb');

$this->assertEquals('thumb/some-folder/targetpath.jpg', $targetPath);
}
Expand All @@ -181,7 +181,7 @@ public function testResolveRedirectsOnExisting()
;

$resolver = new AwsS3Resolver($s3, 'images.example.com');
$response = $resolver->resolve(new Request(), '/some-folder/targetpath.jpg', 'thumb');
$response = $resolver->resolve('/some-folder/targetpath.jpg', 'thumb');

$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$this->assertEquals(301, $response->getStatusCode());
Expand Down
17 changes: 6 additions & 11 deletions Tests/Imagine/Cache/Resolver/CacheResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Liip\ImagineBundle\Tests\AbstractTest;
use Liip\ImagineBundle\Tests\Fixtures\MemoryCache;

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

/**
Expand All @@ -21,23 +20,21 @@ class CacheResolverTest extends AbstractTest

public function testResolveIsSavedToCache()
{
$request = new Request();

$resolver = $this->getMockResolver();
$resolver
->expects($this->once())
->method('resolve')
->with($request, $this->path, $this->filter)
->with($this->path, $this->filter)
->will($this->returnValue($this->targetPath))
;

$cacheResolver = new CacheResolver(new MemoryCache(), $resolver);

$this->assertEquals($this->targetPath, $cacheResolver->resolve($request, $this->path, $this->filter));
$this->assertEquals($this->targetPath, $cacheResolver->resolve($this->path, $this->filter));

// Call multiple times to verify the cache is used.
$this->assertEquals($this->targetPath, $cacheResolver->resolve($request, $this->path, $this->filter));
$this->assertEquals($this->targetPath, $cacheResolver->resolve($request, $this->path, $this->filter));
$this->assertEquals($this->targetPath, $cacheResolver->resolve($this->path, $this->filter));
$this->assertEquals($this->targetPath, $cacheResolver->resolve($this->path, $this->filter));
}

public function testStoreIsForwardedToResolver()
Expand Down Expand Up @@ -92,13 +89,11 @@ public function testGetBrowserPath()
*/
public function testRemoveUsesIndex()
{
$request = new Request();

$resolver = $this->getMockResolver();
$resolver
->expects($this->once())
->method('resolve')
->with($request, $this->path, $this->filter)
->with($this->path, $this->filter)
->will($this->returnValue($this->targetPath))
;
$resolver
Expand All @@ -110,7 +105,7 @@ public function testRemoveUsesIndex()
$cache = new MemoryCache();

$cacheResolver = new CacheResolver($cache, $resolver);
$cacheResolver->resolve($request, $this->path, $this->filter);
$cacheResolver->resolve($this->path, $this->filter);

/*
* Three items:
Expand Down
Loading