<?php

(defined('BASEPATH')) OR exit('No direct script access allowed');

/* load the MX_Loader class */
require realpath('vendor/wiredesignz/hmvc/third_party/MX/Loader.php');

class RT_Loader extends MX_Loader
{

    protected $allBootstrapLoaded = FALSE;

    /**
     * Загружает файлы bootstrap.php всех модулей с папки config в глобальный конфиг
     */
    public function allBootstrap()
    {
        if (!$this->allBootstrapLoaded) {
            $folders = CI::$APP->config->item('modules_locations');

            foreach (array_keys($folders) as $dir) {
                $modules = array_diff(scandir($dir), array('..', '.'));
                if (!empty($modules)) {
                    foreach ($modules as $item) {
                        if (!is_dir($dir . $item)) {
                            continue;
                        }
                        $file = $dir . $item . '/config/bootstrap.php';
                        if (file_exists($file)) {
                            $tmp = include_once $file;
                            $rs = $this->merge_recursive(CI::$APP->config->config, $tmp);
                            CI::$APP->config->config = $rs;
                        }
                    }
                }
            }
            $this->allBootstrapLoaded = TRUE;
        }
    }

    public function library($library, $params = NULL, $object_name = NULL)
    {
        parent::library($library, $params, $object_name);

        if (!is_null($object_name) || !is_array($library)) {
            $lib = strtolower(basename($library));
            if (isset($this->_ci_varmap[$lib])) {
                $lib = $this->_ci_varmap[$lib];
            }

            return !is_null($object_name) ? CI::$APP->{$object_name} : (isset(CI::$APP->{$lib}) ? CI::$APP->{$lib} : $this);
        }

        return $this;
    }
    
    public function model($model, $object_name = NULL, $connect = FALSE)
    {
        parent::model($model, $object_name, $connect);
        
        if (!is_array($model)) {
            $model_name = strtolower(basename($model));
            $obj = is_null($object_name) ? CI::$APP->{$model_name} : CI::$APP->{$object_name};
            $this->config('events', false, true);
            if ($obj instanceof \Ciext\Model\AbstractEventModel && !empty(CI::$APP->config->item('events_models')[$model])) {
                foreach (CI::$APP->config->item('events_models')[$model] as $listener) {
                    $obj->attach($this->library($listener));
                }
            }
        }
    }

    protected function merge_recursive($arr1 = array(), $arr2 = array())
    {
        foreach ($arr2 as $k2 => $v2) {
            if (is_array($v2)) {
                $tmp = isset($arr1[$k2]) ? $arr1[$k2] : array();
                $arr1[$k2] = $this->merge_recursive($tmp, $v2);
            }
            if (!isset($arr1[$k2])) {
                $arr1[$k2] = $v2;
            } elseif (isset($arr1[$k2]) && is_array($arr1) && is_int($k2)) {
                $arr1[] = $v2;
            }
        }

        return $arr1;
    }

    protected function _ci_object_to_array($object)
    {
        return is_object($object) ? get_object_vars($object) : $object;
    }

}
