TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Messaging;
|
||||
|
||||
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
|
||||
|
||||
/**
|
||||
* A class used for any kind of messages.
|
||||
*/
|
||||
abstract class AbstractMessage implements \JsonSerializable
|
||||
{
|
||||
protected string $title = '';
|
||||
protected string $message = '';
|
||||
protected ContextualFeedbackSeverity $severity = ContextualFeedbackSeverity::OK;
|
||||
|
||||
public function getTitle(): string
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function setTitle(string $title): void
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
public function getMessage(): string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
public function setMessage(string $message): void
|
||||
{
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function getSeverity(): ContextualFeedbackSeverity
|
||||
{
|
||||
return $this->severity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the message' severity
|
||||
*
|
||||
* @param ContextualFeedbackSeverity $severity
|
||||
*/
|
||||
public function setSeverity(ContextualFeedbackSeverity $severity = ContextualFeedbackSeverity::OK): void
|
||||
{
|
||||
$this->severity = $severity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string representation of the message. Useful for command
|
||||
* line use.
|
||||
*
|
||||
* @return string A string representation of the message.
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$title = '';
|
||||
if ($this->title !== '') {
|
||||
$title = ' - ' . $this->title;
|
||||
}
|
||||
return $this->severity->name . $title . ': ' . $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array Data which can be serialized by json_encode()
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'severity' => $this->getSeverity()->value,
|
||||
'title' => $this->getTitle(),
|
||||
'message' => $this->getMessage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Messaging;
|
||||
|
||||
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* A class representing flash messages.
|
||||
*/
|
||||
class FlashMessage extends AbstractMessage
|
||||
{
|
||||
/**
|
||||
* Defines whether the message should be stored in the session (to survive redirects) or only for one request (default)
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $storeInSession = false;
|
||||
|
||||
/**
|
||||
* Constructor for a flash message
|
||||
*
|
||||
* @param string $message The message.
|
||||
* @param string $title Optional message title.
|
||||
* @param ContextualFeedbackSeverity $severity
|
||||
* @param bool $storeInSession Optional, defines whether the message should be stored in the session or only for one request (default)
|
||||
*/
|
||||
public function __construct($message, $title = '', ContextualFeedbackSeverity $severity = ContextualFeedbackSeverity::OK, $storeInSession = false)
|
||||
{
|
||||
$this->setMessage($message);
|
||||
$this->setTitle($title);
|
||||
$this->setSeverity($severity);
|
||||
$this->setStoreInSession($storeInSession);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method. Useful when creating flash messages from a jsonSerialize json_decode() call.
|
||||
*
|
||||
* @param array<string, string|int|bool> $data
|
||||
*/
|
||||
public static function createFromArray(array $data): self
|
||||
{
|
||||
return GeneralUtility::makeInstance(
|
||||
static::class,
|
||||
(string)($data['message'] ?? ''),
|
||||
(string)($data['title'] ?? ''),
|
||||
ContextualFeedbackSeverity::tryFrom($data['severity'] ?? ContextualFeedbackSeverity::OK->value) ?? ContextualFeedbackSeverity::OK,
|
||||
(bool)($data['storeInSession'] ?? false)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the message's storeInSession flag.
|
||||
*
|
||||
* @return bool TRUE if message should be stored in the session, otherwise FALSE.
|
||||
*/
|
||||
public function isSessionMessage()
|
||||
{
|
||||
return $this->storeInSession;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the message's storeInSession flag
|
||||
*
|
||||
* @param bool $storeInSession The persistence flag
|
||||
*/
|
||||
public function setStoreInSession($storeInSession)
|
||||
{
|
||||
$this->storeInSession = (bool)$storeInSession;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array Data which can be serialized by json_encode()
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
$data = parent::jsonSerialize();
|
||||
$data['storeInSession'] = $this->storeInSession;
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Messaging;
|
||||
|
||||
use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
|
||||
use TYPO3\CMS\Core\Exception;
|
||||
use TYPO3\CMS\Core\Messaging\Renderer\FlashMessageRendererInterface;
|
||||
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
|
||||
|
||||
/**
|
||||
* A class which collects and renders flash messages.
|
||||
*/
|
||||
class FlashMessageQueue extends \SplQueue implements \JsonSerializable
|
||||
{
|
||||
public const FLASHMESSAGE_QUEUE = 'core.template.flashMessages';
|
||||
public const NOTIFICATION_QUEUE = 'core.template.notifications';
|
||||
|
||||
/**
|
||||
* A unique identifier for this queue
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $identifier;
|
||||
|
||||
/**
|
||||
* @param string $identifier The unique identifier for this queue
|
||||
*/
|
||||
public function __construct($identifier)
|
||||
{
|
||||
$this->identifier = $identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getIdentifier()
|
||||
{
|
||||
return $this->identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a message either to the BE_USER session (if the $message has the storeInSession flag set)
|
||||
* or it enqueues the message.
|
||||
*
|
||||
* @param FlashMessage $message Instance of \TYPO3\CMS\Core\Messaging\FlashMessage, representing a message
|
||||
* @throws \TYPO3\CMS\Core\Exception
|
||||
*/
|
||||
public function enqueue($message): void
|
||||
{
|
||||
if (!($message instanceof FlashMessage)) {
|
||||
throw new Exception(
|
||||
'FlashMessageQueue::enqueue() expects an object of type \TYPO3\CMS\Core\Messaging\FlashMessage but got type "' . get_debug_type($message) . '"',
|
||||
1376833554
|
||||
);
|
||||
}
|
||||
if ($message->isSessionMessage()) {
|
||||
$this->addFlashMessageToSession($message);
|
||||
} else {
|
||||
parent::enqueue($message);
|
||||
}
|
||||
}
|
||||
|
||||
public function addMessage(FlashMessage ...$messages)
|
||||
{
|
||||
foreach ($messages as $message) {
|
||||
$this->enqueue($message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is empty, as it will not move any flash message (e.g. from the session)
|
||||
*
|
||||
* @phpstan-return null
|
||||
*/
|
||||
public function dequeue(): mixed
|
||||
{
|
||||
// deliberately empty
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given flash message to the array of
|
||||
* flash messages that will be stored in the session.
|
||||
*/
|
||||
protected function addFlashMessageToSession(FlashMessage $message)
|
||||
{
|
||||
$queuedFlashMessages = $this->getFlashMessagesFromSession();
|
||||
$queuedFlashMessages[] = $message;
|
||||
$this->storeFlashMessagesInSession($queuedFlashMessages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all messages from the current PHP session and from the current request.
|
||||
*
|
||||
* @param ContextualFeedbackSeverity|null $severity
|
||||
* @return FlashMessage[]
|
||||
*/
|
||||
public function getAllMessages(?ContextualFeedbackSeverity $severity = null)
|
||||
{
|
||||
// Get messages from user session
|
||||
$queuedFlashMessagesFromSession = $this->getFlashMessagesFromSession();
|
||||
$queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, $this->toArray());
|
||||
if ($severity !== null) {
|
||||
$filteredFlashMessages = [];
|
||||
foreach ($queuedFlashMessages as $message) {
|
||||
if ($message->getSeverity() === $severity) {
|
||||
$filteredFlashMessages[] = $message;
|
||||
}
|
||||
}
|
||||
return $filteredFlashMessages;
|
||||
}
|
||||
|
||||
return $queuedFlashMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all messages from the current PHP session and from the current request.
|
||||
* After fetching the messages the internal queue and the message queue in the session
|
||||
* will be emptied.
|
||||
*
|
||||
* @param ContextualFeedbackSeverity|null $severity
|
||||
* @return FlashMessage[]
|
||||
*/
|
||||
public function getAllMessagesAndFlush(?ContextualFeedbackSeverity $severity = null)
|
||||
{
|
||||
$queuedFlashMessages = $this->getAllMessages($severity);
|
||||
// Reset messages in user session
|
||||
$this->removeAllFlashMessagesFromSession($severity);
|
||||
// Reset internal messages
|
||||
$this->clear($severity);
|
||||
return $queuedFlashMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores given flash messages in the session
|
||||
*
|
||||
* @param FlashMessage[]|null $flashMessages
|
||||
*/
|
||||
protected function storeFlashMessagesInSession(?array $flashMessages = null)
|
||||
{
|
||||
if (is_array($flashMessages)) {
|
||||
$flashMessages = array_map(json_encode(...), $flashMessages);
|
||||
}
|
||||
$user = $this->getUserByContext();
|
||||
$user?->setAndSaveSessionData($this->identifier, $flashMessages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all flash messages from the session
|
||||
*
|
||||
* @param ContextualFeedbackSeverity|null $severity
|
||||
*/
|
||||
protected function removeAllFlashMessagesFromSession(?ContextualFeedbackSeverity $severity = null)
|
||||
{
|
||||
if (!$this->getUserByContext() instanceof AbstractUserAuthentication) {
|
||||
return;
|
||||
}
|
||||
if ($severity === null) {
|
||||
$this->storeFlashMessagesInSession();
|
||||
} else {
|
||||
$messages = $this->getFlashMessagesFromSession();
|
||||
foreach ($messages as $index => $message) {
|
||||
if ($message->getSeverity() === $severity) {
|
||||
unset($messages[$index]);
|
||||
}
|
||||
}
|
||||
$this->storeFlashMessagesInSession($messages);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current flash messages from the session, making sure to always
|
||||
* return an array.
|
||||
*
|
||||
* @return FlashMessage[]
|
||||
*/
|
||||
protected function getFlashMessagesFromSession(): array
|
||||
{
|
||||
$sessionMessages = [];
|
||||
$user = $this->getUserByContext();
|
||||
if ($user !== null) {
|
||||
$messagesFromSession = $user->getSessionData($this->identifier);
|
||||
$messagesFromSession = is_array($messagesFromSession) ? $messagesFromSession : [];
|
||||
foreach ($messagesFromSession as $messageData) {
|
||||
$sessionMessages[] = FlashMessage::createFromArray(json_decode($messageData, true));
|
||||
}
|
||||
}
|
||||
return $sessionMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets user object by context.
|
||||
* This class is also used in install tool, where $GLOBALS['BE_USER'] is not set and can be null.
|
||||
*
|
||||
* @todo: This construct needs to be removed. Methods that use this should be changed to
|
||||
* get the user hand over explicitly from the caller! A patch should make this global
|
||||
* access a b/w compat fallback only and adapt consuming methods accordingly.
|
||||
*/
|
||||
protected function getUserByContext(): ?AbstractUserAuthentication
|
||||
{
|
||||
if (($GLOBALS['TYPO3_REQUEST'] ?? null)
|
||||
&& $GLOBALS['TYPO3_REQUEST']->getAttribute('frontend.user') instanceof FrontendUserAuthentication
|
||||
) {
|
||||
return $GLOBALS['TYPO3_REQUEST']->getAttribute('frontend.user');
|
||||
}
|
||||
return $GLOBALS['BE_USER'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches and renders all available flash messages from the queue.
|
||||
*
|
||||
* @param FlashMessageRendererInterface|null $flashMessageRenderer
|
||||
* @return string All flash messages in the queue rendered by context based FlashMessageRendererResolver.
|
||||
*/
|
||||
public function renderFlashMessages(?FlashMessageRendererInterface $flashMessageRenderer = null)
|
||||
{
|
||||
$content = '';
|
||||
$flashMessages = $this->getAllMessagesAndFlush();
|
||||
|
||||
if (!empty($flashMessages)) {
|
||||
if ($flashMessageRenderer === null) {
|
||||
$flashMessageRenderer = GeneralUtility::makeInstance(FlashMessageRendererResolver::class)->resolve();
|
||||
}
|
||||
$content = $flashMessageRenderer->render($flashMessages);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all items of the queue as array
|
||||
*
|
||||
* @return FlashMessage[]
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
$array = [];
|
||||
$this->rewind();
|
||||
while ($this->valid()) {
|
||||
$array[] = $this->current();
|
||||
$this->next();
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all items from the queue
|
||||
*
|
||||
* @param ContextualFeedbackSeverity|null $severity
|
||||
*/
|
||||
public function clear(?ContextualFeedbackSeverity $severity = null)
|
||||
{
|
||||
$this->rewind();
|
||||
if ($severity === null) {
|
||||
while (!$this->isEmpty()) {
|
||||
parent::dequeue();
|
||||
}
|
||||
} else {
|
||||
$keysToRemove = [];
|
||||
while ($cur = $this->current()) {
|
||||
if ($cur->getSeverity() === $severity) {
|
||||
$keysToRemove[] = $this->key();
|
||||
}
|
||||
$this->next();
|
||||
}
|
||||
// keys are renumbered when unsetting elements
|
||||
// so unset them from last to first
|
||||
$keysToRemove = array_reverse($keysToRemove);
|
||||
foreach ($keysToRemove as $key) {
|
||||
$this->offsetUnset($key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array Data which can be serialized by json_encode()
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Messaging;
|
||||
|
||||
use TYPO3\CMS\Core\Core\Environment;
|
||||
use TYPO3\CMS\Core\Http\ApplicationType;
|
||||
use TYPO3\CMS\Core\Http\ServerRequest;
|
||||
use TYPO3\CMS\Core\Messaging\Renderer\BootstrapRenderer;
|
||||
use TYPO3\CMS\Core\Messaging\Renderer\FlashMessageRendererInterface;
|
||||
use TYPO3\CMS\Core\Messaging\Renderer\ListRenderer;
|
||||
use TYPO3\CMS\Core\Messaging\Renderer\PlaintextRenderer;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* A class for rendering flash messages.
|
||||
*/
|
||||
class FlashMessageRendererResolver
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $renderer = [
|
||||
'BE' => BootstrapRenderer::class,
|
||||
'FE' => ListRenderer::class,
|
||||
'CLI' => PlaintextRenderer::class,
|
||||
'_default' => PlaintextRenderer::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* This method resolves a FlashMessageRendererInterface for the given $context.
|
||||
*
|
||||
* In case $context is null, the context will be detected automatic.
|
||||
*/
|
||||
public function resolve(): FlashMessageRendererInterface
|
||||
{
|
||||
$rendererClass = $this->resolveFlashMessageRenderClass();
|
||||
$renderer = GeneralUtility::makeInstance($rendererClass);
|
||||
if (!$renderer instanceof FlashMessageRendererInterface) {
|
||||
throw new \RuntimeException('Renderer ' . get_class($renderer)
|
||||
. ' does not implement FlashMessageRendererInterface', 1476958086);
|
||||
}
|
||||
return $renderer;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method resolves the renderer class by given context.
|
||||
*/
|
||||
protected function resolveFlashMessageRenderClass(): string
|
||||
{
|
||||
$context = $this->resolveContext();
|
||||
$renderClass = $this->renderer['_default'];
|
||||
|
||||
if (!empty($this->renderer[$context])) {
|
||||
$renderClass = $this->renderer[$context];
|
||||
}
|
||||
|
||||
return $renderClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method detect the current context and return one of the
|
||||
* following strings:
|
||||
* - FE
|
||||
* - BE
|
||||
* - CLI
|
||||
*/
|
||||
protected function resolveContext(): string
|
||||
{
|
||||
$context = '';
|
||||
$request = $GLOBALS['TYPO3_REQUEST'] ?? null;
|
||||
if (Environment::isCli()) {
|
||||
$context = 'CLI';
|
||||
} elseif ($request instanceof ServerRequest && ApplicationType::fromRequest($request)->isBackend()) {
|
||||
$context = 'BE';
|
||||
} elseif ($request instanceof ServerRequest && ApplicationType::fromRequest($request)->isFrontend()) {
|
||||
$context = 'FE';
|
||||
}
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Messaging;
|
||||
|
||||
use TYPO3\CMS\Core\SingletonInterface;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* A class representing flash messages.
|
||||
*/
|
||||
class FlashMessageService implements SingletonInterface
|
||||
{
|
||||
/**
|
||||
* Array of \TYPO3\CMS\Core\Messaging\FlashMessageQueue objects
|
||||
*
|
||||
* @var FlashMessageQueue[]
|
||||
*/
|
||||
protected $flashMessageQueues = [];
|
||||
|
||||
/**
|
||||
* Return the message queue for the given identifier.
|
||||
* If no queue exists, an empty one will be created.
|
||||
*
|
||||
* @param string $identifier
|
||||
*/
|
||||
public function getMessageQueueByIdentifier($identifier = FlashMessageQueue::FLASHMESSAGE_QUEUE): FlashMessageQueue
|
||||
{
|
||||
if (!isset($this->flashMessageQueues[$identifier])) {
|
||||
$this->flashMessageQueues[$identifier] = GeneralUtility::makeInstance(
|
||||
FlashMessageQueue::class,
|
||||
$identifier
|
||||
);
|
||||
}
|
||||
return $this->flashMessageQueues[$identifier];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Messaging\Renderer;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use TYPO3\CMS\Core\Imaging\IconFactory;
|
||||
use TYPO3\CMS\Core\Imaging\IconSize;
|
||||
use TYPO3\CMS\Core\Messaging\FlashMessage;
|
||||
|
||||
/**
|
||||
* A class representing a bootstrap flash messages.
|
||||
* This class renders flash messages as markup, based on the
|
||||
* bootstrap HTML/CSS framework. It is used in backend context.
|
||||
* The created output contains all classes which are required for
|
||||
* the TYPO3 backend. Any kind of message contains also a nice icon.
|
||||
*/
|
||||
#[Autoconfigure(public: true)]
|
||||
readonly class BootstrapRenderer implements FlashMessageRendererInterface
|
||||
{
|
||||
public function __construct(
|
||||
private IconFactory $iconFactory,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Gets the message rendered as clean and secure markup
|
||||
*
|
||||
* @param FlashMessage[] $flashMessages
|
||||
* @return string Representation of the flash message
|
||||
*/
|
||||
public function render(array $flashMessages): string
|
||||
{
|
||||
$markup = [];
|
||||
$markup[] = '<div class="typo3-messages">';
|
||||
foreach ($flashMessages as $flashMessage) {
|
||||
$messageTitle = $flashMessage->getTitle();
|
||||
$markup[] = '<div class="alert alert-' . htmlspecialchars($flashMessage->getSeverity()->getCssClass()) . '">';
|
||||
$markup[] = ' <div class="alert-inner">';
|
||||
$markup[] = ' <div class="alert-icon">';
|
||||
$markup[] = ' <span class="icon-emphasized">';
|
||||
$markup[] = $this->iconFactory->getIcon($flashMessage->getSeverity()->getIconIdentifier(), IconSize::SMALL)->render();
|
||||
$markup[] = ' </span>';
|
||||
$markup[] = ' </div>';
|
||||
$markup[] = ' <div class="alert-content">';
|
||||
if ($messageTitle !== '') {
|
||||
$markup[] = ' <div class="alert-title">' . htmlspecialchars($messageTitle) . '</div>';
|
||||
}
|
||||
$markup[] = ' <p class="alert-message">' . htmlspecialchars($flashMessage->getMessage()) . '</p>';
|
||||
$markup[] = ' </div>';
|
||||
$markup[] = ' </div>';
|
||||
$markup[] = '</div>';
|
||||
}
|
||||
$markup[] = '</div>';
|
||||
return implode('', $markup);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Messaging\Renderer;
|
||||
|
||||
use TYPO3\CMS\Core\Messaging\FlashMessage;
|
||||
|
||||
/**
|
||||
* Interface must be implemented by all flash message renderer classes
|
||||
*/
|
||||
interface FlashMessageRendererInterface
|
||||
{
|
||||
/**
|
||||
* Render method
|
||||
*
|
||||
* @param FlashMessage[] $flashMessages
|
||||
* @return string Representation of the flash message
|
||||
*/
|
||||
public function render(array $flashMessages): string;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Messaging\Renderer;
|
||||
|
||||
use TYPO3\CMS\Core\Messaging\FlashMessage;
|
||||
|
||||
/**
|
||||
* A class representing a html flash message as unordered markup list.
|
||||
* It is used in frontend context by default.
|
||||
* The created output contains css classes which can be used to style
|
||||
* the output individual. Any message contains the message and an
|
||||
* optional title which is rendered as <h4> tag if it is set in
|
||||
* the FlashMessage object.
|
||||
*/
|
||||
readonly class ListRenderer implements FlashMessageRendererInterface
|
||||
{
|
||||
/**
|
||||
* Gets the message rendered as clean and secure markup
|
||||
*
|
||||
* @param FlashMessage[] $flashMessages
|
||||
* @return string Representation of the flash message
|
||||
*/
|
||||
public function render(array $flashMessages): string
|
||||
{
|
||||
$markup = [];
|
||||
$markup[] = '<ul class="typo3-messages">';
|
||||
foreach ($flashMessages as $flashMessage) {
|
||||
$messageTitle = $flashMessage->getTitle();
|
||||
$markup[] = '<li class="alert alert-' . htmlspecialchars($flashMessage->getSeverity()->getCssClass()) . '">';
|
||||
if ($messageTitle !== '') {
|
||||
$markup[] = '<h4 class="alert-title">' . htmlspecialchars($messageTitle) . '</h4>';
|
||||
}
|
||||
$markup[] = '<p class="alert-message">' . htmlspecialchars($flashMessage->getMessage()) . '</p>';
|
||||
$markup[] = '</li>';
|
||||
}
|
||||
$markup[] = '</ul>';
|
||||
return implode('', $markup);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Messaging\Renderer;
|
||||
|
||||
use TYPO3\CMS\Core\Messaging\FlashMessage;
|
||||
|
||||
/**
|
||||
* A class representing a html flash message as plain text.
|
||||
* It is used in CLI context per default.
|
||||
* The created output contains at least the severity and the message
|
||||
* in the following format:
|
||||
* [SEVERITY] <message>
|
||||
*
|
||||
* Example:
|
||||
* [ERROR] No record found
|
||||
*
|
||||
* In case the FlashMessage object contains also a title, the
|
||||
* following format is used:
|
||||
* [SEVERITY] <title>: <message>
|
||||
*
|
||||
* Example:
|
||||
* [ERROR] An error occurred: No record found
|
||||
*
|
||||
* Multiple messages are separated by a new line (LF).
|
||||
*/
|
||||
readonly class PlaintextRenderer implements FlashMessageRendererInterface
|
||||
{
|
||||
/**
|
||||
* Render method
|
||||
*
|
||||
* @param FlashMessage[] $flashMessages
|
||||
* @return string Representation of the flash message as plain text
|
||||
*/
|
||||
public function render(array $flashMessages): string
|
||||
{
|
||||
$messages = [];
|
||||
foreach ($flashMessages as $flashMessage) {
|
||||
$message = $flashMessage->getMessage();
|
||||
if ($flashMessage->getTitle() !== '') {
|
||||
$message = $flashMessage->getTitle() . ': ' . $message;
|
||||
}
|
||||
$messages[] = '[' . $flashMessage->getSeverity()->name . '] ' . $message;
|
||||
}
|
||||
return implode(LF, $messages);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Messaging;
|
||||
|
||||
/**
|
||||
* A semantic interface for messages that can be put into
|
||||
* a message bus in order to be serialized.
|
||||
*
|
||||
* Recommendations for webhook messages:
|
||||
* - POPOs like DTOs or custom message objects
|
||||
* - No services, events, requests, or models
|
||||
*/
|
||||
interface WebhookMessageInterface extends \JsonSerializable {}
|
||||
Reference in New Issue
Block a user