From fe7fc8f0de4518ce2469c5cfa2138a508c79d1f0 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Wed, 20 Jan 2016 22:10:29 +0100 Subject: [PATCH 01/19] Added support for filtering, fixes #11 --- README.md | 4 ++++ composer.json | 3 ++- composer.lock | 48 ++++++++++++++++++++++++++++++++++++++++++-- src/Plugin.php | 21 ++++++++++++++++++- tests/PluginTest.php | 35 ++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8f71c9c..1ebf536 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ return array( 'shortenTimeout' => 15 // If after this amount of seconds no url shortener has come up with a short URL the normal URL will be used. (Not in effect when there are no shorteners listening.) + // or + + 'filter' => null // Any valid filter implementing Phergie\Irc\Plugin\React\EventFilter\FilterInterface to filter which messages should be handled + )), ) diff --git a/composer.json b/composer.json index 01d25fb..45f0491 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "phergie/phergie-irc-bot-react": "~1.0", "nojimage/twitter-text-php": "1.1.1", "phergie/phergie-irc-plugin-http": "^3.0.0-alpha1", - "react/promise": "~1.0|~2.0" + "react/promise": "~1.0|~2.0", + "phergie/phergie-irc-plugin-react-eventfilter": "^1.0||^2.0" }, "require-dev": { "phergie/phergie-irc-bot-react-development": "^1.0.2|^1.1" diff --git a/composer.lock b/composer.lock index 8f3b1dd..9c06ec4 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": "d0072db27d441f768e943e0ba28dab03", - "content-hash": "94bdd2d4158ea4388156b1fc16c94c1a", + "hash": "4ec773555525dcb42c13387250a92056", + "content-hash": "4df6fea214869f357b6df127e6525995", "packages": [ { "name": "evenement/evenement", @@ -773,6 +773,50 @@ ], "time": "2015-11-15 10:55:33" }, + { + "name": "phergie/phergie-irc-plugin-react-eventfilter", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/phergie/phergie-irc-plugin-react-eventfilter.git", + "reference": "5d21541025745f6dbf1ee132ebf183d553d54105" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phergie/phergie-irc-plugin-react-eventfilter/zipball/5d21541025745f6dbf1ee132ebf183d553d54105", + "reference": "5d21541025745f6dbf1ee132ebf183d553d54105", + "shasum": "" + }, + "require": { + "phergie/phergie-irc-bot-react": "~1" + }, + "require-dev": { + "phake/phake": "2.0.0-beta2", + "phergie/phergie-irc-plugin-react-usermode": "~1", + "phpunit/phpunit": "4.1.*" + }, + "suggest": { + "phergie/phergie-irc-plugin-react-usermode": "supports filtering of events by channel modes of originating users" + }, + "type": "library", + "autoload": { + "psr-4": { + "Phergie\\Irc\\Plugin\\React\\EventFilter\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause" + ], + "description": "Phergie plugin for limiting processing of incoming events based on event metadata", + "keywords": [ + "bot", + "irc", + "plugin", + "react" + ], + "time": "2015-04-06 13:39:08" + }, { "name": "psr/http-message", "version": "1.0", diff --git a/src/Plugin.php b/src/Plugin.php index b624d94..4b11069 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -16,6 +16,7 @@ use Phergie\Irc\Bot\React\EventQueue; use Phergie\Irc\Client\React\LoopAwareInterface; use Phergie\Irc\Event\UserEvent; +use Phergie\Irc\Plugin\React\EventFilter\FilterInterface; use React\Promise\Deferred; use Phergie\Plugin\Http\Request; @@ -27,7 +28,8 @@ */ class Plugin extends AbstractPlugin implements LoopAwareInterface { - const URL_HANDLER_INTERFACE = 'Phergie\Irc\Plugin\React\Url\UrlHandlerInterface'; + const URL_HANDLER_INTERFACE = 'Phergie\Irc\Plugin\React\Url\UrlHandlerInterface'; + const EVENT_FILTER_INTERFACE = 'Phergie\Irc\Plugin\React\EventFilter\FilterInterface'; /** * @var UrlHandlerInterface @@ -42,6 +44,11 @@ class Plugin extends AbstractPlugin implements LoopAwareInterface */ protected $hostUrlEmitsOnly = false; + /** + * @var FilterInterface + */ + protected $filter = null; + /** * @var LoopInterface */ @@ -73,6 +80,12 @@ public function __construct(array $config = []) if (isset($config['hostUrlEmitsOnly'])) { $this->hostUrlEmitsOnly = boolval($config['hostUrlEmitsOnly']); } + if ( + isset($config['filter']) && + in_array(static::EVENT_FILTER_INTERFACE, class_implements($config['filter'])) + ) { + $this->filter = $config['filter']; + } } /** @@ -113,6 +126,12 @@ public function logDebug($message) public function handleIrcReceived(UserEvent $event, EventQueue $queue) { + if ( + $this->filter !== null && + $this->filter->filter($event) !== false + ) { + return; + } $params = $event->getParams(); $extractor = new \Twitter_Extractor($params['text']); $urls = $extractor->extractURLs(); diff --git a/tests/PluginTest.php b/tests/PluginTest.php index f4c4c21..1b1cb3c 100644 --- a/tests/PluginTest.php +++ b/tests/PluginTest.php @@ -12,6 +12,7 @@ use GuzzleHttp\Message\Response; use Phake; +use Phergie\Irc\Plugin\React\EventFilter\NotFilter; use Phergie\Irc\Plugin\React\Url\Plugin; use React\Promise\FulfilledPromise; @@ -351,4 +352,38 @@ public function testHandleUrlRequest() { )) ); } + + public function testFiltered() + { + $event = Phake::mock('Phergie\Irc\Event\UserEvent'); + Phake::when($event)->getParams()->thenReturn([ + 'text' => '', + ]); + $queue = Phake::mock('Phergie\Irc\Bot\React\EventQueue'); + $filter = Phake::mock('Phergie\Irc\Plugin\React\EventFilter\FilterInterface'); + Phake::when($filter)->filter($this->isInstanceOf('Phergie\Irc\Event\EventInterface'))->thenReturn(false); + + (new Plugin([ + 'filter' => new NotFilter($filter), + ]))->handleIrcReceived($event, $queue); + + Phake::verify($event, Phake::never())->getParams(); + } + + public function testNotFiltered() + { + $event = Phake::mock('Phergie\Irc\Event\UserEvent'); + Phake::when($event)->getParams()->thenReturn([ + 'text' => '', + ]); + $queue = Phake::mock('Phergie\Irc\Bot\React\EventQueue'); + $filter = Phake::mock('Phergie\Irc\Plugin\React\EventFilter\FilterInterface'); + Phake::when($filter)->filter($this->isInstanceOf('Phergie\Irc\Event\EventInterface'))->thenReturn(true); + + (new Plugin([ + 'filter' => new NotFilter($filter), + ]))->handleIrcReceived($event, $queue); + + Phake::verify($event)->getParams(); + } } From 252137dd9bcbebc50791250cd82cfd90d2e0482e Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Thu, 21 Jan 2016 09:25:03 +0100 Subject: [PATCH 02/19] 5.4 => 5.5 ::class class name resolution --- src/Plugin.php | 7 ++----- tests/PluginTest.php | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 4b11069..ddb7d4a 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -28,9 +28,6 @@ */ class Plugin extends AbstractPlugin implements LoopAwareInterface { - const URL_HANDLER_INTERFACE = 'Phergie\Irc\Plugin\React\Url\UrlHandlerInterface'; - const EVENT_FILTER_INTERFACE = 'Phergie\Irc\Plugin\React\EventFilter\FilterInterface'; - /** * @var UrlHandlerInterface */ @@ -68,7 +65,7 @@ public function __construct(array $config = []) { if ( isset($config['handler']) && - in_array(static::URL_HANDLER_INTERFACE, class_implements($config['handler'])) + in_array(UrlHandlerInterface::class, class_implements($config['handler'])) ) { $this->handler = $config['handler']; } else { @@ -82,7 +79,7 @@ public function __construct(array $config = []) } if ( isset($config['filter']) && - in_array(static::EVENT_FILTER_INTERFACE, class_implements($config['filter'])) + in_array(FilterInterface::class, class_implements($config['filter'])) ) { $this->filter = $config['filter']; } diff --git a/tests/PluginTest.php b/tests/PluginTest.php index 1b1cb3c..d1ae931 100644 --- a/tests/PluginTest.php +++ b/tests/PluginTest.php @@ -12,6 +12,10 @@ use GuzzleHttp\Message\Response; use Phake; +use Phergie\Irc\Bot\React\EventQueue; +use Phergie\Irc\Event\EventInterface; +use Phergie\Irc\Event\UserEvent; +use Phergie\Irc\Plugin\React\EventFilter\FilterInterface; use Phergie\Irc\Plugin\React\EventFilter\NotFilter; use Phergie\Irc\Plugin\React\Url\Plugin; use React\Promise\FulfilledPromise; @@ -355,13 +359,13 @@ public function testHandleUrlRequest() { public function testFiltered() { - $event = Phake::mock('Phergie\Irc\Event\UserEvent'); + $event = Phake::mock(UserEvent::class); Phake::when($event)->getParams()->thenReturn([ 'text' => '', ]); - $queue = Phake::mock('Phergie\Irc\Bot\React\EventQueue'); - $filter = Phake::mock('Phergie\Irc\Plugin\React\EventFilter\FilterInterface'); - Phake::when($filter)->filter($this->isInstanceOf('Phergie\Irc\Event\EventInterface'))->thenReturn(false); + $queue = Phake::mock(EventQueue::class); + $filter = Phake::mock(FilterInterface::class); + Phake::when($filter)->filter($this->isInstanceOf(EventInterface::class))->thenReturn(false); (new Plugin([ 'filter' => new NotFilter($filter), @@ -372,13 +376,13 @@ public function testFiltered() public function testNotFiltered() { - $event = Phake::mock('Phergie\Irc\Event\UserEvent'); + $event = Phake::mock(UserEvent::class); Phake::when($event)->getParams()->thenReturn([ 'text' => '', ]); - $queue = Phake::mock('Phergie\Irc\Bot\React\EventQueue'); - $filter = Phake::mock('Phergie\Irc\Plugin\React\EventFilter\FilterInterface'); - Phake::when($filter)->filter($this->isInstanceOf('Phergie\Irc\Event\EventInterface'))->thenReturn(true); + $queue = Phake::mock(EventQueue::class); + $filter = Phake::mock(FilterInterface::class); + Phake::when($filter)->filter($this->isInstanceOf(EventInterface::class))->thenReturn(true); (new Plugin([ 'filter' => new NotFilter($filter), From d0bac367960301331c1d518e95d4e322af5a8fb3 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 23 Jan 2016 23:44:14 +0100 Subject: [PATCH 03/19] Event and filter for url's --- src/Filter/UrlEvent.php | 85 +++++++++++++++++++++++++++ src/Filter/UrlSectionFilter.php | 52 ++++++++++++++++ tests/Filter/UrlEventTest.php | 56 ++++++++++++++++++ tests/Filter/UrlSectionFilterTest.php | 44 ++++++++++++++ 4 files changed, 237 insertions(+) create mode 100644 src/Filter/UrlEvent.php create mode 100644 src/Filter/UrlSectionFilter.php create mode 100644 tests/Filter/UrlEventTest.php create mode 100644 tests/Filter/UrlSectionFilterTest.php diff --git a/src/Filter/UrlEvent.php b/src/Filter/UrlEvent.php new file mode 100644 index 0000000..a60470b --- /dev/null +++ b/src/Filter/UrlEvent.php @@ -0,0 +1,85 @@ +url = $url; + $this->parsedUrl = parse_url($url); + $this->event = $event; + } + + public function setMessage($message) + { + return $this->event->setMessage($message); + } + + public function getMessage() + { + return $this->event->getMessage(); + } + + public function setConnection(ConnectionInterface $connection) + { + return $this->event->setConnection($connection); + } + + public function getConnection() + { + return $this->event->getConnection(); + } + + public function setParams(array $params) + { + return $this->event->setParams($params); + } + + public function getParams() + { + return $this->event->getParams(); + } + + public function setCommand($command) + { + return $this->event->setCommand($command); + } + + public function getCommand() + { + return $this->event->getCommand(); + } + + public function getUrlSection($section) + { + if (isset($this->parsedUrl[$section])) { + return $this->parsedUrl[$section]; + } + + return false; + } +} diff --git a/src/Filter/UrlSectionFilter.php b/src/Filter/UrlSectionFilter.php new file mode 100644 index 0000000..dd19806 --- /dev/null +++ b/src/Filter/UrlSectionFilter.php @@ -0,0 +1,52 @@ +section = $section; + $this->value = $value; + } + + /** + * @param EventInterface $event + * @return bool|null + */ + public function filter(EventInterface $event) + { + if (!($event instanceof UrlEvent)) { + return null; + } + + $section = $event->getUrlSection($this->section); + if ($section === false) { + return null; + } + + $pattern = '/^' . str_replace('*', '.*', $this->value) . '$/'; + if (preg_match($pattern, $section)) { + return true; + } + + return false; + } +} diff --git a/tests/Filter/UrlEventTest.php b/tests/Filter/UrlEventTest.php new file mode 100644 index 0000000..5edc516 --- /dev/null +++ b/tests/Filter/UrlEventTest.php @@ -0,0 +1,56 @@ + 'http', + 'host' => 'phergie.org', + 'port' => false, + ] + ]; + + yield [ + 'https://phergie.org:80/', + [ + 'scheme' => 'https', + 'host' => 'phergie.org', + 'port' => 80, + ] + ]; + } + + /** + * @dataProvider provideGetUrlSection + */ + public function testGetUrlSection($url, array $iterations) + { + $event = Phake::mock(EventInterface::class); + $urlEvent = new UrlEvent($url, $event); + foreach ($iterations as $key => $value) { + $this->assertSame($value, $urlEvent->getUrlSection($key)); + } + } +} diff --git a/tests/Filter/UrlSectionFilterTest.php b/tests/Filter/UrlSectionFilterTest.php new file mode 100644 index 0000000..6180ff4 --- /dev/null +++ b/tests/Filter/UrlSectionFilterTest.php @@ -0,0 +1,44 @@ +assertSame($output, $filter->filter($urlEvent)); + } +} From 9a9f44c4ae0d1f1dcd643c0b05c601df97bb110b Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 23 Jan 2016 23:51:36 +0100 Subject: [PATCH 04/19] Pluging now runs each URL through the filter --- src/Plugin.php | 13 +++++++------ tests/PluginTest.php | 14 +++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index ddb7d4a..57aedfb 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -11,6 +11,7 @@ namespace Phergie\Irc\Plugin\React\Url; use GuzzleHttp\Message\Response; +use Phergie\Irc\Plugin\React\Url\Filter\UrlEvent; use React\EventLoop\LoopInterface; use Phergie\Irc\Bot\React\AbstractPlugin; use Phergie\Irc\Bot\React\EventQueue; @@ -123,17 +124,17 @@ public function logDebug($message) public function handleIrcReceived(UserEvent $event, EventQueue $queue) { - if ( - $this->filter !== null && - $this->filter->filter($event) !== false - ) { - return; - } $params = $event->getParams(); $extractor = new \Twitter_Extractor($params['text']); $urls = $extractor->extractURLs(); foreach ($urls as $url) { + if ( + $this->filter !== null && + $this->filter->filter(new UrlEvent($url, $event)) !== false + ) { + continue; + } $this->handleUrl($url, $event, $queue); } } diff --git a/tests/PluginTest.php b/tests/PluginTest.php index d1ae931..f9d75cb 100644 --- a/tests/PluginTest.php +++ b/tests/PluginTest.php @@ -361,33 +361,33 @@ public function testFiltered() { $event = Phake::mock(UserEvent::class); Phake::when($event)->getParams()->thenReturn([ - 'text' => '', + 'text' => 'http://phergie.org', ]); $queue = Phake::mock(EventQueue::class); $filter = Phake::mock(FilterInterface::class); - Phake::when($filter)->filter($this->isInstanceOf(EventInterface::class))->thenReturn(false); + Phake::when($filter)->filter($this->isInstanceOf(EventInterface::class))->thenReturn(true); (new Plugin([ - 'filter' => new NotFilter($filter), + 'filter' => $filter, ]))->handleIrcReceived($event, $queue); - Phake::verify($event, Phake::never())->getParams(); + Phake::verify($filter, Phake::times(1))->filter($this->isInstanceOf(EventInterface::class)); } public function testNotFiltered() { $event = Phake::mock(UserEvent::class); Phake::when($event)->getParams()->thenReturn([ - 'text' => '', + 'text' => 'http://phergie.org http://wyrihaximus.net', ]); $queue = Phake::mock(EventQueue::class); $filter = Phake::mock(FilterInterface::class); Phake::when($filter)->filter($this->isInstanceOf(EventInterface::class))->thenReturn(true); (new Plugin([ - 'filter' => new NotFilter($filter), + 'filter' => $filter, ]))->handleIrcReceived($event, $queue); - Phake::verify($event)->getParams(); + Phake::verify($filter, Phake::times(2))->filter($this->isInstanceOf(EventInterface::class)); } } From d4ec81af14c7835ddad7e4eade10358d3ccd61e6 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 24 Jan 2016 00:03:15 +0100 Subject: [PATCH 05/19] Refactored filtered test --- tests/PluginTest.php | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/tests/PluginTest.php b/tests/PluginTest.php index f9d75cb..7ce3811 100644 --- a/tests/PluginTest.php +++ b/tests/PluginTest.php @@ -357,28 +357,32 @@ public function testHandleUrlRequest() { ); } - public function testFiltered() + public function provideFiltered() { - $event = Phake::mock(UserEvent::class); - Phake::when($event)->getParams()->thenReturn([ - 'text' => 'http://phergie.org', - ]); - $queue = Phake::mock(EventQueue::class); - $filter = Phake::mock(FilterInterface::class); - Phake::when($filter)->filter($this->isInstanceOf(EventInterface::class))->thenReturn(true); - - (new Plugin([ - 'filter' => $filter, - ]))->handleIrcReceived($event, $queue); - - Phake::verify($filter, Phake::times(1))->filter($this->isInstanceOf(EventInterface::class)); + yield [ + '', + 0, + ]; + + yield [ + 'http://phergie.org', + 1, + ]; + + yield [ + 'http://phergie.org http://wyrihaximus.net', + 2, + ]; } - public function testNotFiltered() + /** + * @dataProvider provideFiltered + */ + public function testFiltered($text, $times) { $event = Phake::mock(UserEvent::class); Phake::when($event)->getParams()->thenReturn([ - 'text' => 'http://phergie.org http://wyrihaximus.net', + 'text' => $text, ]); $queue = Phake::mock(EventQueue::class); $filter = Phake::mock(FilterInterface::class); @@ -388,6 +392,6 @@ public function testNotFiltered() 'filter' => $filter, ]))->handleIrcReceived($event, $queue); - Phake::verify($filter, Phake::times(2))->filter($this->isInstanceOf(EventInterface::class)); + Phake::verify($filter, Phake::times($times))->filter($this->isInstanceOf(EventInterface::class)); } } From 9b5b8f8186166f18806e5b81ee8b71477eb3244b Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 24 Jan 2016 00:07:07 +0100 Subject: [PATCH 06/19] Added readme section about the UrlSectionFilter --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 1ebf536..152d0dd 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,17 @@ This plugins also emits two events for url shortening. Only called when there ar * `url.shorten.HOSTNAME` For example `url.shorten.twitter.com` (`www.` is stripped from the hostname). * `url.shorten.all` For all hostnames. +## UrlSectionFilter + +This plugin comes with the `UrlSectionFilter` that lets you filter on the different key value pairs coming out of [`parse_url`](http://php.net/parse_url). The following example filter allows `www.phergie.org`, `www2.phergie.org`, and `phergie.org`: + +```php +new OrFilter([ + new UrlSectionFilter('host', '*.phergie.org'), + new UrlSectionFilter('host', 'phergie.org'), +]) +`` + ## Tests To run the unit test suite: From 05af0f01a9623adaa9a0cd0c0466f15bde7b40c9 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Wed, 20 Jan 2016 22:10:29 +0100 Subject: [PATCH 07/19] Added support for filtering, fixes #11 --- README.md | 4 ++++ composer.json | 5 +++-- src/Plugin.php | 21 ++++++++++++++++++++- tests/PluginTest.php | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 559dc76..e00f164 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ return array( 'shortenTimeout' => 15 // If after this amount of seconds no url shortener has come up with a short URL the normal URL will be used. (Not in effect when there are no shorteners listening.) + // or + + 'filter' => null // Any valid filter implementing Phergie\Irc\Plugin\React\EventFilter\FilterInterface to filter which messages should be handled + )), ) diff --git a/composer.json b/composer.json index 5ad8367..41eb992 100644 --- a/composer.json +++ b/composer.json @@ -16,8 +16,9 @@ "php": "^5.5|^7.0", "phergie/phergie-irc-bot-react": "~2", "nojimage/twitter-text-php": "1.1.1", - "phergie/phergie-irc-plugin-http": "^4", - "react/promise": "~1.0|~2.0" + "phergie/phergie-irc-plugin-http": "^3.0.0-alpha1", + "react/promise": "~1.0|~2.0", + "phergie/phergie-irc-plugin-react-eventfilter": "^1.0||^2.0" }, "require-dev": { "phake/phake": "dev-VerifierResultConstraint-issue", diff --git a/src/Plugin.php b/src/Plugin.php index b624d94..4b11069 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -16,6 +16,7 @@ use Phergie\Irc\Bot\React\EventQueue; use Phergie\Irc\Client\React\LoopAwareInterface; use Phergie\Irc\Event\UserEvent; +use Phergie\Irc\Plugin\React\EventFilter\FilterInterface; use React\Promise\Deferred; use Phergie\Plugin\Http\Request; @@ -27,7 +28,8 @@ */ class Plugin extends AbstractPlugin implements LoopAwareInterface { - const URL_HANDLER_INTERFACE = 'Phergie\Irc\Plugin\React\Url\UrlHandlerInterface'; + const URL_HANDLER_INTERFACE = 'Phergie\Irc\Plugin\React\Url\UrlHandlerInterface'; + const EVENT_FILTER_INTERFACE = 'Phergie\Irc\Plugin\React\EventFilter\FilterInterface'; /** * @var UrlHandlerInterface @@ -42,6 +44,11 @@ class Plugin extends AbstractPlugin implements LoopAwareInterface */ protected $hostUrlEmitsOnly = false; + /** + * @var FilterInterface + */ + protected $filter = null; + /** * @var LoopInterface */ @@ -73,6 +80,12 @@ public function __construct(array $config = []) if (isset($config['hostUrlEmitsOnly'])) { $this->hostUrlEmitsOnly = boolval($config['hostUrlEmitsOnly']); } + if ( + isset($config['filter']) && + in_array(static::EVENT_FILTER_INTERFACE, class_implements($config['filter'])) + ) { + $this->filter = $config['filter']; + } } /** @@ -113,6 +126,12 @@ public function logDebug($message) public function handleIrcReceived(UserEvent $event, EventQueue $queue) { + if ( + $this->filter !== null && + $this->filter->filter($event) !== false + ) { + return; + } $params = $event->getParams(); $extractor = new \Twitter_Extractor($params['text']); $urls = $extractor->extractURLs(); diff --git a/tests/PluginTest.php b/tests/PluginTest.php index f4c4c21..1b1cb3c 100644 --- a/tests/PluginTest.php +++ b/tests/PluginTest.php @@ -12,6 +12,7 @@ use GuzzleHttp\Message\Response; use Phake; +use Phergie\Irc\Plugin\React\EventFilter\NotFilter; use Phergie\Irc\Plugin\React\Url\Plugin; use React\Promise\FulfilledPromise; @@ -351,4 +352,38 @@ public function testHandleUrlRequest() { )) ); } + + public function testFiltered() + { + $event = Phake::mock('Phergie\Irc\Event\UserEvent'); + Phake::when($event)->getParams()->thenReturn([ + 'text' => '', + ]); + $queue = Phake::mock('Phergie\Irc\Bot\React\EventQueue'); + $filter = Phake::mock('Phergie\Irc\Plugin\React\EventFilter\FilterInterface'); + Phake::when($filter)->filter($this->isInstanceOf('Phergie\Irc\Event\EventInterface'))->thenReturn(false); + + (new Plugin([ + 'filter' => new NotFilter($filter), + ]))->handleIrcReceived($event, $queue); + + Phake::verify($event, Phake::never())->getParams(); + } + + public function testNotFiltered() + { + $event = Phake::mock('Phergie\Irc\Event\UserEvent'); + Phake::when($event)->getParams()->thenReturn([ + 'text' => '', + ]); + $queue = Phake::mock('Phergie\Irc\Bot\React\EventQueue'); + $filter = Phake::mock('Phergie\Irc\Plugin\React\EventFilter\FilterInterface'); + Phake::when($filter)->filter($this->isInstanceOf('Phergie\Irc\Event\EventInterface'))->thenReturn(true); + + (new Plugin([ + 'filter' => new NotFilter($filter), + ]))->handleIrcReceived($event, $queue); + + Phake::verify($event)->getParams(); + } } From 0c30d2e8174cca2b6fcf9ec00809059aaff0a3da Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Thu, 21 Jan 2016 09:25:03 +0100 Subject: [PATCH 08/19] 5.4 => 5.5 ::class class name resolution --- src/Plugin.php | 7 ++----- tests/PluginTest.php | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 4b11069..ddb7d4a 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -28,9 +28,6 @@ */ class Plugin extends AbstractPlugin implements LoopAwareInterface { - const URL_HANDLER_INTERFACE = 'Phergie\Irc\Plugin\React\Url\UrlHandlerInterface'; - const EVENT_FILTER_INTERFACE = 'Phergie\Irc\Plugin\React\EventFilter\FilterInterface'; - /** * @var UrlHandlerInterface */ @@ -68,7 +65,7 @@ public function __construct(array $config = []) { if ( isset($config['handler']) && - in_array(static::URL_HANDLER_INTERFACE, class_implements($config['handler'])) + in_array(UrlHandlerInterface::class, class_implements($config['handler'])) ) { $this->handler = $config['handler']; } else { @@ -82,7 +79,7 @@ public function __construct(array $config = []) } if ( isset($config['filter']) && - in_array(static::EVENT_FILTER_INTERFACE, class_implements($config['filter'])) + in_array(FilterInterface::class, class_implements($config['filter'])) ) { $this->filter = $config['filter']; } diff --git a/tests/PluginTest.php b/tests/PluginTest.php index 1b1cb3c..d1ae931 100644 --- a/tests/PluginTest.php +++ b/tests/PluginTest.php @@ -12,6 +12,10 @@ use GuzzleHttp\Message\Response; use Phake; +use Phergie\Irc\Bot\React\EventQueue; +use Phergie\Irc\Event\EventInterface; +use Phergie\Irc\Event\UserEvent; +use Phergie\Irc\Plugin\React\EventFilter\FilterInterface; use Phergie\Irc\Plugin\React\EventFilter\NotFilter; use Phergie\Irc\Plugin\React\Url\Plugin; use React\Promise\FulfilledPromise; @@ -355,13 +359,13 @@ public function testHandleUrlRequest() { public function testFiltered() { - $event = Phake::mock('Phergie\Irc\Event\UserEvent'); + $event = Phake::mock(UserEvent::class); Phake::when($event)->getParams()->thenReturn([ 'text' => '', ]); - $queue = Phake::mock('Phergie\Irc\Bot\React\EventQueue'); - $filter = Phake::mock('Phergie\Irc\Plugin\React\EventFilter\FilterInterface'); - Phake::when($filter)->filter($this->isInstanceOf('Phergie\Irc\Event\EventInterface'))->thenReturn(false); + $queue = Phake::mock(EventQueue::class); + $filter = Phake::mock(FilterInterface::class); + Phake::when($filter)->filter($this->isInstanceOf(EventInterface::class))->thenReturn(false); (new Plugin([ 'filter' => new NotFilter($filter), @@ -372,13 +376,13 @@ public function testFiltered() public function testNotFiltered() { - $event = Phake::mock('Phergie\Irc\Event\UserEvent'); + $event = Phake::mock(UserEvent::class); Phake::when($event)->getParams()->thenReturn([ 'text' => '', ]); - $queue = Phake::mock('Phergie\Irc\Bot\React\EventQueue'); - $filter = Phake::mock('Phergie\Irc\Plugin\React\EventFilter\FilterInterface'); - Phake::when($filter)->filter($this->isInstanceOf('Phergie\Irc\Event\EventInterface'))->thenReturn(true); + $queue = Phake::mock(EventQueue::class); + $filter = Phake::mock(FilterInterface::class); + Phake::when($filter)->filter($this->isInstanceOf(EventInterface::class))->thenReturn(true); (new Plugin([ 'filter' => new NotFilter($filter), From 637af134215ac0bfc1b605085aa32dd1cc84aa13 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 23 Jan 2016 23:44:14 +0100 Subject: [PATCH 09/19] Event and filter for url's --- src/Filter/UrlEvent.php | 85 +++++++++++++++++++++++++++ src/Filter/UrlSectionFilter.php | 52 ++++++++++++++++ tests/Filter/UrlEventTest.php | 56 ++++++++++++++++++ tests/Filter/UrlSectionFilterTest.php | 44 ++++++++++++++ 4 files changed, 237 insertions(+) create mode 100644 src/Filter/UrlEvent.php create mode 100644 src/Filter/UrlSectionFilter.php create mode 100644 tests/Filter/UrlEventTest.php create mode 100644 tests/Filter/UrlSectionFilterTest.php diff --git a/src/Filter/UrlEvent.php b/src/Filter/UrlEvent.php new file mode 100644 index 0000000..a60470b --- /dev/null +++ b/src/Filter/UrlEvent.php @@ -0,0 +1,85 @@ +url = $url; + $this->parsedUrl = parse_url($url); + $this->event = $event; + } + + public function setMessage($message) + { + return $this->event->setMessage($message); + } + + public function getMessage() + { + return $this->event->getMessage(); + } + + public function setConnection(ConnectionInterface $connection) + { + return $this->event->setConnection($connection); + } + + public function getConnection() + { + return $this->event->getConnection(); + } + + public function setParams(array $params) + { + return $this->event->setParams($params); + } + + public function getParams() + { + return $this->event->getParams(); + } + + public function setCommand($command) + { + return $this->event->setCommand($command); + } + + public function getCommand() + { + return $this->event->getCommand(); + } + + public function getUrlSection($section) + { + if (isset($this->parsedUrl[$section])) { + return $this->parsedUrl[$section]; + } + + return false; + } +} diff --git a/src/Filter/UrlSectionFilter.php b/src/Filter/UrlSectionFilter.php new file mode 100644 index 0000000..dd19806 --- /dev/null +++ b/src/Filter/UrlSectionFilter.php @@ -0,0 +1,52 @@ +section = $section; + $this->value = $value; + } + + /** + * @param EventInterface $event + * @return bool|null + */ + public function filter(EventInterface $event) + { + if (!($event instanceof UrlEvent)) { + return null; + } + + $section = $event->getUrlSection($this->section); + if ($section === false) { + return null; + } + + $pattern = '/^' . str_replace('*', '.*', $this->value) . '$/'; + if (preg_match($pattern, $section)) { + return true; + } + + return false; + } +} diff --git a/tests/Filter/UrlEventTest.php b/tests/Filter/UrlEventTest.php new file mode 100644 index 0000000..5edc516 --- /dev/null +++ b/tests/Filter/UrlEventTest.php @@ -0,0 +1,56 @@ + 'http', + 'host' => 'phergie.org', + 'port' => false, + ] + ]; + + yield [ + 'https://phergie.org:80/', + [ + 'scheme' => 'https', + 'host' => 'phergie.org', + 'port' => 80, + ] + ]; + } + + /** + * @dataProvider provideGetUrlSection + */ + public function testGetUrlSection($url, array $iterations) + { + $event = Phake::mock(EventInterface::class); + $urlEvent = new UrlEvent($url, $event); + foreach ($iterations as $key => $value) { + $this->assertSame($value, $urlEvent->getUrlSection($key)); + } + } +} diff --git a/tests/Filter/UrlSectionFilterTest.php b/tests/Filter/UrlSectionFilterTest.php new file mode 100644 index 0000000..6180ff4 --- /dev/null +++ b/tests/Filter/UrlSectionFilterTest.php @@ -0,0 +1,44 @@ +assertSame($output, $filter->filter($urlEvent)); + } +} From 06a3cb99d923a9af48c72a0d962e5fcf09aa24ff Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 23 Jan 2016 23:51:36 +0100 Subject: [PATCH 10/19] Pluging now runs each URL through the filter --- src/Plugin.php | 13 +++++++------ tests/PluginTest.php | 14 +++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index ddb7d4a..57aedfb 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -11,6 +11,7 @@ namespace Phergie\Irc\Plugin\React\Url; use GuzzleHttp\Message\Response; +use Phergie\Irc\Plugin\React\Url\Filter\UrlEvent; use React\EventLoop\LoopInterface; use Phergie\Irc\Bot\React\AbstractPlugin; use Phergie\Irc\Bot\React\EventQueue; @@ -123,17 +124,17 @@ public function logDebug($message) public function handleIrcReceived(UserEvent $event, EventQueue $queue) { - if ( - $this->filter !== null && - $this->filter->filter($event) !== false - ) { - return; - } $params = $event->getParams(); $extractor = new \Twitter_Extractor($params['text']); $urls = $extractor->extractURLs(); foreach ($urls as $url) { + if ( + $this->filter !== null && + $this->filter->filter(new UrlEvent($url, $event)) !== false + ) { + continue; + } $this->handleUrl($url, $event, $queue); } } diff --git a/tests/PluginTest.php b/tests/PluginTest.php index d1ae931..f9d75cb 100644 --- a/tests/PluginTest.php +++ b/tests/PluginTest.php @@ -361,33 +361,33 @@ public function testFiltered() { $event = Phake::mock(UserEvent::class); Phake::when($event)->getParams()->thenReturn([ - 'text' => '', + 'text' => 'http://phergie.org', ]); $queue = Phake::mock(EventQueue::class); $filter = Phake::mock(FilterInterface::class); - Phake::when($filter)->filter($this->isInstanceOf(EventInterface::class))->thenReturn(false); + Phake::when($filter)->filter($this->isInstanceOf(EventInterface::class))->thenReturn(true); (new Plugin([ - 'filter' => new NotFilter($filter), + 'filter' => $filter, ]))->handleIrcReceived($event, $queue); - Phake::verify($event, Phake::never())->getParams(); + Phake::verify($filter, Phake::times(1))->filter($this->isInstanceOf(EventInterface::class)); } public function testNotFiltered() { $event = Phake::mock(UserEvent::class); Phake::when($event)->getParams()->thenReturn([ - 'text' => '', + 'text' => 'http://phergie.org http://wyrihaximus.net', ]); $queue = Phake::mock(EventQueue::class); $filter = Phake::mock(FilterInterface::class); Phake::when($filter)->filter($this->isInstanceOf(EventInterface::class))->thenReturn(true); (new Plugin([ - 'filter' => new NotFilter($filter), + 'filter' => $filter, ]))->handleIrcReceived($event, $queue); - Phake::verify($event)->getParams(); + Phake::verify($filter, Phake::times(2))->filter($this->isInstanceOf(EventInterface::class)); } } From feccdf5cc9afdfd61eea22dfa536d4a476db64ad Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 24 Jan 2016 00:03:15 +0100 Subject: [PATCH 11/19] Refactored filtered test --- tests/PluginTest.php | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/tests/PluginTest.php b/tests/PluginTest.php index f9d75cb..7ce3811 100644 --- a/tests/PluginTest.php +++ b/tests/PluginTest.php @@ -357,28 +357,32 @@ public function testHandleUrlRequest() { ); } - public function testFiltered() + public function provideFiltered() { - $event = Phake::mock(UserEvent::class); - Phake::when($event)->getParams()->thenReturn([ - 'text' => 'http://phergie.org', - ]); - $queue = Phake::mock(EventQueue::class); - $filter = Phake::mock(FilterInterface::class); - Phake::when($filter)->filter($this->isInstanceOf(EventInterface::class))->thenReturn(true); - - (new Plugin([ - 'filter' => $filter, - ]))->handleIrcReceived($event, $queue); - - Phake::verify($filter, Phake::times(1))->filter($this->isInstanceOf(EventInterface::class)); + yield [ + '', + 0, + ]; + + yield [ + 'http://phergie.org', + 1, + ]; + + yield [ + 'http://phergie.org http://wyrihaximus.net', + 2, + ]; } - public function testNotFiltered() + /** + * @dataProvider provideFiltered + */ + public function testFiltered($text, $times) { $event = Phake::mock(UserEvent::class); Phake::when($event)->getParams()->thenReturn([ - 'text' => 'http://phergie.org http://wyrihaximus.net', + 'text' => $text, ]); $queue = Phake::mock(EventQueue::class); $filter = Phake::mock(FilterInterface::class); @@ -388,6 +392,6 @@ public function testNotFiltered() 'filter' => $filter, ]))->handleIrcReceived($event, $queue); - Phake::verify($filter, Phake::times(2))->filter($this->isInstanceOf(EventInterface::class)); + Phake::verify($filter, Phake::times($times))->filter($this->isInstanceOf(EventInterface::class)); } } From aa1a8a5923b67eaaccee80449763838452d6079c Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 24 Jan 2016 00:07:07 +0100 Subject: [PATCH 12/19] Added readme section about the UrlSectionFilter --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index e00f164..08f8733 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,17 @@ Selection of response headers from: [en.wikipedia.org/wiki/List_of_HTTP_header_f * `%header-server%` * `%header-x-powered-by%` +## UrlSectionFilter + +This plugin comes with the `UrlSectionFilter` that lets you filter on the different key value pairs coming out of [`parse_url`](http://php.net/parse_url). The following example filter allows `www.phergie.org`, `www2.phergie.org`, and `phergie.org`: + +```php +new OrFilter([ + new UrlSectionFilter('host', '*.phergie.org'), + new UrlSectionFilter('host', 'phergie.org'), +]) +`` + ## Tests To run the unit test suite: From ef1897b02eb90d3c22b445fce03501326367004c Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 24 Jan 2016 00:30:10 +0100 Subject: [PATCH 13/19] Rebased from master and updated composer.* --- composer.json | 2 +- composer.lock | 78 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 62 insertions(+), 18 deletions(-) diff --git a/composer.json b/composer.json index 41eb992..304fe59 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "php": "^5.5|^7.0", "phergie/phergie-irc-bot-react": "~2", "nojimage/twitter-text-php": "1.1.1", - "phergie/phergie-irc-plugin-http": "^3.0.0-alpha1", + "phergie/phergie-irc-plugin-http": "^4.0", "react/promise": "~1.0|~2.0", "phergie/phergie-irc-plugin-react-eventfilter": "^1.0||^2.0" }, diff --git a/composer.lock b/composer.lock index c2880d7..8e88359 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": "7626892017d5178352ab99728c18e13b", - "content-hash": "3a219ee43e432ba1562f642029c78d1a", + "hash": "1fdefb02dd9ca0c2c1233628d0a8d779", + "content-hash": "ffcc99df122f057d76690c85cdbc92e7", "packages": [ { "name": "evenement/evenement", @@ -113,16 +113,16 @@ }, { "name": "guzzlehttp/psr7", - "version": "1.2.1", + "version": "1.2.2", "source": { "type": "git", "url": "https://github.com/guzzle/psr7.git", - "reference": "4d0bdbe1206df7440219ce14c972aa57cc5e4982" + "reference": "f5d04bdd2881ac89abde1fb78cc234bce24327bb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/psr7/zipball/4d0bdbe1206df7440219ce14c972aa57cc5e4982", - "reference": "4d0bdbe1206df7440219ce14c972aa57cc5e4982", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/f5d04bdd2881ac89abde1fb78cc234bce24327bb", + "reference": "f5d04bdd2881ac89abde1fb78cc234bce24327bb", "shasum": "" }, "require": { @@ -167,7 +167,7 @@ "stream", "uri" ], - "time": "2015-11-03 01:34:55" + "time": "2016-01-23 01:23:02" }, { "name": "guzzlehttp/ringphp", @@ -714,16 +714,16 @@ }, { "name": "phergie/phergie-irc-plugin-http", - "version": "4.0.0", + "version": "4.0.1", "source": { "type": "git", "url": "https://github.com/phergie/plugin-http.git", - "reference": "6f44190806e0817c0fd1dfb06bf221903dddf7cc" + "reference": "b6ad183e193f25ef09119e3fc65057f5be23cffd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phergie/plugin-http/zipball/6f44190806e0817c0fd1dfb06bf221903dddf7cc", - "reference": "6f44190806e0817c0fd1dfb06bf221903dddf7cc", + "url": "https://api.github.com/repos/phergie/plugin-http/zipball/b6ad183e193f25ef09119e3fc65057f5be23cffd", + "reference": "b6ad183e193f25ef09119e3fc65057f5be23cffd", "shasum": "" }, "require": { @@ -760,7 +760,51 @@ "plugin", "react" ], - "time": "2015-12-29 12:41:31" + "time": "2016-01-13 14:24:19" + }, + { + "name": "phergie/phergie-irc-plugin-react-eventfilter", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/phergie/phergie-irc-plugin-react-eventfilter.git", + "reference": "d5c4024034f466e510572927cac8956c10c1cac5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phergie/phergie-irc-plugin-react-eventfilter/zipball/d5c4024034f466e510572927cac8956c10c1cac5", + "reference": "d5c4024034f466e510572927cac8956c10c1cac5", + "shasum": "" + }, + "require": { + "phergie/phergie-irc-bot-react": "~2" + }, + "require-dev": { + "phake/phake": "2.0.0-beta2", + "phergie/phergie-irc-plugin-react-usermode": "~2", + "phpunit/phpunit": "4.1.*" + }, + "suggest": { + "phergie/phergie-irc-plugin-react-usermode": "supports filtering of events by channel modes of originating users" + }, + "type": "library", + "autoload": { + "psr-4": { + "Phergie\\Irc\\Plugin\\React\\EventFilter\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause" + ], + "description": "Phergie plugin for limiting processing of incoming events based on event metadata", + "keywords": [ + "bot", + "irc", + "plugin", + "react" + ], + "time": "2015-12-22 03:29:44" }, { "name": "psr/http-message", @@ -2660,16 +2704,16 @@ }, { "name": "symfony/event-dispatcher", - "version": "v2.8.1", + "version": "v2.8.2", "source": { "type": "git", "url": "https://github.com/symfony/event-dispatcher.git", - "reference": "a5eb815363c0388e83247e7e9853e5dbc14999cc" + "reference": "ee278f7c851533e58ca307f66305ccb9188aceda" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/a5eb815363c0388e83247e7e9853e5dbc14999cc", - "reference": "a5eb815363c0388e83247e7e9853e5dbc14999cc", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/ee278f7c851533e58ca307f66305ccb9188aceda", + "reference": "ee278f7c851533e58ca307f66305ccb9188aceda", "shasum": "" }, "require": { @@ -2716,7 +2760,7 @@ ], "description": "Symfony EventDispatcher Component", "homepage": "https://symfony.com", - "time": "2015-10-30 20:15:42" + "time": "2016-01-13 10:28:07" }, { "name": "symfony/filesystem", From 5880cc52c40955bd6ceefff04af3a6195682feca Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 24 Jan 2016 00:31:25 +0100 Subject: [PATCH 14/19] Ditched 5.4 and replaced it by '7' in the dunit config --- .dunitconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.dunitconfig b/.dunitconfig index a209a65..ccdaa46 100644 --- a/.dunitconfig +++ b/.dunitconfig @@ -1,8 +1,8 @@ // the list of docker images to run against images=" - vectorface/php5.4 vectorface/php5.5 vectorface/php5.6 + vectorface/php-nightly vectorface/hhvm"; From 8e3c51d70d7ae63ae94a817db21eb1370183ccae Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 24 Jan 2016 15:25:55 +0100 Subject: [PATCH 15/19] Missing @param for section --- src/Filter/UrlSectionFilter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Filter/UrlSectionFilter.php b/src/Filter/UrlSectionFilter.php index dd19806..4af9a4c 100644 --- a/src/Filter/UrlSectionFilter.php +++ b/src/Filter/UrlSectionFilter.php @@ -19,6 +19,7 @@ class UrlSectionFilter implements FilterInterface /** * + * @param string $section * @param string $value */ public function __construct($section, $value) From d2109d243ad37bff902d418aac201215d0415611 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 24 Jan 2016 17:13:49 +0100 Subject: [PATCH 16/19] Ensure nonstrict mode --- tests/Filter/UrlSectionFilterTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/Filter/UrlSectionFilterTest.php b/tests/Filter/UrlSectionFilterTest.php index 6180ff4..a1fe63a 100644 --- a/tests/Filter/UrlSectionFilterTest.php +++ b/tests/Filter/UrlSectionFilterTest.php @@ -29,6 +29,13 @@ public function provideFilter() 'http://phergie.org/', true, ]; + + yield [ + 'port', + 80, + 'http://phergie.org/', + null, + ]; } /** From 862ed452e5924e5f8ac1dd141fd85d2bed02ad2f Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 24 Jan 2016 17:17:50 +0100 Subject: [PATCH 17/19] Missing ` --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 08f8733..7aaec80 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ new OrFilter([ new UrlSectionFilter('host', '*.phergie.org'), new UrlSectionFilter('host', 'phergie.org'), ]) -`` +``` ## Tests From 2aed4c3fc4d311c4cf87e655779d323eb6f4e4b0 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 24 Jan 2016 17:20:10 +0100 Subject: [PATCH 18/19] Added strict mode to the filter --- README.md | 2 ++ src/Filter/UrlSectionFilter.php | 12 ++++++-- tests/Filter/UrlSectionFilterTest.php | 42 +++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7aaec80..5e91d4b 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,8 @@ new OrFilter([ ]) ``` +The filter comes with a third `strict` parameter where instead of declaring out of scope on missing an URL part it return `false`. + ## Tests To run the unit test suite: diff --git a/src/Filter/UrlSectionFilter.php b/src/Filter/UrlSectionFilter.php index 4af9a4c..0ca7883 100644 --- a/src/Filter/UrlSectionFilter.php +++ b/src/Filter/UrlSectionFilter.php @@ -17,15 +17,23 @@ class UrlSectionFilter implements FilterInterface */ protected $section; + /** + * @var null|false + */ + protected $strictResponse = null; + /** * * @param string $section * @param string $value */ - public function __construct($section, $value) + public function __construct($section, $value, $strict = false) { $this->section = $section; $this->value = $value; + if ($strict === true) { + $this->strictResponse = false; + } } /** @@ -40,7 +48,7 @@ public function filter(EventInterface $event) $section = $event->getUrlSection($this->section); if ($section === false) { - return null; + return $this->strictResponse; } $pattern = '/^' . str_replace('*', '.*', $this->value) . '$/'; diff --git a/tests/Filter/UrlSectionFilterTest.php b/tests/Filter/UrlSectionFilterTest.php index a1fe63a..64082db 100644 --- a/tests/Filter/UrlSectionFilterTest.php +++ b/tests/Filter/UrlSectionFilterTest.php @@ -48,4 +48,46 @@ public function testFilter($section, $value, $url, $output) $filter = new UrlSectionFilter($section, $value); $this->assertSame($output, $filter->filter($urlEvent)); } + + public function provideStrictFilter() + { + yield [ + 'host', + 'phergie.org', + 'http://phergie.org/', + true, + ]; + + yield [ + 'host', + 'phergie.org', + 'http://www.phergie.org/', + false, + ]; + + yield [ + 'host', + '*.org', + 'http://phergie.org/', + true, + ]; + + yield [ + 'port', + 80, + 'http://phergie.org/', + false, + ]; + } + + /** + * @dataProvider provideStrictFilter + */ + public function testStrictFilter($section, $value, $url, $output) + { + $event = Phake::mock(EventInterface::class); + $urlEvent = new UrlEvent($url, $event); + $filter = new UrlSectionFilter($section, $value, true); + $this->assertSame($output, $filter->filter($urlEvent)); + } } From 30ee474b323c66396c9cc71bbf90e5103d02e810 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 24 Jan 2016 17:49:35 +0100 Subject: [PATCH 19/19] Swapped false with null as it makes more sense semantically --- src/Filter/UrlEvent.php | 2 +- src/Filter/UrlSectionFilter.php | 2 +- tests/Filter/UrlEventTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Filter/UrlEvent.php b/src/Filter/UrlEvent.php index a60470b..9a101d9 100644 --- a/src/Filter/UrlEvent.php +++ b/src/Filter/UrlEvent.php @@ -80,6 +80,6 @@ public function getUrlSection($section) return $this->parsedUrl[$section]; } - return false; + return null; } } diff --git a/src/Filter/UrlSectionFilter.php b/src/Filter/UrlSectionFilter.php index 0ca7883..a14aca8 100644 --- a/src/Filter/UrlSectionFilter.php +++ b/src/Filter/UrlSectionFilter.php @@ -47,7 +47,7 @@ public function filter(EventInterface $event) } $section = $event->getUrlSection($this->section); - if ($section === false) { + if ($section === null) { return $this->strictResponse; } diff --git a/tests/Filter/UrlEventTest.php b/tests/Filter/UrlEventTest.php index 5edc516..af518f3 100644 --- a/tests/Filter/UrlEventTest.php +++ b/tests/Filter/UrlEventTest.php @@ -28,7 +28,7 @@ public function provideGetUrlSection() [ 'scheme' => 'http', 'host' => 'phergie.org', - 'port' => false, + 'port' => null, ] ];