Added feature #13428: Caching of the ExtDirect API configuration
authorOliver Hader <oliver.hader@typo3.org>
Fri, 19 Feb 2010 23:19:03 +0000 (23:19 +0000)
committerOliver Hader <oliver.hader@typo3.org>
Fri, 19 Feb 2010 23:19:03 +0000 (23:19 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@6918 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
t3lib/extjs/class.t3lib_extjs_extdirectapi.php

index 7b7bf6f..afb6812 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2010-02-20  Oliver Hader  <oliver@typo3.org>
+
+       * Added feature #13428: Caching of the ExtDirect API configuration (Thanks to Stefan Galinski)
+
 2010-02-19  Benjamin Mack  <benni@typo3.org>
 
        * Fixed bug #12007: Docblock comments in TCEforms suggest class are wrong (Thanks to Andreas Wolf)
index 9e82960..c5d872c 100644 (file)
  */
 class t3lib_extjs_ExtDirectApi {
        /**
-        * Parses the ExtDirect configuration array "$GLOBALS['TYPO3_CONF_VARS']['BE']['ExtDirect']"
-        * and feeds the given typo3ajax instance with the resulting informations. The get parameter
+        * Parses the ExtDirect configuration array "$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']"
+        * and feeds the given typo3ajax instance with the resulting information. The get parameter
         * "namespace" will be used to filter the configuration.
         *
         * This method makes usage of the reflection mechanism to fetch the methods inside the
-        * defined classes together with their amount of parameters. This informations are building
-        * the API and are required by ExtDirect.
+        * defined classes together with their amount of parameters. This information are building
+        * the API and are required by ExtDirect. The result is cached to improve the overall
+        * performance.
         *
         * @param array $ajaxParams ajax parameters
         * @param TYPO3AJAX $ajaxObj typo3ajax instance
@@ -49,6 +50,56 @@ class t3lib_extjs_ExtDirectApi {
        public function getAPI($ajaxParams, TYPO3AJAX $ajaxObj) {
                $filterNamespace = t3lib_div::_GET('namespace');
 
+               // look up into the cache
+               $cacheIdentifier = 'ExtDirectApi';
+               $cacheHash = md5($cacheIdentifier . $filterNamespace);
+               $cacheContent = t3lib_pageSelect::getHash($cacheHash);
+
+               // generate the javascript content if it wasn't found inside the cache and cache it!
+               if (!$cacheContent) {
+                       $javascriptNamespaces = $this->generateAPI($filterNamespace);
+                       t3lib_pageSelect::storeHash(
+                               $cacheHash,
+                               serialize($javascriptNamespaces),
+                               $cacheIdentifier
+                       );
+               } else {
+                       $javascriptNamespaces = unserialize($cacheContent);
+               }
+
+               // return the generated javascript API configuration
+               if (count($javascriptNamespaces)) {
+                       $setup = '
+                               if (typeof Ext.app.ExtDirectAPI !== "object") {
+                                       Ext.app.ExtDirectAPI = {};
+                               }
+
+                               if (typeof Object.extend !== "function") {
+                                       Object.extend = function(destination, source) {
+                                               for (var property in source) {
+                                                       destination[property] = source[property];
+                                               }
+                                               return destination;
+                                       };
+                               }
+                       ';
+
+                       $ajaxObj->setContent($javascriptNamespaces);
+                       $ajaxObj->setContentFormat('javascript');
+                       $ajaxObj->setJavascriptCallbackWrap(
+                               $setup . 'Ext.app.ExtDirectAPI = Object.extend(Ext.app.ExtDirectAPI, |);'
+                       );
+               }
+       }
+
+       /**
+        * Generates the API that is configured inside the ExtDirect configuration
+        * array "$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']".
+        *
+        * @param string $filerNamespace namespace that should be loaded like TYPO3.Backend
+        * @return array javascript API configuration
+        */
+       protected function generateAPI($filterNamespace) {
                $javascriptNamespaces = array();
                if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'])) {
                        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'] as $javascriptName => $className) {
@@ -84,27 +135,7 @@ class t3lib_extjs_ExtDirectApi {
                        }
                }
 
-               if (count($javascriptNamespaces)) {
-                       $setup = '
-                               if (typeof Ext.app.ExtDirectAPI != "object") {
-                                       Ext.app.ExtDirectAPI = {};
-                               }
-
-
-                               if (typeof Object.extend != "function") {
-                                       Object.extend = function(destination, source) {
-                                               for (var property in source) {
-                                                       destination[property] = source[property];
-                                               }
-                                               return destination;
-                                       }
-                               }
-                       ';
-
-                       $ajaxObj->setContent($javascriptNamespaces);
-                       $ajaxObj->setContentFormat('javascript');
-                       $ajaxObj->setJavascriptCallbackWrap($setup . 'Ext.app.ExtDirectAPI = Object.extend(Ext.app.ExtDirectAPI, |);');
-               }
+               return $javascriptNamespaces;
        }
 }