<commit_before><?php

namespace Http\HttplugBundle\DependencyInjection;

use Http\Client\Common\BatchClient;
use Http\Client\Common\BatchClientInterface;
use Http\Client\Common\FlexibleHttpClient;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\HttpMethodsClientInterface;
use Http\Client\Common\Plugin\AuthenticationPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\Common\PluginClientFactory;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Message\Authentication\BasicAuth;
use Http\Message\Authentication\Bearer;
use Http\Message\Authentication\QueryParam;
use Http\Message\Authentication\Wsse;
use Http\Mock\Client as MockClient;
use Psr\Http\Message\UriInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Twig\Environment as TwigEnvironment;

/**
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class HttplugExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = $this->getConfiguration($configs, $container);
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

        $loader->load('services.xml');
        $loader->load('plugins.xml');
        if (\class_exists(MockClient::class)) {
            $loader->load('mock-client.xml');
        }

        // Register default services
        foreach ($config['classes'] as $service => $class) {
            if (!empty($class)) {
                $container->register(sprintf('httplug.%s.default', $service), $class);
            }
        }

        // Set main aliases
        foreach ($config['main_alias'] as $type => $id) {
            $container->setAlias(sprintf('httplug.%s', $type), new Alias($id, true));
        }

        // Configure toolbar
        $profilingEnabled = $this->isConfigEnabled($container, $config['profiling']);
        if ($profilingEnabled) {
            $loader->load('data-collector.xml');

            if (!empty($config['profiling']['formatter'])) {
                // Add custom formatter
                $container
                    ->getDefinition('httplug.collector.formatter')
                    ->replaceArgument(0, new Reference($config['profiling']['formatter']))
                ;
            }

            $container
                ->getDefinition('httplug.formatter.full_http_message')
                ->addArgument($config['profiling']['captured_body_length'])
            ;

            if (!class_exists(TwigEnvironment::class) && !class_exists(\Twig_Environment::class)) {
                $container->removeDefinition('httplug.collector.twig.http_message');
            }
        }

        $this->configureClients($container, $config);
        $this->configurePlugins($container, $config['plugins']); // must be after clients, as clients.X.plugins might use plugins as templates that will be removed
        $this->configureAutoDiscoveryClients($container, $config);

        if (!$config['default_client_autowiring']) {
            $container->removeAlias(HttpAsyncClient::class);
            $container->removeAlias(HttpClient::class);
        }
    }

    /**
     * Configure client services.
     *
     * @param ContainerBuilder $container
     * @param array            $config
     */
    private function configureClients(ContainerBuilder $container, array $config)
    {
        $first = null;
        $clients = [];

        foreach ($config['clients'] as $name => $arguments) {
            if (null === $first) {
                // Save the name of the first configured client.
                $first = $name;
            }

            $this->configureClient($container, $name, $arguments);
            $clients[] = $name;
        }

        // If we have clients configured
        if (null !== $first) {
            // If we do not have a client named 'default'
            if (!array_key_exists('default', $config['clients'])) {
                $serviceId = 'httplug.client.'.$first;
                // Alias the first client to httplug.client.default
                $container->setAlias('httplug.client.default', $serviceId);
                $default = $first;
            } else {
                $default = 'default';
            }

            // Autowiring alias for special clients, if they are enabled on the default client
            if ($config['clients'][$default]['flexible_client']) {
                $container->setAlias(FlexibleHttpClient::class, $serviceId.'.flexible');
            }
            if ($config['clients'][$default]['http_methods_client']) {
                if (\interface_exists(HttpMethodsClientInterface::class)) {
                    // support for client-common 1.9
                    $container->setAlias(HttpMethodsClientInterface::class, $serviceId.'.http_methods');
                }
            }
            if ($config['clients'][$default]['batch_client']) {
                if (\interface_exists(BatchClientInterface::class)) {
                    // support for client-common 1.9
                    $container->setAlias(BatchClientInterface::class, $serviceId.'.batch_client');
                }
            }
        }
    }

    /**
     * Configure all Httplug plugins or remove their service definition.<commit_msg>Fix error on undefined variable when a client is named default in config<commit_after><?php

namespace Http\HttplugBundle\DependencyInjection;

use Http\Client\Common\BatchClient;
use Http\Client\Common\BatchClientInterface;
use Http\Client\Common\FlexibleHttpClient;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\HttpMethodsClientInterface;
use Http\Client\Common\Plugin\AuthenticationPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\Common\PluginClientFactory;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Message\Authentication\BasicAuth;
use Http\Message\Authentication\Bearer;
use Http\Message\Authentication\QueryParam;
use Http\Message\Authentication\Wsse;
use Http\Mock\Client as MockClient;
use Psr\Http\Message\UriInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Twig\Environment as TwigEnvironment;

/**
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class HttplugExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = $this->getConfiguration($configs, $container);
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

        $loader->load('services.xml');
        $loader->load('plugins.xml');
        if (\class_exists(MockClient::class)) {
            $loader->load('mock-client.xml');
        }

        // Register default services
        foreach ($config['classes'] as $service => $class) {
            if (!empty($class)) {
                $container->register(sprintf('httplug.%s.default', $service), $class);
            }
        }

        // Set main aliases
        foreach ($config['main_alias'] as $type => $id) {
            $container->setAlias(sprintf('httplug.%s', $type), new Alias($id, true));
        }

        // Configure toolbar
        $profilingEnabled = $this->isConfigEnabled($container, $config['profiling']);
        if ($profilingEnabled) {
            $loader->load('data-collector.xml');

            if (!empty($config['profiling']['formatter'])) {
                // Add custom formatter
                $container
                    ->getDefinition('httplug.collector.formatter')
                    ->replaceArgument(0, new Reference($config['profiling']['formatter']))
                ;
            }

            $container
                ->getDefinition('httplug.formatter.full_http_message')
                ->addArgument($config['profiling']['captured_body_length'])
            ;

            if (!class_exists(TwigEnvironment::class) && !class_exists(\Twig_Environment::class)) {
                $container->removeDefinition('httplug.collector.twig.http_message');
            }
        }

        $this->configureClients($container, $config);
        $this->configurePlugins($container, $config['plugins']); // must be after clients, as clients.X.plugins might use plugins as templates that will be removed
        $this->configureAutoDiscoveryClients($container, $config);

        if (!$config['default_client_autowiring']) {
            $container->removeAlias(HttpAsyncClient::class);
            $container->removeAlias(HttpClient::class);
        }
    }

    /**
     * Configure client services.
     *
     * @param ContainerBuilder $container
     * @param array            $config
     */
    private function configureClients(ContainerBuilder $container, array $config)
    {
        $first = null;
        $clients = [];

        foreach ($config['clients'] as $name => $arguments) {
            if (null === $first) {
                // Save the name of the first configured client.
                $first = $name;
            }

            $this->configureClient($container, $name, $arguments);
            $clients[] = $name;
        }

        // If we have clients configured
        if (null !== $first) {
            // If we do not have a client named 'default'
            if (!array_key_exists('default', $config['clients'])) {
                $serviceId = 'httplug.client.'.$first;
                // Alias the first client to httplug.client.default
                $container->setAlias('httplug.client.default', $serviceId);
                $default = $first;
            } else {
                $default = 'default';
                $serviceId = 'httplug.client.'.$default;
            }

            // Autowiring alias for special clients, if they are enabled on the default client
            if ($config['clients'][$default]['flexible_client']) {
                $container->setAlias(FlexibleHttpClient::class, $serviceId.'.flexible');
            }
            if ($config['clients'][$default]['http_methods_client']) {
                if (\interface_exists(HttpMethodsClientInterface::class)) {
                    // support for client-common 1.9
                    $container->setAlias(HttpMethodsClientInterface::class, $serviceId.'.http_methods');
                }
            }
            if ($config['clients'][$default]['batch_client']) {
                if (\interface_exists(BatchClientInterface::class)) {
                    // support for client-common 1.9
                    $container->setAlias(BatchClientInterface::class, $serviceId.'.batch_client');
                }
            }
        }
    }

    /**
     * Configure all Httplug plugins or remove their service definition.
{'fraction_non_alphanumeric': 0.12757862698681097, 'fraction_numerical': 0.0008454514710855597, 'mean_word_length': 2.527885475693409, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 69, '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': '7381928', 'n_tokens_mistral': 3104, 'n_tokens_neox': 2986, 'n_words': 593}