Skip to content

Commit

Permalink
Update error renderers for PHP 7 code structure, update Exception/Thr…
Browse files Browse the repository at this point in the history
…owable references to only reference Throwable
  • Loading branch information
Michael Babker committed Aug 28, 2017
1 parent 350268a commit acb66bb
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 218 deletions.
2 changes: 1 addition & 1 deletion installation/controller/removefolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public function __construct($data)
}

// Check if we are dealing with an error.
if ($data instanceof Exception || $data instanceof Throwable)
if ($data instanceof Throwable)
{
// Prepare the error response.
$this->error = true;
Expand Down
4 changes: 2 additions & 2 deletions installation/error/json.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class InstallationErrorJson extends AbstractRenderer
/**
* Render the error page for the given object
*
* @param \Throwable|\Exception $error The error object to be rendered
* @param Throwable $error The error object to be rendered
*
* @return string
*
* @since 4.0
*/
protected function doRender($error)
public function render(Throwable $error): string
{
return json_encode(new InstallationResponseJson($error));
}
Expand Down
2 changes: 1 addition & 1 deletion installation/response/json.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct($data)
}

// Check if we are dealing with an error.
if ($data instanceof Exception || $data instanceof Throwable)
if ($data instanceof Throwable)
{
// Prepare the error response.
$this->error = true;
Expand Down
10 changes: 4 additions & 6 deletions libraries/src/Document/ErrorDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ErrorDocument extends Document
/**
* Error Object
*
* @var \Exception|\Throwable
* @var \Throwable
* @since 11.1
*/
public $error;
Expand All @@ -64,7 +64,7 @@ class ErrorDocument extends Document
/**
* Error Object
*
* @var \Exception|\Throwable
* @var \Throwable
* @since 11.1
*/
protected $_error;
Expand All @@ -90,17 +90,15 @@ public function __construct($options = array())
/**
* Set error object
*
* @param \Exception|\Throwable $error Error object to set
* @param \Throwable $error Error object to set
*
* @return boolean True on success
*
* @since 11.1
*/
public function setError($error)
{
$expectedClass = PHP_MAJOR_VERSION >= 7 ? '\\Throwable' : '\\Exception';

if ($error instanceof $expectedClass)
if ($error instanceof \Throwable)
{
$this->_error = & $error;

Expand Down
77 changes: 23 additions & 54 deletions libraries/src/Error/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

namespace Joomla\CMS\Error;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Document\Document;
use Joomla\CMS\Factory;

/**
* Base class for error page renderers
*
Expand All @@ -16,9 +21,9 @@
abstract class AbstractRenderer implements RendererInterface
{
/**
* The JDocument instance
* The Document instance
*
* @var \JDocument
* @var Document
* @since 4.0
*/
protected $document;
Expand All @@ -32,24 +37,13 @@ abstract class AbstractRenderer implements RendererInterface
protected $type;

/**
* Render the error page for the given object
*
* @param \Throwable|\Exception $error The error object to be rendered
* Retrieve the Document instance attached to this renderer
*
* @return string
* @return Document
*
* @since 4.0
*/
abstract protected function doRender($error);

/**
* Retrieve the JDocument instance attached to this renderer
*
* @return \JDocument
*
* @since 4.0
*/
public function getDocument()
public function getDocument(): Document
{
// Load the document if not already
if (!$this->document)
Expand All @@ -70,15 +64,15 @@ public function getDocument()
* @since 4.0
* @throws \InvalidArgumentException
*/
public static function getRenderer($type)
public static function getRenderer(string $type)
{
// Build the class name
$class = __NAMESPACE__ . '\\Renderer\\' . ucfirst(strtolower($type)) . 'Renderer';

// First check if an object may exist in the container and prefer that over everything else
if (\JFactory::getContainer()->exists($class))
if (Factory::getContainer()->has($class))
{
return \JFactory::getContainer()->get($class);
return Factory::getContainer()->get($class);
}

// Next check if a local class exists and use that
Expand All @@ -92,54 +86,29 @@ public static function getRenderer($type)
}

/**
* Create the JDocument object for this renderer
* Create the Document object for this renderer
*
* @return \JDocument
* @return Document
*
* @since 4.0
*/
protected function loadDocument()
protected function loadDocument(): Document
{
$attributes = array(
$attributes = [
'charset' => 'utf-8',
'lineend' => 'unix',
'tab' => "\t",
'language' => 'en-GB',
'direction' => 'ltr',
);

// If there is a JLanguage instance in JFactory then let's pull the language and direction from its metadata
if (\JFactory::$language)
{
$attributes['language'] = \JFactory::getLanguage()->getTag();
$attributes['direction'] = \JFactory::getLanguage()->isRtl() ? 'rtl' : 'ltr';
}

return \JDocument::getInstance($this->type, $attributes);
}
];

/**
* Render the error page for the given object
*
* @param \Throwable|\Exception $error The error object to be rendered
*
* @return string
*
* @since 4.0
* @throws \InvalidArgumentException if a non-Throwable object was provided
*/
public function render($error)
{
// If this isn't a Throwable then bail out
if (!($error instanceof \Throwable) && !($error instanceof \Exception))
// If there is a Language instance in Factory then let's pull the language and direction from its metadata
if (Factory::$language)
{
$expectedType = PHP_VERSION_ID >= 70000 ? 'a Throwable' : 'an Exception';

throw new \InvalidArgumentException(
sprintf('The error renderer requires %1$s object, a %2$s object was given instead.', $expectedType, get_class($error))
);
$attributes['language'] = Factory::getLanguage()->getTag();
$attributes['direction'] = Factory::getLanguage()->isRtl() ? 'rtl' : 'ltr';
}

return $this->doRender($error);
return Document::getInstance($this->type, $attributes);
}
}
10 changes: 6 additions & 4 deletions libraries/src/Error/Renderer/CliRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Joomla\CMS\Error\Renderer;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Error\AbstractRenderer;

/**
Expand All @@ -28,13 +30,13 @@ class CliRenderer extends AbstractRenderer
/**
* Render the error for the given object.
*
* @param \Throwable|\Exception $error The error object to be rendered
* @param \Throwable $error The error object to be rendered
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
protected function doRender($error)
public function render(\Throwable $error): string
{
$buffer = PHP_EOL . 'Error occurred: ' . $error->getMessage() . PHP_EOL . $this->getTrace($error);

Expand All @@ -49,13 +51,13 @@ protected function doRender($error)
/**
* Returns a trace for the given error.
*
* @param \Throwable|\Exception $error The error
* @param \Throwable $error The error
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
private function getTrace($error)
private function getTrace(\Throwable $error): string
{
// Include the stack trace only if in debug mode
if (!JDEBUG)
Expand Down
2 changes: 2 additions & 0 deletions libraries/src/Error/Renderer/FeedRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Joomla\CMS\Error\Renderer;

defined('JPATH_PLATFORM') or die;

/**
* RSS/Atom feed error page renderer
*
Expand Down
9 changes: 6 additions & 3 deletions libraries/src/Error/Renderer/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

namespace Joomla\CMS\Error\Renderer;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Error\AbstractRenderer;
use Joomla\CMS\Factory;

/**
* HTML error page renderer
Expand All @@ -29,15 +32,15 @@ class HtmlRenderer extends AbstractRenderer
/**
* Render the error page for the given object
*
* @param \Throwable|\Exception $error The error object to be rendered
* @param \Throwable $error The error object to be rendered
*
* @return string
*
* @since 4.0
*/
protected function doRender($error)
public function render(\Throwable $error): string
{
$app = \JFactory::getApplication();
$app = Factory::getApplication();

// Get the current template from the application
$template = $app->getTemplate();
Expand Down
6 changes: 4 additions & 2 deletions libraries/src/Error/Renderer/JsonRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Joomla\CMS\Error\Renderer;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Error\AbstractRenderer;

/**
Expand All @@ -28,13 +30,13 @@ class JsonRenderer extends AbstractRenderer
/**
* Render the error page for the given object
*
* @param \Throwable|\Exception $error The error object to be rendered
* @param \Throwable $error The error object to be rendered
*
* @return string
*
* @since 4.0
*/
protected function doRender($error)
public function render(\Throwable $error): string
{
// Create our data object to be rendered
$data = [
Expand Down
6 changes: 4 additions & 2 deletions libraries/src/Error/Renderer/XmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Joomla\CMS\Error\Renderer;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Error\AbstractRenderer;

/**
Expand All @@ -28,13 +30,13 @@ class XmlRenderer extends AbstractRenderer
/**
* Render the error page for the given object
*
* @param \Throwable|\Exception $error The error object to be rendered
* @param \Throwable $error The error object to be rendered
*
* @return string
*
* @since 4.0
*/
protected function doRender($error)
public function render(\Throwable $error): string
{
// Create our data object to be rendered
$xw = new \XMLWriter;
Expand Down
15 changes: 9 additions & 6 deletions libraries/src/Error/RendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

namespace Joomla\CMS\Error;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Document\Document;

/**
* Interface defining the rendering engine for the error handling layer
*
Expand All @@ -16,23 +20,22 @@
interface RendererInterface
{
/**
* Retrieve the JDocument instance attached to this renderer
* Retrieve the Document instance attached to this renderer
*
* @return \JDocument
* @return Document
*
* @since 4.0
*/
public function getDocument();
public function getDocument(): Document;

/**
* Render the error page for the given object
*
* @param \Throwable|\Exception $error The error object to be rendered
* @param \Throwable $error The error object to be rendered
*
* @return string
*
* @since 4.0
* @throws \InvalidArgumentException if a non-Throwable object was provided
*/
public function render($error);
public function render(\Throwable $error): string;
}

0 comments on commit acb66bb

Please sign in to comment.