<commit_before><?php

namespace Glad;

use Glad\GladProvider;
use ReflectionMethod;
use ReflectionClass;

/**
 * Dependency injection class
 *
 * @author <NAME>
 * @category Authentication
 * @package Glad
 * @copyright 2015
 * @license http://opensource.org/licenses/MIT MIT license
 * @link https://github.com/atayahmet/glad
 */

class Injector {
    
    /**
    * Provider
    *
    * @var object
    */
    protected static $gladProvider;

    /**
    * Object Container
    *
    * @var object
    */
    protected static $container;

    /**
    * Class constructor
    *
    * @return void
    */    
    public function __construct()
    {
        static::$gladProvider = GladProvider::register();
    }

    /**
    * Inject method
    *
    * @param string $class
    * @param string $method
    * @param array $parm
    *
    * @return void
    */   
    public static function inject($class, $method = null, array $parm = null)
    {
        return static::setInjectsParameters($class, $method, $parm);
    }

    public static function add($key, $class)
    {
        self::$container[$key] = $class;
    }

    public static function resolve($class = null)
    {
        if(is_object($class)){

            $resolved = preg_split('/\\\\/', get_class($class));

            if(count($resolved) > 0) {
                static::add(end($resolved), $class);
            }
        }
    }

    public function make()
    {

    }
    
    /**
    * method setInjectsParameters
    *
    * @param string $class
    * @param string $method
    * @param array $parm
    *
    * @return all types
    */ 
    private static function setInjectsParameters($class, $method, array $parm = null)
    {
        $methods = array('__construct', $method);
        $instance = null;

        foreach($methods as $k => $m) {

            if(method_exists($class, $m)){

                $export = ReflectionMethod::export($class, $m, true);

                // Parametreler ayıklanıyor.
                preg_match_all('/\[(.*?)\]/', $export, $matches, PREG_PATTERN_ORDER);

                // Gereksiz değerler diziden çıkarılıyor
                unset($matches[1][0], $matches[1][1]);
                
                // Yeni enjekte edilecek parametrelerin toplanacağı dizi değişkeni.
                $injects = array();

                foreach($matches[1] as $in){

                    $t = preg_split('/ /',$in);
                    $segments = preg_split("/\\\\/", $t[2]);
                    $last = end($segments);

                    if($last == 'array'){
                        foreach($parm as $_parm){
                            $injects[] = $_parm;
                        }
                    }else{
                        if(isset(static::$container[$last])){
                            $injects[] = static::$container[$last];
                        }else{
                            if(isset(static::$gladProvider[$last])){
                                $new = new static::$gladProvider[$last];

                                static::$container[$last] = $new;
                                
                                $injects[] = $new;
                            }
                        }
                    }
                }

                // Geçerli class'ın __construct methodu var ise parametreler enjekte ediliyor ve instance alınıyor..
                if(method_exists($class, '__construct') && is_null($instance)){

                    $c = new ReflectionClass($class);
                    $instance = $c->newInstanceArgs($injects);
                }else{
                    if(is_null($instance)){
                        $instance = new $class();
                    }

                    $reflectionMethod = new ReflectionMethod($class, $m);<commit_msg>Bug fixed: outside the object and array parameters did not reach the target method<commit_after><?php

namespace Glad;

use Glad\GladProvider;
use ReflectionMethod;
use ReflectionClass;

/**
 * Dependency injection class
 *
 * @author <NAME>
 * @category Authentication
 * @package Glad
 * @copyright 2015
 * @license http://opensource.org/licenses/MIT MIT license
 * @link https://github.com/atayahmet/glad
 */

class Injector {
    
    /**
    * Provider
    *
    * @var object
    */
    protected static $gladProvider;

    /**
    * Object Container
    *
    * @var object
    */
    protected static $container;

    /**
    * Class constructor
    *
    * @return void
    */    
    public function __construct()
    {
        static::$gladProvider = GladProvider::register();
    }

    /**
    * Inject method
    *
    * @param string $class
    * @param string $method
    * @param array $parm
    *
    * @return void
    */   
    public static function inject($class, $method = null, array $parm = null)
    {
        return static::setInjectsParameters($class, $method, $parm);
    }

    public static function add($key, $class)
    {
        self::$container[$key] = $class;
    }

    public static function resolve($class = null)
    {
        if(is_object($class)){

            $resolved = preg_split('/\\\\/', get_class($class));

            if(count($resolved) > 0) {
                static::add(end($resolved), $class);
            }
        }
    }

    public function make()
    {

    }
    
    /**
    * method setInjectsParameters
    *
    * @param string $class
    * @param string $method
    * @param array $parm
    *
    * @return all types
    */ 
    private static function setInjectsParameters($class, $method, array $parm = null)
    {
        $methods = array('__construct', $method);
        $instance = null;

        foreach($methods as $k => $m) {

            if(method_exists($class, $m)){

                $export = ReflectionMethod::export($class, $m, true);

                // Parametreler ayıklanıyor.
                preg_match_all('/\[(.*?)\]/', $export, $matches, PREG_PATTERN_ORDER);

                // Gereksiz değerler diziden çıkarılıyor
                unset($matches[1][0], $matches[1][1]);

                // Yeni enjekte edilecek parametrelerin toplanacağı dizi değişkeni.
                $injects = array();

                foreach($matches[1] as $in){

                    $t = preg_split('/ /',$in);
                    $segments = preg_split("/\\\\/", $t[2]);
                    $last = end($segments);

                    if($last == 'array' || strpos($last, '$') !== false){
                        foreach($parm as $_parm){
                            $injects[] = $_parm;
                        }
                    }else{
                        if(isset(static::$container[$last])){
                            $injects[] = static::$container[$last];
                        }else{
                            if(isset(static::$gladProvider[$last])){
                                $new = new static::$gladProvider[$last];

                                static::$container[$last] = $new;
                                
                                $injects[] = $new;
                            }
                        }
                    }
                }

                // Geçerli class'ın __construct methodu var ise parametreler enjekte ediliyor ve instance alınıyor..
                if(method_exists($class, '__construct') && is_null($instance)){

                    $c = new ReflectionClass($class);
                    $instance = $c->newInstanceArgs($injects);
                }else{
                    if(is_null($instance)){
                        $instance = new $class();
                    }

                    $reflectionMethod = new ReflectionMethod($class, $m);
{'fraction_non_alphanumeric': 0.1301859799713877, 'fraction_numerical': 0.002861230329041488, 'mean_word_length': 1.4482648837949696, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '6053279', 'n_tokens_mistral': 2203, 'n_tokens_neox': 2078, 'n_words': 448}