[TASK] Introduce BackendActionController 49/43349/5
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Tue, 15 Sep 2015 15:49:53 +0000 (17:49 +0200)
committerBenjamin Kott <info@bk2k.info>
Sun, 4 Oct 2015 11:48:35 +0000 (13:48 +0200)
To make use of the newly introduced ModuleTemplate API, a new
Controller for Backend Modules provided with extbase is created.
It prepares the ModuleTemplate and some convinience methods to
unify the docheader throughout all modules.

Change-Id: I29b1b65d79abc9a1a9035ce7c4cc7069719caea9
Resolves: #69842
Relates: #69841
Releases: master
Reviewed-on: http://review.typo3.org/43349
Reviewed-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Tested-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Benjamin Kott <info@bk2k.info>
Tested-by: Benjamin Kott <info@bk2k.info>
typo3/sysext/extbase/Classes/Mvc/Controller/ActionController.php
typo3/sysext/extbase/Classes/Mvc/Controller/BackendActionController.php [new file with mode: 0644]

index 85c0e66..8cb8cd4 100644 (file)
@@ -266,49 +266,9 @@ class ActionController extends AbstractController {
         * @api
         */
        protected function callActionMethod() {
-               $preparedArguments = array();
-               /** @var \TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument */
-               foreach ($this->arguments as $argument) {
-                       $preparedArguments[] = $argument->getValue();
-               }
-               $validationResult = $this->arguments->getValidationResults();
-               if (!$validationResult->hasErrors()) {
-                       $this->emitBeforeCallActionMethodSignal($preparedArguments);
-                       $actionResult = call_user_func_array(array($this, $this->actionMethodName), $preparedArguments);
-               } else {
-                       $methodTagsValues = $this->reflectionService->getMethodTagsValues(get_class($this), $this->actionMethodName);
-                       $ignoreValidationAnnotations = array();
-                       if (isset($methodTagsValues['ignorevalidation'])) {
-                               $ignoreValidationAnnotations = $methodTagsValues['ignorevalidation'];
-                       }
-                       // if there exist errors which are not ignored with @ignorevalidation => call error method
-                       // else => call action method
-                       $shouldCallActionMethod = TRUE;
-                       foreach ($validationResult->getSubResults() as $argumentName => $subValidationResult) {
-                               if (!$subValidationResult->hasErrors()) {
-                                       continue;
-                               }
-                               if (array_search('$' . $argumentName, $ignoreValidationAnnotations) !== FALSE) {
-                                       continue;
-                               }
-                               $shouldCallActionMethod = FALSE;
-                               break;
-                       }
-                       if ($shouldCallActionMethod) {
-                               $this->emitBeforeCallActionMethodSignal($preparedArguments);
-                               $actionResult = call_user_func_array(array($this, $this->actionMethodName), $preparedArguments);
-                       } else {
-                               $actionResult = call_user_func(array($this, $this->errorMethodName));
-                       }
-               }
-
-               if ($actionResult === NULL && $this->view instanceof ViewInterface) {
-                       $this->response->appendContent($this->view->render());
-               } elseif (is_string($actionResult) && $actionResult !== '') {
-                       $this->response->appendContent($actionResult);
-               } elseif (is_object($actionResult) && method_exists($actionResult, '__toString')) {
-                       $this->response->appendContent((string)$actionResult);
-               }
+               $actionResult = $this->processActionResult();
+               $content = $this->resolveActionResult($actionResult);
+               $this->appendContent($content);
        }
 
        /**
@@ -613,4 +573,76 @@ class ActionController extends AbstractController {
                return $result;
        }
 
-}
+       /**
+        * If the action returns a string, it is appended to the content in the
+        * response object. If the action does not return anything and a valid
+        * view exists, the view is rendered automatically.
+        *
+        * @param string|object $actionResult
+        * @return NULL|string
+        */
+       protected function resolveActionResult($actionResult) {
+               if ($actionResult === NULL && $this->view instanceof ViewInterface) {
+                       return $this->view->render();
+               } elseif (is_string($actionResult) && $actionResult !== '') {
+                       return $actionResult;
+               } elseif (is_object($actionResult) && method_exists($actionResult, '__toString')) {
+                       return (string)$actionResult;
+               }
+               return NULL;
+       }
+
+       /**
+        * Appends content to response object.
+        *
+        * @param string $content
+        */
+       protected function appendContent($content) {
+               $this->response->appendContent($content);
+       }
+
+       /**
+        * Calls the specified action method and passes the arguments.
+        *
+       * @return string|object
+        */
+       protected function processActionResult() {
+               $preparedArguments = array();
+               foreach ($this->arguments as $argument) {
+                       $preparedArguments[] = $argument->getValue();
+               }
+               $validationResult = $this->arguments->getValidationResults();
+               if (!$validationResult->hasErrors()) {
+                       $this->emitBeforeCallActionMethodSignal($preparedArguments);
+                       $actionResult = call_user_func_array(array($this, $this->actionMethodName), $preparedArguments);
+                       return $actionResult;
+               } else {
+                       $methodTagsValues = $this->reflectionService->getMethodTagsValues(get_class($this), $this->actionMethodName);
+                       $ignoreValidationAnnotations = array();
+                       if (isset($methodTagsValues['ignorevalidation'])) {
+                               $ignoreValidationAnnotations = $methodTagsValues['ignorevalidation'];
+                       }
+                       // if there exist errors which are not ignored with @ignorevalidation => call error method
+                       // else => call action method
+                       $shouldCallActionMethod = TRUE;
+                       foreach ($validationResult->getSubResults() as $argumentName => $subValidationResult) {
+                               if (!$subValidationResult->hasErrors()) {
+                                       continue;
+                               }
+                               if (array_search('$' . $argumentName, $ignoreValidationAnnotations) !== FALSE) {
+                                       continue;
+                               }
+                               $shouldCallActionMethod = FALSE;
+                               break;
+                       }
+                       if ($shouldCallActionMethod) {
+                               $this->emitBeforeCallActionMethodSignal($preparedArguments);
+                               $actionResult = call_user_func_array(array($this, $this->actionMethodName), $preparedArguments);
+                               return $actionResult;
+                       } else {
+                               $actionResult = call_user_func(array($this, $this->errorMethodName));
+                               return $actionResult;
+                       }
+               }
+       }
+}
\ No newline at end of file
diff --git a/typo3/sysext/extbase/Classes/Mvc/Controller/BackendActionController.php b/typo3/sysext/extbase/Classes/Mvc/Controller/BackendActionController.php
new file mode 100644 (file)
index 0000000..cf5f254
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+namespace TYPO3\CMS\Extbase\Mvc\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!
+ */
+
+use TYPO3\CMS\Backend\Template\ModuleTemplate;
+use TYPO3\CMS\Core\Imaging\IconFactory;
+
+/**
+ * For Usage in Backend Modules.
+ *
+ * Renders the DocHeader component before the content
+ * of the module and serves as generic layout provider.
+ */
+class BackendActionController extends ActionController {
+
+       /**
+        * @var ModuleTemplate
+        */
+       protected $moduleTemplate;
+
+       /**
+        * @var IconFactory
+        */
+       protected $iconFactory;
+
+       /**
+        * @param ModuleTemplate $moduleTemplate
+        */
+       public function injectModuleTemplate(ModuleTemplate $moduleTemplate) {
+               $this->moduleTemplate = $moduleTemplate;
+       }
+
+       /**
+        * @param IconFactory $iconFactory
+        */
+       public function injectIconFactory(IconFactory $iconFactory) {
+               $this->iconFactory = $iconFactory;
+       }
+
+       /**
+        * Appends content to response object.
+        * The actual module content is wrapped
+        * in the module template component.
+        *
+        * @param string $content
+        */
+       protected function appendContent($content) {
+               $this->moduleTemplate->setContent($content);
+               $this->response->appendContent($this->moduleTemplate->renderContent());
+       }
+
+}
\ No newline at end of file