From f82d42e72909e5e4310ffb34cfaf50f7940cf9ba Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Wed, 12 Nov 2014 10:58:21 +0000 Subject: [PATCH] Add "buttonGenerate" event --- .../Button/Generator/AbstractGenerator.php | 25 ++++++++++++++++++- .../Mibew/Button/Generator/ImageGenerator.php | 4 +-- .../Generator/OperatorCodeGenerator.php | 6 ++--- .../Mibew/Button/Generator/TextGenerator.php | 6 ++--- .../classes/Mibew/EventDispatcher/Events.php | 13 ++++++++++ 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/mibew/libs/classes/Mibew/Button/Generator/AbstractGenerator.php b/src/mibew/libs/classes/Mibew/Button/Generator/AbstractGenerator.php index 843cfcc0..10857ed2 100644 --- a/src/mibew/libs/classes/Mibew/Button/Generator/AbstractGenerator.php +++ b/src/mibew/libs/classes/Mibew/Button/Generator/AbstractGenerator.php @@ -19,13 +19,15 @@ namespace Mibew\Button\Generator; +use Mibew\EventDispatcher\EventDispatcher; +use Mibew\EventDispatcher\Events; use Mibew\Routing\Generator\SecureUrlGeneratorInterface as RouteUrlGeneratorInterface; use Mibew\Style\ChatStyle; /** * Contains base button generation functionality. */ -abstract class AbstractGenerator +abstract class AbstractGenerator implements GeneratorInterface { /** * A routes URL generator. @@ -73,6 +75,27 @@ abstract class AbstractGenerator return isset($this->options[$name]) ? $this->options[$name] : $default; } + /** + * {@inheritdoc} + */ + public function generate() + { + $args = array( + 'button' => $this->doGenerate(), + 'generator' => $this, + ); + EventDispatcher::getInstance()->triggerEvent(Events::BUTTON_GENERATE, $args); + + return (string)$args['button']; + } + + /** + * Really generates the button. + * + * @return \Canteen\HTML5\Fragment Button's markup. + */ + abstract protected function doGenerate(); + /** * Generates URL for the specified route. * diff --git a/src/mibew/libs/classes/Mibew/Button/Generator/ImageGenerator.php b/src/mibew/libs/classes/Mibew/Button/Generator/ImageGenerator.php index e478778b..dd37a32d 100644 --- a/src/mibew/libs/classes/Mibew/Button/Generator/ImageGenerator.php +++ b/src/mibew/libs/classes/Mibew/Button/Generator/ImageGenerator.php @@ -59,7 +59,7 @@ class ImageGenerator extends TextGenerator /** * {@inheritdoc} */ - public function generate() + public function doGenerate() { $image_link_args = array( 'i' => $this->getOption('image'), @@ -88,7 +88,7 @@ class ImageGenerator extends TextGenerator $button->addChild($this->getWidgetCode()); $button->addChild(HTML5\html('comment', '/ mibew button')); - return (string)$button; + return $button; } /** diff --git a/src/mibew/libs/classes/Mibew/Button/Generator/OperatorCodeGenerator.php b/src/mibew/libs/classes/Mibew/Button/Generator/OperatorCodeGenerator.php index c8898ef6..f83ad334 100644 --- a/src/mibew/libs/classes/Mibew/Button/Generator/OperatorCodeGenerator.php +++ b/src/mibew/libs/classes/Mibew/Button/Generator/OperatorCodeGenerator.php @@ -24,12 +24,12 @@ use Canteen\HTML5; /** * Generates an Operator's Code field. */ -class OperatorCodeGenerator extends AbstractGenerator implements GeneratorInterface +class OperatorCodeGenerator extends AbstractGenerator { /** * {@inheritdoc} */ - public function generate() + public function doGenerate() { $form = HTML5\html('form'); $form->setAttributes(array( @@ -60,6 +60,6 @@ class OperatorCodeGenerator extends AbstractGenerator implements GeneratorInterf $button->addChild($form); $button->addChild(HTML5\html('comment', '/ mibew operator code field')); - return (string)$button; + return $button; } } diff --git a/src/mibew/libs/classes/Mibew/Button/Generator/TextGenerator.php b/src/mibew/libs/classes/Mibew/Button/Generator/TextGenerator.php index a6266482..ad0acd44 100644 --- a/src/mibew/libs/classes/Mibew/Button/Generator/TextGenerator.php +++ b/src/mibew/libs/classes/Mibew/Button/Generator/TextGenerator.php @@ -24,19 +24,19 @@ use Canteen\HTML5; /** * Generates a Text button. */ -class TextGenerator extends AbstractGenerator implements GeneratorInterface +class TextGenerator extends AbstractGenerator { /** * {@inheritdoc} */ - public function generate() + public function doGenerate() { $button = HTML5\html('fragment'); $button->addChild(HTML5\html('comment', 'mibew text link')); $button->addChild($this->getPopup($this->getOption('caption'))); $button->addChild(HTML5\html('comment', '/ mibew text link')); - return (string)$button; + return $button; } /** diff --git a/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php b/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php index 0dc35971..4535b674 100644 --- a/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php +++ b/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php @@ -54,6 +54,19 @@ final class Events */ const BAN_DELETE = 'banDelete'; + /** + * A button is generated. + * + * This event is triggered after a button has been deleted. An associative + * array with the following items is passed to the event handlers: + * - "button": an instance of {@link \Canteen\HTML5\Fragment} which + * represents markup of the button. + * - "generator": an instance of + * {@link \Mibew\Button\Generator\GeneratorInterface} which is used for + * button generation. + */ + const BUTTON_GENERATE = 'buttonGenerate'; + /** * Cron is run. *