2 /***************************************************************
5 * (c) 2010-2011 Sebastian Kurfürst <sebastian@typo3.org>
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.
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.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * Ext Direct API Generator
31 * @author Sebastian Kurfürst <sebastian@typo3.org>
32 * @author Stefan Galinski <stefan.galinski@gmail.com>
35 class t3lib_extjs_ExtDirectApi
{
39 protected $settings = array();
42 * Constructs this object.
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'];
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.
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
60 * @param array $ajaxParams ajax parameters
61 * @param TYPO3AJAX $ajaxObj typo3ajax instance
64 public function getAPI($ajaxParams, TYPO3AJAX
$ajaxObj) {
65 $ajaxObj->setContent(array());
69 * Get the API for a given nameapace
71 * @throws InvalidArgumentException
72 * @param array $filterNamespaces
75 public function getApiPhp(array $filterNamespaces) {
76 $javascriptNamespaces = $this->getExtDirectApi($filterNamespaces);
77 // return the generated javascript API configuration
78 if (count($javascriptNamespaces)) {
80 if (!Ext.isObject(Ext.app.ExtDirectAPI)) {
81 Ext.app.ExtDirectAPI = {};
83 Ext.apply(Ext.app.ExtDirectAPI, ' .
84 json_encode($javascriptNamespaces) . ');
87 $errorMessage = $this->getNamespaceError($filterNamespaces);
88 throw new InvalidArgumentException(
97 * Generates the API that is configured inside the ExtDirect configuration
98 * array "$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']".
100 * @param array $filerNamespace namespace that should be loaded like array('TYPO3.Backend')
101 * @return array javascript API configuration
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);
111 // only items inside the wanted namespace
112 if (!$this->findNamespace($javascriptNamespace, $filterNamespaces)) {
116 if (!isset($javascriptNamespaces[$javascriptNamespace])) {
117 $javascriptNamespaces[$javascriptNamespace] = array(
118 'url' => $this->getRoutingUrl($javascriptNamespace),
119 'type' => 'remoting',
120 'actions' => array(),
121 'namespace' => $javascriptNamespace
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
);
133 $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName][] = array(
134 'name' => $methodName,
135 'len' => $numberOfParameters,
136 'formHandler' => $formHandler
142 return $javascriptNamespaces;
146 * Returns the convenient path for the routing Urls based on the TYPO3 mode.
148 * @param string $namespace
151 public function getRoutingUrl($namespace) {
153 if (TYPO3_MODE
=== 'FE') {
154 $url = t3lib_div
::locationHeaderUrl('?eID=ExtDirect&action=route&namespace=');
156 $url = t3lib_div
::locationHeaderUrl(
157 t3lib_div
::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir
.
158 'ajax.php?ajaxID=ExtDirect::route&namespace='
161 $url .= rawurlencode($namespace);
167 * Generates the API or reads it from cache
169 * @param array $filterNamespaces
170 * @param bool $checkGetParam
171 * @return string $javascriptNamespaces
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'
179 $noCache = t3lib_div
::_GET('no_cache') ? TRUE
: $noCache;
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'));
186 // with no_cache always generate the javascript content
187 $cacheContent = $noCache ?
'' : t3lib_pageSelect
::getHash($cacheHash);
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(
195 serialize($javascriptNamespaces),
200 $javascriptNamespaces = unserialize($cacheContent);
203 return $javascriptNamespaces;
207 * Generates the error message
209 * @param array $filterNamespaces
210 * @return string $errorMessage
212 protected function getNamespaceError(array $filterNamespaces) {
213 if (count($filterNamespaces)) {
215 $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:ExtDirect.namespaceError'),
216 __CLASS__
, implode(',', $filterNamespaces)
220 // no namespace given
221 $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:ExtDirect.noNamespace'),
226 return $errorMessage;
230 * Looks if the given namespace is present in $filterNamespaces
232 * @param string $namespace
233 * @param array $filterNamespaces
236 protected function findNamespace($namespace, array $filterNamespaces) {
237 if ($filterNamespaces === array('TYPO3')) {
241 foreach ($filterNamespaces as $filter) {
242 if (t3lib_div
::isFirstPartOfStr($filter, $namespace)) {
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']);