<?PHPclassdocumentstore{protected $data= []; Public functionAdddocument (documentable$document) { $key=$document-getId (); $value=$document-getcontent (); $this->data[$key] =$value; } Public functiongetdocuments () {return $this-data; } }Interfacedocumentable{ Public functiongetId (); Public functiongetcontent ();}classHTMLDocumentImplementsdocumentable{protected $url; Public function__construct ($url) { $this->url =$url; } Public functiongetId () {return $this-URL; } Public functiongetcontent () {$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; }}classStreamdocumentImplementsdocumentable{protected $resource; protected $buffer; Public function__construct ($resource,$buffer= 4096) { $this-Resource=$resource; $this->buffer =$buffer; } Public functiongetId () {return' resource-'. (int)$this-Resource; } Public functiongetcontent () {$streamContent= ' '; Rewind($this-Resource); while(feof($this-Resource) ===false){ $streamContent.=fread($this-Resource,$this-buffer); } return $streamContent; }}classCommandoutputdocumentImplementsdocumentable{protected $command; Public function__construct ($command) { $this->command =$command; } Public functiongetId () {return $this-command; } Public functiongetcontent () {return shell_exec($this-command); }}$documentStore=NewDocumentstore ();//Add an HTML document$htmlDoc=NewHTMLDocument (' https://php.net ');$documentStore->adddocument ($htmlDoc);//Add a flow document$streamDoc=NewStreamdocument (fopen(' Stream.txt ', ' RB '));$documentStore->adddocument ($streamDoc);//Add terminal commands document$cmdDoc=NewCommandoutputdocument (' cat/etc/hosts ');$documentStore->adddocument ($cmdDoc);Print_r($documentStore->getdocuments ());
Sample code for PHP Interface (interface)