Fixed bug #17167: Cache ExtDirect::getAPI calls
[Packages/TYPO3.CMS.git] / t3lib / extjs / class.t3lib_extjs_extdirectapi.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Sebastian Kurf├╝rst <sebastian@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Ext Direct API Generator
30 *
31 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
32 * @author Stefan Galinski <stefan.galinski@gmail.com>
33 * @package TYPO3
34 */
35 class t3lib_extjs_ExtDirectApi {
36 /**
37 * @var array
38 */
39 protected $settings = array();
40
41 /**
42 * Constructs this object.
43 */
44 public function __construct() {
45 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'])) {
46 $this->settings = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'];
47 }
48 }
49
50 /**
51 * Parses the ExtDirect configuration array "$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']"
52 * and feeds the given typo3ajax instance with the resulting information. The get parameter
53 * "namespace" will be used to filter the configuration.
54 *
55 * This method makes usage of the reflection mechanism to fetch the methods inside the
56 * defined classes together with their amount of parameters. This information are building
57 * the API and are required by ExtDirect. The result is cached to improve the overall
58 * performance.
59 *
60 * @param array $ajaxParams ajax parameters
61 * @param TYPO3AJAX $ajaxObj typo3ajax instance
62 * @return void
63 */
64 public function getAPI($ajaxParams, TYPO3AJAX $ajaxObj) {
65 $filterNamespace = t3lib_div::_GET('namespace');
66
67 // Check GET-parameter no_cache and extCache setting
68 $extCache = isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['extCache']) && (
69 $GLOBALS['TYPO3_CONF_VARS']['SYS']['extCache'] === 0 ||
70 $GLOBALS['TYPO3_CONF_VARS']['SYS']['extCache'] === '0'
71 );
72 $noCache = t3lib_div::_GET('no_cache') ? TRUE : $extCache;
73
74 // look up into the cache
75 $cacheIdentifier = 'ExtDirectApi';
76 $cacheHash = md5($cacheIdentifier . $filterNamespace . t3lib_div::getIndpEnv('TYPO3_SSL') .
77 serialize($this->settings) . TYPO3_MODE . t3lib_div::getIndpEnv('HTTP_HOST'));
78
79 // with no_cache always generate the javascript content
80 $cacheContent = $noCache ? '' : t3lib_pageSelect::getHash($cacheHash);
81
82 // generate the javascript content if it wasn't found inside the cache and cache it!
83 if (!$cacheContent) {
84 $javascriptNamespaces = $this->generateAPI($filterNamespace);
85 if (!empty($javascriptNamespaces)) {
86 t3lib_pageSelect::storeHash(
87 $cacheHash,
88 serialize($javascriptNamespaces),
89 $cacheIdentifier
90 );
91 }
92 } else {
93 $javascriptNamespaces = unserialize($cacheContent);
94 }
95
96 // enable caching
97 $expireDate = date('r', $GLOBALS['EXEC_TIME'] + 3600 * 24 * 30);
98 header('Expires: ' . $expireDate);
99 header('Cache-control: public');
100 header('Pragma:');
101
102 // return the generated javascript API configuration
103 if (count($javascriptNamespaces)) {
104 $setup = '
105 if (typeof Ext.app.ExtDirectAPI !== "object") {
106 Ext.app.ExtDirectAPI = {};
107 }
108
109 if (typeof Object.extend !== "function") {
110 Object.extend = function(destination, source) {
111 for (var property in source) {
112 destination[property] = source[property];
113 }
114 return destination;
115 };
116 }
117 ';
118
119 $ajaxObj->setContent($javascriptNamespaces);
120 $ajaxObj->setContentFormat('javascript');
121 $ajaxObj->setJavascriptCallbackWrap(
122 $setup . 'Ext.app.ExtDirectAPI = Object.extend(Ext.app.ExtDirectAPI, |);'
123 );
124 } else {
125 if ($filterNamespace) {
126 // namespace error
127 $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:ExtDirect.namespaceError'),
128 __CLASS__, $filterNamespace
129 );
130 }
131 else {
132 // no namespace given
133 $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:ExtDirect.noNamespace'),
134 __CLASS__
135 );
136 }
137 // make js multiline message
138 $msg = t3lib_div::trimExplode(LF, str_replace('"', '\"', $errorMessage), TRUE);
139 $errorMessage = '';
140 foreach ($msg as $line) {
141 $errorMessage .= '"' . $line . '" + ' . LF;
142 }
143 $errorMessage = substr(trim($errorMessage), 0, -1);
144 //generate the javascript
145 $ajaxObj->setContentFormat('javascript');
146 $ajaxObj->setJavascriptCallbackWrap('
147 errorMessage = ' . $errorMessage . ';
148 if (typeof console === "object") {
149 console.log(errorMessage);
150 } else {
151 alert(errorMessage);
152 }
153 ');
154 }
155 }
156
157 /**
158 * Generates the API that is configured inside the ExtDirect configuration
159 * array "$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']".
160 *
161 * @param string $filerNamespace namespace that should be loaded like TYPO3.Backend
162 * @return array javascript API configuration
163 */
164 protected function generateAPI($filterNamespace) {
165 $javascriptNamespaces = array();
166 if (is_array($this->settings)) {
167 foreach ($this->settings as $javascriptName => $className) {
168 $splittedJavascriptName = explode('.', $javascriptName);
169 $javascriptObjectName = array_pop($splittedJavascriptName);
170 $javascriptNamespace = implode('.', $splittedJavascriptName);
171
172 // only items inside the wanted namespace
173 if (strpos($javascriptNamespace, $filterNamespace) !== 0) {
174 continue;
175 }
176
177 if (!isset($javascriptNamespaces[$javascriptNamespace])) {
178 $javascriptNamespaces[$javascriptNamespace] = array(
179 'url' => $this->getRoutingUrl($javascriptNamespace),
180 'type' => 'remoting',
181 'actions' => array(),
182 'namespace' => $javascriptNamespace
183 );
184 }
185
186 $serverObject = t3lib_div::getUserObj($className, FALSE);
187 $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName] = array();
188 foreach (get_class_methods($serverObject) as $methodName) {
189 $reflectionMethod = new ReflectionMethod($serverObject, $methodName);
190 $numberOfParameters = $reflectionMethod->getNumberOfParameters();
191 $docHeader = $reflectionMethod->getDocComment();
192 $formHandler = (strpos($docHeader, '@formHandler') !== FALSE);
193
194 $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName][] = array(
195 'name' => $methodName,
196 'len' => $numberOfParameters,
197 'formHandler' => $formHandler
198 );
199 }
200 }
201 }
202
203 return $javascriptNamespaces;
204 }
205
206 /**
207 * Returns the convenient path for the routing Urls based on the TYPO3 mode.
208 *
209 * @param string $namespace
210 * @return string
211 */
212 public function getRoutingUrl($namespace) {
213 $url = '';
214 if (TYPO3_MODE === 'FE') {
215 $url = t3lib_div::locationHeaderUrl('?eID=ExtDirect&action=route&namespace=');
216 } else {
217 $url = t3lib_div::locationHeaderUrl('ajax.php?ajaxID=ExtDirect::route&namespace=');
218 }
219 $url .= rawurlencode($namespace);
220
221 return $url;
222 }
223 }
224
225 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/extjs/class.t3lib_extjs_extdirectapi.php'])) {
226 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/extjs/class.t3lib_extjs_extdirectapi.php']);
227 }
228
229 ?>