Skip to content

Commit

Permalink
added fix for Symfony2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalkaoz committed Feb 7, 2014
1 parent db4c9ec commit 13d2d8c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
30 changes: 21 additions & 9 deletions Imagine/Cache/Resolver/ProxyResolver.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php


namespace Liip\ImagineBundle\Imagine\Cache\Resolver;

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

use Symfony\Component\HttpKernel\Kernel;

/**
* ProxyResolver
Expand Down Expand Up @@ -87,12 +86,25 @@ public function clear($cachePrefix)

private function rewriteResponse($response)
{
if ($response instanceof RedirectResponse && $this->hosts) {
$path = parse_url($response->getTargetUrl(), PHP_URL_PATH);
if ($path == $response->getTargetUrl()) {
//relative path, so strip of SCRIPT_FILE_NAME if existient
$path = substr($path, (strpos($path, '.php') !== false ? strpos($path, '.php') + 4 : 0));
}
if (!$response instanceof RedirectResponse || !$this->hosts) {
return;
}

if ('2' == Kernel::MAJOR_VERSION && '0' == Kernel::MINOR_VERSION) {
$redirectLocation = $response->headers->get('Location');
} else {
$redirectLocation = $response->getTargetUrl();
}
$path = parse_url($redirectLocation, PHP_URL_PATH);

if ($path == $redirectLocation) {
//relative path, so strip of SCRIPT_FILE_NAME if existient
$path = substr($path, (strpos($path, '.php') !== false ? strpos($path, '.php') + 4 : 0));
}

if ('2' == Kernel::MAJOR_VERSION && '0' == Kernel::MINOR_VERSION) {
$response->headers->set('Location', $this->createProxyUrl($path));
} else {
$response->setTargetUrl($this->createProxyUrl($path));
}
}
Expand All @@ -103,4 +115,4 @@ private function createProxyUrl($path)

return $domain . $path;
}
}
}
22 changes: 19 additions & 3 deletions Tests/Imagine/Cache/Resolver/ProxyResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel;


/**
Expand Down Expand Up @@ -45,7 +46,12 @@ public function testResolveWithResponse()
$result = $this->resolver->resolve(new Request(), '/foo/bar/bazz.png', 'test');

$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result->getTargetUrl());

if ('2' == Kernel::MAJOR_VERSION && '0' == Kernel::MINOR_VERSION) {
$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result->headers->get('Location'));
} else {
$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result->getTargetUrl());
}
}

public function testResolveWithoutResponse()
Expand All @@ -70,7 +76,12 @@ public function testStoreWithResponse()
$result = $this->resolver->store(new Response(), '/foo/bar/bazz.png', 'test');

$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result->getTargetUrl());

if ('2' == Kernel::MAJOR_VERSION && '0' == Kernel::MINOR_VERSION) {
$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result->headers->get('Location'));
} else {
$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result->getTargetUrl());
}
}

public function testStoreWithoutResponse()
Expand All @@ -95,7 +106,12 @@ public function testGetBrowserPathWithResponse()
$result = $this->resolver->getBrowserPath('/foo/bar/bazz.png', 'test');

$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result->getTargetUrl());

if ('2' == Kernel::MAJOR_VERSION && '0' == Kernel::MINOR_VERSION) {
$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result->headers->get('Location'));
} else {
$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result->getTargetUrl());
}
}

public function testGetBrowserPathWithoutResponse()
Expand Down

0 comments on commit 13d2d8c

Please sign in to comment.