PHP interface(介面)的示例程式碼

洋蔥土豆隨心匠發表於2016-11-26
<?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());

 

相關文章