Skip to content

Commit

Permalink
Merge pull request #569 from tft7000/fix_servers_from_env_vars
Browse files Browse the repository at this point in the history
Fix servers from env vars
  • Loading branch information
dbu committed Nov 8, 2021
2 parents ca640e7 + a939aee commit 6aae06d
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ private function getHttpDispatcherNode()
->requiresAtLeastOneElement()
->prototype('scalar')->end()
->end()
->scalarNode('servers_from_jsonenv')
->variableNode('servers_from_jsonenv')
->info('Addresses of the hosts the caching proxy is running on (env var that contains a json array as a string). The values may be hostnames or ips, and with :port if not the default port 80.')
->end()
->scalarNode('base_url')
Expand Down
56 changes: 56 additions & 0 deletions tests/Functional/DependencyInjection/ServersFromEnvTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace FOS\HttpCacheBundle\Tests\Functional\DependencyInjection;

use FOS\HttpCache\ProxyClient\HttpDispatcher;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\KernelInterface;

class ServersFromEnvTest extends KernelTestCase
{
/**
* Boots a special kernel with a compiler pass to make all services public for this test.
*
* @return KernelInterface A KernelInterface instance
*/
protected function bootDebugKernel()
{
static::ensureKernelShutdown();
static::$kernel = static::createKernel();
assert(static::$kernel instanceof \AppKernel);
static::$kernel->addCompilerPass(new ServicesPublicPass());
$fs = new Filesystem();
$fs->remove(static::$kernel->getCacheDir());
static::$kernel->boot();

return static::$kernel;
}

public function testServersFromEnv()
{
// define the kernel config to use for this test
$_ENV['KERNEL_CONFIG'] = 'config_servers_from_env.yml';

// test env var as json string, that will get deserialized and injected into http dispatcher
$_ENV['VARNISH_SERVERS'] = '["localhost:123","https://any.host:456"]';

/** @var Container $container */
$container = $this->bootDebugKernel()->getContainer();

/** @var HttpDispatcher $fosHttpCache */
$fosHttpCache = $container->get('fos_http_cache.proxy_client.varnish.http_dispatcher');

$reflectionObject = new \ReflectionClass($fosHttpCache);
$reflectionGetServers = $reflectionObject->getMethod('getServers');
$reflectionGetServers->setAccessible(true);
$uris = $reflectionGetServers->invoke($fosHttpCache);
$servers = array_map(function ($uri) { return $uri->__toString(); }, $uris);

static::assertEquals(['http://localhost:123', 'https://any.host:456'], $servers);

// unset env vars, so next tests do not fail (KERNEL_CONFIG)
unset($_ENV['KERNEL_CONFIG'], $_ENV['VARNISH_SERVERS']);
}
}
19 changes: 0 additions & 19 deletions tests/Functional/DependencyInjection/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
namespace FOS\HttpCacheBundle\Tests\Functional\DependencyInjection;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\KernelInterface;

Expand Down Expand Up @@ -62,20 +60,3 @@ public function testCanBeLoaded()
}
}
}

class ServicesPublicPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
foreach ($container->getServiceIds() as $id) {
if (strncmp('fos_http_cache.', $id, 15)) {
continue;
}
if ($container->hasDefinition($id)) {
$container->getDefinition($id)->setPublic(true);
} elseif ($container->hasAlias($id)) {
$container->getAlias($id)->setPublic(true);
}
}
}
}
23 changes: 23 additions & 0 deletions tests/Functional/DependencyInjection/ServicesPublicPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace FOS\HttpCacheBundle\Tests\Functional\DependencyInjection;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ServicesPublicPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
foreach ($container->getServiceIds() as $id) {
if (strncmp('fos_http_cache.', $id, 15)) {
continue;
}
if ($container->hasDefinition($id)) {
$container->getDefinition($id)->setPublic(true);
} elseif ($container->hasAlias($id)) {
$container->getAlias($id)->setPublic(true);
}
}
}
}
5 changes: 5 additions & 0 deletions tests/Functional/Fixtures/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function registerBundles()
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
if (isset($_ENV['KERNEL_CONFIG']) && $_ENV['KERNEL_CONFIG']) {
$loader->load(__DIR__.'/config/'.$_ENV['KERNEL_CONFIG']);
} else {
$loader->load(__DIR__.'/config/config.yml');
}
if (\version_compare(Kernel::VERSION, '5.0', '>=')) {
$loader->load(__DIR__.'/config/config_50.yml');
} elseif (\version_compare(Kernel::VERSION, '4.1', '>=')) {
Expand Down
3 changes: 0 additions & 3 deletions tests/Functional/Fixtures/app/config/config3.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
imports:
- { resource: config.yml }

framework:
router:
resource: "%kernel.root_dir%/config/routing.yml"
3 changes: 0 additions & 3 deletions tests/Functional/Fixtures/app/config/config_41.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# configuration to make symfony 4.1 work as expected

imports:
- { resource: config.yml }

framework:
router:
resource: "%kernel.project_dir%/tests/Functional/Fixtures/app/config/routing_41.yml"
Expand Down
3 changes: 0 additions & 3 deletions tests/Functional/Fixtures/app/config/config_50.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# configuration to make symfony 5.0 work as expected

imports:
- { resource: config.yml }

framework:
router:
resource: "%kernel.project_dir%/tests/Functional/Fixtures/app/config/routing_41.yml"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
framework:
secret: fos

fos_http_cache:
proxy_client:
varnish:
http:
servers_from_jsonenv: '%env(json:VARNISH_SERVERS)%'

0 comments on commit 6aae06d

Please sign in to comment.