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());
    ?>
  • 相关阅读:
    Logback的配置说明
    BAT批处理修改MSDOS窗口标题、大小、字体及背景颜色
    Java修饰符public,private,protected及默认的区别
    Class loading problem regarding ear's lib directory
    MySQL开发规范
    学习如何用自己的 appender 来扩展 log4j 框架
    指令重排、内存屏障概念解析
    高性能线程间队列 DISRUPTOR 简介
    Log4j2的性能为什么这么好?
    java多线程
  • 原文地址:https://www.cnblogs.com/bai-jimmy/p/3707311.html
Copyright © 2011-2022 走看看