[TASK] Remove dependency to extbase from reports module 25/56125/10
authorGeorg Ringer <georg.ringer@gmail.com>
Tue, 13 Mar 2018 08:13:36 +0000 (09:13 +0100)
committerSusanne Moog <susanne.moog@typo3.org>
Thu, 15 Mar 2018 20:46:24 +0000 (21:46 +0100)
Simplify the reports module by removing usage of extbase.

Resolves: #84231
Releases: master
Change-Id: I33d55e60b454b0945a7a98f7cafb15bfc69dc18d
Reviewed-on: https://review.typo3.org/56125
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
typo3/sysext/reports/Classes/Controller/ReportController.php
typo3/sysext/reports/Classes/Report/ServicesListReport.php
typo3/sysext/reports/Resources/Private/Templates/Report/Detail.html
typo3/sysext/reports/Resources/Private/Templates/Report/Index.html
typo3/sysext/reports/Tests/Unit/Report/ServicesListReportTest.php
typo3/sysext/reports/ext_tables.php

index d411ca1..a800bee 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+declare(strict_types = 1);
 namespace TYPO3\CMS\Reports\Controller;
 
 /*
@@ -13,30 +14,35 @@ namespace TYPO3\CMS\Reports\Controller;
  *
  * The TYPO3 project - inspiring people to share!
  */
-use TYPO3\CMS\Backend\View\BackendTemplateView;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use TYPO3\CMS\Backend\Routing\UriBuilder;
+use TYPO3\CMS\Backend\Template\ModuleTemplate;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
+use TYPO3\CMS\Core\Http\HtmlResponse;
+use TYPO3\CMS\Core\Http\RedirectResponse;
 use TYPO3\CMS\Core\Localization\LanguageService;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
-use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
+use TYPO3\CMS\Fluid\View\StandaloneView;
 use TYPO3\CMS\Reports\ReportInterface;
+use TYPO3Fluid\Fluid\View\ViewInterface;
 
 /**
  * Reports controller
  */
