1. <?php
  2. class DocumentStore
  3. {
  4. protected $data = [];
  5.  
  6. public function addDocument(Documentable $document)
  7. {
  8. $key = $document->getId();
  9. $value = $document->getContent();
  10. $this->data[$key] = $value;
  11. }
  12.  
  13. public function getDocuments()
  14. {
  15. return $this->data;
  16. }
  17.  
  18. }
  19.  
  20. interface Documentable
  21. {
  22. public function getId();
  23.  
  24. public function getContent();
  25. }
  26.  
  27. class HtmlDocument implements Documentable
  28. {
  29. protected $url;
  30.  
  31. public function __construct($url)
  32. {
  33. $this->url = $url;
  34. }
  35.  
  36. public function getId()
  37. {
  38. return $this->url;
  39. }
  40.  
  41. public function getContent()
  42. {
  43. $ch = curl_init();
  44. curl_setopt($ch, CURLOPT_URL, $this->url);
  45. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  46. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  47. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  48. curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
  49. $html = curl_exec($ch);
  50. curl_close($ch);
  51.  
  52. return $html;
  53. }
  54. }
  55.  
  56. class StreamDocument implements Documentable
  57. {
  58. protected $resource;
  59. protected $buffer;
  60.  
  61. public function __construct($resource, $buffer = 4096)
  62. {
  63. $this->resource = $resource;
  64. $this->buffer = $buffer;
  65. }
  66.  
  67. public function getId()
  68. {
  69. return 'resource-' . (int)$this->resource;
  70. }
  71.  
  72. public function getContent()
  73. {
  74. $streamContent = '';
  75. rewind($this->resource);
  76. while (feof($this->resource) === false){
  77. $streamContent .= fread($this->resource, $this->buffer);
  78. }
  79.  
  80. return $streamContent;
  81. }
  82. }
  83.  
  84. class CommandOutputDocument implements Documentable
  85. {
  86. protected $command;
  87.  
  88. public function __construct($command)
  89. {
  90. $this->command = $command;
  91. }
  92.  
  93. public function getId()
  94. {
  95. return $this->command;
  96. }
  97.  
  98. public function getContent()
  99. {
  100. return shell_exec($this->command);
  101. }
  102. }
  103.  
  104. $documentStore = new DocumentStore();
  105.  
  106. //添加HTML文档
  107. $htmlDoc = new HtmlDocument('https://php.net');
  108. $documentStore->addDocument($htmlDoc);
  109.  
  110. //添加流文档
  111. $streamDoc = new StreamDocument(fopen('stream.txt', 'rb'));
  112. $documentStore->addDocument($streamDoc);
  113.  
  114. //添加终端命令文档
  115. $cmdDoc = new CommandOutputDocument('cat /etc/hosts');
  116. $documentStore->addDocument($cmdDoc);
  117.  
  118. print_r($documentStore->getDocuments());

PHP interface(接口)的示例代码的更多相关文章

  1. java对接申通下单接口示例代码

    上面是控制台示例代码 public class Sample{ private final static String URL = "http://order.sto-express.cn: ...

  2. wechat开发笔记之1.接口示例代码

    修改后的php示例代码! <?php /** * wechat php test */ //define your token define("TOKEN", "w ...

  3. C/C++ 开源库及示例代码

    C/C++ 开源库及示例代码 Table of Contents 说明 1 综合性的库 2 数据结构 & 算法 2.1 容器 2.1.1 标准容器 2.1.2 Lockfree 的容器 2.1 ...

  4. 左右JAVA示例代码事件分发和监督机制来实现-绝对原创有用

    文章标题:左右JAVA示例代码事件分发和监督机制来实现 文章地址: http://blog.csdn.net/5iasp/article/details/37054171 作者: javaboy201 ...

  5. 【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )

    作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50824912 相关地址介绍 : -- Universal I ...

  6. go interface接口

    一:接口概要 接口是一种重要的类型,他是一组确定的方法集合. 一个接口变量可以存储任何实现了接口方法的具体值.一个重要的例子就是io.Reader和io.Writer type Reader inte ...

  7. JDK静态代理示例代码

    JDK静态代理示例代码 业务接口 接口的实现类 代理类,实现接口,并扩展实现类的功能 1.业务接口 /** * 业务接口 * @author pc * */ public interface User ...

  8. 在ASP.NET Web API 2中使用Owin OAuth 刷新令牌(示例代码)

    在上篇文章介绍了Web Api中使用令牌进行授权的后端实现方法,基于WebApi2和OWIN OAuth实现了获取access token,使用token访问需授权的资源信息.本文将介绍在Web Ap ...

  9. Golang基础(8):go interface接口

    一:接口概要 接口是一种重要的类型,他是一组确定的方法集合. 一个接口变量可以存储任何实现了接口方法的具体值.一个重要的例子就是io.Reader和io.Writer type Reader inte ...

  10. 三、从GitHub浏览Prism示例代码的方式入门WPF下的Prism之Mvvm的08-12示例

    这一篇是学习了前2篇RegionManager关联视图,和通过不同的方式加载Module示例之后的开始进入MVVM了. 从第08示例开始,进入了MVVM部分. 从08示例开始学习Prism下的MVVM ...

随机推荐

  1. CSS之伪类

    1. :link                     向未被访问的链接添加样式 :visited                向已被访问的链接添加样式 :hover               ...

  2. AJAX发送参数到后台,前台火狐debug报undefine

    后面经过查找:估计是数据并不是Json格式,由于var PATIENT_ID=getIdSelections();其中PATIENT_ID是数组,所以必须转成字符串. $('#table').on(' ...

  3. async 和 await小结

    三大返回值: 返回类型 - Task<TResult> 返回类型 - Task 返回类型 - void 当你添加 async 关键字后,需要返回一个将用于后续操作的对象,请使用 Task& ...

  4. 使用jsonp跨域请求后可以获得数据,但是进入error方法,返回parseerror

    $.ajax({ url:url, dataType:'jsonp', jsonp: 'callback',//回调函数名字 jsonpCallback: 'success_jsonpCallback ...

  5. GCD的简单用法

    /* 创建一个队列用来执行任务,TA属于系统预定义的并行队列即全局队列,目前系统预定义了四个不同运行优先级的全局队列,我们可以通过dispatch_get_global_queue来获取它们 四种优先 ...

  6. centos7 搭建GlusterFS

    centos7 搭建GlusterFS 转载http://zhaijunming5.blog.51cto.com/10668883/1704535 实验需求:4台机器安装GlusterFS组成一个集群 ...

  7. laravel5.1学习(1)--安装

    主要学习的是laravel5.1版本,服务器用的是wampserver3.0.4集成环境: 首先,安装composer(windows系统) 下载地址:https://getcomposer.org/ ...

  8. margin属性

    可以设置position:absolute/relative/fixed,通过调节top/bottom/left/right实现元素的定位,这样很好,但是有时候想通过margin来实现. 下面是mar ...

  9. yii框架的理解

    Yii Framework是一个基于组件.用于开发大型 Web 应用的高性能 PHP 框架.Yii提供了今日Web 2.0应用开发所需要的几乎一切功能.Yii是最有效率的PHP框架之一. yii框架里 ...

  10. IOS 调用系统照相机和相册

    /** *  调用照相机 */ - (void)openCamera { UIImagePickerController *picker = [[UIImagePickerController all ...