<commit_before><?php
/*
 * Copyright (c) Ouzo contributors, http://ouzoframework.org
 * This file is made available under the MIT License (view the LICENSE file for more information).
 */

namespace Ouzo\Injection;

use BadMethodCallException;

class InstanceRepository
{
    /** @var object[] */
    private $instances = [];
    /** @var object[] */
    private $factoryInstances = [];

    /** @var Bindings */
    private $bindings;

    /**
     * @param Bindings $bindings
     */
    public function __construct(Bindings $bindings)
    {
        $this->bindings = $bindings;
    }

    /**
     * @param InstanceFactory $factory
     * @param Binder $binder
     * @return object
     * @throws BadMethodCallException
     * @throws InjectorException
     */
    public function getInstance(InstanceFactory $factory, Binder $binder)
    {
        $instance = $binder->getInstance();
        if ($instance) {
            return $instance;
        }

        $factoryClassName = $binder->getFactoryClassName();
        if ($factoryClassName) {
            return $this->createInstanceThroughFactory($factory, $binder);
        }

        $className = $binder->getBoundClassName() ?: $binder->getClassName();
        $scope = $binder->getScope();
        if ($scope == Scope::SINGLETON) {
            return $this->singletonInstance($factory, $className);
        }
        if ($scope == Scope::PROTOTYPE) {
            return $factory->createInstance($this, $className);
        }
        throw new BadMethodCallException("Unknown scope: $scope");
    }

    /**
     * @param InstanceFactory $factory
     * @param $className
     * @return object
     */
    public function singletonInstance(InstanceFactory $factory, $className)
    {
        if (isset($this->instances[$className])) {
            return $this->instances[$className];
        }
        $instance = $factory->createInstance($this, $className);
        $this->instances[$className] = $instance;
        return $instance;
    }

    /**
     * @param InstanceFactory $factory
     * @param Binder $binder
     * @return mixed
     * @throws InjectorException
     */
    private function createInstanceThroughFactory(InstanceFactory $factory, Binder $binder)
    {
        $factoryClassName = $binder->getFactoryClassName();

        if (!in_array(Factory::class, class_implements($factoryClassName))) {
            throw new InjectorException("Factory class $factoryClassName does not implemented \Ouzo\Injection\Factory interface.");
        }

        if ($binder->getScope() == Scope::SINGLETON) {
            if (isset($this->factoryInstances[$factoryClassName])) {
                return $this->factoryInstances[$factoryClassName];
            }
        }

        return $this->createInstanceThroughFactoryAsPrototype($factory, $factoryClassName);
    }

    private function createInstanceThroughFactoryAsPrototype(InstanceFactory $factory, string $factoryClassName)
    {
        $factoryBinder = $this->bindings->getBinder($factoryClassName);
        /** @var Factory $factoryObject */
        $factoryObject = $this->getInstance($factory, $factoryBinder);
        $object = $factoryObject->create();
        $this->factoryInstances[$factoryClassName] = $object;

        return $object;
    }
}
<commit_msg>[Core] Interceptors are creating by a IoC container - fix php 5.6.<commit_after><?php
/*
 * Copyright (c) Ouzo contributors, http://ouzoframework.org
 * This file is made available under the MIT License (view the LICENSE file for more information).
 */

namespace Ouzo\Injection;

use BadMethodCallException;

class InstanceRepository
{
    /** @var object[] */
    private $instances = [];
    /** @var object[] */
    private $factoryInstances = [];

    /** @var Bindings */
    private $bindings;

    /**
     * @param Bindings $bindings
     */
    public function __construct(Bindings $bindings)
    {
        $this->bindings = $bindings;
    }

    /**
     * @param InstanceFactory $factory
     * @param Binder $binder
     * @return object
     * @throws BadMethodCallException
     * @throws InjectorException
     */
    public function getInstance(InstanceFactory $factory, Binder $binder)
    {
        $instance = $binder->getInstance();
        if ($instance) {
            return $instance;
        }

        $factoryClassName = $binder->getFactoryClassName();
        if ($factoryClassName) {
            return $this->createInstanceThroughFactory($factory, $binder);
        }

        $className = $binder->getBoundClassName() ?: $binder->getClassName();
        $scope = $binder->getScope();
        if ($scope == Scope::SINGLETON) {
            return $this->singletonInstance($factory, $className);
        }
        if ($scope == Scope::PROTOTYPE) {
            return $factory->createInstance($this, $className);
        }
        throw new BadMethodCallException("Unknown scope: $scope");
    }

    /**
     * @param InstanceFactory $factory
     * @param $className
     * @return object
     */
    public function singletonInstance(InstanceFactory $factory, $className)
    {
        if (isset($this->instances[$className])) {
            return $this->instances[$className];
        }
        $instance = $factory->createInstance($this, $className);
        $this->instances[$className] = $instance;
        return $instance;
    }

    /**
     * @param InstanceFactory $factory
     * @param Binder $binder
     * @return mixed
     * @throws InjectorException
     */
    private function createInstanceThroughFactory(InstanceFactory $factory, Binder $binder)
    {
        $factoryClassName = $binder->getFactoryClassName();

        if (!in_array(Factory::class, class_implements($factoryClassName))) {
            throw new InjectorException("Factory class $factoryClassName does not implemented \Ouzo\Injection\Factory interface.");
        }

        if ($binder->getScope() == Scope::SINGLETON) {
            if (isset($this->factoryInstances[$factoryClassName])) {
                return $this->factoryInstances[$factoryClassName];
            }
        }

        return $this->createInstanceThroughFactoryAsPrototype($factory, $factoryClassName);
    }

    /**
     * @param InstanceFactory $factory
     * @param string $factoryClassName
     * @return mixed
     */
    private function createInstanceThroughFactoryAsPrototype(InstanceFactory $factory, $factoryClassName)
    {
        $factoryBinder = $this->bindings->getBinder($factoryClassName);
        /** @var Factory $factoryObject */
        $factoryObject = $this->getInstance($factory, $factoryBinder);
        $object = $factoryObject->create();
        $this->factoryInstances[$factoryClassName] = $object;

        return $object;
    }
}

{'fraction_non_alphanumeric': 0.1260940513276962, 'fraction_numerical': 0.0002966918854769322, 'mean_word_length': 2.7186982901268615, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '7143283', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1772, 'n_words': 389}