Added feature #15513: Log ExtDirect Exceptions in DebugConsole (Thanks to Stefan...
authorSteffen Kamper <info@sk-typo3.de>
Mon, 23 Aug 2010 14:22:38 +0000 (14:22 +0000)
committerSteffen Kamper <info@sk-typo3.de>
Mon, 23 Aug 2010 14:22:38 +0000 (14:22 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@8648 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
t3lib/class.t3lib_pagerenderer.php
t3lib/core_autoload.php
t3lib/extjs/class.t3lib_extjs_extdirectdebug.php [new file with mode: 0644]
t3lib/extjs/class.t3lib_extjs_extdirectrouter.php
typo3/backend.php

index 99aa5d7..ca7c129 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2010-08-23 Steffen Kamper  <steffen@typo3.org>
+
+       * Added feature #15513: Log ExtDirect Exceptions in DebugConsole (Thanks to Stefan Galinski)
+
 2010-08-23  Xavier Perseguers  <typo3@perseguers.ch>
 
        * Raised DBAL to version 1.2.0alpha1
index 0e64759..4c59433 100644 (file)
@@ -873,6 +873,57 @@ class t3lib_PageRenderer implements t3lib_Singleton {
                }
        }
 
+       /**
+        * Adds the ExtDirect code
+        *
+        * @return void
+        */
+       public function addExtDirectCode() {
+                       // Note: we need to iterate thru the object, because the addProvider method
+                       // does this only with multiple arguments
+               $this->addExtOnReadyCode(
+                       'for (var api in Ext.app.ExtDirectAPI) {
+                               Ext.Direct.addProvider(Ext.app.ExtDirectAPI[api]);
+                       }
+
+                       var extDirectDebug = function(message, header, group) {
+                               var TYPO3ViewportInstance = null;
+
+                               if (top && top.TYPO3 && typeof top.TYPO3.Backend === "object") {
+                                       TYPO3ViewportInstance = top.TYPO3.Backend;
+                               } else if (typeof TYPO3 === "object" && typeof TYPO3.Backend === "object") {
+                                       TYPO3ViewportInstance = TYPO3.Backend;
+                               }
+
+                               if (TYPO3ViewportInstance !== null) {
+                                       TYPO3ViewportInstance.DebugConsole.addTab(message, header, group);
+                               } else {
+                                       document.write(message);
+                               }
+                       };
+
+                       Ext.Direct.on("exception", function(event) {
+                               extDirectDebug(
+                                       "<p>" + event.message + "</p>" +
+                                       "<p style=\"margin-top: 20px;\">" +
+                                               "<strong>Backtrace:</strong><br />" +
+                                               event.where.replace(/#/g, "<br />#") +
+                                       "</p>",
+                                       event.method,
+                                       "ExtDirect - Exception"
+                               );
+                       });
+
+                       Ext.Direct.on("event", function(event, provider) {
+                               if (typeof event.debug !== "undefined" && event.debug !== "") {
+                                       extDirectDebug(event.debug, event.method, "ExtDirect - Debug");
+                               }
+                       });
+                       ',
+                       TRUE
+               );
+       }
+
        /* CSS Files */
 
        /**
index 18fe45a..ce55bf3 100644 (file)
@@ -137,6 +137,7 @@ $t3libClasses = array(
        't3lib_spritemanager_spritegenerator' => PATH_t3lib . 'spritemanager/class.t3lib_spritemanager_spritegenerator.php',
        't3lib_spritemanager_spriteicongenerator' => PATH_t3lib . 'interfaces/interface.t3lib_spritemanager_spriteicongenerator.php',
        't3lib_spritemanager_simplehandler' => PATH_t3lib . 'spritemanager/class.t3lib_spritemanager_simplehandler.php',
+       't3lib_extjs_extdirectdebug' => PATH_t3lib . 'extjs/class.t3lib_extjs_extdirectdebug.php',
 );
 
 $tslibClasses = require(PATH_typo3 . 'sysext/cms/ext_autoload.php');
diff --git a/t3lib/extjs/class.t3lib_extjs_extdirectdebug.php b/t3lib/extjs/class.t3lib_extjs_extdirectdebug.php
new file mode 100644 (file)
index 0000000..97f4fcf
--- /dev/null
@@ -0,0 +1,67 @@
+<?php\r
+/***************************************************************\r
+*  Copyright notice\r
+*\r
+*  (c) 2010 Stefan Galinski <stefan.galinski@gmail.com>\r
+*  All rights reserved\r
+*\r
+*  This script is part of the TYPO3 project. The TYPO3 project is\r
+*  free software; you can redistribute it and/or modify\r
+*  it under the terms of the GNU General Public License as published by\r
+*  the Free Software Foundation; either version 2 of the License, or\r
+*  (at your option) any later version.\r
+*\r
+*  The GNU General Public License can be found at\r
+*  http://www.gnu.org/copyleft/gpl.html.\r
+*  A copy is found in the textfile GPL.txt and important notices to the license\r
+*  from the author is found in LICENSE.txt distributed with these scripts.\r
+*\r
+*\r
+*  This script is distributed in the hope that it will be useful,\r
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+*  GNU General Public License for more details.\r
+*\r
+*  This copyright notice MUST APPEAR in all copies of the script!\r
+***************************************************************/\r
+\r
+/**\r
+ * Ext Direct Debug\r
+ *\r
+ * @author     Stefan Galinski <stefan.galinski@gmail.com>\r
+ * @package    TYPO3\r
+ */\r
+final class t3lib_extjs_ExtDirectDebug {\r
+       /**\r
+        * Internal debug message array\r
+        *\r
+        * @var array\r
+        */\r
+       protected static $debugMessages = array();\r
+\r
+       /**\r
+        * Adds a new message of any data type to the internal debug message array.\r
+        *\r
+        * @param mixed $message\r
+        * @return void\r
+        */\r
+       public static function debug($message) {\r
+               self::$debugMessages[] = $message;\r
+       }\r
+\r
+       /**\r
+        * Returns the internal debug messages as a string.\r
+        *\r
+        * @return string\r
+        */\r
+       public static function toString() {\r
+               $messagesAsString = '';\r
+               if (count(self::$debugMessages)) {\r
+                       $messagesAsString = t3lib_div::view_array(self::$debugMessages);\r
+               }\r
+\r
+               return $messagesAsString;\r
+       }\r
+}\r
+\r
+?>
\ No newline at end of file
index 8956bae..068b5ff 100644 (file)
@@ -3,6 +3,7 @@
 *  Copyright notice
 *
 *  (c) 2010 Sebastian Kurfuerst <sebastian@typo3.org>
