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