| Current Path : /var/www/html/aipa/common/components/ |
| Current File : /var/www/html/aipa/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;
}
/**
*
* @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)
{
if($module_tag)
$content = $this->loadModule($content, $this->module, $runtime);
$twig = new PortalModuleViewRenderer(['options' => ['debug' => true]]);
if(empty($view)) {
return $twig->render(null, $content, $runtime);
}
return $twig->render($view, $content, $runtime);
}
public function loadModule($content, $module, $runtime) {
foreach ($module as $modkey) {
$content = preg_replace_callback("#\[((?:" . $modkey . ")+?)\]([\s\S]*?|(?R))\[\/((?:" . $modkey . ")+?)\]#", function ($matches) use ($runtime) {
return $this->matchModuleTag($runtime, $matches);
}, $content);
}
return $content;
}
public function matchModuleTag($runtime, $matches) {
// unset global variable
unset($runtime['__dyna_content__']);
unset($runtime['__dyna_content_row__']);
unset($runtime['__dyna_page_id']);
unset($runtime['__dyna_header']);
// 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]);
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;
}
}