diff --git a/src/Classes/LimelightWord.php b/src/Classes/LimelightWord.php index f94db63..45d843f 100644 --- a/src/Classes/LimelightWord.php +++ b/src/Classes/LimelightWord.php @@ -82,6 +82,11 @@ class LimelightWord */ private $converter; + /** + * @var Limelight\Limelight + */ + private $limelight; + /** * Flag for calling Converter. * @@ -96,12 +101,14 @@ class LimelightWord * @param array $properties * @param Converter $converter */ - public function __construct($token, $properties, Converter $converter) + public function __construct($token, $properties, Converter $converter, Limelight $limelight) { $this->setProperties($token, $properties); $this->converter = $converter; + $this->limelight = $limelight; + $this->setMissingParameters(); } @@ -426,9 +433,7 @@ private function setMissingParameters() $this->pronunciation = $this->word; - $limelight = new Limelight(); - - $results = $limelight->noParse($this->word()); + $results = $this->limelight->noParse($this->word(), ['Romaji'], true); $this->setPluginData('Romaji', $results->toRomaji()->words()); } diff --git a/src/Events/Dispatcher.php b/src/Events/Dispatcher.php index 20c5e72..fd206b8 100644 --- a/src/Events/Dispatcher.php +++ b/src/Events/Dispatcher.php @@ -13,6 +13,13 @@ class Dispatcher */ protected $registeredListeners = []; + /** + * When false, events are fired. + * + * @var boolean + */ + protected $supressEvents = false; + /** * Construct. * @@ -72,7 +79,7 @@ public function getListeners() */ public function fire($eventName, $payload = null) { - if (isset($this->registeredListeners[$eventName])) { + if (isset($this->registeredListeners[$eventName]) && $this->supressEvents === false) { $listeners = $this->registeredListeners[$eventName]; return array_map(function (LimelightListener $listener) use ($payload) { @@ -82,4 +89,22 @@ public function fire($eventName, $payload = null) return false; } + + /** + * Turn eventing on/off. + * + * @param bool $supressEvents + * + * @return bool + */ + public function toggleEvents($supressEvents) + { + if ($supressEvents === true && $this->supressEvents === false) { + $this->supressEvents = true; + } elseif ($supressEvents === true && $this->supressEvents === true) { + $this->supressEvents = false; + } + + return $this->supressEvents; + } } diff --git a/src/Limelight.php b/src/Limelight.php index 7b29d85..fcb75b5 100644 --- a/src/Limelight.php +++ b/src/Limelight.php @@ -63,14 +63,21 @@ public function parse($text, $runPlugins = true) * * @param string $text * @param array $pluginWhiteList [Plugins to run] + * @param bool $fireEvents [When false, events will not be fired] * * @return Limelight\Classes\LimelightResults/ InvalidInputException */ - public function noParse($text, $pluginWhiteList = ['Romaji']) + public function noParse($text, $pluginWhiteList = ['Romaji'], $supressEvents = false) { + $this->dispatcher->toggleEvents($supressEvents); + $noParser = new NoParser($this, $this->dispatcher); - return $noParser->handle($text, $pluginWhiteList); + $results = $noParser->handle($text, $pluginWhiteList); + + $this->dispatcher->toggleEvents($supressEvents); + + return $results; } /** diff --git a/src/Parse/NoParser.php b/src/Parse/NoParser.php index 5b06ba1..575d29c 100644 --- a/src/Parse/NoParser.php +++ b/src/Parse/NoParser.php @@ -57,7 +57,7 @@ public function handle($text, array $pluginWhiteList) $properties = $this->buildProperties(); - $words = [new LimelightWord($token, $properties, $converter)]; + $words = [new LimelightWord($token, $properties, $converter, $this->limelight)]; $this->dispatcher->fire('WordWasCreated', $words[0]); diff --git a/src/Parse/TokenParser.php b/src/Parse/TokenParser.php index ccdfecc..b9576fe 100644 --- a/src/Parse/TokenParser.php +++ b/src/Parse/TokenParser.php @@ -172,7 +172,7 @@ private function appendWordToLast($current, $properties, $previousWord) */ private function makeNewWord($current, $properties, Converter $converter) { - $word = new LimelightWord($current, $properties, $converter); + $word = new LimelightWord($current, $properties, $converter, $this->limelight); $this->dispatcher->fire('WordWasCreated', $word); diff --git a/tests/Integration/LimelightTest.php b/tests/Integration/LimelightTest.php index 6084a6a..2d2e69a 100644 --- a/tests/Integration/LimelightTest.php +++ b/tests/Integration/LimelightTest.php @@ -210,4 +210,48 @@ public function it_fires_event_that_sets_new_listener_after_results_object_is_cr $config->resetConfig(); } + + /** + * @test + */ + public function it_doesnt_fires_event_when_supressed_in_noparse() + { + $config = Config::getInstance(); + + $config->set(['Limelight\Tests\Stubs\TestListener'], 'listeners', 'WordWasCreated'); + + $this->assertEquals('Limelight\Tests\Stubs\TestListener', $config->get('listeners')['WordWasCreated'][0]); + + $limelight = new Limelight(); + + $results = $limelight->noParse('できるかな。。。', ['Romaji'], true); + + $this->assertEquals(['Limelight\Tests\Stubs\TestListener'], $config->get('listeners')['WordWasCreated']); + + $config->resetConfig(); + } + + /** + * @test + */ + public function it_turns_events_back_on_after_running_noparse_with_event_supression() + { + $config = Config::getInstance(); + + $config->set(['Limelight\Tests\Stubs\TestListener'], 'listeners', 'WordWasCreated'); + + $this->assertEquals('Limelight\Tests\Stubs\TestListener', $config->get('listeners')['WordWasCreated'][0]); + + $limelight = new Limelight(); + + $results = $limelight->noParse('できるかな。。。', ['Romaji'], true); + + $this->assertEquals(['Limelight\Tests\Stubs\TestListener'], $config->get('listeners')['WordWasCreated']); + + $results = $limelight->parse('出来るかな。。。'); + + $this->assertEquals([], $config->get('listeners')['WordWasCreated']); + + $config->resetConfig(); + } } diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index 0b1a43d..9f774f3 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -133,6 +133,40 @@ public function dispatcher_sends_payload() $this->assertEquals('Payload says Hello!', $result[0]); } + /** + * @test + */ + public function dispatcher_can_be_supressed() + { + $dispatcher = $this->buildDispatcher(); + + $dispatcher->toggleEvents(true); + + $listener = new TestListener(); + + $dispatcher->addListeners($listener, 'WordWasCreated'); + + $result = $dispatcher->fire('WordWasCreated'); + + $this->assertFalse($result); + } + + /** + * @test + */ + public function dispatcher_toggles_event_supression() + { + $dispatcher = $this->buildDispatcher(); + + $supressEvents = $dispatcher->toggleEvents(true); + + $this->assertTrue($supressEvents); + + $supressEvents = $dispatcher->toggleEvents(true); + + $this->assertFalse($supressEvents); + } + /** * @test *