zoukankan      html  css  js  c++  java
  • PHP命令行模式

    <?php
    
    error_reporting(E_ALL);
    header('Content-Type:text/plain;charset=utf-8');
    
    interface CommandApp {
    
        public function execute();
    }
    
    abstract class CommandBase implements CommandApp {
    
        public $resource;
    
        public function setRsrcParams($key, $value) {
            $this->resource[$key] = $value;
        }
    
        public function getRsrcParams($key) {
            return isset($this->resource[$key]) ? $this->resource[$key] : array();
        }
    
        public function setTplData($value) {
            $this->setRsrcParams('tplData', $value);
        }
    
        public function getTplData() {
            return $this->getRsrcParams('tplData');
        }
    
    }
    
    class Invoker {
    
        private $cmdlist = array();
    
        public function getCommandList() {
            return $this->cmdlist;
        }
    
        public function setCommand($cmd, $append = false) {
            $append !== true && $this->cmdlist = array();
            if (!is_object($cmd)) {
                return;
            }
            $kls = get_class($cmd);
            if (isset($this->cmdlist[$kls])) {
                unset($this->cmdlist[$kls]);
            }
            $this->cmdlist[$kls] = $cmd;
        }
    
        public function setCommandList($cmdlist, $append = false) {
            if ($append !== true) {
                $this->cmdlist = array();
            }
            if (is_array($cmdlist)) {
                foreach ($cmdlist as $cmd) {
                    $this->setCommand($cmd, true);
                }
            }
        }
    
        public function clearCommandList() {
            foreach ($this->cmdlist as $kls => $cmd) {
                unset($this->cmdlist[$kls]);
            }
        }
    
        public function execute() {
            $res = array();
            foreach ($this->cmdlist as $kls => $cmd) {
                $cmd->execute();
            }
            return $res;
        }
    
    }
    
    class Cyber extends CommandBase {
    
        public function execute() {
            $tplData['cyber'] = '第一个命令';
            $this->setTplData($tplData);
        }
    
    }
    
    class Imbar extends CommandBase {
    
        public function execute() {
            $tplData['imbar'] = '第二个命令';
            $this->setTplData($tplData);
        }
    
    }
    
    class cia extends CommandBase {
    
        public function execute() {
            $invoker = new Invoker();
            $cmd = new Cyber();
            $commandlist[get_class($cmd)] = $cmd;
            $cmd = new Imbar();
            $commandlist[get_class($cmd)] = $cmd;
            $invoker->setCommandList($commandlist);
            $invoker->execute();
            $cia = array();
            foreach ($invoker->getCommandList() as $cmd) {
                $cia = array_merge($cia, $cmd->getTplData());
            }
            $invoker->clearCommandList();
            $tplData['cia'] = $cia;
            $this->setTplData($tplData);
        }
    
    }
    
    $cia = new cia();
    $cia->execute();
    print_r($cia->getTplData());
    ?>
  • 相关阅读:
    EventBus
    Date 时间 日期 常用方法函数
    线程 Thread Handler
    MySQL-DoubleWrite
    MySQL各版本优化器变化
    MySQL优化器-条件过滤(condition_fanout_filter)
    PXC集群搭建
    mysql主从不一致--relay_log_recovery设置成0
    MySQL5.7-sql_mode
    根据ibd文件进行数据恢复或导入
  • 原文地址:https://www.cnblogs.com/bai-jimmy/p/3707311.html
Copyright © 2011-2022 走看看