Skip to content

Commit

Permalink
Add eventing system.
Browse files Browse the repository at this point in the history
Add error handling for event system.
  • Loading branch information
zachleigh committed Apr 18, 2016
1 parent 097e555 commit d9882ba
Show file tree
Hide file tree
Showing 14 changed files with 521 additions and 24 deletions.
5 changes: 1 addition & 4 deletions limelight_console
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Limelight\Limelight;

require __DIR__ . '/vendor/autoload.php';

$input = (isset($argv[1]) ?: 'こんにちは');
$input = (isset($argv[1]) ? $argv[1]: 'こんにちは');

$method = (isset($argv[2]) ? $argv[2] : null);

Expand All @@ -23,6 +23,3 @@ if ($method && $flag) {
} else {
var_dump($results);
}
$tagger = new \MeCab\Tagger();
$class = new ReflectionClass($tagger);
var_dump($class->getMethods());
2 changes: 1 addition & 1 deletion src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Config
{
/**
* config.php.
*q
*
* @var array
*/
private $configFile;
Expand Down
85 changes: 85 additions & 0 deletions src/Events/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Limelight\Events;

use Limelight\Exceptions\EventErrorException;

class Dispatcher
{
/**
* Registered listeners.
*
* @var array
*/
protected $registeredListeners = [];

/**
* Construct.
*
* @param array $configListeners
*/
public function __construct(array $configListeners)
{
$this->addAllListeners($configListeners);
}

/**
* Add array of listeners from config.
*
* @param array $configListeners
*/
public function addAllListeners(array $configListeners)
{
array_walk($configListeners, [$this, 'addListeners']);
}

/**
* Add a single listener.
*
* @param LimelightListener|array $listeners
* @param string $eventName
*/
public function addListeners($listeners, $eventName)
{
$listeners = (is_array($listeners) ? $listeners : [$listeners]);

array_walk($listeners, function ($listener) use ($eventName) {
if (is_string($listener) && !class_exists($listener)) {
throw new EventErrorException("Class {$listener} does not exist.");
}

$this->registeredListeners[$eventName][] = new $listener();
});
}

/**
* Get all registered listeners.
*
* @return array
*/
public function getListeners()
{
return $this->registeredListeners;
}

/**
* Call handle method on all listeners for event.
*
* @param string $eventName
* @param mixed $payload
*
* @return mixed
*/
public function fire($eventName, $payload = null)
{
if (isset($this->registeredListeners[$eventName])) {
$listeners = $this->registeredListeners[$eventName];

return array_map(function (LimelightListener $listener) use ($payload) {
return $listener->handle($payload);
}, $listeners);
}

return false;
}
}
13 changes: 13 additions & 0 deletions src/Events/LimelightListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Limelight\Events;

interface LimelightListener
{
/**
* Respond to event.
*
* @param object $payload
*/
public function handle($payload);
}
33 changes: 33 additions & 0 deletions src/Exceptions/EventErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Limelight\Exceptions;

class EventErrorException extends LimelightException
{
/**
* @var string
*/
protected $message;

/**
* Construct.
*
* @param string $message
*/
public function __construct($message = 'Event error.')
{
$this->message = $message;

parent::__construct($message);
}

/**
* How to display error.
*
* @return string
*/
public function __toString()
{
return $this->handle();
}
}
18 changes: 14 additions & 4 deletions src/Limelight.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Limelight\Config\Config;
use Limelight\Parse\NoParser;
use Limelight\Parse\Tokenizer;
use Limelight\Events\Dispatcher;
use Limelight\Parse\TokenParser;

class Limelight
Expand All @@ -19,13 +20,22 @@ class Limelight
*/
private $mecab;

/**
* Dispatcher for eventing.
*
* @var Limelight\Events\Dispatcher
*/
private $dispatcher;

/**
* Boot.
*/
public function __construct()
{
$config = Config::getInstance();

$this->dispatcher = new Dispatcher($config->get('listeners'));

$this->mecab = $config->make('Limelight\Mecab\Mecab');
}

Expand All @@ -41,9 +51,9 @@ public function parse($text, $runPlugins = true)
{
$tokenizer = new Tokenizer();

$tokenParser = new TokenParser();
$tokenParser = new TokenParser($this, $this->dispatcher);

$parser = new Parser($this->mecab, $tokenizer, $tokenParser);
$parser = new Parser($this->mecab, $tokenizer, $tokenParser, $this->dispatcher);

return $parser->handle($text, $runPlugins);
}
Expand All @@ -58,13 +68,13 @@ public function parse($text, $runPlugins = true)
*/
public function noParse($text, $pluginWhiteList = ['Romaji'])
{
$noParser = new NoParser();
$noParser = new NoParser($this, $this->dispatcher);

return $noParser->handle($text, $pluginWhiteList);
}

/**
* Dynamically set config values.
* Dynamically set config values. Could be dangerous, be careful.
*
* @param string $value
* @param string $key1
Expand Down
34 changes: 30 additions & 4 deletions src/Parse/NoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Limelight\Parse;

use Limelight\Limelight;
use Limelight\Events\Dispatcher;
use Limelight\Helpers\Converter;
use Limelight\Helpers\PluginHelper;
use Limelight\Classes\LimelightWord;
Expand All @@ -15,6 +16,27 @@ class NoParser
use PluginHelper;
use JapaneseHelpers;

/**
* @var Limelight\Limelight
*/
private $limelight;

/**
* @var Limelight\Events\Dispatcher
*/
private $dispatcher;

/**
* Construct.
*
* @param Dispatcher $dispatcher
*/
public function __construct(Limelight $limelight, Dispatcher $dispatcher)
{
$this->limelight = $limelight;
$this->dispatcher = $dispatcher;
}

/**
* Handle the no-parse for given text.
*
Expand All @@ -29,19 +51,23 @@ public function handle($text, array $pluginWhiteList)
throw new InvalidInputException('Text must not contain kanji.');
}

$limelight = new Limelight();

$converter = new Converter($limelight);
$converter = new Converter($this->limelight);

$token = $this->buildToken($text);

$properties = $this->buildProperties();

$words = [new LimelightWord($token, $properties, $converter)];

$this->dispatcher->fire('WordWasCreated', $words[0]);

$pluginResults = $this->runPlugins($text, null, $token, $words, $pluginWhiteList);

return new LimelightResults($text, $words, $pluginResults);
$results = new LimelightResults($text, $words, $pluginResults);

$this->dispatcher->fire('ParseWasSuccessful', $results);

return $results;
}

/**
Expand Down
17 changes: 14 additions & 3 deletions src/Parse/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Limelight\Parse;

use Limelight\Mecab\Mecab;
use Limelight\Events\Dispatcher;
use Limelight\Helpers\PluginHelper;
use Limelight\Classes\LimelightResults;

Expand All @@ -11,7 +12,7 @@ class Parser
use PluginHelper;

/**
* @var implements Limelight\Mecab\Mecab
* @var Limelight\Mecab\Mecab
*/
private $mecab;

Expand All @@ -25,18 +26,24 @@ class Parser
*/
private $tokenParser;

/**
* @var Limelight\Events\Dispatcher
*/
private $dispatcher;

/**
* Construct.
*
* @param Mecab $mecab
* @param Tokenizer $tokenizer
* @param TokenParser $tokenParser
*/
public function __construct(Mecab $mecab, Tokenizer $tokenizer, TokenParser $tokenParser)
public function __construct(Mecab $mecab, Tokenizer $tokenizer, TokenParser $tokenParser, Dispatcher $dispatcher)
{
$this->mecab = $mecab;
$this->tokenizer = $tokenizer;
$this->tokenParser = $tokenParser;
$this->dispatcher = $dispatcher;
}

/**
Expand All @@ -57,6 +64,10 @@ public function handle($text, $runPlugins)

$pluginResults = ($runPlugins ? $this->runPlugins($text, $node, $tokens, $words) : null);

return new LimelightResults($text, $words, $pluginResults);
$results = new LimelightResults($text, $words, $pluginResults);

$this->dispatcher->fire('ParseWasSuccessful', $results);

return $results;
}
}
28 changes: 25 additions & 3 deletions src/Parse/TokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
namespace Limelight\Parse;

use Limelight\Limelight;
use Limelight\Events\Dispatcher;
use Limelight\Helpers\Converter;
use Limelight\Classes\LimelightWord;
use Limelight\Parse\PartOfSpeech\POSRegistry;

class TokenParser
{
/**
* @var Limelight\Limelight
*/
private $limelight;

/**
* @var Limelight\Events\Dispatcher
*/
private $dispatcher;

/**
* @var array
*/
Expand All @@ -29,16 +40,25 @@ class TokenParser
'updatePOS' => false,
];

/**
* Construct.
*
* @param Limelight $limelight [description]
*/
public function __construct(Limelight $limelight, Dispatcher $dispatcher)
{
$this->limelight = $limelight;
$this->dispatcher = $dispatcher;
}

/**
* Parse the text by filtering through the tokens.
*
* @return array
*/
public function parseTokens($tokens)
{
$limelight = new Limelight();

$converter = new Converter($limelight);
$converter = new Converter($this->limelight);

$registry = POSRegistry::getInstance();

Expand Down Expand Up @@ -154,6 +174,8 @@ private function makeNewWord($current, $properties, Converter $converter)
{
$word = new LimelightWord($current, $properties, $converter);

$this->dispatcher->fire('WordWasCreated', $word);

$this->words[] = $word;
}

Expand Down
Loading

0 comments on commit d9882ba

Please sign in to comment.