[TASK] Set TYPO3 version to 7.3-dev
[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 /**
18 * Get username from backend user id
19 *
20 * @author Christian Kuhn <lolli@schwarzbu.ch>
21 * @internal
22 */
23 class UsernameViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
24
25 /**
26 * @var \TYPO3\CMS\Extbase\Domain\Repository\BackendUserRepository
27 * @inject
28 */
29 protected $backendUserRepository;
30
31 /**
32 * First level cache of user names
33 *
34 * @var array
35 */
36 static protected $usernameRuntimeCache = array();
37
38 /**
39 * Resolve user name from backend user id.
40 *
41 * @param int $uid Uid of the user
42 * @return string Username or an empty string if there is no user with that UID
43 */
44 public function render($uid) {
45 if (isset(static::$usernameRuntimeCache[$uid])) {
46 return static::$usernameRuntimeCache[$uid];
47 }
48
49 /** @var $user \TYPO3\CMS\Extbase\Domain\Model\BackendUser */
50 $user = $this->backendUserRepository->findByUid($uid);
51 // $user may be NULL if user was deleted from DB, set it to empty string to always return a string
52 static::$usernameRuntimeCache[$uid] = ($user === NULL) ? '' : $user->getUserName();
53 return static::$usernameRuntimeCache[$uid];
54 }
55
56 }