<commit_before><?php

namespace jtreminio\Container;

use \Pimple;
use \ReflectionClass;

/**
 * This is a wrapper for Pimple that adds several useful features to make it easier to manage your code!
 *
 * @package jtreminio/Container
 * @author  <NAME> <<EMAIL>>
 */
abstract class Container
{
    /** @var Pimple */
    private static $pimple;

    /**
     * Set the Pimple instance
     *
     * @param Pimple $pimple
     */
    public static function setPimple(Pimple $pimple)
    {
        static::$pimple = $pimple;
    }

    /**
     * Get service from the container
     *
     * @param string $service    Service name, "\Namespace\Component\UserIdentity"
     * @param array  $parameters Parameters service may require for constructor
     * @return mixed
     */
    public static function get($service, array $parameters = array())
    {
        self::$pimple->offsetSet('parameters', $parameters);

        // If service does not exist, add it to Pimple
        if (!self::$pimple->offsetExists($service)) {
            self::createNewService($service, $parameters);
        }

        // Flag for finalizing a definition, preventing overwrites
        $finalServiceName = "{$service}-isFinal";

        // Do this again for services called with new set of parameters
        if (!empty($parameters) && !self::$pimple->offsetExists($finalServiceName)) {
            self::createNewService($service, $parameters);
        }

        return self::$pimple[$service];
    }

    /**
     * @param string $serviceName Service name - fully qualified
     * @param mixed  $service     Value to return
     * @param bool   $final       Flag to override service when parameters are sent. This is useful when injecting a
     *                            mocked object from our tests and not allowing our code to override the mock with a
     *                            new service definition.
     */
    public static function set($serviceName, $service, $final = false)
    {
        self::$pimple->offsetUnset($serviceName);

        self::$pimple->offsetSet($serviceName, $service);

        if ($final) {
            $finalServiceName = "{$serviceName}-isFinal";
            self::$pimple->offsetSet($finalServiceName, 1);
        }
    }

    /**
     * Clear all defined services
     */
    public static function clear()
    {
        foreach (self::$pimple->keys() as $key) {
            self::$pimple->offsetUnset($key);
        }
    }

    /**
     * Create a new service in Pimple provider
     *
     * @param string $service    Service name - fully qualified
     * @param array  $parameters Array of optional parameters
     */
    private static function createNewService($service, $parameters)
    {
        $object = static::getUndefined($service, $parameters);

        self::$pimple[$service] = function () use ($object) {
            return $object;
        };
    }

    /**
     * If the service has not been defined within the container, attempt to instantiate the class directly
     *
     * @param string $fqClassName Fully qualified class name
     * @return object
     */
    private static function getUndefined($fqClassName)
    {
        $reflectionClass = new ReflectionClass($fqClassName);

        $parameters = self::$pimple->offsetExists('parameters') ? self::$pimple->offsetGet('parameters') : false;

        if (!empty($parameters)) {
            return $reflectionClass->newInstanceArgs($parameters);
        }

        return $reflectionClass->newInstance();
    }
}
<commit_msg>Added final flag to ::get(); Using ReflectionClass only when parameters are defined<commit_after><?php

namespace jtreminio\Container;

use \Pimple;
use \ReflectionClass;

/**
 * This is a wrapper for Pimple that adds several useful features to make it easier to manage your code!
 *
 * @package jtreminio/Container
 * @author  <NAME> <<EMAIL>>
 */
abstract class Container
{
    /** @var Pimple */
    private static $pimple;

    /**
     * Set the Pimple instance
     *
     * @param Pimple $pimple
     */
    public static function setPimple(Pimple $pimple)
    {
        static::$pimple = $pimple;
    }

    /**
     * Get service from the container
     *
     * @param string $service    Service name, "\Namespace\Component\UserIdentity"
     * @param array  $parameters Parameters service may require for constructor
     * @param bool   $final      Flag to override service when parameters are sent.
     * @return mixed
     */
    public static function get($service, array $parameters = array(), $final = false)
    {
        self::$pimple->offsetSet('parameters', $parameters);

        $finalServiceName = "{$service}-isFinal";

        if ($final) {
            self::$pimple->offsetSet($finalServiceName, $service);
        }

        // If service does not exist, add it to Pimple
        if (!self::$pimple->offsetExists($service)) {
            self::createNewService($service);

            return self::$pimple[$service];
        }

        // Do this again for services called with new set of parameters
        if (!empty($parameters) && !self::$pimple->offsetExists($finalServiceName)) {
            self::createNewService($service);
        }

        return self::$pimple[$service];
    }

    /**
     * @param string $serviceName Service name - fully qualified
     * @param mixed  $service     Value to return
     * @param bool   $final       Flag to override service when parameters are sent. This is useful when injecting a
     *                            mocked object from our tests and not allowing our code to override the mock with a
     *                            new service definition.
     */
    public static function set($serviceName, $service, $final = false)
    {
        self::$pimple->offsetUnset($serviceName);

        self::$pimple->offsetSet($serviceName, $service);

        if ($final) {
            $finalServiceName = "{$serviceName}-isFinal";
            self::$pimple->offsetSet($finalServiceName, 1);
        }
    }

    /**
     * Clear all defined services
     */
    public static function clear()
    {
        foreach (self::$pimple->keys() as $key) {
            self::$pimple->offsetUnset($key);
        }
    }

    /**
     * Create a new service in Pimple provider
     *
     * @param string $service    Service name - fully qualified
     */
    private static function createNewService($service)
    {
        $object = static::getUndefined($service);

        self::$pimple[$service] = function () use ($object) {
            return $object;
        };
    }

    /**
     * If the service has not been defined within the container, attempt to instantiate the class directly
     *
     * @param string $fqClassName Fully qualified class name
     * @return object
     */
    private static function getUndefined($fqClassName)
    {
        $parameters = self::$pimple->offsetExists('parameters') ? self::$pimple->offsetGet('parameters') : false;

        // Only use ReflectionClass if parameters are needed in constructor
        if (!empty($parameters)) {
            $reflectionClass = new ReflectionClass($fqClassName);
            return $reflectionClass->newInstanceArgs($parameters);
        }

        return new $fqClassName;
    }
}
{'fraction_non_alphanumeric': 0.11172337787574046, 'fraction_numerical': 0.00027552004408320705, 'mean_word_length': 2.365785813630042, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2749190', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1880, 'n_words': 580}