Commit d209528e authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Amélioration de l'import des messages du FlashMessenger par l'aide de vue Messenger

parent 100ada69
......@@ -164,78 +164,76 @@ class Messenger extends AbstractHelper implements MessageAwareInterface, EventMa
/**
* Importe les messages n-1 du FlashMessenger.
*
* @param string $namespace Facultatif, ex: 'these/danger' ou 'these/*'
* @param string $namespace
* @return static
*/
protected function importFlashMessages($namespace = null)
{
$fm = $this->getPluginFlashMessenger();
if ($namespace) {
$array = $this->extractSeveritiesAndNamespaces($namespace);
foreach ($array as $sev => $ns) {
foreach ($fm->getMessagesFromNamespace($ns) as $message) {
$this->addMessage($message, $sev);
}
}
}
else {
foreach ($fm->getErrorMessages() as $message) {
$this->addMessage($message, self::ERROR);
}
foreach ($fm->getSuccessMessages() as $message) {
$this->addMessage($message, self::SUCCESS);
}
foreach ($fm->getInfoMessages() as $message) {
$this->addMessage($message, self::INFO);
}
foreach ($fm->getWarningMessages() as $message) {
$this->addMessage($message, self::WARNING);
}
}
return $this;
return $this->_importFromFlashMessages($currentMessages = false, $namespace);
}
/**
* Importe les messages courants du FlashMessenger.
*
* @param string $namespace Facultatif, ex: 'these/danger' ou 'these/*'
* @param string $namespace
* @return static
*/
protected function importCurrentFlashMessages($namespace = null)
{
return $this->_importFromFlashMessages($currentMessages = true, $namespace);
}
/**
* @param bool $currentMessages
* @param string $namespace Facultatif, ex: 'info', 'these/danger', 'these/*', 'these' (i.e. 'these/*')
* @return static
*/
private function _importFromFlashMessages($currentMessages, $namespace = null)
{
$getMethod = $currentMessages ? 'getCurrentMessagesFromNamespace' : 'getMessagesFromNamespace';
$fm = $this->getPluginFlashMessenger();
if ($namespace) {
$array = $this->extractSeveritiesAndNamespaces($namespace);
foreach ($array as $sev => $ns) {
foreach ($fm->getCurrentMessagesFromNamespace($ns) as $message) {
$this->addMessage($message, $sev);
$severities = [self::ERROR, self::SUCCESS, self::INFO, self::WARNING];
if (in_array($namespace, $severities)) {
foreach ($fm->$getMethod($namespace) as $message) {
$this->addMessage($message, $namespace);
}
if ($currentMessages) {
/* Si on importe alors on nettoie pour éviter un deuxième affichage */
$fm->clearCurrentMessagesFromNamespace($namespace);
}
} else {
$array = $this->extractSeveritiesAndNamespaces($namespace);
foreach ($array as $sev => $ns) {
foreach ($fm->$getMethod($ns) as $message) {
$this->addMessage($message, $sev);
}
if ($currentMessages) {
/* Si on importe alors on nettoie pour éviter un deuxième affichage */
$fm->clearCurrentMessagesFromNamespace($ns);
}
}
/* Si on importe alors on nettoie pour éviter un deuxième affichage */
$fm->clearCurrentMessagesFromNamespace($ns);
}
}
else {
foreach ($fm->getCurrentErrorMessages() as $message) {
foreach ($fm->$getMethod('error') as $message) {
$this->addMessage($message, self::ERROR);
}
foreach ($fm->getCurrentSuccessMessages() as $message) {
foreach ($fm->$getMethod('success') as $message) {
$this->addMessage($message, self::SUCCESS);
}
foreach ($fm->getCurrentInfoMessages() as $message) {
foreach ($fm->$getMethod('info') as $message) {
$this->addMessage($message, self::INFO);
}
foreach ($fm->getCurrentWarningMessages() as $message) {
foreach ($fm->$getMethod('warning') as $message) {
$this->addMessage($message, self::WARNING);
}
/* Si on importe alors on nettoie pour éviter un deuxième affichage */
$fm->clearCurrentMessagesFromContainer();
if ($currentMessages) {
$fm->clearCurrentMessagesFromContainer();
}
}
return $this;
......@@ -247,29 +245,29 @@ class Messenger extends AbstractHelper implements MessageAwareInterface, EventMa
*/
private function extractSeveritiesAndNamespaces($namespace)
{
// $namespace peut être de la forme 'namespace/severity' (ex: 'these/danger')
if (($pos = strrpos($namespace, $sep = self::NAMESPACED_SEVERITY_SEPARATOR)) !== false) {
$parts = explode($sep, $namespace);
$severity = array_pop($parts);
// si $namespace est de la forme 'namespace/*' (ex: 'these/*'), cela revient à importer les namespaces
// 'namespace/danger', 'namespace/success', 'namespace/info' et 'namespace/warning'
if ($severity === '*') {
$namespacePrefix = implode(self::NAMESPACED_SEVERITY_SEPARATOR, $parts) . '/';
$array = [
$s = self::ERROR => $namespacePrefix . $s,
$s = self::SUCCESS => $namespacePrefix . $s,
$s = self::INFO => $namespacePrefix . $s,
$s = self::WARNING => $namespacePrefix . $s,
];
} else {
$array = [
$severity => $namespace,
];
}
// Normalisation : $namespace doit être de la forme 'namespace/severity' ou 'namespace/*'.
// Ex: 'these/danger' ou 'these/*'
$separatorFound = strrpos($namespace, $sep = self::NAMESPACED_SEVERITY_SEPARATOR) !== false;
if (! $separatorFound) {
$namespace = $namespace . '/*';
}
else {
$parts = explode($sep, $namespace);
$severity = array_pop($parts);
// Si $namespace est de la forme 'namespace/*', cela revient à importer les namespaces
// 'namespace/danger', 'namespace/success', 'namespace/info' et 'namespace/warning'.
if ($severity === '*') {
$namespacePrefix = implode(self::NAMESPACED_SEVERITY_SEPARATOR, $parts) . '/';
$array = [
$s = self::ERROR => $namespacePrefix . $s,
$s = self::SUCCESS => $namespacePrefix . $s,
$s = self::INFO => $namespacePrefix . $s,
$s = self::WARNING => $namespacePrefix . $s,
];
} else {
$array = [
self::INFO => $namespace,
$severity => $namespace,
];
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment