[TASK] Move ViewHelper arguments to initializeArguments() in ext:belog
[Packages/TYPO3.CMS.git] / typo3 / sysext / belog / Classes / ViewHelpers / UsernameViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Belog\ViewHelpers;
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 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Domain\Repository\BackendUserRepository;
19 use TYPO3\CMS\Extbase\Object\ObjectManager;
20 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22
23 /**
24 * Get username from backend user id
25 * @internal
26 */
27 class UsernameViewHelper extends AbstractViewHelper
28 {
29 /**
30 * First level cache of user names
31 *
32 * @var array
33 */
34 protected static $usernameRuntimeCache = array();
35
36 /**
37 * Initializes the arguments
38 */
39 public function initializeArguments()
40 {
41 parent::initializeArguments();
42 $this->registerArgument('uid', 'int', 'Uid of the user', true);
43 }
44
45 /**
46 * Resolve user name from backend user id.
47 *
48 * @return string Username or an empty string if there is no user with that UID
49 */
50 public function render()
51 {
52 return static::renderStatic(
53 $this->arguments,
54 $this->buildRenderChildrenClosure(),
55 $this->renderingContext
56 );
57 }
58
59 /**
60 * @param array $arguments
61 * @param \Closure $renderChildrenClosure
62 * @param RenderingContextInterface $renderingContext
63 *
64 * @return string
65 */
66 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
67 {
68 $uid = $arguments['uid'];
69 if (isset(static::$usernameRuntimeCache[$uid])) {
70 return static::$usernameRuntimeCache[$uid];
71 }
72
73 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
74 $backendUserRepository = $objectManager->get(BackendUserRepository::class);
75 /** @var \TYPO3\CMS\Extbase\Domain\Model\BackendUser $user */
76 $user = $backendUserRepository->findByUid($uid);
77 // $user may be NULL if user was deleted from DB, set it to empty string to always return a string
78 static::$usernameRuntimeCache[$uid] = $user === null ? '' : $user->getUserName();
79 return static::$usernameRuntimeCache[$uid];
80 }
81 }