8000 Add a new `disallow_search_engine_index` option in framework bundle · symfony/symfony@29b9d02 · GitHub
[go: up one dir, main page]

Skip to content

Commit 29b9d02

Browse files
committed
Add a new disallow_search_engine_index option in framework bundle
1 parent 2b5b443 commit 29b9d02

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public function getConfigTreeBuilder()
108108
$this->addWebLinkSection($rootNode);
109109
$this->addLockSection($rootNode);
110110
$this->addMessengerSection($rootNode);
111+
$this->addRobotsIndexSection($rootNode);
111112

112113
return $treeBuilder;
113114
}
@@ -1156,4 +1157,18 @@ function ($a) {
11561157
->end()
11571158
;
11581159
}
1160+
1161+
private function addRobotsIndexSection(ArrayNodeDefinition $rootNode)
1162+
{
1163+
$rootNode
1164+
->children()
1165+
->booleanNode('disallow_search_engine_index')
1166+
->info('Ask search engine to avoid indexing your application. Enabled by default when debug is enabled.')
1167+
->example('"true" to enable it. "false" to disable.')
1168+
->defaultValue($this->debug)
1169+
->treatNullLike($this->debug)
1170+
->end()
1171+
->end()
1172+
;
1173+
}
11591174
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,11 @@ public function load(array $configs, ContainerBuilder $container)
394394
// remove tagged iterator argument for resource checkers
395395
$container->getDefinition('config_cache_factory')->setArguments([]);
396396
}
397+
398+
if ($config['disallow_search_engine_index'] ?? false) {
399+
$container->register(DisallowRobotsIndexingListener::class)
400+
->addTag('kernel.event_listener', ['event' => KernelEvents::RESPONSE, 'priority' => -255]);
401+
}
397402
}
398403

399404
/**
@@ -707,8 +712,6 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con
707712

708713
if ($debug) {
709714
$container->setParameter('debug.container.dump', '%kernel.cache_dir%/%kernel.container_class%.xml');
710-
$container->register(DisallowRobotsIndexingListener::class)
711-
->addTag('kernel.event_listener', ['event' => KernelEvents::RESPONSE, 'priority' => -255]);
712715
}
713716

714717
if ($debug && class_exists(Stopwatch::class)) {

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
330330
'default_bus' => null,
331331
'buses' => ['messenger.bus.default' => ['default_middleware' => true, 'middleware' => []]],
332332
],
333+
'disallow_search_engine_index' => true,
333334
];
334335
}
335336
}

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,13 @@ public function testRobotsTagListenerIsRegisteredInDebugMode()
13421342
$definition->getTag('kernel.event_listener')
13431343
);
13441344

1345+
$container = $this->createContainer(['kernel.debug' => true]);
1346+
(new FrameworkExtension())->load([['disallow_search_engine_index' => false]], $container);
1347+
$this->assertFalse(
1348+
$container->has(DisallowRobotsIndexingListener::class),
1349+
'DisallowRobotsIndexingListener should not be registered when explicitly disabled'
1350+
);
1351+
13451352
$container = $this->createContainer(['kernel.debug' => false]);
13461353
(new FrameworkExtension())->load([], $container);
13471354
$this->assertFalse($container->has(DisallowRobotsIndexingListener::class), 'DisallowRobotsIndexingListener should NOT be registered');

0 commit comments

Comments
 (0)
0