Skip to content

Commit 3327326

Browse files
committed
[FrameworkBundle][Routing][Translation][Workflow] Move some compiler passes from FrameworkBundle to components
1 parent df35f90 commit 3327326

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* Add PHP attributes to register listeners and guards
1313
* Deprecate `GuardEvent::getContext()` method that will be removed in 7.0
1414
* Revert: Mark `Symfony\Component\Workflow\Registry` as internal
15+
* Add `WorkflowGuardListenerPass` (moved from `FrameworkBundle`)
1516

1617
6.2
1718
---
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Workflow\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Exception\LogicException;
17+
18+
/**
19+
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
20+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
21+
*/
22+
class WorkflowGuardListenerPass implements CompilerPassInterface
23+
{
24+
public function process(ContainerBuilder $container): void
25+
{
26+
if (!$container->hasParameter('workflow.has_guard_listeners')) {
27+
return;
28+
}
29+
30+
$container->getParameterBag()->remove('workflow.has_guard_listeners');
31+
32+
$servicesNeeded = [
33+
'security.token_storage',
34+
'security.authorization_checker',
35+
'security.authentication.trust_resolver',
36+
'security.role_hierarchy',
37+
];
38+
39+
foreach ($servicesNeeded as $service) {
40+
if (!$container->has($service)) {
41+
throw new LogicException(sprintf('The "%s" service is needed to be able to use the workflow guard listener.', $service));
42+
}
43+
}
44+
}
45+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Workflow\Tests\DependencyInjection;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Exception\LogicException;
17+
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
18+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19+
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
20+
use Symfony\Component\Security\Core\Role\RoleHierarchy;
21+
use Symfony\Component\Validator\Validator\ValidatorInterface;
22+
use Symfony\Component\Workflow\DependencyInjection\WorkflowGuardListenerPass;
23+
24+
class WorkflowGuardListenerPassTest extends TestCase
25+
{
26+
private ContainerBuilder $container;
27+
private WorkflowGuardListenerPass $compilerPass;
28+
29+
protected function setUp(): void
30+
{
31+
$this->container = new ContainerBuilder();
32+
$this->compilerPass = new WorkflowGuardListenerPass();
33+
}
34+
35+
public function testNoExeptionIfParameterIsNotSet()
36+
{
37+
$this->compilerPass->process($this->container);
38+
39+
$this->assertFalse($this->container->hasParameter('workflow.has_guard_listeners'));
40+
}
41+
42+
public function testNoExeptionIfAllDependenciesArePresent()
43+
{
44+
$this->container->setParameter('workflow.has_guard_listeners', true);
45+
$this->container->register('security.token_storage', TokenStorageInterface::class);
46+
$this->container->register('security.authorization_checker', AuthorizationCheckerInterface::class);
47+
$this->container->register('security.authentication.trust_resolver', AuthenticationTrustResolverInterface::class);
48+
$this->container->register('security.role_hierarchy', RoleHierarchy::class);
49+
$this->container->register('validator', ValidatorInterface::class);
50+
51+
$this->compilerPass->process($this->container);
52+
53+
$this->assertFalse($this->container->hasParameter('workflow.has_guard_listeners'));
54+
}
55+
56+
public function testExceptionIfTheTokenStorageServiceIsNotPresent()
57+
{
58+
$this->container->setParameter('workflow.has_guard_listeners', true);
59+
$this->container->register('security.authorization_checker', AuthorizationCheckerInterface::class);
60+
$this->container->register('security.authentication.trust_resolver', AuthenticationTrustResolverInterface::class);
61+
$this->container->register('security.role_hierarchy', RoleHierarchy::class);
62+
63+
$this->expectException(LogicException::class);
64+
$this->expectExceptionMessage('The "security.token_storage" service is needed to be able to use the workflow guard listener.');
65+
66+
$this->compilerPass->process($this->container);
67+
}
68+
69+
public function testExceptionIfTheAuthorizationCheckerServiceIsNotPresent()
70+
{
71+
$this->container->setParameter('workflow.has_guard_listeners', true);
72+
$this->container->register('security.token_storage', TokenStorageInterface::class);
73+
$this->container->register('security.authentication.trust_resolver', AuthenticationTrustResolverInterface::class);
74+
$this->container->register('security.role_hierarchy', RoleHierarchy::class);
75+
76+
$this->expectException(LogicException::class);
77+
$this->expectExceptionMessage('The "security.authorization_checker" service is needed to be able to use the workflow guard listener.');
78+
79+
$this->compilerPass->process($this->container);
80+
}
81+
82+
public function testExceptionIfTheAuthenticationTrustResolverServiceIsNotPresent()
83+
{
84+
$this->container->setParameter('workflow.has_guard_listeners', true);
85+
$this->container->register('security.token_storage', TokenStorageInterface::class);
86+
$this->container->register('security.authorization_checker', AuthorizationCheckerInterface::class);
87+
$this->container->register('security.role_hierarchy', RoleHierarchy::class);
88+
89+
$this->expectException(LogicException::class);
90+
$this->expectExceptionMessage('The "security.authentication.trust_resolver" service is needed to be able to use the workflow guard listener.');
91+
92+
$this->compilerPass->process($this->container);
93+
}
94+
95+
public function testExceptionIfTheRoleHierarchyServiceIsNotPresent()
96+
{
97+
$this->container->setParameter('workflow.has_guard_listeners', true);
98+
$this->container->register('security.token_storage', TokenStorageInterface::class);
99+
$this->container->register('security.authorization_checker', AuthorizationCheckerInterface::class);
100+
$this->container->register('security.authentication.trust_resolver', AuthenticationTrustResolverInterface::class);
101+
102+
$this->expectException(LogicException::class);
103+
$this->expectExceptionMessage('The "security.role_hierarchy" service is needed to be able to use the workflow guard listener.');
104+
105+
$this->compilerPass->process($this->container);
106+
}
107+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy