[TASK] Move ViewHelper arguments to initializeArguments() in ext:belog 68/48768/5
authorWouter Wolters <typo3@wouterwolters.nl>
Fri, 1 Jul 2016 12:01:01 +0000 (14:01 +0200)
committerSusanne Moog <typo3@susannemoog.de>
Thu, 21 Jul 2016 05:12:58 +0000 (07:12 +0200)
Resolves: #76907
Releases: master
Change-Id: I585c785c0eb83980e7d7804fd1699de8dafffc82
Reviewed-on: https://review.typo3.org/48768
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Tested-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Susanne Moog <typo3@susannemoog.de>
Tested-by: Susanne Moog <typo3@susannemoog.de>
typo3/sysext/belog/Classes/ViewHelpers/Be/PagePathViewHelper.php
typo3/sysext/belog/Classes/ViewHelpers/ErrorIconViewHelper.php
typo3/sysext/belog/Classes/ViewHelpers/FormatDetailsViewHelper.php
typo3/sysext/belog/Classes/ViewHelpers/HistoryEntryViewHelper.php
typo3/sysext/belog/Classes/ViewHelpers/IsExtensionLoadedViewHelper.php
typo3/sysext/belog/Classes/ViewHelpers/UsernameViewHelper.php
typo3/sysext/belog/Classes/ViewHelpers/WorkspaceTitleViewHelper.php

index 382430e..cd4df4c 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Belog\ViewHelpers\Be;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Fluid\ViewHelpers\Be\AbstractBackendViewHelper;
 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 