-class ReportController extends ActionController
+class ReportController
 {
     /**
-     * @var BackendTemplateView
+     * ModuleTemplate object
+     *
+     * @var ModuleTemplate
      */
-    protected $view;
+    protected $moduleTemplate;
 
     /**
-     * BackendTemplateView Container
-     *
-     * @var BackendTemplateView
+     * @var ViewInterface
      */
-    protected $defaultViewObjectName = BackendTemplateView::class;
+    protected $view;
 
     /**
      * Module name for the shortcut
@@ -46,37 +52,65 @@ class ReportController extends ActionController
     protected $shortcutName;
 
     /**
-     * Redirect to the saved report
+     * Instantiate the report controller
      */
-    public function initializeAction()
+    public function __construct()
     {
-        $vars = GeneralUtility::_GET('tx_reports_system_reportstxreportsm1');
-        if (!isset($vars['redirect']) && $vars['action'] !== 'index' && !isset($vars['extension']) && is_array($GLOBALS['BE_USER']->uc['reports']['selection'])) {
+        $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
+    }
+
+    /**
+     * Injects the request object for the current request, and renders correct action
+     *
+     * @param ServerRequestInterface $request the current request
+     * @return ResponseInterface the response with the content
+     */
+    public function handleRequest(ServerRequestInterface $request): ResponseInterface
+    {
+        $actionDefault = $request->getQueryParams()['action'] ?? $request->getParsedBody()['action'];
+        $action = $actionDefault ?: 'index';
+        $extension = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
+        $isRedirect = $request->getQueryParams()['redirect'] ?? $request->getParsedBody()['redirect'] ?? false;
+
+        if ($actionDefault !== 'index' && !$isRedirect && !$extension
+            && is_array($GLOBALS['BE_USER']->uc['reports']['selection'])) {
             $previousSelection = $GLOBALS['BE_USER']->uc['reports']['selection'];
             if (!empty($previousSelection['extension']) && !empty($previousSelection['report'])) {
-                $this->redirect('detail', 'Report', null, [
+                $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
+                return new RedirectResponse((string)$uriBuilder->buildUriFromRoute('system_reports', [
+                    'action' => 'detail',
                     'extension' => $previousSelection['extension'],
                     'report' => $previousSelection['report'],
                     'redirect' => 1,
-                ]);
-            } else {
-                $this->redirect('index');
+                ]), 303);
             }
         }
+
+        $this->initializeView($action);
+
+        $result = call_user_func_array([$this, $action . 'Action'], [$request]);
+        if ($result instanceof ResponseInterface) {
+            return $result;
+        }
+
+        $this->generateMenu($request);
+        $this->generateButtons();
+
+        $this->moduleTemplate->setContent($this->view->render());
+        return new HtmlResponse($this->moduleTemplate->renderContent());
     }
 
     /**
-     * Initialize the view
-     *
-     * @param ViewInterface $view The view
+     * @param string $templateName
      */
-    protected function initializeView(ViewInterface $view)
+    protected function initializeView(string $templateName)
     {
-        /** @var BackendTemplateView $view */
-        parent::initializeView($view);
-        $view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation([]);
-        $this->generateMenu();
-        $this->generateButtons();
+        $this->view = GeneralUtility::makeInstance(StandaloneView::class);
+        $this->view->setTemplate($templateName);
+        $this->view->setTemplateRootPaths(['EXT:reports/Resources/Private/Templates/Report']);
+        $this->view->setPartialRootPaths(['EXT:reports/Resources/Private/Partials']);
+        $this->view->setLayoutRootPaths(['EXT:reports/Resources/Private/Layouts']);
+        $this->view->getRequest()->setControllerExtensionName('Reports');
     }
 
     /**
@@ -91,13 +125,15 @@ class ReportController extends ActionController
     /**
      * Display a single report
      *
-     * @param string $extension Extension
-     * @param string $report Report
+     * @param ServerRequestInterface $request
      */
-    public function detailAction($extension, $report)
+    public function detailAction(ServerRequestInterface $request)
     {
-        $content = ($error = '');
-        $reportClass = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report]['report'] ?? $this->redirect('index');
+        $content = $error = '';
+        $extension = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
+        $report = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'];
+
+        $reportClass = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'][$extension][$report]['report'] ?? null;
 
         $reportInstance = GeneralUtility::makeInstance($reportClass, $this);
         if ($reportInstance instanceof ReportInterface) {
@@ -115,57 +151,58 @@ class ReportController extends ActionController
 
     /**
      * Generates the menu
+     *
+     * @param ServerRequestInterface $request
      */
-    protected function generateMenu()
+    protected function generateMenu(ServerRequestInterface $request)
     {
+        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
         $lang = $this->getLanguageService();
         $lang->includeLLFile('EXT:reports/Resources/Private/Language/locallang.xlf');
-        $menu = $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
+        $menu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
         $menu->setIdentifier('WebFuncJumpMenu');
         $menuItem = $menu
             ->makeMenuItem()
             ->setHref(
-                $this->uriBuilder->reset()->uriFor('index', null, 'Report')
+                $uriBuilder->buildUriFromRoute('system_reports', ['action' => 'index'])
             )
             ->setTitle($lang->getLL('reports_overview'));
         $menu->addMenuItem($menuItem);
         $this->shortcutName = $lang->getLL('reports_overview');
+
+        $extensionParam = $request->getQueryParams()['extension'] ?? $request->getParsedBody()['extension'];
+        $reportParam = $request->getQueryParams()['report'] ?? $request->getParsedBody()['report'];
+
         foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports'] as $extKey => $reports) {
             foreach ($reports as $reportName => $report) {
                 $menuItem = $menu
                     ->makeMenuItem()
-                    ->setHref($this->uriBuilder->reset()->uriFor('detail', ['extension' => $extKey, 'report' => $reportName], 'Report'))
+                    ->setHref($uriBuilder->buildUriFromRoute(
+                        'system_reports',
+                        ['action' => 'detail', 'extension' => $extKey, 'report' => $reportName]
+                    ))
                     ->setTitle($this->getLanguageService()->sL($report['title']));
-                if ($this->arguments->hasArgument('extension') && $this->arguments->hasArgument('report')) {
-                    if ($this->arguments->getArgument('extension')->getValue() === $extKey && $this->arguments->getArgument('report')->getValue() === $reportName) {
-                        $menuItem->setActive(true);
-                        $this->shortcutName = $menuItem->getTitle();
-                    }
+                if ($extensionParam === $extKey && $reportParam === $reportName) {
+                    $menuItem->setActive(true);
+                    $this->shortcutName = $menuItem->getTitle();
                 }
                 $menu->addMenuItem($menuItem);
             }
         }
-        $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
+        $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
     }
 
     /**
-     * Gets all buttons for the docheader
+     * Gets all buttons for the docHeader
      */
     protected function generateButtons()
     {
-        $buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar();
-        $moduleName = $this->request->getPluginName();
-        $getVars = $this->request->hasArgument('getVars') ? $this->request->getArgument('getVars') : [];
-        $setVars = $this->request->hasArgument('setVars') ? $this->request->getArgument('setVars') : [];
-        if (count($getVars) === 0) {
-            $modulePrefix = strtolower('tx_' . $this->request->getControllerExtensionName() . '_' . $moduleName);
-            $getVars = ['id', 'route', $modulePrefix];
-        }
+        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
+
         $shortcutButton = $buttonBar->makeShortcutButton()
-            ->setModuleName($moduleName)
-            ->setGetVariables($getVars)
-            ->setDisplayName($this->shortcutName)
-            ->setSetVariables($setVars);
+            ->setModuleName('system_reports')
+            ->setGetVariables(['action', 'extension', 'report'])
+            ->setDisplayName($this->shortcutName);
         $buttonBar->addButton($shortcutButton);
     }
 
@@ -175,7 +212,7 @@ class ReportController extends ActionController
      * @param string $extension Extension name
      * @param string $report Report name
      */
-    protected function saveState($extension = '', $report = '')
+    protected function saveState(string $extension = '', string $report = '')
     {
         $this->getBackendUser()->uc['reports']['selection'] = [
             'extension' => $extension,
@@ -187,7 +224,7 @@ class ReportController extends ActionController
     /**
      * @return BackendUserAuthentication
      */
-    protected function getBackendUser()
+    protected function getBackendUser(): BackendUserAuthentication
     {
         return $GLOBALS['BE_USER'];
     }
@@ -195,7 +232,7 @@ class ReportController extends ActionController
     /**
      * @return LanguageService
      */
-    protected function getLanguageService()
+    protected function getLanguageService(): LanguageService
     {
         return $GLOBALS['LANG'];
     }
index be7cf9f..6f93e72 100644 (file)
@@ -52,6 +52,7 @@ class ServicesListReport implements ReportInterface
     {
         // Rendering of the output via fluid
         $view = GeneralUtility::makeInstance(StandaloneView::class);
+        $view->getRequest()->setControllerExtensionName('Reports');
         $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
             'EXT:reports/Resources/Private/Templates/ServicesListReport.html'
         ));
index c3f2569..fcd0b19 100644 (file)
@@ -1,4 +1,7 @@
-{namespace r=TYPO3\CMS\Reports\ViewHelpers}
+<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
+                       xmlns:r="http://typo3.org/ns/TYPO3/CMS/Reports/ViewHelpers"
+                       xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers"
+                       data-namespace-typo3-fluid="true">
 
 <f:layout name="Default" />
 
                <f:for each="{reports}" as="reportOverview" key="extensionKey">
                        <f:for each="{reportOverview}" key="reportName" as="report">
                                <dt>
-                                       <f:link.action action="detail" arguments="{extension:extensionKey,report:reportName}">
+                                       <a href="{be:moduleLink(route:'system_reports',arguments:{action:'detail', extension:extensionKey, report:reportName})}">
                                                <r:icon icon="{report.icon}" title="{f:translate(key:report.title)}" />
                                                <f:translate key="{report.title}" />
-                                       </f:link.action>
+                                       </a>
                                </dt>
                                <dd><f:translate key="{report.description}" /></dd>
                        </f:for>
                </f:for>
        </dl>
-</f:section>
\ No newline at end of file
+</f:section>
index bd4eddc..a242603 100644 (file)
@@ -19,6 +19,7 @@ use Prophecy\Argument;
 use Prophecy\Prophecy\ObjectProphecy;
 use TYPO3\CMS\Core\Localization\LanguageService;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Mvc\Request;
 use TYPO3\CMS\Fluid\View\StandaloneView;
 use TYPO3\CMS\Reports\Controller\ReportController;
 use TYPO3\CMS\Reports\Report\ServicesListReport;
@@ -70,7 +71,10 @@ class ServicesListReportTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestC
         $templatePath = GeneralUtility::getFileAbsFileName(
             'EXT:reports/Resources/Private/Templates/ServicesListReport.html'
         );
+        $serverRequestProphecy = $this->prophesize(Request::class);
+        /** @var ObjectProphecy $standaloneViewProphecy */
         $standaloneViewProphecy = $this->prophesize(StandaloneView::class);
+        $standaloneViewProphecy->getRequest()->willReturn($serverRequestProphecy->reveal());
         $standaloneViewProphecy->setTemplatePathAndFilename($templatePath)->shouldBeCalled();
         $standaloneViewProphecy->assignMultiple(Argument::any())->willReturn($standaloneViewProphecy->reveal());
         $standaloneViewProphecy->render()->willReturn('<p>Template output</p>');
index 2fe2d1a..d6d8935 100644 (file)
@@ -1,16 +1,15 @@
 <?php
 defined('TYPO3_MODE') or die();
 
-\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerModule(
-    'TYPO3.CMS.Reports',
+\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule(
     'system',
-    'txreportsM1',
+    'reports',
+    '',
     '',
     [
-        'Report' => 'index,detail'
-    ],
-    [
+        'routeTarget' => \TYPO3\CMS\Reports\Controller\ReportController::class . '::handleRequest',
         'access' => 'admin',
+        'name' => 'system_reports',
         'icon' => 'EXT:reports/Resources/Public/Icons/module-reports.svg',
         'labels' => 'LLL:EXT:reports/Resources/Private/Language/locallang.xlf'
     ]