Skip to content

Commit

Permalink
Merge pull request #687 from formapro-forks/proxy-resolver-allow-find…
Browse files Browse the repository at this point in the history
…-and-replace-and-regexp-strategies

Proxy resolver allow find and replace and regexp strategies
  • Loading branch information
makasim committed Dec 29, 2015
2 parents 2bb76e6 + 35c4e76 commit 2f6bf50
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function addConfiguration(ArrayNodeDefinition $builder)
->end()
->arrayNode('proxies')
->defaultValue(array())
->prototype('scalar')->end()
->prototype('array')->end()
->end()
->end()
;
Expand Down
19 changes: 16 additions & 3 deletions Imagine/Cache/Resolver/ProxyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,22 @@ protected function rewriteUrl($url)
return $url;
}

$host = parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST);
$proxyHost = $this->hosts[rand(0, count($this->hosts) - 1)];
$randKey = array_rand($this->hosts, 1);

return str_replace($host, $proxyHost, $url);
// BC
if (is_numeric($randKey)) {
$host = parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST);
$proxyHost = $this->hosts[$randKey];

return str_replace($host, $proxyHost, $url);
}

if (0 === strpos($randKey, 'regexp/')) {
$regExp = substr($randKey, 6);

return preg_replace($regExp, $this->hosts[$randKey], $url);
}

return str_replace($randKey, $this->hosts[$randKey], $url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,39 @@ public function testWrapResolverWithProxyAndCacheOnCreate()
$this->assertEquals('liip_imagine.cache.resolver.theresolvername.cached', $resolverDefinition->getArgument(1));
}

public function testWrapResolverWithProxyMatchReplaceStrategyOnCreate()
{
$container = new ContainerBuilder();

$resolver = new AwsS3ResolverFactory();

$resolver->create($container, 'theResolverName', array(
'client_config' => array(),
'bucket' => 'aBucket',
'acl' => 'aAcl',
'url_options' => array(),
'get_options' => array(),
'put_options' => array(),
'cache' => 'theCacheServiceId',
'proxies' => array('foo' => 'bar'),
));

$this->assertTrue($container->hasDefinition('liip_imagine.cache.resolver.theresolvername.proxied'));
$proxiedResolverDefinition = $container->getDefinition('liip_imagine.cache.resolver.theresolvername.proxied');
$this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $proxiedResolverDefinition);
$this->assertEquals('liip_imagine.cache.resolver.prototype.aws_s3', $proxiedResolverDefinition->getParent());

$this->assertTrue($container->hasDefinition('liip_imagine.cache.resolver.theresolvername.cached'));
$cachedResolverDefinition = $container->getDefinition('liip_imagine.cache.resolver.theresolvername.cached');
$this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $cachedResolverDefinition);
$this->assertEquals('liip_imagine.cache.resolver.prototype.proxy', $cachedResolverDefinition->getParent());

$this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $cachedResolverDefinition->getArgument(0));
$this->assertEquals('liip_imagine.cache.resolver.theresolvername.proxied', $cachedResolverDefinition->getArgument(0));

$this->assertEquals(array('foo' => 'bar'), $cachedResolverDefinition->getArgument(1));
}

public function testSetCachePrefixIfDefined()
{
$container = new ContainerBuilder();
Expand Down
8 changes: 4 additions & 4 deletions Tests/Functional/Controller/ImagineControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,20 @@ public function testShouldResolveWithCustomFiltersFromCache()
public function testShouldResolvePathWithSpecialCharactersAndWhiteSpaces()
{
$this->filesystem->dumpFile(
$this->cacheRoot.'/thumbnail_web_path/images/mačací obrázok.jpeg',
$this->cacheRoot.'/thumbnail_web_path/images/foo bar.jpeg',
'anImageContent'
);

// we are calling url with encoded file name as it will be called by browser
$urlEncodedFileName = 'ma%C4%8Dac%C3%AD+obr%C3%A1zok';
$urlEncodedFileName = 'foo+bar';
$this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/images/'.$urlEncodedFileName.'.jpeg');

$response = $this->client->getResponse();

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

$this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/mačací obrázok.jpeg');
$this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/foo bar.jpeg');
}
}
2 changes: 1 addition & 1 deletion Tests/Functional/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public static function getKernelClass()
{
require_once __DIR__.'/app/AppKernel.php';

return 'AppKernel';
return 'Liip\ImagineBundle\Tests\Functional\app\AppKernel';
}
}
6 changes: 4 additions & 2 deletions Tests/Functional/app/AppKernel.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Liip\ImagineBundle\Tests\Functional\app;

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

Expand All @@ -11,8 +13,8 @@ class AppKernel extends Kernel
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Liip\ImagineBundle\LiipImagineBundle(),
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \Liip\ImagineBundle\LiipImagineBundle(),
);

return $bundles;
Expand Down
Binary file not shown.
42 changes: 42 additions & 0 deletions Tests/Imagine/Cache/Resolver/ProxyResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,48 @@ public function testProxyCallAndRewriteReturnedUrlEvenSchemesDiffersOnResolve()
$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result);
}

public function testProxyCallAndRewriteReturnedUrlWithMatchReplaceOnResolve()
{
$expectedPath = '/foo/bar/bazz.png';
$expectedFilter = 'test';

$this->primaryResolver
->expects($this->once())
->method('resolve')
->with($expectedPath, $expectedFilter)
->will($this->returnValue('https://s3-eu-west-1.amazonaws.com/s3-cache.example.com/thumbs/foo/bar/bazz.png'))
;

$this->resolver = new ProxyResolver($this->primaryResolver, array(
'https://s3-eu-west-1.amazonaws.com/s3-cache.example.com' => 'http://images.example.com',
));

$result = $this->resolver->resolve($expectedPath, $expectedFilter);

$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result);
}

public function testProxyCallAndRewriteReturnedUrlWithRegExpOnResolve()
{
$expectedPath = '/foo/bar/bazz.png';
$expectedFilter = 'test';

$this->primaryResolver
->expects($this->once())
->method('resolve')
->with($expectedPath, $expectedFilter)
->will($this->returnValue('http://foo.com/thumbs/foo/bar/bazz.png'))
;

$this->resolver = new ProxyResolver($this->primaryResolver, array(
'regexp/http:\/\/.*?\//' => 'http://bar.com/',
));

$result = $this->resolver->resolve($expectedPath, $expectedFilter);

$this->assertEquals('http://bar.com/thumbs/foo/bar/bazz.png', $result);
}

public function testProxyCallAndReturnedValueOnIsStored()
{
$expectedPath = 'thePath';
Expand Down

0 comments on commit 2f6bf50

Please sign in to comment.