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