[TASK] Streamline phpdoc annotations in EXT:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / Widget / AjaxWidgetContextHolder.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\Widget;
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 * This object stores the WidgetContext for the currently active widgets
19 * of the current user, to make sure the WidgetContext is available in
20 * Widget AJAX requests.
21 *
22 * @internal This class is only used internally by the widget framework.
23 */
24 class AjaxWidgetContextHolder implements \TYPO3\CMS\Core\SingletonInterface
25 {
26 /**
27 * An array $ajaxWidgetIdentifier => $widgetContext
28 * which stores the widget context.
29 *
30 * @var array
31 */
32 protected $widgetContexts = [];
33
34 /**
35 * @var string
36 */
37 protected $widgetContextsStorageKey = 'TYPO3\\CMS\\Fluid\\Core\\Widget\\AjaxWidgetContextHolder_widgetContexts';
38
39 /**
40 * Constructor
41 */
42 public function __construct()
43 {
44 $this->loadWidgetContexts();
45 }
46
47 /**
48 * Loads the windget contexts from the TYPO3 user session
49 */
50 protected function loadWidgetContexts()
51 {
52 if (TYPO3_MODE === 'FE') {
53 $this->widgetContexts = unserialize($GLOBALS['TSFE']->fe_user->getKey('ses', $this->widgetContextsStorageKey));
54 } else {
55 $this->widgetContexts = isset($GLOBALS['BE_USER']->uc[$this->widgetContextsStorageKey]) ? unserialize($GLOBALS['BE_USER']->uc[$this->widgetContextsStorageKey]) : [];
56 $GLOBALS['BE_USER']->writeUC();
57 }
58 }
59
60 /**
61 * Get the widget context for the given $ajaxWidgetId.
62 *
63 * @param string $ajaxWidgetId
64 * @return \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
65 */
66 public function get($ajaxWidgetId)
67 {
68 if (!isset($this->widgetContexts[$ajaxWidgetId])) {
69 throw new \TYPO3\CMS\Fluid\Core\Widget\Exception\WidgetContextNotFoundException('No widget context was found for the Ajax Widget Identifier "' . $ajaxWidgetId . '". This only happens if AJAX URIs are called without including the widget on a page.', 1284793775);
70 }
71 return $this->widgetContexts[$ajaxWidgetId];
72 }
73
74 /**
75 * Stores the WidgetContext inside the Context, and sets the
76 * AjaxWidgetIdentifier inside the Widget Context correctly.
77 *
78 * @param \TYPO3\CMS\Fluid\Core\Widget\WidgetContext $widgetContext
79 */
80 public function store(\TYPO3\CMS\Fluid\Core\Widget\WidgetContext $widgetContext)
81 {
82 $ajaxWidgetId = md5(uniqid(mt_rand(), true));
83 $widgetContext->setAjaxWidgetIdentifier($ajaxWidgetId);
84 $this->widgetContexts[$ajaxWidgetId] = $widgetContext;
85 $this->storeWidgetContexts();
86 }
87
88 /**
89 * Persists the widget contexts in the TYPO3 user session
90 */
91 protected function storeWidgetContexts()
92 {
93 if (TYPO3_MODE === 'FE') {
94 $GLOBALS['TSFE']->fe_user->setKey('ses', $this->widgetContextsStorageKey, serialize($this->widgetContexts));
95 $GLOBALS['TSFE']->fe_user->storeSessionData();
96 } else {
97 $GLOBALS['BE_USER']->uc[$this->widgetContextsStorageKey] = serialize($this->widgetContexts);
98 $GLOBALS['BE_USER']->writeUc();
99 }
100 }
101 }