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

Allow to configure the HTTP response code for redirects #970

Merged
merged 1 commit into from
Aug 31, 2017
Merged
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
23 changes: 16 additions & 7 deletions Controller/ImagineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,32 @@ class ImagineController
protected $logger;

/**
* @param DataManager $dataManager
* @param FilterManager $filterManager
* @param CacheManager $cacheManager
* @param SignerInterface $signer
* @var int
*/
protected $redirectResponseCode;

/**
* @param DataManager $dataManager
* @param FilterManager $filterManager
* @param CacheManager $cacheManager
* @param SignerInterface $signer
* @param LoggerInterface|null $logger
* @param int $redirectResponseCode
*/
public function __construct(
DataManager $dataManager,
FilterManager $filterManager,
CacheManager $cacheManager,
SignerInterface $signer,
LoggerInterface $logger = null
LoggerInterface $logger = null,
$redirectResponseCode = 301
) {
$this->dataManager = $dataManager;
$this->filterManager = $filterManager;
$this->cacheManager = $cacheManager;
$this->signer = $signer;
$this->logger = $logger;
$this->redirectResponseCode = $redirectResponseCode;
}

/**
Expand Down Expand Up @@ -109,7 +118,7 @@ public function filterAction(Request $request, $path, $filter)
);
}

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

Expand Down Expand Up @@ -177,7 +186,7 @@ public function filterRuntimeAction(Request $request, $hash, $path, $filter)
$resolver
);

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

Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public function getConfigTreeBuilder()
->children()
->scalarNode('filter_action')->defaultValue('liip_imagine.controller:filterAction')->end()
->scalarNode('filter_runtime_action')->defaultValue('liip_imagine.controller:filterRuntimeAction')->end()
->scalarNode('redirect_response_code')->defaultValue(301)->end()
->end()
->end()
->arrayNode('filter_sets')
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/LiipImagineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter('liip_imagine.controller.filter_action', $config['controller']['filter_action']);
$container->setParameter('liip_imagine.controller.filter_runtime_action', $config['controller']['filter_runtime_action']);
$container->setParameter('liip_imagine.controller.redirect_response_code', $config['controller']['redirect_response_code']);

$resources = $container->hasParameter('twig.form.resources') ? $container->getParameter('twig.form.resources') : array();
$resources[] = 'LiipImagineBundle:Form:form_div_layout.html.twig';
Expand Down
1 change: 1 addition & 0 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<argument type="service" id="liip_imagine.cache.manager" />
<argument type="service" id="liip_imagine.cache.signer" />
<argument type="service" id="logger" on-invalid="ignore" />
<argument>%liip_imagine.controller.redirect_response_code%</argument>
</service>

<service id="liip_imagine.meta_data.reader" class="%liip_imagine.meta_data.reader.class%" public="false" />
Expand Down
55 changes: 54 additions & 1 deletion Tests/Controller/ImagineControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Liip\ImagineBundle\Controller\ImagineController;
use Liip\ImagineBundle\Tests\AbstractTest;
use Symfony\Component\HttpFoundation\Request;

/**
* @covers \Liip\ImagineBundle\Controller\ImagineController
Expand All @@ -26,7 +27,59 @@ public function testConstruction()
$this->createFilterManagerMock(),
$this->createCacheManagerMock(),
$this->createSignerInterfaceMock(),
$this->createLoggerInterfaceMock()
$this->createLoggerInterfaceMock(),
301
);
}

public function testRedirectCodeIsConfigurable()
{
$redirectResponseCode = 307;
$path = '/foo';
$filter = 'filter';
$binary = $this->createObjectMock('\Liip\ImagineBundle\Model\Binary');
$hash = 'hash';

$dataManager = $this->createDataManagerMock();
$dataManager
->method('find')
->with($filter, $path)
->willReturn($binary);

$filterManager = $this->createFilterManagerMock();
$filterManager
->method('applyFilter')
->with($binary, $filter)
->willReturn($binary);

$cacheManager = $this->createCacheManagerMock();
$cacheManager
->method('resolve')
->willReturn($path, $filter)
->willReturn('/target');

$signer = $this->createSignerInterfaceMock();
$signer
->expects($this->once())
->method('check')
->with($hash, $path, array())
->willReturn(true);

$controller = new ImagineController(
$dataManager,
$filterManager,
$cacheManager,
$signer,
$this->createLoggerInterfaceMock(),
$redirectResponseCode
);

$response = $controller->filterAction(new Request(), $path, $filter);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertSame($redirectResponseCode, $response->getStatusCode());

$response = $controller->filterRuntimeAction(new Request(), $hash, $path, $filter);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertSame($redirectResponseCode, $response->getStatusCode());
}
}
1 change: 1 addition & 0 deletions Tests/DependencyInjection/LiipImagineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function testLoadWithDefaults()
new Reference('liip_imagine.cache.manager'),
new Reference('liip_imagine.cache.signer'),
new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
'%liip_imagine.controller.redirect_response_code%'
)
);
}
Expand Down
10 changes: 5 additions & 5 deletions Tests/Functional/Controller/ImagineControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testShouldResolvePopulatingCacheFirst()
$response = $this->client->getResponse();

$this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertEquals(301, $response->getStatusCode());
$this->assertEquals(302, $response->getStatusCode());
$this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $response->getTargetUrl());

$this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
Expand All @@ -55,7 +55,7 @@ public function testShouldResolveFromCache()
$response = $this->client->getResponse();

$this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertEquals(301, $response->getStatusCode());
$this->assertEquals(302, $response->getStatusCode());
$this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $response->getTargetUrl());

$this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
Expand Down Expand Up @@ -131,7 +131,7 @@ public function testShouldResolveWithCustomFiltersPopulatingCacheFirst()
$response = $this->client->getResponse();

$this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertEquals(301, $response->getStatusCode());
$this->assertEquals(302, $response->getStatusCode());
$this->assertEquals('http://localhost/media/cache/'.$expectedCachePath, $response->getTargetUrl());

$this->assertFileExists($this->cacheRoot.'/'.$expectedCachePath);
Expand Down Expand Up @@ -166,7 +166,7 @@ public function testShouldResolveWithCustomFiltersFromCache()
$response = $this->client->getResponse();

$this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertEquals(301, $response->getStatusCode());
$this->assertEquals(302, $response->getStatusCode());
$this->assertEquals('http://localhost/media/cache'.'/'.$expectedCachePath, $response->getTargetUrl());

$this->assertFileExists($this->cacheRoot.'/'.$expectedCachePath);
Expand All @@ -186,7 +186,7 @@ public function testShouldResolvePathWithSpecialCharactersAndWhiteSpaces()
$response = $this->client->getResponse();

$this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertEquals(301, $response->getStatusCode());
$this->assertEquals(302, $response->getStatusCode());
$this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/foo bar.jpeg', $response->getTargetUrl());

$this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/foo bar.jpeg');
Expand Down
4 changes: 3 additions & 1 deletion Tests/Functional/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ liip_imagine:
filters:
thumbnail: { size: [223, 223], mode: inset }

...

controller:
redirect_response_code: 302