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

最新文章

  1. win10 右键菜单添加Git Hash Here
  2. mysql 远程连接数据库的二种方法
  3. mavan 常用命令
  4. Xcode 的ARC转化功能以及跟非ARC共存方法
  5. js屏蔽回车键
  6. 使用 FileZilla FTP Client连接Vsftpd在执行LIST命令后提示连接超时
  7. linux下crontab实现定时服务详解
  8. 恒天云单节点部署指南--OpenStack H版本虚拟机单节点部署解决方案
  9. Spark Tungsten揭秘 Day4 内存和CPU优化使用
  10. 使用SALT-API进入集成开发的简单样例
  11. [条款36]绝不重新定义继承而来的non-virtual函数
  12. js chart
  13. margin:0px auto和text-align:center区别
  14. pycharm的快捷键
  15. django建立管理系统之五----单页ajax数据交互
  16. request获取各种路径
  17. mysql基础理论
  18. LeetCode(283. 移动零)
  19. web项目上传图片需要刷新文件夹才能显示
  20. flash初步尝试

热门文章

  1. Innodb中怎么查看锁信息
  2. miniFTP项目实战四
  3. STM32—驱动RFID-RC522模块
  4. SpringBoot - 集成RocketMQ实现延迟消息队列
  5. C# volatile 的使用
  6. jQuery中ajax请求的六种方法(三、五):$.getScript()方法
  7. 一些Java知识点
  8. mybaits源码分析(一)
  9. 你知道 JavaScript 中的 Arguments 对象都有哪些用途吗?
  10. 读vue-cli3 官方文档的一些学习记录