diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 73236ce..7fdcd44 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -50,6 +50,9 @@ public function getConfigTreeBuilder() }) ->end() ->end() + ->scalarNode('proxy_host_filter_service') + ->defaultNull() + ->end() ->end() ->end() ->end() diff --git a/src/DependencyInjection/PHPMentorsProxyURLRewriteExtension.php b/src/DependencyInjection/PHPMentorsProxyURLRewriteExtension.php index 7991192..18a02ec 100644 --- a/src/DependencyInjection/PHPMentorsProxyURLRewriteExtension.php +++ b/src/DependencyInjection/PHPMentorsProxyURLRewriteExtension.php @@ -51,7 +51,7 @@ private function transformConfigToContainer(array $config, ContainerBuilder $con if ($config['enabled']) { foreach ($config['proxy_urls'] as $id => $proxyUrl) { $definition = new DefinitionDecorator('phpmentors_proxy_url_rewrite.proxy_url'); - $definition->setArguments(array($id, $proxyUrl['path'], $proxyUrl['proxy_url'])); + $definition->setArguments(array($id, $proxyUrl['path'], $proxyUrl['proxy_url'], $proxyUrl['proxy_host_filter_service'] === null ? null : new Reference($proxyUrl['proxy_host_filter_service']))); $serviceId = 'phpmentors_proxy_url_rewrite.proxy_url.'.sha1($id); $container->setDefinition($serviceId, $definition); diff --git a/src/ProxyUrl/ProxyHostFilterInterface.php b/src/ProxyUrl/ProxyHostFilterInterface.php new file mode 100644 index 0000000..7d42d48 --- /dev/null +++ b/src/ProxyUrl/ProxyHostFilterInterface.php @@ -0,0 +1,28 @@ +, + * All rights reserved. + * + * This file is part of PHPMentorsProxyURLRewriteBundle. + * + * This program and the accompanying materials are made available under + * the terms of the BSD 2-Clause License which accompanies this + * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause + */ + +namespace PHPMentors\ProxyURLRewriteBundle\ProxyUrl; + +use PHPMentors\DomainKata\Service\ServiceInterface; + +/** + * @since Interface available since Release 1.2.0 + */ +interface ProxyHostFilterInterface extends ServiceInterface +{ + /** + * @param string + * + * @return string + */ + public function filter($host); +} diff --git a/src/ProxyUrl/ProxyUrlFactory.php b/src/ProxyUrl/ProxyUrlFactory.php index e87ac27..b1478af 100644 --- a/src/ProxyUrl/ProxyUrlFactory.php +++ b/src/ProxyUrl/ProxyUrlFactory.php @@ -17,16 +17,21 @@ class ProxyUrlFactory implements ServiceInterface { /** - * @param int|string $proxyUrlId - * @param string $path - * @param string $proxyUrl + * @param int|string $proxyUrlId + * @param string $path + * @param string $proxyUrl + * @param ProxyHostFilterInterface $hostFilterService * * @return ProxyUrl */ - public function create($proxyUrlId, $path, $proxyUrl) + public function create($proxyUrlId, $path, $proxyUrl, ProxyHostFilterInterface $proxyHostFilter = null) { list($proxyUrlPath, $proxyUrlHost, $proxyUrlScheme, $proxyUrlPort) = static::parseUrl($proxyUrl); + if ($proxyHostFilter !== null) { + $proxyUrlHost = $proxyHostFilter->filter($proxyUrlHost); + } + return new ProxyUrl($proxyUrlId, $path, $proxyUrlPath, $proxyUrlHost, $proxyUrlScheme, $proxyUrlPort); } diff --git a/tests/Functional/Bundle/TestBundle/ProxyUrl/ProxyHostFilter.php b/tests/Functional/Bundle/TestBundle/ProxyUrl/ProxyHostFilter.php new file mode 100644 index 0000000..c983c97 --- /dev/null +++ b/tests/Functional/Bundle/TestBundle/ProxyUrl/ProxyHostFilter.php @@ -0,0 +1,33 @@ +, + * All rights reserved. + * + * This file is part of PHPMentorsProxyURLRewriteBundle. + * + * This program and the accompanying materials are made available under + * the terms of the BSD 2-Clause License which accompanies this + * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause + */ + +namespace PHPMentors\ProxyURLRewriteBundle\Functional\Bundle\TestBundle\ProxyUrl; + +use PHPMentors\ProxyURLRewriteBundle\ProxyUrl\ProxyHostFilterInterface; + +/** + * @since Class available since Release 1.2.0 + */ +class ProxyHostFilter implements ProxyHostFilterInterface +{ + /** + * {@inheritdoc} + */ + public function filter($host) + { + if ($host === null) { + return null; + } + + return 'baz.'.$host; + } +} diff --git a/tests/Functional/HostFilterTest.php b/tests/Functional/HostFilterTest.php new file mode 100644 index 0000000..950df21 --- /dev/null +++ b/tests/Functional/HostFilterTest.php @@ -0,0 +1,114 @@ +, + * All rights reserved. + * + * This file is part of PHPMentorsProxyURLRewriteBundle. + * + * This program and the accompanying materials are made available under + * the terms of the BSD 2-Clause License which accompanies this + * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause + */ + +namespace PHPMentors\ProxyURLRewriteBundle\Functional; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; + +/** + * @since Class available since Release 1.2.0 + */ +class HostFilterTest extends WebTestCase +{ + /** + * {@inheritdoc} + */ + protected function setUp() + { + parent::setUp(); + + $_SERVER['KERNEL_DIR'] = __DIR__.'/app'; + $_SERVER['SYMFONY__SECRET'] = hash('sha1', uniqid(mt_rand())); + + $this->removeCacheDir(); + } + + /** + * {@inheritdoc} + */ + protected function tearDown() + { + parent::tearDown(); + + $this->removeCacheDir(); + } + + /** + * {@inheritdoc} + */ + protected static function createKernel(array $options = array()) + { + $kernel = KernelTestCase::createKernel($options); + if (array_key_exists('config', $options)) { + $kernel->setConfig($options['config']); + } + + return $kernel; + } + + protected function removeCacheDir() + { + $fileSystem = new Filesystem(); + $fileSystem->remove($_SERVER['KERNEL_DIR'].'/cache/test'); + } + + public function filterData() + { + return array( + array('/foo/bar/', true, 'http://backend1.example.com/foo/bar/url-rewriting-in-controllers/'), + array('/foo/bar/', false, 'http://backend1.example.com/foo/bar/url-rewriting-in-controllers/'), + array('//example.com/foo/bar/', true, 'http://baz.example.com/foo/bar/url-rewriting-in-controllers/'), + array('//example.com/foo/bar/', false, 'http://example.com/foo/bar/url-rewriting-in-controllers/'), + array('http://example.com/foo/bar/', true, 'http://baz.example.com/foo/bar/url-rewriting-in-controllers/'), + array('http://example.com/foo/bar/', false, 'http://example.com/foo/bar/url-rewriting-in-controllers/'), + array('https://example.com/foo/bar/', true, 'https://baz.example.com/foo/bar/url-rewriting-in-controllers/'), + array('https://example.com/foo/bar/', false, 'https://example.com/foo/bar/url-rewriting-in-controllers/'), + array('http://example.com:8180/foo/bar/', true, 'http://baz.example.com:8180/foo/bar/url-rewriting-in-controllers/'), + array('http://example.com:8180/foo/bar/', false, 'http://example.com:8180/foo/bar/url-rewriting-in-controllers/'), + ); + } + + /** + * @test + * @dataProvider filterData + * + * @param string $proxyUrl + * @param bool $proxyHostFilterService + * @param string $rewroteUrl + */ + public function filter($proxyUrl, $proxyHostFilterService, $rewroteUrl) + { + $client = $this->createClient(array('config' => function (ContainerBuilder $container) use ($proxyUrl, $proxyHostFilterService) { + $config = array( + 'path' => '!^.*!', + 'proxy_url' => $proxyUrl, + ); + if ($proxyHostFilterService) { + $config['proxy_host_filter_service'] = 'phpmentors_proxy_url_rewrite_test.proxy_host_filter'; + } + + $container->loadFromExtension('phpmentors_proxy_url_rewrite', array( + 'proxy_urls' => array( + 'foo' => $config, + ), )); + })); + + $client->request('GET', sprintf('http://backend1.example.com:8080/url-rewriting-in-controllers/?referenceType=%s', UrlGeneratorInterface::ABSOLUTE_URL)); + + $this->assertThat($client->getResponse()->getStatusCode(), $this->equalTo(200), $client->getResponse()->getContent()); + $this->assertThat($client->getCrawler()->filterXpath("//*[@id='generateUrl']")->text(), $this->equalTo($rewroteUrl)); + } +} diff --git a/tests/Functional/app/config/config.yml b/tests/Functional/app/config/config.yml index 9a00b0c..b0b39d0 100644 --- a/tests/Functional/app/config/config.yml +++ b/tests/Functional/app/config/config.yml @@ -1,3 +1,6 @@ +imports: + - { resource: services.yml } + framework: secret: "%secret%" router: diff --git a/tests/Functional/app/config/services.yml b/tests/Functional/app/config/services.yml new file mode 100644 index 0000000..403a83e --- /dev/null +++ b/tests/Functional/app/config/services.yml @@ -0,0 +1,6 @@ +parameters: + phpmentors_proxy_url_rewrite_test.proxy_host_filter.class: "PHPMentors\\ProxyURLRewriteBundle\\Functional\\Bundle\\TestBundle\\ProxyUrl\\ProxyHostFilter" + +services: + phpmentors_proxy_url_rewrite_test.proxy_host_filter: + class: "%phpmentors_proxy_url_rewrite_test.proxy_host_filter.class%"