Your IP : 216.73.216.79


Current Path : /var/www/html/jkdm/common/components/
Upload File :
Current File : /var/www/html/jkdm/common/components/PortalEngine.php

<?php 

namespace common\components;

use yii\BaseYii as Yii;
use common\components\LanguageSwitcher;
use yii\db\Expression;
use backend\models\frontendmenu\FrontendRoleMapping;
use common\widgets\inspinia\InspiniaAsset;
use yii\base\Component;

class PortalEngine extends Component{

    private $config;

    private $module;

    private $trait;

    private static $instance = null;

    // /*asset bundle*/
    // public $assetbundle;

    public function init(){
        
        /*init config */
        $this->config = require(Yii::getAlias('@portalconfig').'/portal.php');

        /*trait*/
        $this->trait = $this->config['trait'];

        /*module*/
        $this->module = array();

        /* initialize trait */
        $this->traitInit();

        parent::init();
    }

    public function traitInit()
    {
        if(!empty($this->trait)){
            foreach ($this->trait as $class) {
                $ref = new \ReflectionClass($class);
                $obj = $ref->newInstanceArgs();

                try
                {
                    $tag = $ref->getProperty('tag');
                } catch (\Exception $e){
                    echo '<div style="color:red;padding:10px;border:1px solid red; margin:10px;">' .$e->getMessage() . ' in '.$class.'</div>';
                }

                $tagval = $tag->getValue($obj);
                $this->setTag($tagval);
            }
        }
        
        return $this;
    }

    /**
     * 
     * @param content for processing $content
     * @param runtime enviroment $runtime
     * @param boolean $module_tag whether want to use old tag base module , ex [module_tag]content[/module_tag]
     * @return string
     */
    public function load($content, $runtime, $view=null, $module_tag=false)
    {
        return  $content = $this->loadModule($content, $this->module, $runtime);
    }
    
    public function loadModule($content, $module, $runtime) {
        // foreach ($module as $modkey) { // performance tweak
            $content = preg_replace_callback("#\[((?:[a-zA-Z])+?)\]([\s\S]*?|(?R))\[\/((?:\\1)+?)\]#", function ($matches) use ($runtime) {
                return $this->matchModuleTag($runtime, $matches);
            }, $content);
        // }
        return $content;
    }

    public function matchModuleTag($runtime, $matches) {
        // add attribute as a param
        $regex = "/(\S+)=[\"']?((?:.(?![\"']?\s+(?:\S+)=|\s*\/?[>\"']))+.)[\"']?/";
        $param = [];
        preg_match_all($regex, $matches[2], $param);
        // register param on global runtime value
        $runtime += array_combine($param[1], $param[2]);

        if(is_callable($matches[1])) {

            $callable = $matches[1]($matches[2], $runtime); 

            if(is_array($callable)) {
                $arr = $matches[1]($matches[2]);
                
                if(isset($arr['modules']) && $arr['modules'] == true) {
                    return $this->loadModule($arr['contents'], [], $runtime);
                } else {
                    return $arr['contents'];
                }
            }

            return $callable;
        }

        return $this->{$matches[1]}($matches[2], $runtime);
    }


    public function setTag($tag){
        if(!empty($tag))
            foreach ($tag as $tags) {
                
                if(in_array($tags, $this->module)){
                    throw new \Exception("Conflicting tag name :- ". $tags . ". The tag already defined", 1);
                }

                array_push($this->module, $tags);
            }
    }

    public function __call($method, $arguments)
    {
        if(!empty($this->trait))
            foreach ($this->trait as $tval) {
                if (method_exists($tval, $method)) {
                    $ref = new \ReflectionClass($tval);
                    /*get constructor*/
                    $con = $ref->getConstructor();
                    
                    if(!empty($con))
                        $obj = $ref->newInstance($this);
                    else
                        $obj = $ref->newInstance();

                        if(is_array($arguments))
                            list($argument) = $arguments;
                        
                            return $obj->{$method}($argument, $arguments);
                }
            }
    }
    
    // The object is created from within the class itself
    // only if the class has no instance.
    public static function getInstance()
    {
        if (self::$instance == null)
        {
            self::$instance = new PortalEngine();
        }
        
        return self::$instance;
    }
}