| Current Path : /var/www/html/school/common/components/ |
| Current File : /var/www/html/school/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;
// /*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();
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);
}
}
}
public function load($content)
{
/* load trait */
$this->traitInit();
/*OLD VER*/
/* tags
$tags = implode("|", $this->module);
*/
foreach ($this->module as $modkey) {
$content = preg_replace_callback(
"#\[((?:$modkey)+?)\]([\s\S]*?|(?R))\[\/[^'-]*?((?:$modkey)+?)\]#",
function ($matches){
return $this->{$matches[1]}($matches[2]);
},
$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, $argument)
{
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($argument))
$argument = $argument[0];
return $obj->{$method}($argument);
}
}
}
}