Skip to content
Browse files

Merge pull request #4 from CloCkWeRX/master

Swap to SPL Exceptions
  • Loading branch information...
2 parents 63186aa + cc18fe6 commit 3e6e1d9f3d80d47f13c7e802c3d144c85492cc6c @CloCkWeRX CloCkWeRX committed
Showing with 32 additions and 79 deletions.
  1. +22 −53 Mail/Mime2.php
  2. +10 −26 Mail/MimePart2.php
View
75 Mail/Mime2.php
@@ -57,14 +57,6 @@
* and Sascha Schumann <sascha@schumann.cx>
*/
-
-/**
- * require PEAR
- *
- * This package depends on PEAR to raise errors.
- */
-require_once 'PEAR.php';
-
/**
* require Mail_MimePart2
*
@@ -231,7 +223,7 @@ public function getParam($name)
* the existing body, else the old body is
* overwritten
*
- * @return mixed True on success or PEAR_Error object
+ * @return mixed True on success
* @access public
*/
public function setTXTBody($data, $isfile = false, $append = false)
@@ -244,9 +236,6 @@ public function setTXTBody($data, $isfile = false, $append = false)
}
} else {
$cont = $this->_file2str($data);
- if (PEAR::isError($cont)) {
- return $cont;
- }
if (!$append) {
$this->_txtbody = $cont;
} else {
@@ -285,9 +274,6 @@ public function setHTMLBody($data, $isfile = false)
$this->_htmlbody = $data;
} else {
$cont = $this->_file2str($data);
- if (PEAR::isError($cont)) {
- return $cont;
- }
$this->_htmlbody = $cont;
}
@@ -335,9 +321,7 @@ public function addHTMLImage($file,
$filedata = null;
$bodyfile = $file;
} else {
- if (PEAR::isError($filedata = $this->_file2str($file))) {
- return $filedata;
- }
+ $filedata = $this->_file2str($file);
}
$filename = ($name ? $name : $file);
} else {
@@ -389,7 +373,7 @@ public function addHTMLImage($file,
* @param array $add_headers Additional part headers. Array keys can be in form
* of <header_name>:<parameter_name>
*
- * @return mixed True on success or PEAR_Error object
+ * @return mixed True on success
* @access public
*/
public function addAttachment($file,
@@ -415,9 +399,7 @@ public function addAttachment($file,
$filedata = null;
$bodyfile = $file;
} else {
- if (PEAR::isError($filedata = $this->_file2str($file))) {
- return $filedata;
- }
+ $filedata = $this->_file2str($file);
}
// Force the name the user supplied, otherwise use $file
$filename = ($name ? $name : self::_basename($file));
@@ -428,8 +410,7 @@ public function addAttachment($file,
if (!strlen($filename)) {
$msg = "The supplied filename for the attachment can't be empty";
- $err = PEAR::raiseError($msg);
- return $err;
+ throw new InvalidArgumentException($msg);
}
$this->_parts[] = array(
@@ -459,21 +440,19 @@ public function addAttachment($file,
*
* @return string Contents of $file_name
* @access private
+ * @throws InvalidArgumentException
*/
private function _file2str($file_name)
{
// Check state of file and raise an error properly
if (!file_exists($file_name)) {
- $err = PEAR::raiseError('File not found: ' . $file_name);
- return $err;
+ throw new InvalidArgumentException('File not found: ' . $file_name);
}
if (!is_file($file_name)) {
- $err = PEAR::raiseError('Not a regular file: ' . $file_name);
- return $err;
+ throw new InvalidArgumentException('Not a regular file: ' . $file_name);
}
if (!is_readable($file_name)) {
- $err = PEAR::raiseError('File is not readable: ' . $file_name);
- return $err;
+ throw new InvalidArgumentException('File is not readable: ' . $file_name);
}
// Temporarily reset magic_quotes_runtime and read file contents
@@ -705,7 +684,7 @@ private function _addAttachmentPart($obj, $value)
* See that function for more info.
* @param bool $overwrite Overwrite the existing headers with new.
*
- * @return mixed The complete e-mail or PEAR error object
+ * @return mixed The complete e-mail
* @access public
*/
public function getMessage($separation = null, $params = null, $headers = null,
@@ -717,10 +696,6 @@ public function getMessage($separation = null, $params = null, $headers = null,
$body = $this->get($params);
- if (PEAR::isError($body)) {
- return $body;
- }
-
$head = $this->txtHeaders($headers, $overwrite);
$mail = $head . $separation . $body;
return $mail;
@@ -733,7 +708,7 @@ public function getMessage($separation = null, $params = null, $headers = null,
* @param array $params The Build parameters passed to the
* &get() function. See &get for more info.
*
- * @return mixed The e-mail body or PEAR error object
+ * @return mixed The e-mail body
* @access public
* @since 1.6.0
*/
@@ -753,16 +728,16 @@ public function getMessageBody($params = null)
* See that function for more info.
* @param bool $overwrite Overwrite the existing headers with new.
*
- * @return mixed True or PEAR error object
+ * @return mixed True
* @access public
* @since 1.6.0
+ * @throws InvalidArgumentException
*/
public function saveMessage($filename, $params = null, $headers = null, $overwrite = false)
{
// Check state of file and raise an error properly
if (file_exists($filename) && !is_writable($filename)) {
- $err = PEAR::raiseError('File is not writable: ' . $filename);
- return $err;
+ throw new InvalidArgumentException('File is not writable: ' . $filename);
}
// Temporarily reset magic_quotes_runtime and read file contents
@@ -771,14 +746,14 @@ public function saveMessage($filename, $params = null, $headers = null, $overwri
}
if (!($fh = fopen($filename, 'ab'))) {
- $err = PEAR::raiseError('Unable to open file: ' . $filename);
- return $err;
+ throw new InvalidArgumentException('Unable to open file: ' . $filename);
}
// Write message headers into file (skipping Content-* headers)
$head = $this->txtHeaders($headers, $overwrite, true);
if (fwrite($fh, $head) === false) {
- $err = PEAR::raiseError('Error writing to file: ' . $filename);
+ /** @todo Better exception? */
+ throw new InvalidArgumentException('Error writing to file: ' . $filename);
return $err;
}
@@ -801,15 +776,16 @@ public function saveMessage($filename, $params = null, $headers = null, $overwri
* @param array $params The Build parameters passed to the
* &get() function. See &get for more info.
*
- * @return mixed True or PEAR error object
+ * @return mixed True
* @access public
* @since 1.6.0
+ * @throws InvalidArgumentException
*/
public function saveMessageBody($filename, $params = null)
{
// Check state of file and raise an error properly
if (file_exists($filename) && !is_writable($filename)) {
- $err = PEAR::raiseError('File is not writable: ' . $filename);
+ throw new InvalidArgumentException('File is not writable: ' . $filename);
return $err;
}
@@ -819,8 +795,7 @@ public function saveMessageBody($filename, $params = null)
}
if (!($fh = fopen($filename, 'ab'))) {
- $err = PEAR::raiseError('Unable to open file: ' . $filename);
- return $err;
+ throw new InvalidArgumentException('Unable to open file: ' . $filename);
}
// Write the rest of the message into file
@@ -840,7 +815,7 @@ public function saveMessageBody($filename, $params = null)
* @param boolean $skip_head True if you want to return/save only the message
* without headers
*
- * @return mixed The MIME message content string, null or PEAR error object
+ * @return mixed The MIME message content string, null
* @access public
*/
public function get($params = null, $filename = null, $skip_head = false)
@@ -1017,16 +992,10 @@ public function get($params = null, $filename = null, $skip_head = false)
if ($filename) {
// Append mimePart message headers and body into file
$headers = $message->encodeToFile($filename, $boundary, $skip_head);
- if (PEAR::isError($headers)) {
- return $headers;
- }
$this->_headers = array_merge($this->_headers, $headers);
return null;
} else {
$output = $message->encode($boundary, $skip_head);
- if (PEAR::isError($output)) {
- return $output;
- }
$this->_headers = array_merge($this->_headers, $output['headers']);
$body = $output['body'];
return $body;
View
36 Mail/MimePart2.php
@@ -52,7 +52,6 @@
* @link http://pear.php.net/package/Mail_Mime2
*/
-
/**
* The Mail_MimePart2 class is used to create MIME E-mail messages
*
@@ -292,7 +291,7 @@ public function __construct($body = '', $params = array())
*
* @return An associative array containing two elements,
* body and headers. The headers element is itself
- * an indexed array. On error returns PEAR error object.
+ * an indexed array.
* @access public
*/
public function encode($boundary=null)
@@ -310,9 +309,6 @@ public function encode($boundary=null)
for ($i = 0; $i < count($this->_subparts); $i++) {
$encoded['body'] .= '--' . $boundary . $eol;
$tmp = $this->_subparts[$i]->encode();
- if (PEAR::isError($tmp)) {
- return $tmp;
- }
foreach ($tmp['headers'] as $key => $value) {
$encoded['body'] .= $key . ': ' . $value . $eol;
}
@@ -333,9 +329,6 @@ public function encode($boundary=null)
@ini_set('magic_quotes_runtime', $magic_quote_setting);
}
- if (PEAR::isError($body)) {
- return $body;
- }
$encoded['body'] = $body;
} else {
$encoded['body'] = '';
@@ -356,20 +349,18 @@ public function encode($boundary=null)
* @param boolean $skip_head True if you don't want to save headers
*
* @return array An associative array containing message headers
- * or PEAR error object
+ * @throws InvalidArgumentException
* @access public
* @since 1.6.0
*/
public function encodeToFile($filename, $boundary=null, $skip_head=false)
{
if (file_exists($filename) && !is_writable($filename)) {
- $err = PEAR::raiseError('File is not writeable: ' . $filename);
- return $err;
+ throw new InvalidArgumentException('File is not writeable: ' . $filename);
}
if (!($fh = fopen($filename, 'ab'))) {
- $err = PEAR::raiseError('Unable to open file: ' . $filename);
- return $err;
+ throw new InvalidArgumentException('Unable to open file: ' . $filename);
}
// Temporarily reset magic_quotes_runtime for file reads and writes
@@ -385,7 +376,7 @@ public function encodeToFile($filename, $boundary=null, $skip_head=false)
@ini_set('magic_quotes_runtime', $magic_quote_setting);
}
- return PEAR::isError($res) ? $res : $this->_headers;
+ return $this->_headers;
}
/**
@@ -395,7 +386,7 @@ public function encodeToFile($filename, $boundary=null, $skip_head=false)
* @param string $boundary Pre-defined boundary string
* @param boolean $skip_head True if you don't want to save headers
*
- * @return array True on sucess or PEAR error object
+ * @return array True on sucess
* @access private
*/
private function _encodePartToFile($fh, $boundary=null, $skip_head=false)
@@ -420,9 +411,6 @@ private function _encodePartToFile($fh, $boundary=null, $skip_head=false)
for ($i = 0; $i < count($this->_subparts); $i++) {
fwrite($fh, $f_eol . '--' . $boundary . $eol);
$res = $this->_subparts[$i]->_encodePartToFile($fh);
- if (PEAR::isError($res)) {
- return $res;
- }
$f_eol = $eol;
}
@@ -435,9 +423,6 @@ private function _encodePartToFile($fh, $boundary=null, $skip_head=false)
$res = $this->_getEncodedDataFromFile(
$this->_body_file, $this->_encoding, $fh
);
- if (PEAR::isError($res)) {
- return $res;
- }
}
return true;
@@ -500,19 +485,18 @@ private function _getEncodedData($data, $encoding)
* @param resource $fh Output file handle. If set, data will be
* stored into it instead of returning it
*
- * @return string Encoded data or PEAR error object
+ * @return string Encoded data
* @access private
+ * @throws InvalidArgumentException
*/
private function _getEncodedDataFromFile($filename, $encoding, $fh=null)
{
if (!is_readable($filename)) {
- $err = PEAR::raiseError('Unable to read file: ' . $filename);
- return $err;
+ throw new InvalidArgumentException('Unable to read file: ' . $filename);
}
if (!($fd = fopen($filename, 'rb'))) {
- $err = PEAR::raiseError('Could not open file: ' . $filename);
- return $err;
+ throw new InvalidArgumentException('Could not open file: ' . $filename);
}
$data = '';

0 comments on commit 3e6e1d9

Please sign in to comment.
Something went wrong with that request. Please try again.