diff --git a/app/dependencies.php b/app/dependencies.php index 8d600c27e3c9cccab0ec81b6ae66b67ba433141a..f7a2b4617b6089969708a623ce770b06645e09e4 100644 --- a/app/dependencies.php +++ b/app/dependencies.php @@ -23,9 +23,8 @@ $container['view'] = function ($c) { $c->get('request')->getUri() )); $view->addExtension(new Twig_Extension_Debug()); - $view->addExtension(new GrEduLabs\Twig\Extension\Flash( - $c->get('flash'), - 'flash.twig' + $view->addExtension(new Knlv\Slim\Views\TwigMessages( + $c->get('flash') )); return $view; diff --git a/app/src/Twig/Extension/Flash.php b/app/src/Twig/Extension/Flash.php deleted file mode 100644 index 3b6b809128870aafaf2c598a512ff2a048e3cb53..0000000000000000000000000000000000000000 --- a/app/src/Twig/Extension/Flash.php +++ /dev/null @@ -1,76 +0,0 @@ -<?php -/** - * gredu_labs - * - * @link https://github.com/eellak/gredu_labs for the canonical source repository - * @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/) - * @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html - */ - -namespace GrEduLabs\Twig\Extension; - -use Slim\Flash\Messages; -use Twig_Environment; -use Twig_Extension; -use Twig_SimpleFunction; - -class Flash extends Twig_Extension -{ - /** - * @var Twig_Environment - */ - protected $environment; - - /** - * @var Messages - */ - protected $flash; - - /** - * @var string - */ - protected $template; - - public function __construct(Messages $flash, $template) - { - $this->flash = $flash; - $this->template = $template; - } - - public function getName() - { - return 'slim-flash'; - } - - public function initRuntime(Twig_Environment $environment) - { - $this->environment = $environment; - } - - public function getFunctions() - { - return [ - new Twig_SimpleFunction('flash', [$this, 'messages'], [ - 'needs_environment' => true, - 'is_safe' => ['all'], - ]), - ]; - } - - public function messages() - { - $response = ''; - $allMessages = $this->flash->getMessages(); - - if (!empty($allMessages)) { - foreach ($allMessages as $class => $messages) { - $response .= $this->environment->render($this->template, [ - 'class' => $class, - 'messages' => $messages, - ]); - } - } - - return $response; - } -} diff --git a/app/templates/flash.twig b/app/templates/flash.twig index 26135b3f14fae4d45f4cd4aa05c45a8bdaa783f5..e52d69a00620617a9518054851aded40c781c65a 100644 --- a/app/templates/flash.twig +++ b/app/templates/flash.twig @@ -1,5 +1,7 @@ -<ul class="alert alert-{{ class }}" role="alert"> +{% for class, messages in flash() %} +<ul class="list-unstyled alert alert-{{ class }}" role="alert"> {% for message in messages %} <li>{{ message }}</li> {% endfor %} </ul> +{% endfor %} diff --git a/app/templates/layout.twig b/app/templates/layout.twig index c94d7b500781f476e50137051f70b66779803ab2..e63bf13ae50801af5015d85d1e7519624d8d0f94 100644 --- a/app/templates/layout.twig +++ b/app/templates/layout.twig @@ -7,7 +7,7 @@ <link href="{{ base_url() }}/css/style.css" rel="stylesheet" type="text/css"> </head> <body> - {{ flash() }} + {% include 'flash.twig' %} {% block content %}{% endblock %} <script src="//code.jquery.com/jquery-2.2.0.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> diff --git a/composer.json b/composer.json index 1f87d6bdf7cd1167bd2108abf22b25b1fce7911f..1d6144b5d48cd2c8c21b7b607511ca5094eade7c 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "slim/csrf": "^0.6.0", "monolog/monolog": "^1.13", "kanellov/config-merge": "dev-master", + "kanellov/slim-twig-flash": "^0.1", "zendframework/zend-authentication": "^2.0", "zendframework/zend-eventmanager": "^3.0", "zendframework/zend-permissions-acl": "^2.0", diff --git a/composer.lock b/composer.lock index ef5d1f7fa9997aff1aa8e0442c3ab7d2f349721a..cadcf84b11f906bdccff4f8074dfcd46ef8b5612 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "6db91d83a5d7f09ca4aa148dd467aa37", - "content-hash": "9dbc2f0e135613f71e872c3845d833ba", + "hash": "6bab5bd43fd57ed6d83fd7df11328bb7", + "content-hash": "d8c7dd10bd4590b2b0978fca5f170872", "packages": [ { "name": "container-interop/container-interop", @@ -136,6 +136,61 @@ ], "time": "2015-03-16 10:17:44" }, + { + "name": "kanellov/slim-twig-flash", + "version": "0.1.0", + "source": { + "type": "git", + "url": "https://github.com/kanellov/slim-twig-flash.git", + "reference": "6c5ecfdbf7eeb9ea44bbc6dbc4a902c1930a392c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/kanellov/slim-twig-flash/zipball/6c5ecfdbf7eeb9ea44bbc6dbc4a902c1930a392c", + "reference": "6c5ecfdbf7eeb9ea44bbc6dbc4a902c1930a392c", + "shasum": "" + }, + "require": { + "php": ">=5.5.0", + "slim/flash": "^0.1.0", + "twig/twig": "^1.18" + }, + "require-dev": { + "fabpot/php-cs-fixer": "1.*", + "phpunit/phpcov": "2.*", + "phpunit/phpunit": "4.8.0", + "satooshi/php-coveralls": "dev-master" + }, + "type": "library", + "autoload": { + "psr-4": { + "Knlv\\Slim\\Views\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GNU GPLv3" + ], + "authors": [ + { + "name": "Vassilis Kanellopoulos", + "email": "contact@kanellov.com", + "homepage": "http://kanellov.com" + } + ], + "description": "A Twig extension to access Slim Flash messages in templates", + "keywords": [ + "extension", + "flash", + "framework", + "message", + "provider", + "slim", + "twig", + "view" + ], + "time": "2016-01-20 09:08:28" + }, { "name": "monolog/monolog", "version": "1.17.2", diff --git a/test/src/Twig/Extension/FlashTest.php b/test/src/Twig/Extension/FlashTest.php deleted file mode 100644 index fa7fba9314d490c839a80601d7427514ad8fda6d..0000000000000000000000000000000000000000 --- a/test/src/Twig/Extension/FlashTest.php +++ /dev/null @@ -1,88 +0,0 @@ -<?php -/** - * gredu_labs - * - * @link https://github.com/eellak/gredu_labs for the canonical source repository - * @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/) - * @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html - */ - -namespace GrEduLabsTest\Twig\Extension; - -use GrEduLabs\Twig\Extension\Flash; - -class FlashTest extends \PHPUnit_Framework_TestCase -{ - - private $flash; - - private $mockMessages; - - private $template; - - protected function setUp() - { - $this->mockMessages = $this->getMock('\\Slim\\Flash\\Messages'); - $this->mockMessages->expects($this->any()) - ->method('getMessages') - ->will($this->returnValue([ - 'first' => ['test message 1'], - 'other' => [ - 'test message 2', - 'test message 3', - ], - ])); - $this->template = 'flashtemplate.twig'; - - $this->flash = new Flash($this->mockMessages, $this->template); - } - - public function testConstructorSetArgs() - { - $this->assertAttributeSame($this->mockMessages, 'flash', $this->flash); - $this->assertAttributeSame($this->template, 'template', $this->flash); - } - - public function testGetName() - { - $this->assertSame('slim-flash', $this->flash->getName()); - } - - public function testInitRuntimeSetEnvironment() - { - $env = $this->getMock('\\Twig_Environment'); - $this->flash->initRuntime($env); - $this->assertAttributeSame($env, 'environment', $this->flash); - } - - public function testGetFunctionsReturnsArrayWithSimpleFunction() - { - $functions = $this->flash->getFunctions(); - $this->assertInternalType('array', $functions); - $this->assertNotEmpty($functions); - $theFunction = reset($functions); - $this->assertInstanceOf('\\Twig_SimpleFunction', $theFunction); - $this->assertSame('flash', $theFunction->getName()); - $this->assertTrue($theFunction->needsEnvironment()); - $theFunctionRefl = new \ReflectionClass($theFunction); - $options = $theFunctionRefl->getProperty('options'); - $options->setAccessible(true); - $options = $options->getValue($theFunction); - $this->assertSame(['all'], $options['is_safe']); - } - - public function testMessagesReturnsStringWithMessages() - { - $env = new \Twig_Environment( - new \Twig_Loader_Filesystem(__DIR__) - ); - $this->flash->initRuntime($env); - $response = $this->flash->messages(); - $expected = <<< EOF -first: test message 1 -other: test message 2 -other: test message 3 -EOF; - $this->assertContains($expected, $response); - } -} diff --git a/test/src/Twig/Extension/flashtemplate.twig b/test/src/Twig/Extension/flashtemplate.twig deleted file mode 100644 index e99cd597aa0fea232c55f3d914856d6598c2ee3b..0000000000000000000000000000000000000000 --- a/test/src/Twig/Extension/flashtemplate.twig +++ /dev/null @@ -1,3 +0,0 @@ -{% for message in messages %} -{{ class }}: {{ message }} -{% endfor %} \ No newline at end of file