-
Notifications
You must be signed in to change notification settings - Fork 154
[WIP] Feature: Flash Messenger Error Messages #421
Changes from all commits
5eaf5d5
b0f1642
6b772ca
474612e
3cebdeb
fd4b0b7
46e1597
1578993
6ec411e
8b38e8c
bdf0302
6c0ef62
3c8ac55
e10625d
b712894
99df2f5
787abec
036ef32
4db579b
f60753a
d8199a5
c3284a8
b7676ff
203098f
daf648d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| namespace Application\Exception; | ||
|
|
||
| class ViewableUserException extends \Exception implements ViewableUserExceptionInterface | ||
| { | ||
| /* @var string */ | ||
| private $publicMessage; | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function __construct($message = '', $publicMessage = '', $code = 0, \Exception $previous = null) | ||
| { | ||
| $this->publicMessage = (string) $publicMessage; | ||
| parent::__construct($message, $code, $previous); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getPublicMessage() | ||
| { | ||
| return $this->publicMessage; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| namespace Application\Exception; | ||
|
|
||
| interface ViewableUserExceptionInterface | ||
| { | ||
| /** | ||
| * @param string $message Error Message for internal logging | ||
| * @param string $publicMessage Public Error Message | ||
| * @param int $code Error Code | ||
| * @param \Exception $previous Previous Exception | ||
| */ | ||
| public function __construct($message = '', $publicMessage = '', $code = 0, \Exception $previous = null); | ||
|
|
||
| /** | ||
| * Returns the Public Error Message | ||
| * @return string | ||
| */ | ||
| public function getPublicMessage(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| <?php | ||
| namespace Application\View\Helper; | ||
|
|
||
| use Zend\Mvc\Controller\Plugin\FlashMessenger as PluginFlashMessenger; | ||
| use Zend\View\Helper\FlashMessenger as ZendFlashMessenger; | ||
|
|
||
| class FlashMessenger extends ZendFlashMessenger | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why exactly do you need to extend over wrapping it? What effort is necessary to build a new helper instead?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fact that every message needs to be wrapped diffently, if i would setup the class on init the settings would be shared on every message type but we got different messages that don't use the same requirements
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, makes sense, I'm just trying to be very cautious about inheritance, as it is very strong coupling
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use the I think that is enough.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @froschdesign don't see where this solves rendering different messages by namespace. no?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ins0 echo $flash->render('error', array('alert', 'alert-dismissible', 'alert-danger'));
echo $flash->render('info', array('alert', 'alert-dismissible', 'alert-info'));
echo $flash->render('default', array('alert', 'alert-dismissible', 'alert-warning'));
echo $flash->render('success', array('alert', 'alert-dismissible', 'alert-success'));
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @froschdesign ok now i got your point, the current use case should solve that u simple write |
||
| { | ||
| /* @var string[][] */ | ||
| private $classOptions = []; | ||
|
|
||
| /** | ||
| * Render Messages in a BS message format | ||
| * | ||
| * @param callable $renderer | ||
| * @param string $namespace | ||
| * @param string[] $classes | ||
| * @return string | ||
| */ | ||
| private function doRender($renderer, $namespace, array $classes = []) | ||
| { | ||
| $this->classOptions = [ | ||
| PluginFlashMessenger::NAMESPACE_INFO => [ | ||
| 'name' => 'Information', | ||
| 'class' => 'alert alert-info', | ||
| ], | ||
| PluginFlashMessenger::NAMESPACE_ERROR => [ | ||
| 'name' => 'Error', | ||
| 'class' => 'alert alert-danger', | ||
| ], | ||
| PluginFlashMessenger::NAMESPACE_SUCCESS => [ | ||
| 'name' => 'Success', | ||
| 'class' => 'alert alert-success', | ||
| ], | ||
| PluginFlashMessenger::NAMESPACE_DEFAULT => [ | ||
| 'name' => 'Message', | ||
| 'class' => 'alert alert-info', | ||
| ], | ||
| PluginFlashMessenger::NAMESPACE_WARNING => [ | ||
| 'name' => 'Warning', | ||
| 'class' => 'alert alert-warning', | ||
| ], | ||
| ]; | ||
|
|
||
| // if custom namespace handle as default message | ||
| if (!isset($this->classOptions[$namespace])) { | ||
| $this->classOptions = [ | ||
| $namespace => $this->classOptions[PluginFlashMessenger::NAMESPACE_DEFAULT], | ||
| ]; | ||
| } | ||
|
|
||
| $messageOutput = ''; | ||
| $translator = $this->getTranslator(); | ||
|
|
||
| foreach ($this->classOptions as $currentNamespace => $options) { | ||
| $this->classMessages[$currentNamespace] = $options['class']; | ||
| $openingString = sprintf('<div%%s><span class="sr-only">%s</span>', $translator->translate($options['name'])); | ||
|
|
||
| $this->setMessageOpenFormat($openingString); | ||
| $this->setMessageSeparatorString(sprintf('</div>%s', $openingString)); | ||
| $this->setMessageCloseString('</div>'); | ||
|
|
||
| $messageOutput .= $renderer($currentNamespace, $classes); | ||
| } | ||
|
|
||
| return $messageOutput; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function render($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $classes = []) | ||
| { | ||
| $renderer = function ($namespace, $classes) { | ||
| return parent::render($namespace, $classes); | ||
| }; | ||
|
|
||
| return $this->doRender($renderer, $namespace, $classes); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function renderCurrent($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $classes = []) | ||
| { | ||
| $renderer = function ($namespace, $classes) { | ||
| return parent::renderCurrent($namespace, $classes); | ||
| }; | ||
|
|
||
| return $this->doRender($renderer, $namespace, $classes); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| namespace ApplicationTest\Exception; | ||
|
|
||
| use Application\Exception; | ||
| use PHPUnit_Framework_TestCase; | ||
|
|
||
| class ViewableUserExceptionTest extends PHPUnit_Framework_TestCase | ||
| { | ||
| public function testExceptionCanBeCreated() | ||
| { | ||
| $parentExceptionMock = $this->getMock(\Exception::class); | ||
|
|
||
| $exception = new Exception\ViewableUserException( | ||
| 'fooMessage', | ||
| 'fooPublicMessage', | ||
| 666, | ||
| $parentExceptionMock | ||
| ); | ||
|
|
||
| $this->assertSame('fooMessage', $exception->getMessage()); | ||
| $this->assertSame('fooPublicMessage', $exception->getPublicMessage()); | ||
| $this->assertSame(666, $exception->getCode()); | ||
| $this->assertSame($parentExceptionMock, $exception->getPrevious()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
|
|
||
| namespace ApplicationTest\Integration\View\Helper; | ||
|
|
||
| use ApplicationTest\Integration\Util\Bootstrap; | ||
| use PHPUnit_Framework_TestCase; | ||
|
|
||
| /** | ||
| * @covers Application\View\Helper\FlashMessenger | ||
| */ | ||
| class FlashMessengerTest extends PHPUnit_Framework_TestCase | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
| { | ||
| public function testMultipleMessageTypesGetsRendered() | ||
| { | ||
| $serviceManager = Bootstrap::getServiceManager(); | ||
|
|
||
| /* @var $flashMessengerControllerPlugin \Zend\Mvc\Controller\Plugin\FlashMessenger */ | ||
| $flashMessengerControllerPlugin = $serviceManager->get('ControllerPluginManager')->get('FlashMessenger'); | ||
|
|
||
| $flashMessengerControllerPlugin->addMessage('FooMessage'); | ||
| $flashMessengerControllerPlugin->addSuccessMessage('FooSuccess'); | ||
| $flashMessengerControllerPlugin->addWarningMessage('FooWarning'); | ||
| $flashMessengerControllerPlugin->addErrorMessage('FooError'); | ||
| $flashMessengerControllerPlugin->addInfoMessage('FooInfo'); | ||
|
|
||
| /* @var \Application\View\Helper\FlashMessenger $flashMessengerViewHelper */ | ||
| $flashMessengerViewHelper = $serviceManager->get('ViewHelperManager')->get('FlashMessenger'); | ||
|
|
||
| $this->assertEquals( | ||
| '<div class="alert alert-info"><span class="sr-only">Information</span>FooInfo</div>' . | ||
| '<div class="alert alert-danger"><span class="sr-only">Error</span>FooError</div>' . | ||
| '<div class="alert alert-success"><span class="sr-only">Success</span>FooSuccess</div>' . | ||
| '<div class="alert alert-info"><span class="sr-only">Message</span>FooMessage</div>' . | ||
| '<div class="alert alert-warning"><span class="sr-only">Warning</span>FooWarning</div>', | ||
| $flashMessengerViewHelper->renderCurrent() | ||
| ); | ||
| } | ||
|
|
||
| public function testCustomNamespaceMessageGetsRenderedAsInformationMessage() | ||
| { | ||
| $serviceManager = Bootstrap::getServiceManager(); | ||
|
|
||
| /* @var $flashMessengerControllerPlugin \Zend\Mvc\Controller\Plugin\FlashMessenger */ | ||
| $flashMessengerControllerPlugin = $serviceManager->get('ControllerPluginManager')->get('FlashMessenger'); | ||
| $flashMessengerControllerPlugin->setNamespace('FooBar'); | ||
|
|
||
| $flashMessengerControllerPlugin->addMessage('FooMessage'); | ||
|
|
||
| /* @var \Application\View\Helper\FlashMessenger $flashMessengerViewHelper */ | ||
| $flashMessengerViewHelper = $serviceManager->get('ViewHelperManager')->get('FlashMessenger'); | ||
|
|
||
| $this->assertEquals( | ||
| '<div class="alert alert-info"><span class="sr-only">Message</span>FooMessage</div>', | ||
| $flashMessengerViewHelper->renderCurrent('FooBar') | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| namespace ZfModule\Controller\Exception; | ||
|
|
||
| use Application\Exception; | ||
| use Zend\Http; | ||
|
|
||
| final class InvalidDataException extends Exception\ViewableUserException | ||
| { | ||
| /** | ||
| * Generates an Exception from a Invalid Post Request | ||
| * | ||
| * @param string $publicMessage | ||
| * @param Http\Request $request | ||
| * @return self | ||
| */ | ||
| public static function fromInvalidRequest($publicMessage, Http\Request $request) | ||
| { | ||
| return new self( | ||
| sprintf('Invalid Request received [%s]', $request->toString()), | ||
| (string) $publicMessage | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docblock