<commit_before><?php

namespace Oro\Bundle\PlatformBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

use Oro\Component\Config\Loader\CumulativeConfigLoader;
use Oro\Component\Config\Loader\YamlCumulativeFileLoader;
use Oro\Component\DependencyInjection\ExtendedContainerBuilder;
use Oro\Component\PhpUtils\ArrayUtil;

use Oro\Bundle\EntityBundle\ORM\DatabaseDriverInterface;

class OroPlatformExtension extends Extension implements PrependExtensionInterface
{
    /**
     * {@inheritdoc}
     */
    public function prepend(ContainerBuilder $container)
    {
        $configLoader = new CumulativeConfigLoader(
            'oro_app_config',
            new YamlCumulativeFileLoader('Resources/config/oro/app.yml')
        );

        $securityModified = false;
        $securityConfigs = [];
        $extensions = $container->getExtensions();

        //bundles that are loaded later should be able to override configuration of bundles loaded before
        $resources = array_reverse($configLoader->load());
        foreach ($resources as $resource) {
            foreach ($resource->data as $name => $config) {
                if (empty($extensions[$name])) {
                    continue;
                }
                if ($name === 'security') {
                    $securityConfigs[] = $config;
                    $securityModified = true;
                } else {
                    $container->prependExtensionConfig($name, $config);
                }
            }
        }

        if ($securityModified) {
            $securityConfigs = array_reverse($securityConfigs);
            foreach ($securityConfigs as $config) {
                $this->mergeConfigIntoOne($container, 'security', $config);
            }
        }

        // original security config has highest priority
        if ($container->hasExtension('security')) {
            $securityConfig = $container->getExtensionConfig('security');
            $this->mergeConfigIntoOne($container, 'security', reset($securityConfig));
        }

        $this->preparePostgreSql($container);
    }

    /**
     * Enable ATTR_EMULATE_PREPARES for PostgreSQL connections to avoid https://bugs.php.net/bug.php?id=36652
     *
     * @param ContainerBuilder $container
     */
    public function preparePostgreSql(ContainerBuilder $container)
    {
        $dbDriver = $container->getParameter('database_driver');
        if ($dbDriver === DatabaseDriverInterface::DRIVER_POSTGRESQL) {
            $doctrineConfig = $container->getExtensionConfig('doctrine');
            $doctrineConnectionOptions = [];<commit_msg>CRM-5209: Incorrect merge app.yml config with custom config<commit_after><?php

namespace Oro\Bundle\PlatformBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

use Oro\Component\Config\Loader\CumulativeConfigLoader;
use Oro\Component\Config\Loader\YamlCumulativeFileLoader;
use Oro\Component\DependencyInjection\ExtendedContainerBuilder;
use Oro\Component\PhpUtils\ArrayUtil;

use Oro\Bundle\EntityBundle\ORM\DatabaseDriverInterface;

class OroPlatformExtension extends Extension implements PrependExtensionInterface
{
    /**
     * {@inheritdoc}
     */
    public function prepend(ContainerBuilder $container)
    {
        $configLoader = new CumulativeConfigLoader(
            'oro_app_config',
            new YamlCumulativeFileLoader('Resources/config/oro/app.yml')
        );

        // original security config
        $securityConfig = null;
        if ($container->hasExtension('security')) {
            $securityConfig = $container->getExtensionConfig('security');
        }

        $securityModified = false;
        $securityConfigs = [];

        $extensions = $container->getExtensions();

        //bundles that are loaded later should be able to override configuration of bundles loaded before
        $resources = array_reverse($configLoader->load());
        foreach ($resources as $resource) {
            foreach ($resource->data as $name => $config) {
                if (empty($extensions[$name])) {
                    continue;
                }
                if ($name === 'security') {
                    $securityConfigs[] = $config;
                    $securityModified = true;
                } else {
                    $container->prependExtensionConfig($name, $config);
                }
            }
        }

        if ($securityModified) {
            $securityConfigs = array_reverse($securityConfigs);
            foreach ($securityConfigs as $config) {
                $this->mergeConfigIntoOne($container, 'security', $config);
            }
        }

        // original security config has highest priority
        if ($securityConfig) {
            $this->mergeConfigIntoOne($container, 'security', reset($securityConfig));
        }

        $this->preparePostgreSql($container);
    }

    /**
     * Enable ATTR_EMULATE_PREPARES for PostgreSQL connections to avoid https://bugs.php.net/bug.php?id=36652
     *
     * @param ContainerBuilder $container
     */
    public function preparePostgreSql(ContainerBuilder $container)
    {
        $dbDriver = $container->getParameter('database_driver');
        if ($dbDriver === DatabaseDriverInterface::DRIVER_POSTGRESQL) {
            $doctrineConfig = $container->getExtensionConfig('doctrine');
            $doctrineConnectionOptions = [];
{'fraction_non_alphanumeric': 0.10446747069814846, 'fraction_numerical': 0.0023781212841854932, 'mean_word_length': 2.7384126984126986, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '984509', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1441, 'n_words': 278}