Your IP : 216.73.216.79


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

<?php 

namespace common\components;

use yii\BaseYii as Yii;
use yii\base\Component;

class PortalEngine extends Component{

    private $config;

    private $module;

    private $trait;
    
    private static $instance = null;
    
    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;
    }

    public function load($content, $runtime)
    {          
        /*OLD VER*/
        /* tags
        $tags = implode("|", $this->module);
        */   
        foreach ($this->module as $modkey) {
	        $content = preg_replace_callback(
	            "#\[((?:$modkey)+?)\]([\s\S]*?|(?R))\[\/[^'-]*?((?:$modkey)+?)\]#",
	            function ($matches) use ($runtime){
	                
	                //unset global variable
	                unset($runtime['__dyna_content__']);
	                unset($runtime['__dyna_content_row__']);
	                unset($runtime['__dyna_page_id']);
	                unset($runtime['__dyna_header']);
	                
	                return $this->{$matches[1]}($matches[2], $runtime);
	            },
	            $content
	        );	
        }
        
        return $content;
    }

    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;
    }
    
}