Browse files

Drop PEAR for SPL exceptions

  • Loading branch information...
1 parent 928429b commit 7a6b6890c49916e7e48724de9e14d058098e90e9 @CloCkWeRX CloCkWeRX committed Apr 10, 2012
Showing with 25 additions and 57 deletions.
  1. +15 −32 Mail/Mime2.php
  2. +10 −25 Mail/MimePart2.php
View
47 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
*
@@ -425,8 +417,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);
}
$filename = self::_basename($filename);
@@ -456,21 +447,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
@@ -750,13 +739,13 @@ public function getMessageBody($params = null)
* @return mixed True or PEAR error object
* @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
@@ -765,14 +754,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;
}
@@ -795,15 +784,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;
}
@@ -813,8 +803,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
@@ -834,7 +823,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)
@@ -1011,16 +1000,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
35 Mail/MimePart2.php
@@ -264,7 +264,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)
@@ -282,9 +282,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;
}
@@ -305,9 +302,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'] = '';
@@ -328,20 +322,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
@@ -357,7 +349,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;
}
/**
@@ -367,7 +359,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)
@@ -392,9 +384,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;
}
@@ -407,9 +396,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;
@@ -472,19 +458,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 7a6b689

Please sign in to comment.