+*  (c) 2010 Stefan Galinski <stefan.galinski@gmail.com>
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -41,56 +42,67 @@ class t3lib_extjs_ExtDirectRouter {
         * @return void
         */
        public function route($ajaxParams, TYPO3AJAX $ajaxObj) {
-               try {
-                       $isForm = FALSE;
-                       $isUpload = FALSE;
-                       $rawPostData = file_get_contents('php://input');
-                       $postParameters = t3lib_div::_POST();
-                       $namespace = t3lib_div::_GET('namespace');
-
-                       if (!empty($postParameters['extAction'])) {
-                               $isForm = TRUE;
-                               $isUpload = $postParameters['extUpload'] === 'true';
-
-                               $request->action = $postParameters['extAction'];
-                               $request->method = $postParameters['extMethod'];
-                               $request->tid = $postParameters['extTID'];
-                               $request->data = array($_POST + $_FILES);
-                       } elseif (!empty($rawPostData)) {
-                               $request = json_decode($rawPostData);
-                       } else {
-                               throw new t3lib_error_Exception('ExtDirect: Missing Parameters!');
-                       }
+               $isForm = FALSE;
+               $isUpload = FALSE;
+               $rawPostData = file_get_contents('php://input');
+               $postParameters = t3lib_div::_POST();
+               $namespace = t3lib_div::_GET('namespace');
+               $response = array();
+               $request = NULL;
+
+               if (!empty($postParameters['extAction'])) {
+                       $isForm = TRUE;
+                       $isUpload = $postParameters['extUpload'] === 'true';
+
+                       $request = new stdClass;
+                       $request->action = $postParameters['extAction'];
+                       $request->method = $postParameters['extMethod'];
+                       $request->tid = $postParameters['extTID'];
+                       $request->data = array($_POST + $_FILES);
+               } elseif (!empty($rawPostData)) {
+                       $request = json_decode($rawPostData);
+               } else {
+                       $response[] = array(
+                               'type' => 'exception',
+                               'message' => 'Something went wrong with an ExtDirect call!'
+                       );
+               }
 
-                       $response = NULL;
-                       if (is_array($request)) {
-                               $response = array();
-                               foreach ($request as $singleRequest) {
-                                       $response[] = $this->processRpc($singleRequest, $namespace);
-                               }
-                       } else {
-                               $response = $this->processRpc($request, $namespace);
-                       }
+               if (!is_array($request)) {
+                       $request = array($request);
+               }
 
-                       if ($isForm && $isUpload) {
-                               $ajaxObj->setContentFormat('plain');
-                               $response = json_encode($response);
-                               $response = preg_replace('/&quot;/', '\\&quot;', $response);
-
-                               $response = array(
-                                       '<html><body><textarea>' .
-                                       $response .
-                                       '</textarea></body></html>'
-                               );
-                       } else {
-                               $ajaxObj->setContentFormat('jsonbody');
+               foreach ($request as $index => $singleRequest) {
+                       $response[$index] = array(
+                               'tid' => $singleRequest->tid,
+                               'action' => $singleRequest->action,
+                               'method' => $singleRequest->method
+                       );
+
+                       try {
+                               $response[$index]['type'] = 'rpc';
+                               $response[$index]['result'] = $this->processRpc($singleRequest, $namespace);
+                               $response[$index]['debug'] = t3lib_extjs_ExtDirectDebug::toString();
+
+                       } catch (Exception $exception) {
+                               $response[$index]['type'] = 'exception';
+                               $response[$index]['message'] = $exception->getMessage();
+                               $response[$index]['where'] = $exception->getTraceAsString();
                        }
-               } catch (t3lib_error_Exception $exception) {
+               }
+
+               if ($isForm && $isUpload) {
+                       $ajaxObj->setContentFormat('plain');
+                       $response = json_encode($response);
+                       $response = preg_replace('/&quot;/', '\\&quot;', $response);
+
                        $response = array(
-                               'type' => 'exception',
-                               'message' => $exception->getMessage(),
-                               'where' => $exception->getTraceAsString()
+                               '<html><body><textarea>' .
+                               $response .
+                               '</textarea></body></html>'
                        );
+               } else {
+                       $ajaxObj->setContentFormat('jsonbody');
                }
 
                $ajaxObj->setContent($response);
@@ -104,40 +116,27 @@ class t3lib_extjs_ExtDirectRouter {
         *
         * @param object $singleRequest request object from extJS
         * @param string $namespace namespace like TYPO3.Backend
+        * @throws t3lib_error_exception if the remote method couldn't be found
         * @return mixed return value of the called method
         */
        protected function processRpc($singleRequest, $namespace) {
-               try {
-                       $endpointName = $namespace . '.' . $singleRequest->action;
+               $endpointName = $namespace . '.' . $singleRequest->action;
 
                        // theoretically this can never happen, because of an javascript error on
                        // the client side due the missing namespace/endpoint
-                       if (!isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName])) {
-                               throw new t3lib_error_Exception('ExtDirect: Call to undefined endpoint: ' . $endpointName);
-                       }
-
-                       $response = array(
-                               'type' => 'rpc',
-                               'tid' => $singleRequest->tid,
-                               'action' => $singleRequest->action,
-                               'method' => $singleRequest->method
-                       );
-
-                       $endpointObject = t3lib_div::getUserObj(
-                               $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName],
-                               FALSE
-                       );
-
-                       $response['result'] = call_user_func_array(
-                               array($endpointObject, $singleRequest->method),
-                               is_array($singleRequest->data) ? $singleRequest->data : array()
-                       );
-
-               } catch (t3lib_error_Exception $exception) {
-                       throw $exception;
+               if (!isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName])) {
+                       throw new t3lib_error_Exception('ExtDirect: Call to undefined endpoint: ' . $endpointName);
                }
 
-               return $response;
+               $endpointObject = t3lib_div::getUserObj(
+                       $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName],
+                       FALSE
+               );
+
+               return call_user_func_array(
+                       array($endpointObject, $singleRequest->method),
+                       is_array($singleRequest->data) ? $singleRequest->data : array()
+               );
        }
 }
 
index 23ee726..c62f75c 100644 (file)
@@ -100,18 +100,12 @@ class TYPO3backend {
                $this->pageRenderer->loadScriptaculous('builder,effects,controls,dragdrop');
                $this->pageRenderer->loadExtJS();
 
-                       // register the extDirect API providers
-                       // Note: we need to iterate thru the object, because the addProvider method
-                       // does this only with multiple arguments
                $this->pageRenderer->addExtOnReadyCode(
-                       'for (var api in Ext.app.ExtDirectAPI) {
-                               Ext.Direct.addProvider(Ext.app.ExtDirectAPI[api]);
-                       }
-                       TYPO3.Backend = new TYPO3.Viewport(TYPO3.Viewport.configuration);
+                       'TYPO3.Backend = new TYPO3.Viewport(TYPO3.Viewport.configuration);
                        ',
                        TRUE
                );
-
+               $this->pageRenderer->addExtDirectCode();
 
                        // add default BE javascript
                $this->js      = '';