[TASK] Change "null" into "NULL"
[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 $ajaxObj->setContent(array());
66 }
67
68 /**
69 * Get the API for a given nameapace
70 *
71 * @throws InvalidArgumentException
72 * @param array $filterNamespaces
73 * @return string
74 */
75 public function getApiPhp(array $filterNamespaces) {
76 $javascriptNamespaces = $this->getExtDirectApi($filterNamespaces);
77 // return the generated javascript API configuration
78 if (count($javascriptNamespaces)) {
79 return '
80 if (!Ext.isObject(Ext.app.ExtDirectAPI)) {
81 Ext.app.ExtDirectAPI = {};
82 }
83 Ext.apply(Ext.app.ExtDirectAPI, ' .
84 json_encode($javascriptNamespaces) . ');
85 ';
86 } else {
87 $errorMessage = $this->getNamespaceError($filterNamespaces);
88 throw new InvalidArgumentException(
89 $errorMessage,
90 1297645190
91 );
92 }
93 }
94
95
96 /**
97 * Generates the API that is configured inside the ExtDirect configuration
98 * array "$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']".
99 *
100 * @param array $filerNamespace namespace that should be loaded like array('TYPO3.Backend')
101 * @return array javascript API configuration
102 */
103 protected function generateAPI(array $filterNamespaces) {
104 $javascriptNamespaces = array();
105 if (is_array($this->settings)) {
106 foreach ($this->settings as $javascriptName => $className) {
107 $splittedJavascriptName = explode('.', $javascriptName);
108 $javascriptObjectName = array_pop($splittedJavascriptName);
109 $javascriptNamespace = implode('.', $splittedJavascriptName);
110
111 // only items inside the wanted namespace
112 if (!$this->findNamespace($javascriptNamespace, $filterNamespaces)) {
113 continue;
114 }
115
116 if (!isset($javascriptNamespaces[$javascriptNamespace])) {
117 $javascriptNamespaces[$javascriptNamespace] = array(
118 'url' => $this->getRoutingUrl($javascriptNamespace),
119 'type' => 'remoting',
120 'actions' => array(),
121 'namespace' => $javascriptNamespace
122 );
123 }
124
125 $serverObject = t3lib_div::getUserObj($className, FALSE);
126 $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName] = array();
127 foreach (get_class_methods($serverObject) as $methodName) {
128 $reflectionMethod = new ReflectionMethod($serverObject, $methodName);
129 $numberOfParameters = $reflectionMethod->getNumberOfParameters();
130 $docHeader = $reflectionMethod->getDocComment();
131 $formHandler = (strpos($docHeader, '@formHandler') !== FALSE);
132
133 $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName][] = array(
134 'name' => $methodName,
135 'len' => $numberOfParameters,
136 'formHandler' => $formHandler
137 );
138 }
139 }
140 }
141
142 return $javascriptNamespaces;
143 }
144
145 /**
146 * Returns the convenient path for the routing Urls based on the TYPO3 mode.
147 *
148 * @param string $namespace
149 * @return string
150 */
151 public function getRoutingUrl($namespace) {
152 $url = '';
153 if (TYPO3_MODE === 'FE') {
154 $url = t3lib_div::locationHeaderUrl('?eID=ExtDirect&action=route&namespace=');
155 } else {
156 $url = t3lib_div::locationHeaderUrl(
157 t3lib_div::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir .
158 'ajax.php?ajaxID=ExtDirect::route&namespace='
159 );
160 }
161 $url .= rawurlencode($namespace);
162
163 return $url;
164 }
165
166 /**
167 * Generates the API or reads it from cache
168 *
169 * @param array $filterNamespaces
170 * @param bool $checkGetParam
171 * @return string $javascriptNamespaces
172 */
173 protected function getExtDirectApi(array $filterNamespaces) {
174 // Check GET-parameter no_cache and extCache setting
175 $noCache = isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['extCache']) && (
176 $GLOBALS['TYPO3_CONF_VARS']['SYS']['extCache'] === 0 ||
177 $GLOBALS['TYPO3_CONF_VARS']['SYS']['extCache'] === '0'
178 );
179 $noCache = t3lib_div::_GET('no_cache') ? TRUE : $noCache;
180
181 // look up into the cache
182 $cacheIdentifier = 'ExtDirectApi';
183 $cacheHash = md5($cacheIdentifier . implode(',', $filterNamespaces) . t3lib_div::getIndpEnv('TYPO3_SSL') .
184 serialize($this->settings) . TYPO3_MODE . t3lib_div::getIndpEnv('HTTP_HOST'));
185
186 // with no_cache always generate the javascript content
187 $cacheContent = $noCache ? '' : t3lib_pageSelect::getHash($cacheHash);
188
189 // generate the javascript content if it wasn't found inside the cache and cache it!
190 if (!$cacheContent) {
191 $javascriptNamespaces = $this->generateAPI($filterNamespaces);
192 if (count($javascriptNamespaces)) {
193 t3lib_pageSelect::storeHash(
194 $cacheHash,
195 serialize($javascriptNamespaces),
196 $cacheIdentifier
197 );
198 }
199 } else {
200 $javascriptNamespaces = unserialize($cacheContent);
201 }
202
203 return $javascriptNamespaces;
204 }
205
206 /**
207 * Generates the error message
208 *
209 * @param array $filterNamespaces
210 * @return string $errorMessage
211 */
212 protected function getNamespaceError(array $filterNamespaces) {
213 if (count($filterNamespaces)) {
214 // namespace error
215 $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:ExtDirect.namespaceError'),
216 __CLASS__, implode(',', $filterNamespaces)
217 );
218 }
219 else {
220 // no namespace given
221 $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:ExtDirect.noNamespace'),
222 __CLASS__
223 );
224 }
225
226 return $errorMessage;
227 }
228
229 /**
230 * Looks if the given namespace is present in $filterNamespaces
231 *
232 * @param string $namespace
233 * @param array $filterNamespaces
234 * @return bool
235 */
236 protected function findNamespace($namespace, array $filterNamespaces) {
237 if ($filterNamespaces === array('TYPO3')) {
238 return TRUE;
239 }
240 $found = FALSE;
241 foreach ($filterNamespaces as $filter) {
242 if (t3lib_div::isFirstPartOfStr($filter, $namespace)) {
243 $found = TRUE;
244 break;
245 }
246 }
247 return $found;
248 }
249 }
250
251 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/extjs/class.t3lib_extjs_extdirectapi.php'])) {
252 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/extjs/class.t3lib_extjs_extdirectapi.php']);
253 }
254
255 ?>