Skip to content

Commit cfc5f7b

Browse files
authored
Merge pull request #235 from symfony-cmf/routing-bundle-optional
do not load publish workflow request listener if routing is not availble
2 parents 4b5fcee + 26b668c commit cfc5f7b

File tree

4 files changed

+187
-15
lines changed

4 files changed

+187
-15
lines changed

src/DependencyInjection/CmfCoreExtension.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,7 @@ public function load(array $configs, ContainerBuilder $container)
263263
'%cmf_core.persistence.phpcr.manager_name%',
264264
]);
265265
}
266-
if ($config['publish_workflow']['enabled']) {
267-
$this->loadPublishWorkflow($config['publish_workflow'], $loader, $container);
268-
} else {
269-
$loader->load('no-publish-workflow.xml');
270-
}
266+
$this->loadPublishWorkflow($config['publish_workflow'], $loader, $container);
271267

272268
if (isset($config['multilang'])) {
273269
$container->setParameter($this->getAlias().'.multilang.locales', $config['multilang']['locales']);
@@ -315,19 +311,39 @@ public function setupFormTypes(ContainerBuilder $container, LoaderInterface $loa
315311
*/
316312
private function loadPublishWorkflow($config, XmlFileLoader $loader, ContainerBuilder $container)
317313
{
314+
$bundles = $container->getParameter('kernel.bundles');
315+
316+
if (false === $config['enabled']
317+
|| ('auto' === $config['enabled'] && !array_key_exists('SecurityBundle', $bundles))
318+
) {
319+
$loader->load('no-publish-workflow.xml');
320+
321+
return;
322+
}
323+
324+
if (!array_key_exists('SecurityBundle', $bundles)) {
325+
throw new InvalidConfigurationException(
326+
'The "publish_workflow" may not be enabled unless "symfony/security-bundle" is available.'
327+
);
328+
}
329+
318330
$container->setParameter($this->getAlias().'.publish_workflow.view_non_published_role', $config['view_non_published_role']);
319331
$loader->load('publish-workflow.xml');
320332

321-
if (!$config['request_listener']) {
333+
$container->setAlias('cmf_core.publish_workflow.checker', $config['checker_service']);
334+
335+
if (false === $config['request_listener']
336+
|| ('auto' === $config['request_listener'] && !class_exists(DynamicRouter::class))
337+
) {
322338
$container->removeDefinition($this->getAlias().'.publish_workflow.request_listener');
323-
} elseif (!class_exists(DynamicRouter::class)) {
324-
throw new InvalidConfigurationException(sprintf(
325-
'The "publish_workflow.request_listener" may not be enabled unless "%s" is available.',
326-
DynamicRouter::class
327-
));
328-
}
329339

330-
$container->setAlias('cmf_core.publish_workflow.checker', $config['checker_service']);
340+
return;
341+
}
342+
if (!class_exists(DynamicRouter::class)) {
343+
throw new InvalidConfigurationException(
344+
'The "publish_workflow.request_listener" may not be enabled unless "symfony-cmf/routing-bundle" is available.'
345+
);
346+
}
331347
}
332348

333349
/**

src/DependencyInjection/Configuration.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,28 @@ public function getConfigTreeBuilder()
6060
->end()
6161
->arrayNode('publish_workflow')
6262
->addDefaultsIfNotSet()
63-
->canBeDisabled()
63+
->treatFalseLike(['enabled' => false])
64+
->treatTrueLike(['enabled' => true])
65+
->treatNullLike(['enabled' => 'auto'])
66+
->beforeNormalization()
67+
->ifArray()
68+
->then(function ($v) {
69+
$v['enabled'] = isset($v['enabled']) ? $v['enabled'] : true;
70+
71+
return $v;
72+
})
73+
->end()
6474
->children()
75+
->enumNode('enabled')
76+
->values([true, false, 'auto'])
77+
->defaultValue('auto')
78+
->end()
6579
->scalarNode('checker_service')->defaultValue('cmf_core.publish_workflow.checker.default')->end()
6680
->scalarNode('view_non_published_role')->defaultValue('ROLE_CAN_VIEW_NON_PUBLISHED')->end()
67-
->booleanNode('request_listener')->defaultTrue()->end()
81+
->enumNode('request_listener')
82+
->values([true, false, 'auto'])
83+
->defaultValue('auto')
84+
->end()
6885
->end()
6986
->end()
7087
->end()

tests/Functional/DependencyInjection/CmfCoreExtensionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
class CmfCoreExtensionTest extends \PHPUnit_Framework_TestCase
1919
{
20+
/**
21+
* @var ContainerBuilder
22+
*/
2023
private $container;
2124

2225
protected function setUp()
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2017 Symfony CMF
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\Cmf\Bundle\CoreBundle\Tests\Unit\DependencyInjection;
13+
14+
use Symfony\Bundle\SecurityBundle\SecurityBundle;
15+
use Symfony\Cmf\Bundle\CoreBundle\DependencyInjection\CmfCoreExtension;
16+
use Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle;
17+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Definition;
20+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
21+
use Symfony\Component\Routing\Router;
22+
23+
class CmfCoreExtensionTest extends \PHPUnit_Framework_TestCase
24+
{
25+
/**
26+
* @var CmfCoreExtension
27+
*/
28+
protected $extension;
29+
30+
protected function setUp()
31+
{
32+
$this->extension = new CmfCoreExtension();
33+
}
34+
35+
public function testPublishWorkflowAutoSupported()
36+
{
37+
$container = $this->createContainer(['kernel.bundles' => ['SecurityBundle' => SecurityBundle::class]]);
38+
39+
$this->extension->load([['publish_workflow' => ['request_listener' => false]]], $container);
40+
41+
$this->assertTrue($container->hasAlias('cmf_core.publish_workflow.checker'));
42+
$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.checker.default'));
43+
$this->assertTrue($container->hasDefinition('cmf_core.security.publishable_voter'));
44+
$this->assertTrue($container->hasDefinition('cmf_core.security.publish_time_period_voter'));
45+
$this->assertFalse($container->hasDefinition('cmf_core.publish_workflow.request_listener'));
46+
}
47+
48+
public function testPublishWorkflowListenerEnabled()
49+
{
50+
$container = $this->createContainer(['kernel.bundles' => [
51+
'SecurityBundle' => SecurityBundle::class,
52+
'CmfRoutingBundle' => CmfRoutingBundle::class,
53+
]]);
54+
55+
$this->extension->load([], $container);
56+
57+
$this->assertTrue($container->hasAlias('cmf_core.publish_workflow.checker'));
58+
$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.checker.default'));
59+
$this->assertTrue($container->hasDefinition('cmf_core.security.publishable_voter'));
60+
$this->assertTrue($container->hasDefinition('cmf_core.security.publish_time_period_voter'));
61+
$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.request_listener'));
62+
}
63+
64+
public function testPublishWorkflowAutoNotSupported()
65+
{
66+
$container = $this->createContainer(['kernel.bundles' => []]);
67+
68+
$this->extension->load([], $container);
69+
70+
$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.checker'));
71+
$this->assertFalse($container->hasAlias('cmf_core.publish_workflow.checker'));
72+
$this->assertFalse($container->hasDefinition('cmf_core.publish_workflow.checker.default'));
73+
$this->assertFalse($container->hasDefinition('cmf_core.security.publishable_voter'));
74+
$this->assertFalse($container->hasDefinition('cmf_core.security.publish_time_period_voter'));
75+
$this->assertFalse($container->hasDefinition('cmf_core.publish_workflow.request_listener'));
76+
}
77+
78+
public function testPublishWorkflowFalse()
79+
{
80+
$container = $this->createContainer(['kernel.bundles' => [
81+
'SecurityBundle' => SecurityBundle::class,
82+
'CmfRoutingBundle' => CmfRoutingBundle::class,
83+
]]);
84+
85+
$this->extension->load([['publish_workflow' => false]], $container);
86+
87+
$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.checker'));
88+
$this->assertFalse($container->hasAlias('cmf_core.publish_workflow.checker'));
89+
$this->assertFalse($container->hasDefinition('cmf_core.publish_workflow.checker.default'));
90+
$this->assertFalse($container->hasDefinition('cmf_core.security.publishable_voter'));
91+
$this->assertFalse($container->hasDefinition('cmf_core.security.publish_time_period_voter'));
92+
$this->assertFalse($container->hasDefinition('cmf_core.publish_workflow.request_listener'));
93+
}
94+
95+
public function testPublishWorkflowTrueSupported()
96+
{
97+
$container = $this->createContainer(['kernel.bundles' => [
98+
'SecurityBundle' => SecurityBundle::class,
99+
'CmfRoutingBundle' => CmfRoutingBundle::class,
100+
]]);
101+
102+
$this->extension->load([['publish_workflow' => true]], $container);
103+
104+
$this->assertTrue($container->hasAlias('cmf_core.publish_workflow.checker'));
105+
$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.checker.default'));
106+
$this->assertTrue($container->hasDefinition('cmf_core.security.publishable_voter'));
107+
$this->assertTrue($container->hasDefinition('cmf_core.security.publish_time_period_voter'));
108+
$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.request_listener'));
109+
}
110+
111+
public function testPublishWorkflowTrueNotSupported()
112+
{
113+
$container = $this->createContainer(['kernel.bundles' => [
114+
'CmfRoutingBundle' => CmfRoutingBundle::class,
115+
]]);
116+
117+
$this->expectException(InvalidConfigurationException::class);
118+
$this->extension->load([['publish_workflow' => true]], $container);
119+
}
120+
121+
private function createContainer(array $parameters)
122+
{
123+
$parameters = array_merge(['kernel.debug' => false], $parameters);
124+
$container = new ContainerBuilder(
125+
new ParameterBag($parameters)
126+
);
127+
128+
// The cache_manager service depends on the router service
129+
$container->setDefinition(
130+
'router',
131+
new Definition(Router::class)
132+
);
133+
134+
return $container;
135+
}
136+
}

0 commit comments

Comments
 (0)