From 9b8a35fae8fb3eadbf4bd54ed0e5312f29ec0fee Mon Sep 17 00:00:00 2001 From: Martin Aarhof Date: Tue, 8 Apr 2014 16:09:53 +0200 Subject: [PATCH 1/5] made session flashes closeable with configuration --- DependencyInjection/Configuration.php | 3 ++ .../MopaBootstrapExtension.php | 3 +- Resources/config/twig.xml | 3 ++ Resources/doc/configuration-reference.md | 1 + Resources/views/flash.html.twig | 2 +- Twig/FlashExtension.php | 43 +++++++++++++++++++ 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 4c7903685..e5ca0d403 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -366,6 +366,9 @@ protected function addFlashConfig(ArrayNodeDefinition $rootNode) ->arrayNode('flash') ->addDefaultsIfNotSet() ->children() + ->booleanNode('closeable') + ->defaultFalse() + ->end() ->arrayNode('mapping') ->addDefaultsIfNotSet() ->children() diff --git a/DependencyInjection/MopaBootstrapExtension.php b/DependencyInjection/MopaBootstrapExtension.php index 4b3a46474..3798490e3 100644 --- a/DependencyInjection/MopaBootstrapExtension.php +++ b/DependencyInjection/MopaBootstrapExtension.php @@ -86,8 +86,9 @@ public function load(array $configs, ContainerBuilder $container) * Flash */ if (isset($config['flash'])) { - $mapping = array(); + $container->setParameter('mopa_bootstrap.flash.closeable', $config['flash']['closeable']); + $mapping = array(); foreach ($config['flash']['mapping'] as $alertType => $flashTypes) { foreach ($flashTypes as $type) { $mapping[$type] = $alertType; diff --git a/Resources/config/twig.xml b/Resources/config/twig.xml index 3d23735a2..b697bfd13 100644 --- a/Resources/config/twig.xml +++ b/Resources/config/twig.xml @@ -24,6 +24,9 @@ + + + diff --git a/Resources/doc/configuration-reference.md b/Resources/doc/configuration-reference.md index 31256903a..fc95887df 100644 --- a/Resources/doc/configuration-reference.md +++ b/Resources/doc/configuration-reference.md @@ -88,6 +88,7 @@ mopa_bootstrap: diagnostic_mode: false flash: + closeable: false mapping: # alertType => [flashType1, ..] success: [success] diff --git a/Resources/views/flash.html.twig b/Resources/views/flash.html.twig index 5b3eac273..f7f244981 100644 --- a/Resources/views/flash.html.twig +++ b/Resources/views/flash.html.twig @@ -46,7 +46,7 @@ {% set domain = 'FOSUserBundle' %} {% endif %} {% for message in messages %} - {{ flash_messages.flash(mapping[type], message, close, use_raw, class, domain) }} + {{ flash_messages.flash(mapping[type], message, flash_closeable, use_raw, class, domain) }} {% endfor %} {% endfor %} {% endif %} diff --git a/Twig/FlashExtension.php b/Twig/FlashExtension.php index 8dd9b8ea6..b282de53a 100644 --- a/Twig/FlashExtension.php +++ b/Twig/FlashExtension.php @@ -11,6 +11,7 @@ namespace Mopa\Bundle\BootstrapBundle\Twig; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Response; /** @@ -20,6 +21,16 @@ */ class FlashExtension extends \Twig_Extension { + /** + * @var ContainerInterface + */ + protected $container; + + /** + * @var \Twig_Environment + */ + protected $environment; + /** * @var array */ @@ -35,6 +46,38 @@ public function __construct(array $mapping) $this->mapping = $mapping; } + /** + * @param ContainerInterface $container + */ + public function setContainer(ContainerInterface $container) + { + $this->container = $container; + } + + /** + * {@inheritdoc} + */ + public function initRuntime(\Twig_Environment $environment) + { + $this->environment = $environment; + } + + /** + * {@inheritdoc} + */ + public function getGlobals() + { + if ($this->container->hasParameter('mopa_bootstrap.flash.closeable')) { + $closeable = $this->container->getParameter('mopa_bootstrap.flash.closeable'); + } else { + $closeable = false; + } + + return array( + 'flash_closeable' => $closeable, + ); + } + /** * {@inheritdoc} */ From baeb410d1f640239e5ed4d99b01a04ff05836edf Mon Sep 17 00:00:00 2001 From: Martin Aarhof Date: Wed, 9 Apr 2014 16:52:17 +0200 Subject: [PATCH 2/5] changed flash_closeable to only be used if close is not null or close is not defined --- Resources/views/flash.html.twig | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Resources/views/flash.html.twig b/Resources/views/flash.html.twig index f7f244981..509a8257e 100644 --- a/Resources/views/flash.html.twig +++ b/Resources/views/flash.html.twig @@ -41,12 +41,16 @@ {% set flashes = flashes | merge({ (type) : app.session.flashbag.get(type) }) %} {% endfor %} + {% if close is defined and close is not null %} + {% set close = flash_closeable %} + {% endif %} + {% for type, messages in flashes %} {% if type == 'fos_user_success' %} {% set domain = 'FOSUserBundle' %} {% endif %} {% for message in messages %} - {{ flash_messages.flash(mapping[type], message, flash_closeable, use_raw, class, domain) }} + {{ flash_messages.flash(mapping[type], message, close, use_raw, class, domain) }} {% endfor %} {% endfor %} {% endif %} From 267244590aeb01f49a11acc15e6d5ad27fe0f315 Mon Sep 17 00:00:00 2001 From: Martin Aarhof Date: Wed, 9 Apr 2014 17:29:46 +0200 Subject: [PATCH 3/5] removed service container from twig\flash --- Resources/config/twig.xml | 4 ++-- Resources/views/flash.html.twig | 2 +- Twig/FlashExtension.php | 34 ++++++--------------------------- 3 files changed, 9 insertions(+), 31 deletions(-) diff --git a/Resources/config/twig.xml b/Resources/config/twig.xml index b697bfd13..1875a4020 100644 --- a/Resources/config/twig.xml +++ b/Resources/config/twig.xml @@ -24,8 +24,8 @@ - - + + %mopa_bootstrap.flash.closeable% diff --git a/Resources/views/flash.html.twig b/Resources/views/flash.html.twig index 509a8257e..d9b4c3a7e 100644 --- a/Resources/views/flash.html.twig +++ b/Resources/views/flash.html.twig @@ -41,7 +41,7 @@ {% set flashes = flashes | merge({ (type) : app.session.flashbag.get(type) }) %} {% endfor %} - {% if close is defined and close is not null %} + {% if close is null %} {% set close = flash_closeable %} {% endif %} diff --git a/Twig/FlashExtension.php b/Twig/FlashExtension.php index b282de53a..1737e7ee7 100644 --- a/Twig/FlashExtension.php +++ b/Twig/FlashExtension.php @@ -11,9 +11,6 @@ namespace Mopa\Bundle\BootstrapBundle\Twig; -use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\Response; - /** * MopaBootstrap Flash Extension. * @@ -22,14 +19,9 @@ class FlashExtension extends \Twig_Extension { /** - * @var ContainerInterface - */ - protected $container; - - /** - * @var \Twig_Environment + * @var string */ - protected $environment; + protected $closeable; /** * @var array @@ -47,19 +39,11 @@ public function __construct(array $mapping) } /** - * @param ContainerInterface $container + * @param string $closeable */ - public function setContainer(ContainerInterface $container) + public function setCloseable($closeable) { - $this->container = $container; - } - - /** - * {@inheritdoc} - */ - public function initRuntime(\Twig_Environment $environment) - { - $this->environment = $environment; + $this->closeable = $closeable; } /** @@ -67,14 +51,8 @@ public function initRuntime(\Twig_Environment $environment) */ public function getGlobals() { - if ($this->container->hasParameter('mopa_bootstrap.flash.closeable')) { - $closeable = $this->container->getParameter('mopa_bootstrap.flash.closeable'); - } else { - $closeable = false; - } - return array( - 'flash_closeable' => $closeable, + 'flash_closeable' => $this->closeable, ); } From 38224b0d72606682189a83bb72867e3b9daefc0a Mon Sep 17 00:00:00 2001 From: Martin Aarhof Date: Wed, 9 Apr 2014 17:38:54 +0200 Subject: [PATCH 4/5] moved closeable variable to constructor --- Resources/config/twig.xml | 4 +--- Twig/FlashExtension.php | 10 ++-------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/Resources/config/twig.xml b/Resources/config/twig.xml index 1875a4020..5bc3f9f5c 100644 --- a/Resources/config/twig.xml +++ b/Resources/config/twig.xml @@ -23,10 +23,8 @@ + %mopa_bootstrap.flash.closeable% - - %mopa_bootstrap.flash.closeable% - diff --git a/Twig/FlashExtension.php b/Twig/FlashExtension.php index 1737e7ee7..2fb9c959e 100644 --- a/Twig/FlashExtension.php +++ b/Twig/FlashExtension.php @@ -32,17 +32,11 @@ class FlashExtension extends \Twig_Extension * Constructor. * * @param array $mapping - */ - public function __construct(array $mapping) - { - $this->mapping = $mapping; - } - - /** * @param string $closeable */ - public function setCloseable($closeable) + public function __construct(array $mapping, $closeable) { + $this->mapping = $mapping; $this->closeable = $closeable; } From b736fcf5fcd20b5952592f8d70ea14d1432c44ac Mon Sep 17 00:00:00 2001 From: Martin Aarhof Date: Wed, 9 Apr 2014 19:07:11 +0200 Subject: [PATCH 5/5] changed closeable variable to not be a global value, instead made a function --- Resources/views/flash.html.twig | 6 +----- Twig/FlashExtension.php | 17 ++++++++--------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/Resources/views/flash.html.twig b/Resources/views/flash.html.twig index d9b4c3a7e..7977a82db 100644 --- a/Resources/views/flash.html.twig +++ b/Resources/views/flash.html.twig @@ -41,16 +41,12 @@ {% set flashes = flashes | merge({ (type) : app.session.flashbag.get(type) }) %} {% endfor %} - {% if close is null %} - {% set close = flash_closeable %} - {% endif %} - {% for type, messages in flashes %} {% if type == 'fos_user_success' %} {% set domain = 'FOSUserBundle' %} {% endif %} {% for message in messages %} - {{ flash_messages.flash(mapping[type], message, close, use_raw, class, domain) }} + {{ flash_messages.flash(mapping[type], message, mopa_bootstrap_flash_closeable(close), use_raw, class, domain) }} {% endfor %} {% endfor %} {% endif %} diff --git a/Twig/FlashExtension.php b/Twig/FlashExtension.php index 2fb9c959e..3261f1159 100644 --- a/Twig/FlashExtension.php +++ b/Twig/FlashExtension.php @@ -43,21 +43,20 @@ public function __construct(array $mapping, $closeable) /** * {@inheritdoc} */ - public function getGlobals() + public function getFunctions() { return array( - 'flash_closeable' => $this->closeable, + new \Twig_SimpleFunction('mopa_bootstrap_flash_mapping', array($this, 'getMapping'), array('is_safe' => array('html'))), + new \Twig_SimpleFunction('mopa_bootstrap_flash_closeable', array($this, 'getCloseable')), ); } - /** - * {@inheritdoc} - */ - public function getFunctions() + public function getCloseable($close = null) { - return array( - new \Twig_SimpleFunction('mopa_bootstrap_flash_mapping', array($this, 'getMapping'), array('is_safe' => array('html'))), - ); + if ($close === null) { + return $this->closeable; + } + return $close; } /**