Make ParseFromString, ParseFromFile, ParseFromStream static

This commit is contained in:
the-djmaze 2022-01-25 20:18:52 +01:00
parent e0238e9966
commit d757102b9e

View file

@ -22,8 +22,6 @@ class Part
const POS_SUBPARTS = 3; const POS_SUBPARTS = 3;
const POS_CLOSE_BOUNDARY = 4; const POS_CLOSE_BOUNDARY = 4;
const DEFAUL_BUFFER = 8192;
/** /**
* @var string * @var string
*/ */
@ -42,7 +40,7 @@ class Part
/** /**
* @var resource * @var resource
*/ */
public $Body; public $Body = null;
/** /**
* @var PartCollection * @var PartCollection
@ -50,43 +48,24 @@ class Part
public $SubParts; public $SubParts;
/** /**
* @var array * @var string
*/ */
public $LineParts; private $sBoundary = '';
/** /**
* @var string * @var string
*/ */
private $sBoundary; private $sParentCharset = \MailSo\Base\Enumerations\Charset::ISO_8859_1;
/**
* @var string
*/
private $sParentCharset;
/**
* @var int
*/
private $iParseBuffer;
function __construct() function __construct()
{ {
$this->iParseBuffer = self::DEFAUL_BUFFER;
$this->Reset();
}
public function Reset() : self
{
\MailSo\Base\ResourceRegistry::CloseMemoryResource($this->Body);
$this->Body = null;
$this->Headers = new HeaderCollection; $this->Headers = new HeaderCollection;
$this->SubParts = new PartCollection; $this->SubParts = new PartCollection;
$this->LineParts = array(); }
$this->sBoundary = '';
$this->sParentCharset = \MailSo\Base\Enumerations\Charset::ISO_8859_1;
return $this; function __destruct()
{
\MailSo\Base\ResourceRegistry::CloseMemoryResource($this->Body);
} }
public function Boundary() : string public function Boundary() : string
@ -113,13 +92,6 @@ class Part
return $this; return $this;
} }
public function SetParseBuffer(int $iParseBuffer) : self
{
$this->iParseBuffer = $iParseBuffer;
return $this;
}
public function HeaderCharset() : string public function HeaderCharset() : string
{ {
return ($this->Headers) ? \trim(\strtolower($this->Headers->ParameterValue( return ($this->Headers) ? \trim(\strtolower($this->Headers->ParameterValue(
@ -191,47 +163,48 @@ class Part
return $sResult; return $sResult;
} }
public function ParseFromFile(string $sFileName) : self public static function FromFile(string $sFileName) : ?self
{ {
$rStreamHandle = \file_exists($sFileName) ? \fopen($sFileName, 'rb') : false; $rStreamHandle = \file_exists($sFileName) ? \fopen($sFileName, 'rb') : false;
if (\is_resource($rStreamHandle)) if ($rStreamHandle) {
{ try {
$this->ParseFromStream($rStreamHandle); return static::FromStream($rStreamHandle);
} finally {
if (\is_resource($rStreamHandle))
{
\fclose($rStreamHandle); \fclose($rStreamHandle);
} }
} }
return null;
return $this;
} }
public function ParseFromString(string $sRawMessage) : self public static function FromString(string $sRawMessage) : ?self
{ {
$rStreamHandle = \strlen($sRawMessage) ? $rStreamHandle = \strlen($sRawMessage) ?
\MailSo\Base\ResourceRegistry::CreateMemoryResource() : false; \MailSo\Base\ResourceRegistry::CreateMemoryResource() : false;
if ($rStreamHandle) {
if (\is_resource($rStreamHandle))
{
\fwrite($rStreamHandle, $sRawMessage); \fwrite($rStreamHandle, $sRawMessage);
unset($sRawMessage); unset($sRawMessage);
\fseek($rStreamHandle, 0); \fseek($rStreamHandle, 0);
$this->ParseFromStream($rStreamHandle); try {
return static::FromStream($rStreamHandle);
\MailSo\Base\ResourceRegistry::CloseMemoryResource($rStreamHandle); } finally {
\MailSo\Base\ResourceRegistry::CloseMemoryResource($rStreamHandle);
}
} }
return null;
return $this;
} }
/** /**
* @param resource $rStreamHandle * @param resource $rStreamHandle
*/ */
public function ParseFromStream($rStreamHandle) : self public $LineParts = [];
public static function FromStream($rStreamHandle) : ?self
{ {
$this->Reset(); if (!\is_resource($rStreamHandle)) {
return null;
}
$self = new self;
$oParserClass = new Parser\ParserMemory; $oParserClass = new Parser\ParserMemory;
@ -243,14 +216,14 @@ class Part
$aBoundaryStack = array(); $aBoundaryStack = array();
$oParserClass->StartParse($this); $oParserClass->StartParse($self);
$this->LineParts[] =& $this; $self->LineParts[] =& $self;
$this->ParseFromStreamRecursion($rStreamHandle, $oParserClass, $iOffset, $self->ParseFromStreamRecursion($rStreamHandle, $oParserClass, $iOffset,
$sPrevBuffer, $sBuffer, $aBoundaryStack, $bIsOef); $sPrevBuffer, $sBuffer, $aBoundaryStack, $bIsOef);
$sFirstNotNullCharset = null; $sFirstNotNullCharset = null;
foreach ($this->LineParts as /* @var $oMimePart Part */ $oMimePart) foreach ($self->LineParts as /* @var $oMimePart Part */ $oMimePart)
{ {
$sCharset = $oMimePart->HeaderCharset(); $sCharset = $oMimePart->HeaderCharset();
if (\strlen($sCharset)) if (\strlen($sCharset))
@ -263,7 +236,7 @@ class Part
$sForceCharset = self::$ForceCharset; $sForceCharset = self::$ForceCharset;
if (\strlen($sForceCharset)) if (\strlen($sForceCharset))
{ {
foreach ($this->LineParts as /* @var $oMimePart Part */ $oMimePart) foreach ($self->LineParts as /* @var $oMimePart Part */ $oMimePart)
{ {
$oMimePart->SetParentCharset($sForceCharset); $oMimePart->SetParentCharset($sForceCharset);
$oMimePart->Headers->SetParentCharset($sForceCharset); $oMimePart->Headers->SetParentCharset($sForceCharset);
@ -274,7 +247,7 @@ class Part
$sFirstNotNullCharset = (null !== $sFirstNotNullCharset) $sFirstNotNullCharset = (null !== $sFirstNotNullCharset)
? $sFirstNotNullCharset : self::$DefaultCharset; ? $sFirstNotNullCharset : self::$DefaultCharset;
foreach ($this->LineParts as /* @var $oMimePart Part */ $oMimePart) foreach ($self->LineParts as /* @var $oMimePart Part */ $oMimePart)
{ {
$sHeaderCharset = $oMimePart->HeaderCharset(); $sHeaderCharset = $oMimePart->HeaderCharset();
$oMimePart->SetParentCharset((\strlen($sHeaderCharset)) ? $sHeaderCharset : $sFirstNotNullCharset); $oMimePart->SetParentCharset((\strlen($sHeaderCharset)) ? $sHeaderCharset : $sFirstNotNullCharset);
@ -282,15 +255,16 @@ class Part
} }
} }
$oParserClass->EndParse($this); $oParserClass->EndParse($self);
return $this; $self->LineParts = [];
return $self;
} }
/** /**
* @param resource $rStreamHandle * @param resource $rStreamHandle
*/ */
public function ParseFromStreamRecursion($rStreamHandle, $oCallbackClass, int &$iOffset, protected function ParseFromStreamRecursion($rStreamHandle, $oCallbackClass, int &$iOffset,
string &$sPrevBuffer, string &$sBuffer, array &$aBoundaryStack, bool &$bIsOef, bool $bNotFirstRead = false) : self string &$sPrevBuffer, string &$sBuffer, array &$aBoundaryStack, bool &$bIsOef, bool $bNotFirstRead = false) : self
{ {
$oCallbackClass->StartParseMimePart($this); $oCallbackClass->StartParseMimePart($this);
@ -312,7 +286,7 @@ class Part
{ {
if (!$bNotFirstRead) if (!$bNotFirstRead)
{ {
$sBuffer = \fread($rStreamHandle, $this->iParseBuffer); $sBuffer = \fread($rStreamHandle, 8192);
if (false === $sBuffer) if (false === $sBuffer)
{ {
break; break;
@ -479,9 +453,7 @@ class Part
$oSubPart = new self; $oSubPart = new self;
$oSubPart $oSubPart->ParseFromStreamRecursion($rStreamHandle, $oCallbackClass,
->SetParseBuffer($this->iParseBuffer)
->ParseFromStreamRecursion($rStreamHandle, $oCallbackClass,
$iOffset, $sPrevBuffer, $sBuffer, $aBoundaryStack, $bIsOef, true); $iOffset, $sPrevBuffer, $sBuffer, $aBoundaryStack, $bIsOef, true);
$this->SubParts->append($oSubPart); $this->SubParts->append($oSubPart);
@ -539,7 +511,7 @@ class Part
/** /**
* @return resource * @return resource
*/ */
public function Rewind() public function ToStream()
{ {
if ($this->Body && \is_resource($this->Body)) if ($this->Body && \is_resource($this->Body))
{ {
@ -549,14 +521,6 @@ class Part
\rewind($this->Body); \rewind($this->Body);
} }
} }
}
/**
* @return resource
*/
public function ToStream()
{
$this->Rewind();
$aSubStreams = array( $aSubStreams = array(