<commit_before><?php

namespace Lsw\MemcacheBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Definition;

/**
 * Applies the configuration for the Memcached object
 * Based on Emagister\MemcachedBundle by <NAME>
 */
class LswMemcacheExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('config.yml');
        $loader->load('services.yml');

        if (isset($config['session']) && null !== $config['session']['client']) {
            if (!isset($config['clients']) || !isset($config['clients'][$config['session']['client']])) {
                throw new \LogicException(sprintf('The client "%s" does not exist! Cannot enable the session support!', $config['session']['client']));
            }
            $sessionOptions = $container->getParameter('session.storage.options');
            $options = array();
            if (isset($config['session']['ttl'])) {
                $options['ttl'] = $config['session']['ttl'];
            } elseif (isset($sessionOptions['cookie_lifetime'])) {
                $options['ttl'] = $sessionOptions['cookie_lifetime'];
            }
            if (isset($config['session']['prefix'])) {
                $options['prefix'] = $config['session']['prefix'];
            }
            $this->enableSessionSupport($config['session']['client'], $options, $container);
        }
        if (isset($config['clients'])) {
            $this->addclients($config['clients'], $container);
        }
    }

    /**
     * Given a handler (memcache/memcached) enables session support
     *
     * @param string $type
     * @param string $clientId
     * @param array $options
     * @param ContainerBuilder $container
     */
    private function enableSessionSupport($clientId, array $options, ContainerBuilder $container)
    {
        $definition = new Definition($container->getParameter('memcache.session_handler.class'));
        $container->setDefinition('memcache.session_handler',$definition);
        $definition
            ->addArgument(new Reference(sprintf('memcache.%s', $clientId)))
            ->addArgument($options)
        ;
        $this->addClassesToCompile(array($definition->getClass()));
    }

    /**<commit_msg>refactor enable session<commit_after><?php

namespace Lsw\MemcacheBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Definition;

/**
 * Applies the configuration for the Memcached object
 * Based on Emagister\MemcachedBundle by <NAME>
 */
class LswMemcacheExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('config.yml');
        $loader->load('services.yml');

        if (isset($config['session'])) {
            $this->enableSessionSupport($config, $container);
        }
        if (isset($config['clients'])) {
            $this->addclients($config['clients'], $container);
        }
    }

    /**
     * Given a handler (memcache/memcached) enables session support
     *
     * @param string $type
     * @param string $clientId
     * @param array $options
     * @param ContainerBuilder $container
     */
    private function enableSessionSupport($config, ContainerBuilder $container)
    {
        // make sure the client is specified and it exists
        if (null !== $config['session']['client']) {
            return;
        }
        if (!isset($config['clients']) || !isset($config['clients'][$config['session']['client']])) {
            throw new \LogicException(sprintf('The client "%s" does not exist! Cannot enable the session support!', $config['session']['client']));
        }
        // calculate options
        $sessionOptions = $container->getParameter('session.storage.options');
        $options = array();
        if (isset($config['session']['ttl'])) {
            $options['ttl'] = $config['session']['ttl'];
        } elseif (isset($sessionOptions['cookie_lifetime'])) {
            $options['ttl'] = $sessionOptions['cookie_lifetime'];
        }
        if (isset($config['session']['prefix'])) {
            $options['prefix'] = $config['session']['prefix'];
        }
        // load the session handler
        $definition = new Definition($container->getParameter('memcache.session_handler.class'));
        $container->setDefinition('memcache.session_handler',$definition);
        $definition
            ->addArgument(new Reference(sprintf('memcache.%s', $clientId)))
            ->addArgument($options)
        ;
        $this->addClassesToCompile(array($definition->getClass()));
    }

    /**
{'fraction_non_alphanumeric': 0.1497667991017447, 'fraction_numerical': 0.0, 'mean_word_length': 3.2857142857142856, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, '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': '3510289', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1480, 'n_words': 284}