Permalink
Browse files

Moving JResponse to its own package and adding JResponseJson

  • Loading branch information...
1 parent 9699628 commit cdbf41504ee34b190fc7e1151e274073680fbc04 @Chraneco committed Oct 11, 2012
@@ -9,8 +9,6 @@
defined('JPATH_PLATFORM') or die;
-jimport('joomla.environment.response');
-
/**
* Document class, provides an easy interface to parse and display a document
*
@@ -0,0 +1,121 @@
+<?php
+/**
+ * @package Joomla.Platform
+ * @subpackage Response
+ *
+ * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+defined('JPATH_PLATFORM') or die;
+
+/**
+ * JSON Response class.
+ *
+ * This class serves to provide the Joomla Platform with a common interface to access
+ * response variables for e.g. Ajax requests.
+ *
+ * @package Joomla.Platform
+ * @subpackage Response
+ * @since 12.2
+ */
+class JResponseJson
+{
+ /**
+ * Determines whether the request was successful
+ *
+ * @var boolean
+ * @since 12.2
+ */
+ public $success = true;
+
+ /**
+ * The main response message
+ *
+ * @var string
+ * @since 12.2
+ */
+ public $message = null;
+
+ /**
+ * Array of messages gathered in the JApplication object
+ *
+ * @var array
+ * @since 12.2
+ */
+ public $messages = null;
+
+ /**
+ * The response data
+ *
+ * var mixed
+ * @since 12.2
+ */
+ public $data = null;
+
+ /**
+ * Constructor
+ *
+ * @param mixed $response The Response data
+ * @param string $message The main response message
+ * @param boolean $error True, if the success flag shall be set to false, defaults to false
+ * @param boolean $ignoreMessages True, if the message queue shouldn't be included, defaults to false
+ *
+ * @since 12.2
+ */
+ public function __construct($response = null, $message = null, $error = false, $ignoreMessages = false)
+ {
+ $this->message = $message;
+
+ // Get the message queue if requested and available
+ $app = JFactory::$application;
+ if (!$ignoreMessages && !is_null($app) && is_callable(array($app, 'getMessageQueue')))
+ {
+ $messages = $app->getMessageQueue();
+
+ // Build the sorted messages list
+ if (is_array($messages) && count($messages))
+ {
+ foreach ($messages as $message)
+ {
+ if (isset($message['type']) && isset($message['message']))
+ {
+ $lists[$message['type']][] = $message['message'];
+ }
+ }
+ }
+
+ // If messages exist add them to the output
+ if (isset($lists) && is_array($lists))
+ {
+ $this->messages = $lists;
+ }
+ }
+
+ // Check if we are dealing with an error
+ if ($response instanceof Exception)
+ {
+ // Prepare the error response
+ $this->success = false;
+ $this->message = $response->getMessage();
+ }
+ else
+ {
+ // Prepare the response data
+ $this->success = !$error;
+ $this->data = $response;
+ }
+ }
+
+ /**
+ * Magic toString method for sending the response in JSON format
+ *
+ * @return string The response in JSON format
+ *
+ * @since 12.2
+ */
+ public function __toString()
+ {
+ return json_encode($this);
+ }
+}
@@ -1,7 +1,7 @@
<?php
/**
* @package Joomla.Platform
- * @subpackage Environment
+ * @subpackage Response
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
@@ -16,7 +16,7 @@
* response variables. This includes header and body.
*
* @package Joomla.Platform
- * @subpackage Environment
+ * @subpackage Response
* @since 11.1
*/
class JResponse
@@ -9,8 +9,6 @@
defined('JPATH_PLATFORM') or die;
-jimport('joomla.environment.response');
-
/**
* Base class for a Joomla! application.
*
@@ -7,8 +7,6 @@
* @license GNU General Public License version 2 or later; see LICENSE
*/
-require_once JPATH_PLATFORM . '/joomla/environment/response.php';
-
/**
* Test class for JDocument.
* Generated by PHPUnit on 2009-10-09 at 12:13:55.
@@ -40,7 +40,7 @@ protected function setUp()
require_once JPATH_PLATFORM . '/joomla/application/router.php';
require_once JPATH_PLATFORM . '/joomla/environment/request.php';
require_once JPATH_PLATFORM . '/joomla/document/feed/feed.php';
- require_once JPATH_PLATFORM . '/joomla/environment/response.php';
+
$this->saveFactoryState();
JFactory::$application = $this->getMock(
@@ -39,7 +39,6 @@ protected function setUp()
require_once JPATH_PLATFORM . '/joomla/application/router.php';
require_once JPATH_PLATFORM . '/joomla/environment/request.php';
require_once JPATH_PLATFORM . '/joomla/document/feed/feed.php';
- require_once JPATH_PLATFORM . '/joomla/environment/response.php';
$this->saveFactoryState();
@@ -7,7 +7,6 @@
* @license GNU General Public License version 2 or later; see LICENSE
*/
-require_once JPATH_PLATFORM . '/joomla/environment/response.php';
require_once JPATH_PLATFORM . '/joomla/document/opensearch/opensearch.php';
/**
Oops, something went wrong.

0 comments on commit cdbf415

Please sign in to comment.