Skip to content

Commit

Permalink
Handle FlashMessage exceptions inside upgrade wizard
Browse files Browse the repository at this point in the history
When this class is called from the install tool/upgrade wizard, the
globals `LANG` and `BE_USER` may not be set and cause exceptions.
  • Loading branch information
okmiim committed May 14, 2022
1 parent eafc1d3 commit ed55ddb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
9 changes: 4 additions & 5 deletions Classes/Service/TimeTable/ExternalTimeTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use HDNET\Calendarize\Utility\HelperUtility;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

/**
* External service.
Expand Down Expand Up @@ -60,9 +59,9 @@ public function handleConfiguration(array &$times, Configuration $configuration)
try {
$fileName = $this->iCalUrlService->getOrCreateLocalFileForUrl($externalIcsUrl);
} catch (UnableToGetFileForUrlException $e) {
HelperUtility::createFlashMessage(
HelperUtility::createTranslatedTitleFlashMessage(
$e->getMessage(),
LocalizationUtility::translate('flashMessage.invalidExternalUrl.title', 'calendarize') ?? '',
'flashMessage.invalidExternalUrl.title',
FlashMessage::ERROR
);

Expand All @@ -72,9 +71,9 @@ public function handleConfiguration(array &$times, Configuration $configuration)
try {
$externalTimes = $this->iCalService->getEvents($fileName);
} catch (\Exception $e) {
HelperUtility::createFlashMessage(
HelperUtility::createTranslatedTitleFlashMessage(
$e->getMessage(),
LocalizationUtility::translate('flashMessage.unableToProcessEvents.title', 'calendarize') ?? '',
'flashMessage.unableToProcessEvents.title',
FlashMessage::ERROR
);

Expand Down
49 changes: 46 additions & 3 deletions Classes/Utility/HelperUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace HDNET\Calendarize\Utility;

use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
Expand All @@ -18,6 +19,8 @@
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* Helper Utility.
Expand Down Expand Up @@ -57,7 +60,8 @@ public static function getQuery($objectName)
public static function createFlashMessage(string $message, string $title = '', int $mode = FlashMessage::OK): void
{
// Don't store flash messages in CLI context
$storeInSession = !Environment::isCli();
// Note: the getUserByContext check is only required for TYPO3 v10 and is fixed in v11 (94418).
$storeInSession = !Environment::isCli() && null !== self::getUserByContext();
$flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $message, $title, $mode, $storeInSession);
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
$messageQueue = $flashMessageService->getMessageQueueByIdentifier();
Expand All @@ -75,8 +79,32 @@ public static function createFlashMessage(string $message, string $title = '', i
*/
public static function createTranslatedFlashMessage(string $messageKey, string $titleKey = '', int $mode = FlashMessage::OK): void
{
$message = LocalizationUtility::translate($messageKey, 'calendarize') ?? $messageKey;
$title = LocalizationUtility::translate($titleKey, 'calendarize') ?? $titleKey;
try {
$message = LocalizationUtility::translate($messageKey, 'calendarize') ?? $messageKey;
$title = LocalizationUtility::translate($titleKey, 'calendarize') ?? $titleKey;
} catch (\TypeError $e) {
$message = $messageKey;
$title = $titleKey;
}
self::createFlashMessage($message, $title, $mode);
}

/**
* Create a flash message with a translated title.
*
* @param string $messageKey
* @param string $titleKey
* @param int $mode
*
* @throws Exception
*/
public static function createTranslatedTitleFlashMessage(string $message, string $titleKey = '', int $mode = FlashMessage::OK): void
{
try {
$title = LocalizationUtility::translate($titleKey, 'calendarize') ?? $titleKey;
} catch (\TypeError $e) {
$title = $titleKey;
}
self::createFlashMessage($message, $title, $mode);
}

Expand All @@ -91,4 +119,19 @@ public static function getDatabaseConnection($table)
{
return GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
}

/**
* Gets user object by context.
* This class is also used in install tool, where $GLOBALS['BE_USER'] is not set and can be null.
*
* @return AbstractUserAuthentication|null
*/
protected static function getUserByContext(): ?AbstractUserAuthentication
{
if (($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController && $GLOBALS['TSFE']->fe_user instanceof FrontendUserAuthentication) {
return $GLOBALS['TSFE']->fe_user;
}

return $GLOBALS['BE_USER'] ?? null;
}
}

0 comments on commit ed55ddb

Please sign in to comment.