[Resolvido] [Classe]textStream
Um exemplo que fiz para um amigo e que acho que pode sanar certas dúvidas.
Não documentado e relativamente feio,mas eis:
>
textStream.interface.php:
<?php
interface iTextStream {
public function copy(textStream $textStream);
public function setStream($stream);
public function setStreamMode($streamMode);
public function appendToStreamContent($content);
public function getStream();
public function getStreamMode();
public function getStreamContent();
public function open();
public function close();
public function clear();
public function flush();
};
?>textStream.exceptionClass.php:
<?php
if(!defined('textStream_exceptionClass')){
define('textStream_exceptionClass', '');
class textStreamException extends Exception {
public function __toString(){
return '<font color=\'#ff0000\'>'.$this->getMessage().'</font>'.nl2br("\n");
}
};
}
?>textStream.class.php:
<?php
if(!defined('textStream_class')){
define('textStream_class', '');
include 'textStream.interface.php';
include 'textStream.exceptionClass.php';
class textStream implements iTextStream {
const endl = "<br />\n";
const developer = 'eibon';
private $streamHolder;
private $stream;
private $streamMode;
private $streamContent = '';
private $isFopen = 0;
public function __construct($stream = '', $streamMode = '', $openNow = false){
$this->setStream($stream);
$this->setStreamMode($streamMode);
if($openNow) $this->open();
}
public function copy(textStream $textStream){
if(!$this->isFopen()){
$this->setStream($textStream->getStream());
$this->setStreamMode($textStream->getStreamMode());
$this->streamContent = $textStream->getStreamContent();
} else {
$this->throwE(0);
}
}
public function setStream($stream){
if($this->isFopen)
$this->throwE(0);
if(is_string($stream) && $stream)
$this->stream = $stream;
}
public function setStreamMode($streamMode /*[=]input=0|output=1*/){
if($this->isFopen)
$this->throwE(0);
if($streamMode == 0 || $streamMode == 1){
$this->streamMode = $streamMode;
} else {
$this->throwE(1);
}
}
public function appendToStreamContent($content){
$this->streamContent .= $content;
}
public function getStream(){
return $this->stream;
}
public function getStreamMode(){
return $this->streamMode;
}
public function getStreamContent(){
if($this->streamMode == 0){
if(!$this-isFopen) $this->open();
if(!$this->streamContent = stream_get_contents($this->streamHolder)){
$this->throwE(4);
}
}
return $this->streamContent;
}
public function open(){
if($this->check()){
$fopenMode = array('r', 'w');
if(!$this->isFopen){
if($this->streamHolder = fopen($this->stream, $fopenMode[$this->streamMode])){
$this->isFopen = 1;
return null;
}
} else { return null; }
}
$this->throwE(2);
}
public function close(){
if($this->isFopen){
fclose($this->streamHolder);
$this->isFopen = 0;
}
}
public function clear(){
$this->streamContent = '';
}
public function flush(){
if($this->streamMode === 1){
if(!fwrite($this->streamHolder, $this->streamContent)){
$this->throwE(3);
}
}
$this->clear();
}
public function check(){
if(trim($this->stream) != '' && $this->streamMode == 0 || $this->streamMode == 1)
return true;
return false;
}
public function isFopen(){
return $this->isFopen;
}
private function throwE($eNumber){
$exceptionsMessages = array();
$exceptionMessages[] = 'A stream deve estar fechada para se setar novos parametros.';
$exceptionMessages[] = 'Stream mode inválido.'.nl2br("\n").'[0 - input | 1 - output]';
$exceptionMessages[] = 'Não conseguiu abrir a stream \''.$this->stream.'\' no modo \''.$this->streamMode.'\'.';
$exceptionMessages[] = 'Não conseguiu escrever na stream '.$this->stream.'.';
$exceptionMessages[] = 'Não conseguiu ler a stream '.$this->stream.'.';
throw new textStreamException($exceptionMessages[$eNumber]);
}
public function __destruct(){
$this->close();
}
}
}
?>eibonTextStream.class.php:
<?php
if(!defined('eibonTextStream_class')){
define('eibonTextStream_class', '');
include 'textStream.class.php';
class eibonTextStream extends textStream {
public function __construct($stream = '', $streamMode = '', $openNow = false){
parent::__construct($stream, $streamMode, $openNow);
}
public function __sl($string){
parent::appendToStreamContent($string);
}
public function __sr(&$string){
$string = parent::getStreamContent();
}
public function __destruct(){
parent::__destruct();
}
}
}
?>exemplo.php:
<?php
include 'textStream.class.php';
include 'textStream.exceptionClass.php';
include 'eibonTextStream.class.php';
//implemente isso se quiser um errorHandler
function handleError($errno, $errstr, $errfile, $errline){
return true;
}
set_error_handler('handleError');
try {
$textStream = new eibonTextStream('php://output', 1, true);
$textStream->__sl('eibonTextStream class ');
if(extension_loaded('operator')){
$textStream << 'with operator \'<<\' overloaded'.eibonTextStream::endl;
}
$textStream->flush();} catch(textStreamException $e){
echo $e;
exit(0);
}
?>
Abraço!
Discussão (0)
Carregando comentários...