@@ -24,19 +25,24 @@ use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 class PagePathViewHelper extends AbstractBackendViewHelper
 {
     /**
+     * Initializes the arguments
+     */
+    public function initializeArguments()
+    {
+        parent::initializeArguments();
+        $this->registerArgument('pid', 'int', 'Pid of the page', true);
+        $this->registerArgument('titleLimit', 'int', 'Limit of the page title', false, 20);
+    }
+
+    /**
      * Resolve page id to page path string (with automatic cropping to maximum given length).
      *
-     * @param int $pid Pid of the page
-     * @param int $titleLimit Limit of the page title
      * @return string Page path string
      */
-    public function render($pid, $titleLimit = 20)
+    public function render()
     {
         return static::renderStatic(
-            array(
-                'pid' => $pid,
-                'titleLimit' => $titleLimit
-            ),
+            $this->arguments,
             $this->buildRenderChildrenClosure(),
             $this->renderingContext
         );
@@ -51,6 +57,6 @@ class PagePathViewHelper extends AbstractBackendViewHelper
      */
     public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
     {
-        return \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordPath($arguments['pid'], '', $arguments['titleLimit']);
+        return BackendUtility::getRecordPath($arguments['pid'], '', $arguments['titleLimit']);
     }
 }
index 43f18c5..853d9af 100644 (file)
@@ -34,18 +34,24 @@ class ErrorIconViewHelper extends AbstractBackendViewHelper
     protected $escapeOutput = false;
 
     /**
+     * Initializes the arguments
+     */
+    public function initializeArguments()
+    {
+        parent::initializeArguments();
+        $this->registerArgument('errorNumber', 'int', 'The error number (0 ... 3)', false, 0);
+    }
+
+    /**
      * Renders an error icon link as known from the TYPO3 backend.
      * Error codes 2 and three are mapped to "error" and 1 is mapped to "warning".
      *
-     * @param int $errorNumber The error number (0 ... 3)
      * @return string the rendered error icon link
      */
-    public function render($errorNumber = 0)
+    public function render()
     {
         return static::renderStatic(
-            array(
-                'errorNumber' => $errorNumber
-            ),
+            $this->arguments,
             $this->buildRenderChildrenClosure(),
             $this->renderingContext
         );
index 6013495..fd95264 100644 (file)
@@ -25,6 +25,15 @@ use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 class FormatDetailsViewHelper extends AbstractViewHelper
 {
     /**
+     * Initializes the arguments
+     */
+    public function initializeArguments()
+    {
+        parent::initializeArguments();
+        $this->registerArgument('logEntry', LogEntry::class, '', true);
+    }
+
+    /**
      * Create formatted detail string from log row.
      *
      * The method handles two properties of the model: details and logData
@@ -33,15 +42,12 @@ class FormatDetailsViewHelper extends AbstractViewHelper
      * Furthermore, possible files in logData are stripped to their basename if
      * the action logged was a file action
      *
-     * @param LogEntry $logEntry
      * @return string Formatted details
      */
-    public function render(LogEntry $logEntry)
+    public function render()
     {
         return static::renderStatic(
-            array(
-                'logEntry' => $logEntry
-            ),
+            $this->arguments,
             $this->buildRenderChildrenClosure(),
             $this->renderingContext
         );
@@ -53,10 +59,10 @@ class FormatDetailsViewHelper extends AbstractViewHelper
      * @param RenderingContextInterface $renderingContext
      *
      * @return string
-     * @throws Exception
      */
     public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
     {
+        /** @var LogEntry $logEntry */
         $logEntry = $arguments['logEntry'];
         $detailString = $logEntry->getDetails();
         $substitutes = $logEntry->getLogData();
index 04db501..4d72846 100644 (file)
@@ -22,6 +22,7 @@ use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Object\ObjectManager;
 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 
@@ -39,17 +40,23 @@ class HistoryEntryViewHelper extends AbstractViewHelper
     protected $escapeOutput = false;
 
     /**
+     * Initializes the arguments
+     */
+    public function initializeArguments()
+    {
+        parent::initializeArguments();
+        $this->registerArgument('uid', 'int', 'Uid of the log entry', true);
+    }
+
+    /**
      * Get system history record
      *
-     * @param int $uid Uid of the log entry
      * @return string Formatted history entry if one exists, else empty string
      */
-    public function render($uid)
+    public function render()
     {
         return static::renderStatic(
-            array(
-                'uid' => $uid
-            ),
+            $this->arguments,
             $this->buildRenderChildrenClosure(),
             $this->renderingContext
         );
@@ -61,16 +68,19 @@ class HistoryEntryViewHelper extends AbstractViewHelper
      * @param RenderingContextInterface $renderingContext
      *
      * @return string
+     * @throws \InvalidArgumentException
      */
     public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
     {
+        if (!$renderingContext instanceof RenderingContext) {
+            throw new \InvalidArgumentException('The given rendering context is not of type "TYPO3\CMS\Fluid\Core\Rendering\RenderingContext"', 1468363945);
+        }
         /** @var \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager */
         $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
         /** @var \TYPO3\CMS\Belog\Domain\Repository\HistoryEntryRepository $historyEntryRepository */
         $historyEntryRepository = $objectManager->get(HistoryEntryRepository::class);
         /** @var \TYPO3\CMS\Belog\Domain\Model\HistoryEntry $historyEntry */
         $historyEntry = $historyEntryRepository->findOneBySysLogUid($arguments['uid']);
-        /** @var \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext */
         $controllerContext = $renderingContext->getControllerContext();
         /** @var IconFactory $iconFactory */
         $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
index e465152..14e8b16 100644 (file)
@@ -25,17 +25,23 @@ use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 class IsExtensionLoadedViewHelper extends AbstractViewHelper
 {
     /**
+     * Initializes the arguments
+     */
+    public function initializeArguments()
+    {
+        parent::initializeArguments();
+        $this->registerArgument('extensionKey', 'string', 'The extension\'s key', true);
+    }
+
+    /**
      * Checks whether an extension is loaded.
      *
-     * @param string $extensionKey The extension's key
      * @return bool TRUE if extension is loaded, FALSE otherwise
      */
-    public function render($extensionKey)
+    public function render()
     {
         return static::renderStatic(
-            array(
-                'extensionKey' => $extensionKey
-            ),
+            $this->arguments,
             $this->buildRenderChildrenClosure(),
             $this->renderingContext
         );
@@ -46,7 +52,7 @@ class IsExtensionLoadedViewHelper extends AbstractViewHelper
      * @param \Closure $renderChildrenClosure
      * @param RenderingContextInterface $renderingContext
      *
-     * @return string
+     * @return bool
      */
     public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
     {
index e192bd4..f4440d5 100644 (file)
@@ -14,6 +14,9 @@ namespace TYPO3\CMS\Belog\ViewHelpers;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Domain\Repository\BackendUserRepository;
+use TYPO3\CMS\Extbase\Object\ObjectManager;
 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 
@@ -31,17 +34,23 @@ class UsernameViewHelper extends AbstractViewHelper
     protected static $usernameRuntimeCache = array();
 
     /**
+     * Initializes the arguments
+     */
+    public function initializeArguments()
+    {
+        parent::initializeArguments();
+        $this->registerArgument('uid', 'int', 'Uid of the user', true);
+    }
+
+    /**
      * Resolve user name from backend user id.
      *
-     * @param int $uid Uid of the user
      * @return string Username or an empty string if there is no user with that UID
      */
-    public function render($uid)
+    public function render()
     {
         return static::renderStatic(
-            array(
-                'uid' => $uid
-            ),
+            $this->arguments,
             $this->buildRenderChildrenClosure(),
             $this->renderingContext
         );
@@ -57,17 +66,16 @@ class UsernameViewHelper extends AbstractViewHelper
     public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
     {
         $uid = $arguments['uid'];
-
         if (isset(static::$usernameRuntimeCache[$uid])) {
             return static::$usernameRuntimeCache[$uid];
         }
 
-        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
-        $backendUserRepository = $objectManager->get(\TYPO3\CMS\Extbase\Domain\Repository\BackendUserRepository::class);
-        /** @var $user \TYPO3\CMS\Extbase\Domain\Model\BackendUser */
+        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
+        $backendUserRepository = $objectManager->get(BackendUserRepository::class);
+        /** @var \TYPO3\CMS\Extbase\Domain\Model\BackendUser $user */
         $user = $backendUserRepository->findByUid($uid);
         // $user may be NULL if user was deleted from DB, set it to empty string to always return a string
-        static::$usernameRuntimeCache[$uid] = ($user === null) ? '' : $user->getUserName();
+        static::$usernameRuntimeCache[$uid] = $user === null ? '' : $user->getUserName();
         return static::$usernameRuntimeCache[$uid];
     }
 }
index c90d9c7..869252b 100644 (file)
@@ -14,6 +14,12 @@ namespace TYPO3\CMS\Belog\ViewHelpers;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Belog\Domain\Repository\WorkspaceRepository;
+use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Object\ObjectManager;
+use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 
@@ -31,17 +37,23 @@ class WorkspaceTitleViewHelper extends AbstractViewHelper
     protected static $workspaceTitleRuntimeCache = array();
 
     /**
+     * Initializes the arguments
+     */
+    public function initializeArguments()
+    {
+        parent::initializeArguments();
+        $this->registerArgument('uid', 'int', 'UID of the workspace', true);
+    }
+
+    /**
      * Resolve workspace title from UID.
      *
-     * @param int $uid UID of the workspace
      * @return string workspace title or UID
      */
-    public function render($uid)
+    public function render()
     {
         return static::renderStatic(
-            array(
-                'uid' => $uid
-            ),
+            $this->arguments,
             $this->buildRenderChildrenClosure(),
             $this->renderingContext
         );
@@ -53,26 +65,33 @@ class WorkspaceTitleViewHelper extends AbstractViewHelper
      * @param RenderingContextInterface $renderingContext
      *
      * @return string
+     * @throws \InvalidArgumentException
      */
     public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
     {
-        $uid = $arguments['uid'];
+        if (!$renderingContext instanceof RenderingContext) {
+            throw new \InvalidArgumentException('The given rendering context is not of type "TYPO3\CMS\Fluid\Core\Rendering\RenderingContext"', 1468363946);
+        }
 
+        $uid = $arguments['uid'];
         if (isset(static::$workspaceTitleRuntimeCache[$uid])) {
             return static::$workspaceTitleRuntimeCache[$uid];
         }
 
         if ($uid === 0) {
-            static::$workspaceTitleRuntimeCache[$uid] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('live', $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName());
-        } elseif (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
+            static::$workspaceTitleRuntimeCache[$uid] = LocalizationUtility::translate(
+                'live',
+                $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName()
+            );
+        } elseif (!ExtensionManagementUtility::isLoaded('workspaces')) {
             static::$workspaceTitleRuntimeCache[$uid] = '';
         } else {
-            $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
-            $workspaceRepository = $objectManager->get(\TYPO3\CMS\Belog\Domain\Repository\WorkspaceRepository::class);
-            /** @var $workspace \TYPO3\CMS\Belog\Domain\Model\Workspace */
+            $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
+            $workspaceRepository = $objectManager->get(WorkspaceRepository::class);
+            /** @var \TYPO3\CMS\Belog\Domain\Model\Workspace $workspace */
             $workspace = $workspaceRepository->findByUid($uid);
             // $workspace may be null, force empty string in this case
-            static::$workspaceTitleRuntimeCache[$uid] = ($workspace === null) ? '' : $workspace->getTitle();
+            static::$workspaceTitleRuntimeCache[$uid] = $workspace === null ? '' : $workspace->getTitle();
         }
 
         return static::$workspaceTitleRuntimeCache[$uid];