<commit_before><?php

namespace Predis\Silex;

use Silex\Application;
use Silex\ServiceProviderInterface;

use Predis\Client;
use Predis\ClientOptions;
use Predis\ConnectionParameters;

class PredisServiceProvider implements ServiceProviderInterface
{
    protected static $invalidAliases = array(
        'parameters',
        'options',
        'class_path',
        'clients',
    );

    protected $_prefix;

    public function __construct($prefix = 'predis')
    {
        $this->_prefix = $prefix;
    }

    public function register(Application $app)
    {
        $prefix = $this->_prefix;

        if (isset($app["$prefix.class_path"])) {
            $app['autoloader']->registerNamespace('Predis', $app["$prefix.class_path"]);
        }

        if (!isset($app["$prefix.default_parameters"])) {
            $app["$prefix.default_parameters"] = array();
        }

        if (!isset($app["$prefix.default_options"])) {
            $app["$prefix.default_options"] = array();
        }

        $app["$prefix.client_initializer"] = $app->protect(function($arguments) use($app, $prefix) {
            $extract = function($bag, $key) use ($app, $prefix) {
                $default = "default_$key";
                if ($bag instanceof Application) {
                    $key = "$prefix.$key";
                }
                if (!isset($bag[$key])) {
                    return $app["$prefix.$default"];
                }
                if (is_array($bag[$key])) {
                    return array_merge($app["$prefix.$default"], $bag[$key]);
                }

                return $bag[$key];
            };

            if (is_string($arguments)) {
                $parameters = $arguments;
                $options = $app["$prefix.default_options"];
            }
            else {
                $parameters = $extract($arguments, 'parameters');
                $options = $extract($arguments, 'options');
            }

            return new Client($parameters, $options);
        });

        if (isset($app["$prefix.clients"])) {
            foreach ($app["$prefix.clients"] as $alias => $args) {
                if (in_array($alias, self::$invalidAliases, true)) {
                    throw new \InvalidArgumentException("The specified alias '$alias' is not valid.");
                }

                $app["$prefix.$alias"] = $app->share(function() use($app, $prefix, $args) {
                    $initializer = $app["$prefix.client_initializer"];

                    if (!isset($args['parameters'])) {
                        if (!isset($args['options']) && !isset($args['default'])) {
                            $args = array('parameters' => $args);
                        }
                    }

                    return $initializer($args);
                });

                if (isset($args['default']) && $args['default'] == true) {
                    $app[$prefix] = $app->share(function() use($app, $prefix, $alias) {
                        return $app["$prefix.$alias"];
                    });
                }
            }
        }
        else {
            $app[$prefix] = $app->share(function() use($app, $prefix) {
                $initializer = $app["$prefix.client_initializer"];

                return $initializer($app);
            });
        }
    }
}
<commit_msg>Validate the specified prefix.<commit_after><?php

namespace Predis\Silex;

use Silex\Application;
use Silex\ServiceProviderInterface;

use Predis\Client;
use Predis\ClientOptions;
use Predis\ConnectionParameters;

class PredisServiceProvider implements ServiceProviderInterface
{
    protected static $invalidAliases = array(
        'parameters',
        'options',
        'class_path',
        'clients',
    );

    protected $_prefix;

    public function __construct($prefix = 'predis')
    {
        if (empty($prefix)) {
            throw new \InvalidArgumentException('The specified prefix is not valid.');
        }

        $this->_prefix = $prefix;
    }

    public function register(Application $app)
    {
        $prefix = $this->_prefix;

        if (isset($app["$prefix.class_path"])) {
            $app['autoloader']->registerNamespace('Predis', $app["$prefix.class_path"]);
        }

        if (!isset($app["$prefix.default_parameters"])) {
            $app["$prefix.default_parameters"] = array();
        }

        if (!isset($app["$prefix.default_options"])) {
            $app["$prefix.default_options"] = array();
        }

        $app["$prefix.client_initializer"] = $app->protect(function($arguments) use($app, $prefix) {
            $extract = function($bag, $key) use ($app, $prefix) {
                $default = "default_$key";
                if ($bag instanceof Application) {
                    $key = "$prefix.$key";
                }
                if (!isset($bag[$key])) {
                    return $app["$prefix.$default"];
                }
                if (is_array($bag[$key])) {
                    return array_merge($app["$prefix.$default"], $bag[$key]);
                }

                return $bag[$key];
            };

            if (is_string($arguments)) {
                $parameters = $arguments;
                $options = $app["$prefix.default_options"];
            }
            else {
                $parameters = $extract($arguments, 'parameters');
                $options = $extract($arguments, 'options');
            }

            return new Client($parameters, $options);
        });

        if (isset($app["$prefix.clients"])) {
            foreach ($app["$prefix.clients"] as $alias => $args) {
                if (in_array($alias, self::$invalidAliases, true)) {
                    throw new \InvalidArgumentException("The specified alias '$alias' is not valid.");
                }

                $app["$prefix.$alias"] = $app->share(function() use($app, $prefix, $args) {
                    $initializer = $app["$prefix.client_initializer"];

                    if (!isset($args['parameters'])) {
                        if (!isset($args['options']) && !isset($args['default'])) {
                            $args = array('parameters' => $args);
                        }
                    }

                    return $initializer($args);
                });

                if (isset($args['default']) && $args['default'] == true) {
                    $app[$prefix] = $app->share(function() use($app, $prefix, $alias) {
                        return $app["$prefix.$alias"];
                    });
                }
            }
        }
        else {
            $app[$prefix] = $app->share(function() use($app, $prefix) {
                $initializer = $app["$prefix.client_initializer"];

                return $initializer($app);
            });
        }
    }
}

{'fraction_non_alphanumeric': 0.15722900426533315, 'fraction_numerical': 0.0, 'mean_word_length': 1.6771653543307086, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '1875369', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1726, 'n_words': 321}