[TASK] Introduce abstract class AbstractAjaxController 48/51148/8
authorMona Muzaffar <mona.muzaffar@gmx.de>
Wed, 4 Jan 2017 15:31:56 +0000 (16:31 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Mon, 9 Jan 2017 16:25:45 +0000 (17:25 +0100)
Extract duplicate code in method and let FormFlexAjaxController
and FormInlineAjaxController extend from abstract class.

Change-Id: Idef25a758b6df857419983f0e6c93441103954a7
Resolves: #79159
Releases: master
Reviewed-on: https://review.typo3.org/51148
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/backend/Classes/Controller/AbstractFormEngineAjaxController.php [new file with mode: 0644]
typo3/sysext/backend/Classes/Controller/FormFlexAjaxController.php
typo3/sysext/backend/Classes/Controller/FormInlineAjaxController.php

diff --git a/typo3/sysext/backend/Classes/Controller/AbstractFormEngineAjaxController.php b/typo3/sysext/backend/Classes/Controller/AbstractFormEngineAjaxController.php
new file mode 100644 (file)
index 0000000..028ed08
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+declare(strict_types=1);
+namespace TYPO3\CMS\Backend\Controller;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+/**
+ * Abstract class for a couple of FormEngine controllers triggered by
+ * ajax calls. The class containers some helpers to for instance prepare
+ * the form render result for json output.
+ *
+ * @internal Marked as internal for now, methods in this class may change any time.
+ */
+abstract class AbstractFormEngineAjaxController
+{
+    /**
+     * Gets result array from FormEngine and returns string with js modules
+     * that need to be loaded and evaluated by JavaScript.
+     *
+     * @param array $result
+     * @return array
+     */
+    public function createExecutableStringRepresentationOfRegisteredRequireJsModules(array $result): array
+    {
+        if (empty($result['requireJsModules'])) {
+            return [];
+        }
+        $requireJs = [];
+        foreach ($result['requireJsModules'] as $module) {
+            $moduleName = null;
+            $callback = null;
+            if (is_string($module)) {
+                // if $module is a string, no callback
+                $moduleName = $module;
+                $callback = null;
+            } elseif (is_array($module)) {
+                // if $module is an array, callback is possible
+                foreach ($module as $key => $value) {
+                    $moduleName = $key;
+                    $callback = $value;
+                    break;
+                }
+            }
+            if ($moduleName !== null) {
+                $inlineCodeKey = $moduleName;
+                $javaScriptCode = 'require(["' . $moduleName . '"]';
+                if ($callback !== null) {
+                    $inlineCodeKey .= sha1($callback);
+                    $javaScriptCode .= ', ' . $callback;
+                }
+                $javaScriptCode .= ');';
+                $requireJs[] = '/*RequireJS-Module-' . $inlineCodeKey . '*/' . LF . $javaScriptCode;
+            }
+        }
+        return $requireJs;
+    }
+}
index cbcf314..5eba4d0 100644 (file)
@@ -27,7 +27,7 @@ use TYPO3\CMS\Core\Utility\StringUtility;
 /**
  * Handle FormEngine flex field ajax calls
  */
-class FormFlexAjaxController
+class FormFlexAjaxController extends AbstractFormEngineAjaxController
 {
     /**
      * Render a single flex form section container to add it to the DOM
@@ -126,35 +126,8 @@ class FormFlexAjaxController
         }
         // @todo: handle stylesheetFiles, additionalInlineLanguageLabelFiles
 
-        // @todo: copied from inline ajax handler - maybe extract to some abstract?
-        if (!empty($newContainerResult['requireJsModules'])) {
-            foreach ($newContainerResult['requireJsModules'] as $module) {
-                $moduleName = null;
-                $callback = null;
-                if (is_string($module)) {
-                    // if $module is a string, no callback
-                    $moduleName = $module;
-                    $callback = null;
-                } elseif (is_array($module)) {
-                    // if $module is an array, callback is possible
-                    foreach ($module as $key => $value) {
-                        $moduleName = $key;
-                        $callback = $value;
-                        break;
-                    }
-                }
-                if ($moduleName !== null) {
-                    $inlineCodeKey = $moduleName;
-                    $javaScriptCode = 'require(["' . $moduleName . '"]';
-                    if ($callback !== null) {
-                        $inlineCodeKey .= sha1($callback);
-                        $javaScriptCode .= ', ' . $callback;
-                    }
-                    $javaScriptCode .= ');';
-                    $jsonResult['scriptCall'][] = '/*RequireJS-Module-' . $inlineCodeKey . '*/' . LF . $javaScriptCode;
-                }
-            }
-        }
+        $requireJsModule = $this->createExecutableStringRepresentationOfRegisteredRequireJsModules($newContainerResult);
+        $jsonResult['scriptCall'] = array_merge($requireJsModule, $jsonResult['scriptCall']);
 
         $response->getBody()->write(json_encode($jsonResult));
 
index e005f1c..12f6bc9 100644 (file)
@@ -33,7 +33,7 @@ use TYPO3\CMS\Core\Utility\MathUtility;
 /**
  * Handle FormEngine inline ajax calls
  */
-class FormInlineAjaxController
+class FormInlineAjaxController extends AbstractFormEngineAjaxController
 {
     /**
      * Create a new inline child via AJAX.
@@ -678,34 +678,9 @@ class FormInlineAjaxController
 
             $jsonResult['scriptCall'][] = implode(LF, $javaScriptCode);
         }
-        if (!empty($childResult['requireJsModules'])) {
-            foreach ($childResult['requireJsModules'] as $module) {
-                $moduleName = null;
-                $callback = null;
-                if (is_string($module)) {
-                    // if $module is a string, no callback
-                    $moduleName = $module;
-                    $callback = null;
-                } elseif (is_array($module)) {
-                    // if $module is an array, callback is possible
-                    foreach ($module as $key => $value) {
-                        $moduleName = $key;
-                        $callback = $value;
-                        break;
-                    }
-                }
-                if ($moduleName !== null) {
-                    $inlineCodeKey = $moduleName;
-                    $javaScriptCode = 'require(["' . $moduleName . '"]';
-                    if ($callback !== null) {
-                        $inlineCodeKey .= sha1($callback);
-                        $javaScriptCode .= ', ' . $callback;
-                    }
-                    $javaScriptCode .= ');';
-                    $jsonResult['scriptCall'][] = '/*RequireJS-Module-' . $inlineCodeKey . '*/' . LF . $javaScriptCode;
-                }
-            }
-        }
+        $requireJsModule = $this->createExecutableStringRepresentationOfRegisteredRequireJsModules($childResult);
+        $jsonResult['scriptCall'] = array_merge($requireJsModule, $jsonResult['scriptCall']);
+
         return $jsonResult;
     }