Skip to content

Commit 6aae06d

Browse files
authored
Merge pull request #569 from tft7000/fix_servers_from_env_vars
Fix servers from env vars
2 parents ca640e7 + a939aee commit 6aae06d

File tree

9 files changed

+93
-29
lines changed

9 files changed

+93
-29
lines changed

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ private function getHttpDispatcherNode()
547547
->requiresAtLeastOneElement()
548548
->prototype('scalar')->end()
549549
->end()
550-
->scalarNode('servers_from_jsonenv')
550+
->variableNode('servers_from_jsonenv')
551551
->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.')
552552
->end()
553553
->scalarNode('base_url')
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace FOS\HttpCacheBundle\Tests\Functional\DependencyInjection;
4+
5+
use FOS\HttpCache\ProxyClient\HttpDispatcher;
6+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
7+
use Symfony\Component\DependencyInjection\Container;
8+
use Symfony\Component\Filesystem\Filesystem;
9+
use Symfony\Component\HttpKernel\KernelInterface;
10+
11+
class ServersFromEnvTest extends KernelTestCase
12+
{
13+
/**
14+
* Boots a special kernel with a compiler pass to make all services public for this test.
15+
*
16+
* @return KernelInterface A KernelInterface instance
17+
*/
18+
protected function bootDebugKernel()
19+
{
20+
static::ensureKernelShutdown();
21+
static::$kernel = static::createKernel();
22+
assert(static::$kernel instanceof \AppKernel);
23+
static::$kernel->addCompilerPass(new ServicesPublicPass());
24+
$fs = new Filesystem();
25+
$fs->remove(static::$kernel->getCacheDir());
26+
static::$kernel->boot();
27+
28+
return static::$kernel;
29+
}
30+
31+
public function testServersFromEnv()
32+
{
33+
// define the kernel config to use for this test
34+
$_ENV['KERNEL_CONFIG'] = 'config_servers_from_env.yml';
35+
36+
// test env var as json string, that will get deserialized and injected into http dispatcher
37+
$_ENV['VARNISH_SERVERS'] = '["localhost:123","https://any.host:456"]';
38+
39+
/** @var Container $container */
40+
$container = $this->bootDebugKernel()->getContainer();
41+
42+
/** @var HttpDispatcher $fosHttpCache */
43+
$fosHttpCache = $container->get('fos_http_cache.proxy_client.varnish.http_dispatcher');
44+
45+
$reflectionObject = new \ReflectionClass($fosHttpCache);
46+
$reflectionGetServers = $reflectionObject->getMethod('getServers');
47+
$reflectionGetServers->setAccessible(true);
48+
$uris = $reflectionGetServers->invoke($fosHttpCache);
49+
$servers = array_map(function ($uri) { return $uri->__toString(); }, $uris);
50+
51+
static::assertEquals(['http://localhost:123', 'https://any.host:456'], $servers);
52+
53+
// unset env vars, so next tests do not fail (KERNEL_CONFIG)
54+
unset($_ENV['KERNEL_CONFIG'], $_ENV['VARNISH_SERVERS']);
55+
}
56+
}

tests/Functional/DependencyInjection/ServiceTest.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
namespace FOS\HttpCacheBundle\Tests\Functional\DependencyInjection;
1313

1414
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15-
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1615
use Symfony\Component\DependencyInjection\Container;
17-
use Symfony\Component\DependencyInjection\ContainerBuilder;
1816
use Symfony\Component\Filesystem\Filesystem;
1917
use Symfony\Component\HttpKernel\KernelInterface;
2018

@@ -62,20 +60,3 @@ public function testCanBeLoaded()
6260
}
6361
}
6462
}
65-
66-
class ServicesPublicPass implements CompilerPassInterface
67-
{
68-
public function process(ContainerBuilder $container)
69-
{
70-
foreach ($container->getServiceIds() as $id) {
71-
if (strncmp('fos_http_cache.', $id, 15)) {
72-
continue;
73-
}
74-
if ($container->hasDefinition($id)) {
75-
$container->getDefinition($id)->setPublic(true);
76-
} elseif ($container->hasAlias($id)) {
77-
$container->getAlias($id)->setPublic(true);
78-
}
79-
}
80-
}
81-
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace FOS\HttpCacheBundle\Tests\Functional\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
8+
class ServicesPublicPass implements CompilerPassInterface
9+
{
10+
public function process(ContainerBuilder $container)
11+
{
12+
foreach ($container->getServiceIds() as $id) {
13+
if (strncmp('fos_http_cache.', $id, 15)) {
14+
continue;
15+
}
16+
if ($container->hasDefinition($id)) {
17+
$container->getDefinition($id)->setPublic(true);
18+
} elseif ($container->hasAlias($id)) {
19+
$container->getAlias($id)->setPublic(true);
20+
}
21+
}
22+
}
23+
}

tests/Functional/Fixtures/app/AppKernel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public function registerBundles()
5656
*/
5757
public function registerContainerConfiguration(LoaderInterface $loader)
5858
{
59+
if (isset($_ENV['KERNEL_CONFIG']) && $_ENV['KERNEL_CONFIG']) {
60+
$loader->load(__DIR__.'/config/'.$_ENV['KERNEL_CONFIG']);
61+
} else {
62+
$loader->load(__DIR__.'/config/config.yml');
63+
}
5964
if (\version_compare(Kernel::VERSION, '5.0', '>=')) {
6065
$loader->load(__DIR__.'/config/config_50.yml');
6166
} elseif (\version_compare(Kernel::VERSION, '4.1', '>=')) {
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
imports:
2-
- { resource: config.yml }
3-
41
framework:
52
router:
63
resource: "%kernel.root_dir%/config/routing.yml"

tests/Functional/Fixtures/app/config/config_41.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# configuration to make symfony 4.1 work as expected
22

3-
imports:
4-
- { resource: config.yml }
5-
63
framework:
74
router:
85
resource: "%kernel.project_dir%/tests/Functional/Fixtures/app/config/routing_41.yml"

tests/Functional/Fixtures/app/config/config_50.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# configuration to make symfony 5.0 work as expected
22

3-
imports:
4-
- { resource: config.yml }
5-
63
framework:
74
router:
85
resource: "%kernel.project_dir%/tests/Functional/Fixtures/app/config/routing_41.yml"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
framework:
2+
secret: fos
3+
4+
fos_http_cache:
5+
proxy_client:
6+
varnish:
7+
http:
8+
servers_from_jsonenv: '%env(json:VARNISH_SERVERS)%'

0 commit comments

Comments
 (0)