Skip to content
This repository has been archived by the owner on Feb 4, 2021. It is now read-only.

Commit

Permalink
#315: Replace JError with PHP Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
allanpilarca committed Jan 27, 2017
1 parent e332526 commit 77723e7
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,9 @@ public function switchAdminLanguage($cid)
}
else
{
JError::raiseWarning(500, JText::_('COM_LANGUAGES_ERR_NO_LANGUAGE_SELECTED'));
JFactory::getApplication()->enqueueMessage(
JText::_('COM_LANGUAGES_ERR_NO_LANGUAGE_SELECTED'), 'error'
);

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function display($tpl = null)
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode("\n", $errors));
throw new Exception(implode("\n", $errors));

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ public function orderPosition()
}
catch (RuntimeException $e)
{
JError::raiseWarning(500, $e->getMessage());
JFactory::getApplication()->enqueueMessage(
$e->getMessage(), 'error'
);

return '';
}
Expand Down
2 changes: 1 addition & 1 deletion app/site/components/com_users/views/login/view.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function display($tpl = null)
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
throw new Exception(implode('<br />', $errors));

return false;
}
Expand Down
35 changes: 7 additions & 28 deletions lib/libraries/joomla/archive/bzip2.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function extract($archive, $destination, array $options = array())

if (!extension_loaded('bz2'))
{
$this->raiseWarning(100, 'The bz2 extension is not available.');
throw new RuntimeException('The bz2 extension is not available.');
}

if (isset($options['use_streams']) && $options['use_streams'] != false)
Expand All @@ -58,20 +58,20 @@ public function extract($archive, $destination, array $options = array())

if (!$this->_data)
{
return $this->raiseWarning(100, 'Unable to read archive');
throw new RuntimeException('Unable to read archive');
}

$buffer = bzdecompress($this->_data);
unset($this->_data);

if (empty($buffer))
{
return $this->raiseWarning(100, 'Unable to decompress data');
throw new RuntimeException('Unable to decompress data');
}

if (JFile::write($destination, $buffer) === false)
{
return $this->raiseWarning(100, 'Unable to write archive');
throw new RuntimeException('Unable to write archive');
}

return true;
Expand All @@ -96,7 +96,7 @@ protected function extractStream($archive, $destination, $options = array())

if (!$input->open($archive))
{
return $this->raiseWarning(100, 'Unable to read archive (bz2)');
throw new RuntimeException('Unable to read archive (bz2)');

}

Expand All @@ -106,7 +106,7 @@ protected function extractStream($archive, $destination, $options = array())
{
$input->close();

return $this->raiseWarning(100, 'Unable to write archive (bz2)');
throw new RuntimeException('Unable to write archive (bz2)');

}

Expand All @@ -118,7 +118,7 @@ protected function extractStream($archive, $destination, $options = array())
{
$input->close();

return $this->raiseWarning(100, 'Unable to write archive (bz2)');
throw new RuntimeException('Unable to write archive (bz2)');
}
}

Expand All @@ -130,27 +130,6 @@ protected function extractStream($archive, $destination, $options = array())
return true;
}

/**
* Temporary private method to isolate JError from the extract method
* This code should be removed when JError is removed.
*
* @param int $code The application-internal error code for this error
* @param string $msg The error message, which may also be shown the user if need be.
*
* @return JException JException instance if JError class exists
*
* @throws RuntimeException if JError class does not exist
*/
private function raiseWarning($code, $msg)
{
if (class_exists('JError'))
{
return JError::raiseWarning($code, $msg);
}

throw new RuntimeException($msg);
}

/**
* Tests whether this adapter can unpack files on this computer.
*
Expand Down
37 changes: 8 additions & 29 deletions lib/libraries/joomla/archive/gzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function extract($archive, $destination, array $options = array())

if (!extension_loaded('zlib'))
{
return $this->raiseWarning(100, 'The zlib extension is not available.');
throw new RuntimeException('The zlib extension is not available.');
}

if (isset($options['use_streams']) && $options['use_streams'] != false)
Expand All @@ -70,20 +70,20 @@ public function extract($archive, $destination, array $options = array())

if (!$this->_data)
{
return $this->raiseWarning(100, 'Unable to read archive');
throw new RuntimeException('Unable to read archive');
}

$position = $this->_getFilePosition();
$buffer = gzinflate(substr($this->_data, $position, strlen($this->_data) - $position));

if (empty($buffer))
{
return $this->raiseWarning(100, 'Unable to decompress data');
throw new RuntimeException('Unable to decompress data');
}

if (JFile::write($destination, $buffer) === false)
{
return $this->raiseWarning(100, 'Unable to write archive');
throw new RuntimeException('Unable to write archive');
}

return true;
Expand All @@ -108,7 +108,7 @@ protected function extractStream($archive, $destination, $options = array())

if (!$input->open($archive))
{
return $this->raiseWarning(100, 'Unable to read archive (gz)');
throw new RuntimeException('Unable to read archive (gz)');
}

$output = JFactory::getStream();
Expand All @@ -117,7 +117,7 @@ protected function extractStream($archive, $destination, $options = array())
{
$input->close();

return $this->raiseWarning(100, 'Unable to write archive (gz)');
throw new RuntimeException('Unable to write archive (gz)');
}

do
Expand All @@ -128,7 +128,7 @@ protected function extractStream($archive, $destination, $options = array())
{
$input->close();

return $this->raiseWarning(100, 'Unable to write file (gz)');
throw new RuntimeException('Unable to write file (gz)');
}
}

Expand All @@ -140,27 +140,6 @@ protected function extractStream($archive, $destination, $options = array())
return true;
}

/**
* Temporary private method to isolate JError from the extract method
* This code should be removed when JError is removed.
*
* @param int $code The application-internal error code for this error
* @param string $msg The error message, which may also be shown the user if need be.
*
* @return JException JException instance if JError class exists
*
* @throws RuntimeException if JError class does not exist
*/
private function raiseWarning($code, $msg)
{
if (class_exists('JError'))
{
return JError::raiseWarning($code, $msg);
}

throw new RuntimeException($msg);
}

