Skip to content

Commit 08f63a4

Browse files
committed
Merge pull request #202 from FriendsOfSymfony/3.0-compatibility
Better Symfony 2.7/3.0 compatibility
2 parents b7d5319 + 8c04c04 commit 08f63a4

File tree

6 files changed

+73
-7
lines changed

6 files changed

+73
-7
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCacheBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCacheBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* In Symfony < 2.6, replace the new security.token_storage service with the
20+
* deprecated security.context service.
21+
*/
22+
class SecurityContextPass implements CompilerPassInterface
23+
{
24+
const ROLE_PROVIDER_SERVICE = 'fos_http_cache.user_context.role_provider';
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function process(ContainerBuilder $container)
30+
{
31+
if (!$container->has(self::ROLE_PROVIDER_SERVICE)) {
32+
return;
33+
}
34+
35+
if (!$container->has('security.token_storage')) {
36+
$definition = $container->getDefinition(self::ROLE_PROVIDER_SERVICE);
37+
$definition->replaceArgument(0, new Reference('security.context'));
38+
}
39+
}
40+
}

FOSHttpCacheBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace FOS\HttpCacheBundle;
1313

1414
use FOS\HttpCacheBundle\DependencyInjection\Compiler\LoggerPass;
15+
use FOS\HttpCacheBundle\DependencyInjection\Compiler\SecurityContextPass;
1516
use FOS\HttpCacheBundle\DependencyInjection\Compiler\TagSubscriberPass;
1617
use FOS\HttpCacheBundle\DependencyInjection\Compiler\HashGeneratorPass;
1718
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -25,6 +26,7 @@ class FOSHttpCacheBundle extends Bundle
2526
public function build(ContainerBuilder $container)
2627
{
2728
$container->addCompilerPass(new LoggerPass());
29+
$container->addCompilerPass(new SecurityContextPass());
2830
$container->addCompilerPass(new TagSubscriberPass());
2931
$container->addCompilerPass(new HashGeneratorPass());
3032
}

Resources/config/user_context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</service>
3232

3333
<service id="fos_http_cache.user_context.role_provider" class="%fos_http_cache.user_context.role_provider.class%" abstract="true">
34-
<argument type="service" id="security.context" on-invalid="ignore" />
34+
<argument type="service" id="security.token_storage" on-invalid="ignore" />
3535
</service>
3636

3737
<service id="fos_http_cache.user_context.logout_handler" class="%fos_http_cache.user_context.logout_handler.class%">

Tests/Functional/Fixtures/app/config/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ framework:
55
test: ~
66
session:
77
storage_id: session.test_storage
8+
# We need to specify templating because of a bug in Symfony:
9+
# see https://github.com/symfony/symfony/issues/13710
10+
templating:
11+
engines: ['php']
812

913
fos_http_cache:
1014
cache_control:

Tests/Unit/UserContext/RoleProviderTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public function testProvider()
2222
$roles = array(new Role('ROLE_USER'));
2323

2424
$token = \Mockery::mock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
25-
$securityContext = \Mockery::mock('\Symfony\Component\Security\Core\SecurityContext');
26-
25+
26+
$securityContext = $this->getTokenStorageMock();
2727
$securityContext->shouldReceive('getToken')->andReturn($token);
2828
$token->shouldReceive('getRoles')->andReturn($roles);
2929

@@ -39,8 +39,7 @@ public function testProvider()
3939

4040
public function testProviderWithoutToken()
4141
{
42-
$securityContext = \Mockery::mock('\Symfony\Component\Security\Core\SecurityContext');
43-
42+
$securityContext = $this->getTokenStorageMock();
4443
$securityContext->shouldReceive('getToken')->andReturn(null);
4544

4645
$userContext = new UserContext();
@@ -59,4 +58,15 @@ public function testNotUnderFirewall()
5958
$roleProvider = new RoleProvider();
6059
$roleProvider->updateUserContext(new UserContext());
6160
}
61+
62+
private function getTokenStorageMock()
63+
{
64+
if (interface_exists('\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
65+
// Symfony >= 2.6
66+
return \Mockery::mock('\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
67+
}
68+
69+
// Symfony < 2.6 compatibility
70+
return \Mockery::mock('\Symfony\Component\Security\Core\SecurityContext');
71+
}
6272
}

UserContext/RoleProvider.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use FOS\HttpCache\UserContext\ContextProviderInterface;
1515
use FOS\HttpCache\UserContext\UserContext;
1616
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
17+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1718
use Symfony\Component\Security\Core\Role\RoleInterface;
1819
use Symfony\Component\Security\Core\SecurityContextInterface;
1920

@@ -34,10 +35,19 @@ class RoleProvider implements ContextProviderInterface
3435
* firewall. It is however not valid to call updateUserContext when not in
3536
* a firewall context.
3637
*
37-
* @param SecurityContextInterface|null $context
38+
* @param SecurityContextInterface|TokenStorageInterface $context
3839
*/
39-
public function __construct(SecurityContextInterface $context = null)
40+
public function __construct($context = null)
4041
{
42+
if ($context
43+
&& !$context instanceof SecurityContextInterface
44+
&& !$context instanceof TokenStorageInterface
45+
) {
46+
throw new \InvalidArgumentException(
47+
'Context must implement either TokenStorageInterface or SecurityContextInterface'
48+
);
49+
}
50+
4151
$this->context = $context;
4252
}
4353

0 commit comments

Comments
 (0)