<?php
namespace app\common\behavior;
use think\Controller;
use think\Db;
use think\facade\Hook;
class BymallHooks{
	
   public function run($param = [])
    {

		
   	  //$Hook= new \think\Hook; 
        // 动态加入命名空间
        \think\Loader::addNamespace('plugin', PLUGIN_PATH);
        // 获取钩子数据
        $data = cache('hooks');
	
        if(!$data){
            $hooks = Db::name('Hooks')->column('name,addons');
	
            // 获取钩子的实现插件信息
            foreach ($hooks as $key => $value) {
				dump($value);
                if($value){
                    //$map[]=['status', "=", 1];
					$names=array();
						$names         = explode(',', $value);
                        
				        $map[]=['name','IN', $names];
			      
                    $data = db::name('plugin')->where($map)->column('id,name');
                    if($data){
                        $addons = array_intersect($names, $data);
					
						$addon_class=$this->get_addon_class($value);
						
						  Hook::add($key,$addon_class);
                       //Hook::add($key, array_map('get_addon_class',$addons));
                    }
                }
            }	
			
            cache('hooks', Hook::get());
        }else{
            Hook::import($data, false);
         }

    }
	function get_addon_class($name)
{
    // 初始化命名空间及类名
    $class = "plugin\\{$name}\\" . ucfirst($name);
    return $class;
}
	
}