Commit 3b502067 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Aide de vue Messenger : ajout de la possibilité de spécifier le namespace lors...

Aide de vue Messenger : ajout de la possibilité de spécifier le namespace lors de l'importation des messages du FlashMessenger ; test unitaires corrigés et mis à jour.
parent 808323ad
......@@ -79,15 +79,16 @@ trait MessageAwareTrait
return implode($glue, \UnicaenApp\Util::extractArrayLeafNodes($messages));
}
/**
* Ajoute un message.
*
* @param string $message Message
*
* @param string $message Message
* @param string $severity Sévérité, ex: MessageAwareInterface::INFO
* @return self
* @param int $priority Permet d'ordonner les messages au sein d'une même sévérité
* @return static
*/
public function addMessage($message, $severity = null, $priority = 1)
public function addMessage($message, $severity = null, $priority = 0)
{
if (!$severity || !is_string($severity)) {
$severity = MessageAwareInterface::INFO;
......
<?php
namespace UnicaenApp\View\Helper;
use Exception;
use UnicaenApp\Traits\MessageAwareInterface;
use UnicaenApp\Traits\MessageAwareTrait;
use UnicaenApp\Exception\LogicException;
use Zend\View\Helper\AbstractHelper;
use Zend\View\Helper\FlashMessenger;
use Zend\View\Renderer\PhpRenderer;
/**
* Aide de vue permettant de stocker une liste de messages d'information de différentes sévérités
* et de générer le code HTML pour les afficher (affublés d'un icône correspondant à leur sévérité).
*
* Possibilité d'importer les messages du FlashMessenger pour les mettre en forme de la même manière.
* Possibilité d'importer les messages du FlashMessenger pour les mettre en forme de la même manière
* (avec support du namespace).
*
* @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
*/
......@@ -18,18 +22,22 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
{
use MessageAwareTrait;
protected $uiClasses = [ // severity => [ alert class, icon class ]
self::INFO => ['info', 'info-sign'],
self::SUCCESS => ['success', 'ok-sign'],
self::WARNING => ['warning', 'warning-sign'],
self::ERROR => ['danger', 'exclamation-sign'],
const NAMESPACED_SEVERITY_SEPARATOR = '/';
protected $uiClasses = [
// severity => [ alert class, icon class ]
self::INFO => ['info', 'info-sign'],
self::SUCCESS => ['success', 'ok-sign'],
self::WARNING => ['warning', 'warning-sign'],
self::ERROR => ['danger', 'exclamation-sign'],
];
protected $severityOrder = [ // severity => order
self::SUCCESS => 1,
self::ERROR => 2,
self::WARNING => 3,
self::INFO => 4,
protected $severityOrder = [
// severity => order
self::SUCCESS => 1,
self::ERROR => 2,
self::WARNING => 3,
self::INFO => 4,
];
/**
......@@ -44,20 +52,21 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
*/
protected $containerInnerTemplate = '%s';
/**
* @var \Zend\Mvc\Controller\Plugin\FlashMessenger
*/
protected $pluginFlashMessenger;
/**
* Helper entry point.
*
* @return self
* @return static
*/
public function __invoke()
{
return $this;
}
/**
* Retourne le code HTML généré par cette aide de vue.
*
......@@ -67,14 +76,12 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
{
try {
return $this->render();
} catch (\Exception $exc) {
} catch (Exception $exc) {
var_dump($exc->getMessage(), \UnicaenApp\Util::formatTraceString($exc->getTraceAsString()));
die;
}
}
/**
* Génère le code HTML.
*
......@@ -100,8 +107,6 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
return $out;
}
/**
* Génère le code HTML d'un seul message. Pour usage ponctuel.
*
......@@ -119,8 +124,6 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
return sprintf($this->getTemplate($severity ?: 'info'), $message);
}
/**
* @return array
*/
......@@ -143,74 +146,115 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
return $messages;
}
/**
* Importe les messages n-1 du FlashMessenger.
*
* @return self
* @param string $namespace
* @return static
*/
protected function importFlashMessages()
protected function importFlashMessages($namespace = null)
{
/* @var $fm \Zend\View\Helper\FlashMessenger */
$fm = $this->getView()->getHelperPluginManager()->get('flashMessenger');
$fm = $this->getPluginFlashMessenger();
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);
if ($namespace) {
// $namespace peut être de forme 'namespace/severity' (ex: 'these/danger')
if (($pos = strrpos($namespace, $sep = self::NAMESPACED_SEVERITY_SEPARATOR)) !== false) {
$parts = explode($sep, $namespace);
$severity = array_pop($parts);
}
else {
$severity = null;
}
foreach ($fm->getMessagesFromNamespace($namespace) as $message) {
$this->addMessage($message, $severity);
}
}
foreach ($fm->getWarningMessages() as $message) {
$this->addMessage($message, self::WARNING);
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;
}
/**
* Importe les messages courants du FlashMessenger.
*
* @return self
* @param string $namespace
* @return static
*/
protected function importCurrentFlashMessages()
protected function importCurrentFlashMessages($namespace = null)
{
/* @var $fm \Zend\View\Helper\FlashMessenger */
$fm = $this->getView()->getHelperPluginManager()->get('flashMessenger');
foreach ($fm->getCurrentErrorMessages() as $message) {
$this->addMessage($message, self::ERROR);
}
foreach ($fm->getCurrentSuccessMessages() as $message) {
$this->addMessage($message, self::SUCCESS);
}
foreach ($fm->getCurrentInfoMessages() as $message) {
$this->addMessage($message, self::INFO);
$fm = $this->getPluginFlashMessenger();
if ($namespace) {
$severity = $this->extractSeverityFromNamespace($namespace);
foreach ($fm->getCurrentMessagesFromNamespace($namespace) as $message) {
$this->addMessage($message, $severity);
}
/* Si on importe alors on nettoie pour éviter un deuxième affichage */
$fm->clearCurrentMessagesFromNamespace($namespace);
}
foreach ($fm->getCurrentWarningMessages() as $message) {
$this->addMessage($message, self::WARNING);
else {
foreach ($fm->getCurrentErrorMessages() as $message) {
$this->addMessage($message, self::ERROR);
}
foreach ($fm->getCurrentSuccessMessages() as $message) {
$this->addMessage($message, self::SUCCESS);
}
foreach ($fm->getCurrentInfoMessages() as $message) {
$this->addMessage($message, self::INFO);
}
foreach ($fm->getCurrentWarningMessages() as $message) {
$this->addMessage($message, self::WARNING);
}
/* Si on importe alors on nettoie pour éviter un deuxième affichage */
$fm->clearCurrentMessagesFromContainer();
}
/* Si on importe alors on nettoie pour éviter un deuxième affichage */
$fm->clearCurrentMessagesFromContainer();
return $this;
}
/**
* Si $namespace est de la forme 'namespace/severity', on retourne la sévérité, sinon null.
*
* @param string $namespace ex: 'these/danger'
* @return string|null ex: 'danger'
*/
private function extractSeverityFromNamespace($namespace)
{
// $namespace peut être de forme 'namespace/severity' (ex: 'these/danger')
if (($pos = strrpos($namespace, $sep = self::NAMESPACED_SEVERITY_SEPARATOR)) !== false) {
$severity = array_pop(explode($sep, $namespace));
}
else {
$severity = null;
}
return $severity;
}
/**
* Spécifie l'unique message courant au format Exception.
*
* @param \Exception $exception Exception
* @param string $severity Ex: Messenger::INFO
* @param Exception $exception Exception
* @param string $severity Ex: Messenger::INFO
*
* @return Messenger
* @return static
*/
public function setException(\Exception $exception, $severity = self::ERROR)
public function setException(Exception $exception, $severity = self::ERROR)
{
$message = sprintf("<p><strong>%s</strong></p>", $exception->getMessage());
if (($previous = $exception->getPrevious())) {
......@@ -220,72 +264,66 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
return $this->setMessages([$severity => $message]);
}
/**
* Importe les messages courants du FlashMessenger (remplaçant les messages existants).
*
* @return Messenger
* @param string $namespace
* @return static
*/
public function setMessagesFromFlashMessenger()
public function setMessagesFromFlashMessenger($namespace = null)
{
$this->messages = [];
$this->importFlashMessages();
$this->importFlashMessages($namespace);
return $this;
}
/**
* Importe les messages courants du FlashMessenger (remplaçant les messages existants).
*
* @return Messenger
* @param string $namespace
* @return static
*/
public function setCurrentMessagesFromFlashMessenger()
public function setCurrentMessagesFromFlashMessenger($namespace = null)
{
$this->messages = [];
$this->importCurrentFlashMessages();
$this->importCurrentFlashMessages($namespace);
return $this;
}
/**
* Ajoute les messages courants du FlashMessenger.
*
* @return Messenger
* @param string $namespace
* @return static
*/
public function addMessagesFromFlashMessenger()
public function addMessagesFromFlashMessenger($namespace = null)
{
$this->importFlashMessages();
$this->importFlashMessages($namespace);
return $this;
}
/**
* Ajoute les messages courants du FlashMessenger.
*
* @return Messenger
* @param string $namespace
* @return static
*/
public function addCurrentMessagesFromFlashMessenger()
public function addCurrentMessagesFromFlashMessenger($namespace = null)
{
$this->importCurrentFlashMessages();
$this->importCurrentFlashMessages($namespace);
return $this;
}
/**
* Active ou non l'affichage de l'icône.
*
* @param bool $withIcon <tt>true</tt> pour activer l'affichage de l'icône.
*
* @return Messenger
* @return static
*/
public function withIcon($withIcon = true)
{
......@@ -294,8 +332,6 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
return $this;
}
/**
* Retourne le motif utilisé pour générer le conteneur de chaque message à afficher.
*
......@@ -310,14 +346,18 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
throw new LogicException("Sévérité inconnue: " . $severity);
}
$alertClass = $this->uiClasses[$severity][0];
$iconClass = $this->uiClasses[$severity][1];
$innerTemplate = $this->containerInnerTemplate ?: '%s';
$iconMarkup = $this->withIcon ? "<span class=\"glyphicon glyphicon-{$this->uiClasses[$severity][1]}\"></span> " : null;
$iconMarkup = $this->withIcon ? "<span class=\"glyphicon glyphicon-$iconClass\"></span>" : null;
$containerId = $containerId ? sprintf('id="%s"', $containerId) : '';
$template = <<<EOT
<div class="messenger alert alert-{$this->uiClasses[$severity][0]}" {$containerId}>
<div class="messenger alert alert-$alertClass" $containerId>
<button type="button" class="close" title="Fermer cette alerte" data-dismiss="alert">&times;</button>
$iconMarkup
$iconMarkup
$innerTemplate
</div>
EOT;
......@@ -325,13 +365,11 @@ EOT;
return $template . PHP_EOL;
}
/**
*
* @param string $containerInnerTemplate
*
* @return self
* @return static
*/
public function setContainerInnerTemplate($containerInnerTemplate = '%s')
{
......@@ -339,4 +377,21 @@ EOT;
return $this;
}
/**
* @return \Zend\Mvc\Controller\Plugin\FlashMessenger
*/
protected function getPluginFlashMessenger()
{
if (null === $this->pluginFlashMessenger) {
/** @var PhpRenderer $view */
$view = $this->getView();
/* @var $vh FlashMessenger */
$vh = $view->getHelperPluginManager()->get('flashMessenger');
$this->pluginFlashMessenger = $vh->getPluginFlashMessenger();
}
return $this->pluginFlashMessenger;
}
}
\ No newline at end of file
......@@ -18,10 +18,10 @@ class Bootstrap
public static function init()
{
$zf2ModulePaths = array(dirname(__DIR__));
if (($path = static::findParentPath('vendor'))) {
$zf2ModulePaths[] = $path;
}
// $zf2ModulePaths = array(dirname(__DIR__));
// if (($path = static::findParentPath('vendor'))) {
// $zf2ModulePaths[] = $path;
// }
static::initAutoloader();
......@@ -50,7 +50,7 @@ class Bootstrap
protected static function initAutoloader()
{
$vendorPath = static::findParentPath('vendor');
$vendorPath = static::findVendorDirPath();
if (is_readable($vendorPath . '/autoload.php')) {
include $vendorPath . '/autoload.php';
......@@ -83,16 +83,28 @@ class Bootstrap
));
}
protected static function findParentPath($path)
/**
* Recherche le chemin du répertoire vendor contenant "autoload.php".
*
* @return null|string Chemin du répertoire vendor trouvé
*/
protected static function findVendorDirPath()
{
$dir = __DIR__;
$previousDir = '.';
while (!is_dir($dir . '/' . $path)) {
while (true) {
$testedPath = $dir . '/vendor';
if (is_readable($testedPath . '/autoload.php')) {
return $testedPath;
}
$dir = dirname($dir);
if ($previousDir === $dir) return false;
if ($previousDir === $dir) {
return null;
}
$previousDir = $dir;
}
return $dir . '/' . $path;
return null;
}
}
......
<p class="messenger alert alert-info" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-info-sign"></i>-->
Information message.
</p>
<p class="messenger alert alert-success" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-ok-sign"></i>-->
<div class="messenger alert alert-success" >
<button type="button" class="close" title="Fermer cette alerte" data-dismiss="alert">&times;</button>
<span class="glyphicon glyphicon-ok-sign"></span>
Success message.
</p>
<p class="messenger alert alert-danger" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-exclamation-sign"></i>-->
</div>
<div class="messenger alert alert-danger" >
<button type="button" class="close" title="Fermer cette alerte" data-dismiss="alert">&times;</button>
<span class="glyphicon glyphicon-exclamation-sign"></span>
Error message.
</p>
</div>
<div class="messenger alert alert-info" >
<button type="button" class="close" title="Fermer cette alerte" data-dismiss="alert">&times;</button>
<span class="glyphicon glyphicon-info-sign"></span>
Information message.
</div>
<p class="messenger alert alert-info" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-info-sign"></i>-->
<div class="messenger alert alert-info" >
<button type="button" class="close" title="Fermer cette alerte" data-dismiss="alert">&times;</button>
<span class="glyphicon glyphicon-info-sign"></span>
<em>Information message.</em>
</p>
</div>
<div class="messenger alert alert-danger" >
<button type="button" class="close" title="Fermer cette alerte" data-dismiss="alert">&times;</button>
<span class="glyphicon glyphicon-exclamation-sign"></span>
Error message.
</div>
<div class="messenger alert alert-danger" >
<button type="button" class="close" title="Fermer cette alerte" data-dismiss="alert">&times;</button>
<span class="glyphicon glyphicon-exclamation-sign"></span>
Error message from FlashMessenger namespace.
</div>
<p class="messenger alert alert-info" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-info-sign"></i>-->
<div class="messenger alert alert-info" >
<button type="button" class="close" title="Fermer cette alerte" data-dismiss="alert">&times;</button>
<span class="glyphicon glyphicon-info-sign"></span>
Information message.
</p>
</div>
<p class="messenger alert alert-danger" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-exclamation-sign"></i>-->
Error message.
</p>
<p class="messenger alert alert-danger" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-exclamation-sign"></i>-->
Another error message.
</p>
<div class="messenger alert alert-danger" >
<button type="button" class="close" title="Fermer cette alerte" data-dismiss="alert">&times;</button>
<span class="glyphicon glyphicon-exclamation-sign"></span>
Error message.<br />Another error message.
</div>
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