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