<?php
namespace app\common\command;
use think\Controller;
use think\Db;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class Task extends Command{
	  protected function configure()
    {
		 $this->setName('create')              		//定义命令的名字
        ->setDescription('This is my command')     //定义命令的描述
        ->addArgument('name')                      //增加一个名字参数
         ->addArgument('admin')->addArgument('index')->addArgument('hook');
        //$this->setName('create')
           // ->addArgument('name', Argument::OPTIONAL, "your name")
			//->addArgument('hook', Argument::OPTIONAL, "your name")        
           // ->setDescription('This is my command');
    }
//引入需要执行的文件
    protected function execute(Input $input, Output $output)
    {
    //调取事先写好的逻辑类--Addred中方法
	 //获取输入的参数
        $name = $input->getArgument('name');
		$admin = $input->getArgument('admin');
		$index = $input->getArgument('index');
		$hook = $input->getArgument('hook');
        $this->add($name,$admin,$index,$hook);
//在控制台输出
     $output->writeln("{$name} id created ");
   
    }
        /* 增加插件 */
	public function add($name,$admin,$index,$hook){  
	      $admin=isset($admin)?$admin:'0';
		  $index=isset($index)?$index:'0';
		  $data = array(
            'name'=>$name,
            'title'=>$name.'插件',
            'description'=>'这是一个临时描述',
            'status'=>1,
            'author'=>'无名',
            'version'=>'0.1',
			'admin'=>$admin,
			'index'=>$index,
			'ids'=>'',
			'has_config'=>'1',
			'hook'=>$hook?$hook:'',
        );
		 $INSTALL_PATH = str_replace('\\','/',dirname($_SERVER['SCRIPT_NAME']));
if($INSTALL_PATH==="/"){
    $INSTALL_PATH="/";		
}else{
    $INSTALL_PATH= '/'. trim($INSTALL_PATH,'/').'/';
}
  
		$PLUGIN_PATH= $_SERVER['DOCUMENT_ROOT'].$INSTALL_PATH.'plugin/';
		$PLUGIN_PATH= str_replace("application","plugin",env('app_path'));
	
		
		  if(!$data['name']){
		    $this->error('插件标识必须');
		  }
        //检测插件名是否合法
        $plugins_dir             =  $PLUGIN_PATH;
        if(file_exists("{$plugins_dir}{$data['name']}")){
            $this->error('插件已经存在了');
        }
		$class=ucfirst($data['name']);
			
		 $hook = '';
		if($data['hook']){ 
		 $hookarray=explode("_",$data['hook']);	
        foreach ($hookarray as $value) {
			
			
			$hoo['name']=$value;
			if(db("hooks")->where($hoo)->select()){
				 $this->error('钩子'.$value.'已经存在了');		  
			}
			else{$hoo['addons']=$data['name'];
			$hoo['description']=$data['title'].'的钩子';
				 db("hooks")->insert($hoo);	
			}
			
			
            $hook .= <<<str
        //实现的{$value}钩子方法
        public function {$value}(\$param=[]){
           
        }

str;
        }
		}
        $str = <<<str
<?php
namespace plugin\\{$data['name']};
use app\index\behavior\Plugin;
use think\Db;
/**
 * {$data['title']}插件
 * @author {$data['author']}
 */
    class {$class} extends Plugin{

        public \$info = array(
            'name'=>'{$data['name']}',
            'title'=>'{$data['title']}',
            'description'=>'{$data['description']}',
            'status'=>{$data['status']},
            'author'=>'{$data['author']}',
            'version'=>'{$data['version']}',
			'admin'=>'{$data['admin']}',
			'index'=>'{$data['index']}',
			'ids'=>'{$data['ids']}'
        );

        public function install(){
            return true;
        }

        public function uninstall(){
            return true;
        }

{$hook}
    }
str;
		
        
        $plugins_dir             = $PLUGIN_PATH;
        //创建目录结构
        $files          =   array();
        $plugin_dir      =   "$plugins_dir{$data['name']}\\";
		  mkdir($plugin_dir);
        $files[]        =   $plugin_dir;
        $plugin_name     =   "{$class}.php";
        $files[]        =   "{$plugin_dir}{$plugin_name}";
        if(isset($data['has_config'])&&$data['has_config'] == 1);//如果有配置文件
		{   $files[]    =   $plugin_dir.'config.php';
		
		}
            $files[]    =   "{$plugin_dir}controller/";
          
			$files[]    =   "{$plugin_dir}model/";
			
		    $files[]    =   "{$plugin_dir}".ucfirst($data['name']).".php";
			$files[]    =   "{$plugin_dir}view/";
			if($data['admin']){
			  $files[]    =   "{$plugin_dir}view/lists/";
			}
			if($data['index']){
			   $files[]    =   "{$plugin_dir}view/index/";
			}
			$files[]    =   "{$plugin_dir}config/";
			$files[]    =   "{$plugin_dir}validate/";
			$files[]    =   "{$plugin_dir}view/static/";
			$files[]    =   "{$plugin_dir}view/static/images/";
			$files[]    =   "{$plugin_dir}view/static/css/";
			$files[]    =   "{$plugin_dir}view/static/js/";
            $files[]    =   "{$plugin_dir}view/static/css/style.css";
		
     
      foreach ($files as $key => $value) {
            if(substr($value, -1) == '/'){
				  
               mkdir($value);
           }else{
               @file_put_contents($value, '');
	      }
	  }	
	  $ROOT_PATH= str_replace("application","",env('app_path')); 
	 
	  $file = file_get_contents($ROOT_PATH . 'install\conf\Base.conf');
	  $file = str_replace('[SPACE]', strtolower($name), $file); 	
	  file_put_contents($ROOT_PATH . 'plugin\\'.strtolower($name).'\controller\Base.php', $file);
	  
	  $file = file_get_contents($ROOT_PATH . 'install\conf\common.conf');
	  $file = str_replace('[SPACE]', strtolower($name), $file); 	
	  file_put_contents($ROOT_PATH . 'plugin\\'.strtolower($name).'\common.php', $file);
	  
	  $file = file_get_contents($ROOT_PATH . 'install\conf\admin.conf');
	  $file = str_replace('[SPACE]', strtolower($name), $file); 	
	  file_put_contents($ROOT_PATH . 'plugin\\'.strtolower($name).'\controller\Admin.php', $file);
	  
	  if($data['admin']){	
	    $file = file_get_contents($ROOT_PATH . 'install\conf\lists.conf');
	    $file = str_replace('[SPACE]', strtolower($name), $file); 	
	    file_put_contents($ROOT_PATH . 'plugin\\'.strtolower($name).'\controller\Lists.php', $file);
		
		$index="{$plugin_dir}view/lists/index.html";	  
        $indexhtml= file_get_contents($ROOT_PATH . 'install\conf\plugin.conf');
	    file_put_contents($index, $indexhtml); 
	  }
	  
	  if($data['index']){
	    $file = file_get_contents($ROOT_PATH . 'install\conf\index_index.conf');
	    $file = str_replace('[SPACE]', strtolower($name), $file); 	
	    file_put_contents($ROOT_PATH . 'plugin\\'.strtolower($name).'\controller\Index.php', $file);
		
		$index="{$plugin_dir}view/index/index.html";	  
        $indexhtml= file_get_contents($ROOT_PATH . 'install\conf\plugin.conf');
	    file_put_contents($index, $indexhtml); 
	  }
	   
	   
      file_put_contents("{$plugin_dir}{$plugin_name}", $str); 
      $index="{$plugin_dir}view/index.html";		 
      $indexhtml= file_get_contents($ROOT_PATH . 'install\conf\plugin.conf');
	  file_put_contents($index, $indexhtml);
	  
	  $file = file_get_contents($ROOT_PATH . 'install\conf\session.conf');
	  $file = str_replace('[SPACE]', strtolower($name), $file); 	
	  file_put_contents($ROOT_PATH . 'plugin\\'.strtolower($name).'\config\session.php', $file);
	  
	  $file = file_get_contents($ROOT_PATH . 'install\conf\cookie.conf');
	  $file = str_replace('[SPACE]', strtolower($name), $file); 	
	  file_put_contents($ROOT_PATH . 'plugin\\'.strtolower($name).'\config\cookie.php', $file);
	  
      if(isset($data['has_config'])&&$data['has_config'] == 1){ 
   	      $conf= file_get_contents($ROOT_PATH . 'install\conf\config.conf');
		  file_put_contents("{$plugin_dir}config.php",$conf);
	   }
	   
	}
    
	
}
