Modern PHP interface 接口

本文展示了如何使用PHP接口实现文档的处理与存储,包括HTML文档、流文档和命令输出文档,并通过一个示例代码说明了实现过程。

The right way

/dev/hell Code

Response.php

 

接口 demo:

modern-php/
├── data
│   └── stream.txt
└── interface
├── CommandOutputDocument.php
├── DocumentStore.php
├── Documentable.php
├── HtmlDocument.php
├── StreamDocument.php
└── index.php

 

// interface

<?php

interface Documentable {
    public function getId();

    public function getContent();
}
Documentable.php

 

//  class x 3 implements interface

<?php
require_once __DIR__ . '/Documentable.php';

class HtmlDocument implements Documentable {
    protected $url;

    public function __construct($url) {
        $this->url = $url;
    }

    public function getId() {
        return $this->url;
    }

    /**
     * @return string
     */
    public function getContent() {
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL => $this->url,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_CONNECTTIMEOUT => 3,
            CURLOPT_FOLLOWLOCATION => 1,
            CURLOPT_MAXREDIRS => 3
        ]);
        $html = curl_exec($ch);
        curl_close($ch);
        return $html;
    }
}
HtmlDocument.php
 1 <?php
 2 require_once __DIR__ . '/Documentable.php';
 3 
 4 class StreamDocument implements Documentable {
 5 
 6     protected $resource;
 7     protected $buffer;
 8 
 9     public function __construct($resource, $buffer = 4096) {
10         $this->resource = $resource;
11         $this->buffer = $buffer;
12     }
13 
14     public function getId() {
15         return 'resource-' .(int)$this->resource;
16     }
17 
18     public function getContent() {
19         $streamContent = '';
20         rewind($this->resource);
21         while (feof($this->resource) === false) {
22             $streamContent .= fread($this->resource, $this->buffer);
23         }
24         return $streamContent;
25     }
26 }
StreamDocument.php
 1 <?php
 2 require_once __DIR__ . '/Documentable.php';
 3 
 4 class CommandOutputDocument implements Documentable {
 5 
 6     protected $command;
 7 
 8     public function __construct($command) {
 9         $this->command = $command;
10     }
11 
12     public function getId() {
13         return $this->command;
14     }
15 
16     public function getContent() {
17         return shell_exec($this->command);
18     }
19 }
CommandOutputDocument.php

 // Container

 1 <?php
 2 include 'Documentable.php';
 3 
 4 class DocumentStore {
 5     protected $data = [];
 6 
 7     public function addDocument(Documentable $document) {
 8         $key = $document->getId();
 9         $value = $document->getContent();
10         $this->data[$key] = $value;
11     }
12 
13     public function getDocuemnts() {
14         return $this->data;
15     }
16 }
DocumentStore.php

 

// Usage:

<?php
include 'DocumentStore.php';

include 'HtmlDocument.php';
include 'StreamDocument.php';
include 'CommandOutputDocument.php';

$documentStore = new DocumentStore();

$htmlDoc = new HtmlDocument('https://php.net');
$documentStore->addDocument($htmlDoc);

$streamDoc = new StreamDocument(fopen('../data/stream.txt', "rb"));
$documentStore->addDocument($streamDoc);

$cmdDoc = new CommandOutputDocument('cat /etc/hosts');
$documentStore->addDocument($cmdDoc);

print_r($documentStore->getDocuemnts());
index.php

 

转载于:https://www.cnblogs.com/mingzhanghui/p/9312038.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值