<commit_before><?php

namespace Algatux\InfluxDbBundle\DependencyInjection;

use Algatux\InfluxDbBundle\Events\Listeners\InfluxDbEventListener;
use InfluxDB\Database;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
 * This is the class that loads and manages your bundle configuration.
 *
 * @see http://symfony.com/doc/current/cookbook/bundles/extension.html
 */
final class InfluxDbExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('factory.xml');
        $loader->load('registry.xml');
        $loader->load('command.xml');

        // If the default connection if not defined, get the first one.
        $defaultConnection = isset($config['default_connection']) ? $config['default_connection'] : key($config['connections']);
        $this->buildConnections($container, $config, $defaultConnection);

        $this->setDefaultConnectionAlias($container, $defaultConnection);

        if (interface_exists(FormInterface::class)) {
            $loader->load('form.xml');
        }

        return $config;
    }

    /**
     * @param ContainerBuilder $container
     * @param string           $connection The connection name
     * @param array            $config     The connection configuration
     * @param string           $protocol   The connection protocol ('http' or 'udp')
     */
    private function createConnection(ContainerBuilder $container, $connection, array $config, $protocol)
    {
        // Create the connection based from the abstract one.
        $connectionDefinition = new Definition(Database::class, [
            $config['database'],
            $config['host'],
            $config['http_port'],
            $config['udp_port'],
            $config['username'],
            $config['password'],
            'udp' === $protocol,
        ]);
        $connectionDefinition->setFactory([new Reference('algatux_influx_db.connection_factory'), 'createConnection']);
        $connectionDefinition->setPublic(true);

        // E.g.: algatux_influx_db.connection.default.http
        $connectionServiceName = 'algatux_influx_db.connection.'.$connection.'.'.$protocol;
        $container->setDefinition($connectionServiceName, $connectionDefinition);

        // Add the connection to the registry
        $container->getDefinition('algatux_influx_db.connection_registry')
            ->addMethodCall('addConnection', [$connection, $protocol, new Reference($connectionServiceName)]);
    }

    /**
     * @param ContainerBuilder $container
     * @param string           $connection
     * @param string           $defaultConnection
     */
    private function createConnectionListener(ContainerBuilder $container, $connection, string $defaultConnection)
    {
        $listenerArguments = [
            $connection,
            $connection === $defaultConnection,
            new Reference('algatux_influx_db.connection.'.$connection.'.http'),
        ];
        if ($container->hasDefinition('algatux_influx_db.connection.'.$connection.'.udp')) {
            array_push($listenerArguments, new Reference('algatux_influx_db.connection.'.$connection.'.udp'));<commit_msg>Set connection definition to be lazy<commit_after><?php

namespace Algatux\InfluxDbBundle\DependencyInjection;

use Algatux\InfluxDbBundle\Events\Listeners\InfluxDbEventListener;
use InfluxDB\Database;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
 * This is the class that loads and manages your bundle configuration.
 *
 * @see http://symfony.com/doc/current/cookbook/bundles/extension.html
 */
final class InfluxDbExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('factory.xml');
        $loader->load('registry.xml');
        $loader->load('command.xml');

        // If the default connection if not defined, get the first one.
        $defaultConnection = isset($config['default_connection']) ? $config['default_connection'] : key($config['connections']);
        $this->buildConnections($container, $config, $defaultConnection);

        $this->setDefaultConnectionAlias($container, $defaultConnection);

        if (interface_exists(FormInterface::class)) {
            $loader->load('form.xml');
        }

        return $config;
    }

    /**
     * @param ContainerBuilder $container
     * @param string           $connection The connection name
     * @param array            $config     The connection configuration
     * @param string           $protocol   The connection protocol ('http' or 'udp')
     */
    private function createConnection(ContainerBuilder $container, $connection, array $config, $protocol)
    {
        // Create the connection based from the abstract one.
        $connectionDefinition = new Definition(Database::class, [
            $config['database'],
            $config['host'],
            $config['http_port'],
            $config['udp_port'],
            $config['username'],
            $config['password'],
            'udp' === $protocol,
        ]);
        $connectionDefinition->setFactory([new Reference('algatux_influx_db.connection_factory'), 'createConnection']);
        $connectionDefinition->setPublic(true);
        $connectionDefinition->setLazy(true);

        // E.g.: algatux_influx_db.connection.default.http
        $connectionServiceName = 'algatux_influx_db.connection.'.$connection.'.'.$protocol;
        $container->setDefinition($connectionServiceName, $connectionDefinition);

        // Add the connection to the registry
        $container->getDefinition('algatux_influx_db.connection_registry')
            ->addMethodCall('addConnection', [$connection, $protocol, new Reference($connectionServiceName)]);
    }

    /**
     * @param ContainerBuilder $container
     * @param string           $connection
     * @param string           $defaultConnection
     */
    private function createConnectionListener(ContainerBuilder $container, $connection, string $defaultConnection)
    {
        $listenerArguments = [
            $connection,
            $connection === $defaultConnection,
            new Reference('algatux_influx_db.connection.'.$connection.'.http'),
        ];
        if ($container->hasDefinition('algatux_influx_db.connection.'.$connection.'.udp')) {
            array_push($listenerArguments, new Reference('algatux_influx_db.connection.'.$connection.'.udp'));
{'fraction_non_alphanumeric': 0.12234804112976702, 'fraction_numerical': 0.0, 'mean_word_length': 3.3217097862767155, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '2226464', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1919, 'n_words': 379}