/**
* Tests whether this adapter can unpack files on this computer.
*
Expand Down Expand Up @@ -189,7 +168,7 @@ public function _getFilePosition()

if (!$info)
{
return $this->raiseWarning(100, 'Unable to decompress data.');
throw new RuntimeException('Unable to decompress data.');
}

$position += 10;
Expand Down
47 changes: 13 additions & 34 deletions lib/libraries/joomla/archive/zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function extract($archive, $destination, array $options = array())
{
if (!is_file($archive))
{
return $this->raiseWarning(100, 'Archive does not exist');
throw new RuntimeException('Archive does not exist');
}

if ($this->hasNativeSupport())
Expand All @@ -145,27 +145,6 @@ public function extract($archive, $destination, array $options = array())
return $this->extractCustom($archive, $destination);
}

/**
* Temporary private method to isolate JError from the extract method
* This code should be removed when JError is removed.
*
* @param int $code The application-internal error code for this error
* @param string $msg The error message, which may also be shown the user if need be.
*
* @return JException JException instance if JError class exists
*
* @throws RuntimeException if JError class does not exist
*/
private function raiseWarning($code, $msg)
{
if (class_exists('JError'))
{
return JError::raiseWarning($code, $msg);
}

throw new RuntimeException($msg);
}

/**
* Tests whether this adapter can unpack files on this computer.
*
Expand Down Expand Up @@ -227,19 +206,19 @@ protected function extractCustom($archive, $destination)

if (!extension_loaded('zlib'))
{
return $this->raiseWarning(100, 'Zlib not supported');
throw new RuntimeException('Zlib not supported');
}

$this->_data = file_get_contents($archive);

if (!$this->_data)
{
return $this->raiseWarning(100, 'Unable to read archive (zip)');
throw new RuntimeException('Unable to read archive (zip)');
}

if (!$this->_readZipInfo($this->_data))
{
return $this->raiseWarning(100, 'Get ZIP Information failed');
throw new RuntimeException('Get ZIP Information failed');
}

for ($i = 0, $n = count($this->_metadata); $i < $n; $i++)
Expand All @@ -254,12 +233,12 @@ protected function extractCustom($archive, $destination)
// Make sure the destination folder exists
if (!JFolder::create(dirname($path)))
{
return $this->raiseWarning(100, 'Unable to create destination');
throw new RuntimeException('Unable to create destination');
}

if (JFile::write($path, $buffer) === false)
{
return $this->raiseWarning(100, 'Unable to write entry');
throw new RuntimeException('Unable to write entry');
}
}
}
Expand All @@ -284,21 +263,21 @@ protected function extractNative($archive, $destination)

if (!is_resource($zip))
{
return $this->raiseWarning(100, 'Unable to open archive');
throw new RuntimeException('Unable to open archive');
}

// Make sure the destination folder exists
if (!JFolder::create($destination))
{
return $this->raiseWarning(100, 'Unable to create destination');
throw new RuntimeException('Unable to create destination');
}

// Read files in the archive
while ($file = @zip_read($zip))
{
if (!zip_entry_open($zip, $file, "r"))
{
return $this->raiseWarning(100, 'Unable to read entry');
throw new RuntimeException('Unable to read entry');
}

if (substr(zip_entry_name($file), strlen(zip_entry_name($file)) - 1) != "/")
Expand All @@ -307,7 +286,7 @@ protected function extractNative($archive, $destination)

if (JFile::write($destination . '/' . zip_entry_name($file), $buffer) === false)
{
return $this->raiseWarning(100, 'Unable to write entry');
throw new RuntimeException('Unable to write entry');
}

zip_entry_close($file);
Expand Down Expand Up @@ -376,7 +355,7 @@ private function _readZipInfo(&$data)
{
if ($dataLength < $fhStart + 31)
{
return $this->raiseWarning(100, 'Invalid Zip Data');
throw new RuntimeException('Invalid Zip Data');
}

$info = unpack('vMethod/VTime/VCRC32/VCompressed/VUncompressed/vLength', substr($data, $fhStart + 10, 20));
Expand Down Expand Up @@ -406,7 +385,7 @@ private function _readZipInfo(&$data)

if ($dataLength < $fhStart + 43)
{
return $this->raiseWarning(100, 'Invalid ZIP data');
throw new RuntimeException('Invalid ZIP data');
}

$info = unpack('vInternal/VExternal/VOffset', substr($data, $fhStart + 36, 10));
Expand All @@ -421,7 +400,7 @@ private function _readZipInfo(&$data)

if ($dataLength < $lfhStart + 34)
{
return $this->raiseWarning(100, 'Invalid Zip Data');
throw new RuntimeException('Invalid Zip Data');
}

$info = unpack('vMethod/VTime/VCRC32/VCompressed/VUncompressed/vLength/vExtraLength', substr($data, $lfhStart + 8, 25));
Expand Down
Loading

0 comments on commit 77723e7

Please sign in to comment.