diff --git a/DeviceDetector.php b/DeviceDetector.php index cc28433cfa..b4808deb90 100644 --- a/DeviceDetector.php +++ b/DeviceDetector.php @@ -11,6 +11,7 @@ use DeviceDetector\Cache\StaticCache; use DeviceDetector\Cache\Cache; use DeviceDetector\Parser\Bot; +use DeviceDetector\Parser\BotParserAbstract; use DeviceDetector\Parser\Client\Browser; use DeviceDetector\Parser\OperatingSystem; use DeviceDetector\Parser\Client\ClientParserAbstract; @@ -151,6 +152,11 @@ class DeviceDetector */ protected $deviceParsers = array(); + /** + * @var BotParserAbstract[] + */ + public $botParsers = array(); + /** * @var bool */ @@ -180,6 +186,8 @@ public function __construct($userAgent = '') $this->addDeviceParser('Camera'); $this->addDeviceParser('PortableMediaPlayer'); $this->addDeviceParser('Mobile'); + + $this->addBotParser(new Bot()); } public function __call($methodName, $arguments) @@ -272,6 +280,19 @@ public function getDeviceParsers() return $this->deviceParsers; } + /** + * @param BotParserAbstract $parser + */ + public function addBotParser(BotParserAbstract $parser) + { + $this->botParsers[] = $parser; + } + + public function getBotParsers() + { + return $this->botParsers; + } + /** * Sets whether to discard additional bot information * If information is discarded it's only possible check whether UA was detected as bot or not. @@ -539,7 +560,7 @@ public function getBot() /** * Returns true, if userAgent was already parsed with parse() - * + * * @return bool */ public function isParsed() @@ -590,14 +611,21 @@ protected function parseBot() return false; } - $botParser = new Bot(); - $botParser->setUserAgent($this->getUserAgent()); - $botParser->setYamlParser($this->getYamlParser()); - $botParser->setCache($this->getCache()); - if ($this->discardBotInformation) { - $botParser->discardDetails(); + $parsers = $this->getBotParsers(); + + foreach ($parsers as $parser) { + $parser->setUserAgent($this->getUserAgent()); + $parser->setYamlParser($this->getYamlParser()); + $parser->setCache($this->getCache()); + if ($this->discardBotInformation) { + $parser->discardDetails(); + } + $bot = $parser->parse(); + if (!empty($bot)) { + $this->bot = $bot; + break; + } } - $this->bot = $botParser->parse(); } diff --git a/Parser/Bot.php b/Parser/Bot.php index 03f2e0fcb4..cfa845ea54 100644 --- a/Parser/Bot.php +++ b/Parser/Bot.php @@ -16,7 +16,7 @@ * * @package DeviceDetector\Parser */ -class Bot extends ParserAbstract +class Bot extends BotParserAbstract { protected $fixtureFile = 'regexes/bots.yml'; protected $parserName = 'bot'; diff --git a/Parser/BotParserAbstract.php b/Parser/BotParserAbstract.php new file mode 100644 index 0000000000..c5dacc1893 --- /dev/null +++ b/Parser/BotParserAbstract.php @@ -0,0 +1,23 @@ +assertEquals($expected, DeviceDetector::getInfoFromUserAgent($expected['user_agent'])); } + + public function testParseNoDetails() + { + $user_agent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)'; + $dd = new DeviceDetector($user_agent); + $dd->discardBotInformation(); + $dd->parse(); + $this->assertTrue($dd->getBot()); + } + public function testMagicMMethods() { $ua = 'Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.136 Mobile Safari/537.36';