Added feature #13313: ExtDirect API (thanks to Stefan Galinski and the T3UXW09 team)
[Packages/TYPO3.CMS.git] / t3lib / extjs / class.t3lib_extjs_extdirectapi.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Sebastian Kurfuerst <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 Kurfuerst <sebastian@typo3.org>
32 * @author Stefan Galinski <stefan.galinski@gmail.com>
33 * @package TYPO3
34 */
35 class t3lib_ExtJs_ExtDirectAPI {
36 /**
37 * Parses the ExtDirect configuration array "$GLOBALS['TYPO3_CONF_VARS']['BE']['ExtDirect']"
38 * and feeds the given typo3ajax instance with the resulting informations. The get parameter
39 * "namespace" will be used to filter the configuration.
40 *
41 * This method makes usage of the reflection mechanism to fetch the methods inside the
42 * defined classes together with their amount of parameters. This informations are building
43 * the API and are required by ExtDirect.
44 *
45 * @param array $ajaxParams ajax parameters
46 * @param TYPO3AJAX $ajaxObj typo3ajax instance
47 * @return void
48 */
49 public function getAPI($ajaxParams, TYPO3AJAX $ajaxObj) {
50 $filterNamespace = t3lib_div::_GET('namespace');
51
52 $javascriptNamespaces = array();
53 if (is_array($GLOBALS['TYPO3_CONF_VARS']['BE']['ExtDirect'])) {
54 foreach ($GLOBALS['TYPO3_CONF_VARS']['BE']['ExtDirect'] as $javascriptName => $className) {
55 $splittedJavascriptName = explode('.', $javascriptName);
56 $javascriptObjectName = array_pop($splittedJavascriptName);
57 $javascriptNamespace = implode('.', $splittedJavascriptName);
58
59 // only items inside the wanted namespace
60 if (strpos($javascriptNamespace, $filterNamespace) !== 0) {
61 continue;
62 }
63
64 if (!isset($javascriptNamespaces[$javascriptNamespace])) {
65 $javascriptNamespaces[$javascriptNamespace] = array(
66 'url' => 'ajax.php?ajaxID=ExtDirect::route&namespace=' . rawurlencode($javascriptNamespace),
67 'type' => 'remoting',
68 'actions' => array(),
69 'namespace' => $javascriptNamespace
70 );
71 }
72
73 $serverObject = t3lib_div::getUserObj($className, FALSE);
74 $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName] = array();
75 foreach (get_class_methods($serverObject) as $methodName) {
76 $reflectionMethod = new ReflectionMethod($serverObject, $methodName);
77 $numberOfParameters = $reflectionMethod->getNumberOfParameters();
78
79 $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName][] = array(
80 'name' => $methodName,
81 'len' => $numberOfParameters
82 );
83 }
84 }
85 }
86
87 if (count($javascriptNamespaces)) {
88 $setup = '
89 if (typeof Ext.app.ExtDirectAPI != "object") {
90 Ext.app.ExtDirectAPI = {};
91 }
92 ';
93
94 $ajaxObj->setContent($javascriptNamespaces);
95 $ajaxObj->setContentFormat('javascript');
96 $ajaxObj->setJavascriptCallbackWrap($setup . 'Ext.app.ExtDirectAPI = Object.extend(Ext.app.ExtDirectAPI, |);');
97 }
98 }
99 }
100
101 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extjs_extdirectapi.php']) {
102 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extjs_extdirectapi.php']);
103 }
104
105 ?>