<?php

namespace Nelmio\SecurityBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class CspReportFilterCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $services = $container->findTaggedServiceIds('nelmio_security.csp_report_filter');

        $cspViolationLogFilterDefinition = $container->getDefinition('nelmio_security.csp_report.filter');

        foreach ($services as $id => $attributes) {
            $cspViolationLogFilterDefinition->addMethodCall('addNoiseDetector', array(new Reference($id)));
        }
    }
}

<?php

namespace Nelmio\SecurityBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class CspReportFilterCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        if (!$container->hasDefinition('nelmio_security.csp_report.filter')) {
            return;
        }
        
        $services = $container->findTaggedServiceIds('nelmio_security.csp_report_filter');

        $cspViolationLogFilterDefinition = $container->getDefinition('nelmio_security.csp_report.filter');

        foreach ($services as $id => $attributes) {
            $cspViolationLogFilterDefinition->addMethodCall('addNoiseDetector', array(new Reference($id)));
        }
    }
}

Abort CSP compiler pass when CSP is not enabled
Abort CSP compiler pass when CSP is not enabled

When csp enabled is set to false in the configuration csp.xml will not be loaded,
so there is no service `nelmio_security.csp_report.filter` and asking the definition
for that non-existing service results in an exception.

So first we check if the service is available in the container and if it's not we bail out
(return early).