Skip to content
This repository was archived by the owner on May 1, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions module/Application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
],
],
'view_helpers' => [
'invokables' => [
'FlashMessenger' => View\Helper\FlashMessenger::class,
],
'factories' => [
'gitHubRepositoryUrl' => View\Helper\GitHubRepositoryUrlFactory::class,
'sanitizeHtml' => View\Helper\SanitizeHtmlFactory::class,
Expand Down
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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docblock

{
$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();
}
92 changes: 92 additions & 0 deletions module/Application/src/Application/View/Helper/FlashMessenger.php
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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the view_helper_config and the parameter for the classes on rendering.

I think that is enough.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ins0
Look at the docs:

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'));

http://framework.zend.com/manual/current/en/modules/zend.view.helpers.flash-messenger.html#sample-modification-for-twitter-bootstrap-3

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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<?php $this->flashMessenger()->render(); ?> without to know how the messages should look alike. each message from equal namespaces should be rendered in seperate message block and not grouped as every message may not get a direct link to the prev. one. of curse messages from unknown namespaces should be wrapped in the bs3 message style, too. i think this is the cleanest method without to create different classes for render and renderCurrent and don't group messages. this is way to complex to get this achieved over the class options, or?

{
/* @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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add @covers annotations

{
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')
);
}
}
8 changes: 5 additions & 3 deletions module/Application/view/error/404.phtml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<h1>A 404 error occurred</h1>
<h2><?php echo $this->message ?></h2>

<hr>
<?php if (isset($this->reason) && $this->reason): ?>
<?php
$reasonMessage = '';
Expand All @@ -22,7 +21,10 @@
break;
}
?>
<p><?php echo $reasonMessage ?></p>
<div class="alert alert-danger alert-error">
<strong><?php echo $this->message ?></strong>
<p><?php echo $reasonMessage ?></p>
</div>
<?php endif ?>

<?php if (isset($this->controller) && $this->controller): ?>
Expand Down
19 changes: 16 additions & 3 deletions module/Application/view/error/index.phtml
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
<h1>An error occurred</h1>
<h2><?php echo $this->message ?></h2>
<?php
use Application\Exception;
?>
<h1>Oops. Something went wrong.</h1>
<div class="alert alert-danger alert-error">
<strong>Error</strong>
<p>
<?php
if($this->exception instanceof Exception\ViewableUserException && $this->exception->getPublicMessage()): ?>
<?php echo $this->escapeHtml($this->exception->getPublicMessage()); ?>
<?php else: ?>
Sorry, we got no more information
<?php endif; ?>
</p>
</div>

<?php if (isset($this->display_exceptions) && $this->display_exceptions): ?>
<?php if (isset($this->exception) && $this->exception instanceof Exception): ?>
<?php if (isset($this->exception) && $this->exception instanceof \Exception): ?>
<hr/>
<h2>Additional information:</h2>
<h3><?php echo get_class($this->exception); ?></h3>
Expand Down
4 changes: 2 additions & 2 deletions module/User/src/User/GitHub/LoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class LoginListener implements SharedListenerAggregateInterface
protected $listeners = [];

/**
* @inheritdoc
* {@inheritdoc}
*/
public function attachShared(SharedEventManagerInterface $events)
{
Expand All @@ -28,7 +28,7 @@ public function attachShared(SharedEventManagerInterface $events)
}

/**
* @inheritdoc
* {@inheritdoc}
*/
public function detachShared(SharedEventManagerInterface $events)
{
Expand Down
4 changes: 1 addition & 3 deletions module/User/view/zfc-user/user/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
</div>
</div>
</div>
<?php foreach ($this->flashMessenger()->getMessages() as $message): ?>
<h3 class="zf-green"><?php echo $this->escapeHtml($message); ?></h3>
<?php endforeach; ?>
<?php echo $this->flashMessenger()->render(); ?>

<ul class="nav nav-tabs">
<li class="active"><a href="#modules" data-toggle="tab">Registered Modules</a></li>
Expand Down
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
);
}
}
Loading