码迷,mamicode.com
首页 > Web开发 > 详细

PHP interface(接口)的示例代码

时间:2016-11-26 23:11:57      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:host   url   []   log   top   https   comm   nec   bsp   

<?php
class DocumentStore
{
    protected $data = [];
    
    public function addDocument(Documentable $document)
    {
        $key = $document->getId();
        $value = $document->getContent();
        $this->data[$key] = $value;
    }
    
    public function getDocuments()
    {
        return $this->data;
    }
    
    
}

interface Documentable
{
    public function getId();
    
    public function getContent();
}

class HtmlDocument implements Documentable
{
    protected $url;
    
    public function __construct($url)
    {
        $this->url = $url;
    }
    
    public function getId()
    {
        return $this->url;
    }
    
    public function getContent()
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
        $html = curl_exec($ch);
        curl_close($ch);
        
        return $html;
    }
}

class StreamDocument implements Documentable
{
    protected $resource;
    protected $buffer;
    
    public function __construct($resource, $buffer = 4096)
    {
        $this->resource = $resource;
        $this->buffer = $buffer;
    }
    
    public function getId()
    {
        return ‘resource-‘ . (int)$this->resource;
    }
    
    public function getContent()
    {
        $streamContent = ‘‘;
        rewind($this->resource);
        while (feof($this->resource) === false){
            $streamContent .= fread($this->resource, $this->buffer);
        }
        
        return $streamContent;
    }
}

class CommandOutputDocument implements Documentable
{
    protected $command;
    
    public function __construct($command)
    {
        $this->command = $command;
    }
    
    public function getId()
    {
        return $this->command;
    }
    
    public function getContent()
    {
        return shell_exec($this->command);
    }
}


$documentStore = new DocumentStore();

//添加HTML文档
$htmlDoc = new HtmlDocument(‘https://php.net‘);
$documentStore->addDocument($htmlDoc);

//添加流文档
$streamDoc = new StreamDocument(fopen(‘stream.txt‘, ‘rb‘));
$documentStore->addDocument($streamDoc);

//添加终端命令文档
$cmdDoc = new CommandOutputDocument(‘cat /etc/hosts‘);
$documentStore->addDocument($cmdDoc);

print_r($documentStore->getDocuments());

 

PHP interface(接口)的示例代码

标签:host   url   []   log   top   https   comm   nec   bsp   

原文地址:http://www.cnblogs.com/yangcclg/p/6105367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!