Skip to content

Commit

Permalink
Adds bot-parsers as injectionable objects (#5712)
Browse files Browse the repository at this point in the history
* Adds bot-parsers as injectionable objects

Handles the botparser equivalent to the client/device parsers. This way
users of the library can add their own botparser that might not be
usefull for the library, but are needed in their own situation.

As there does not seem to be any usecase for adding multple bot-parsers
in this project, there is no separte directory with bot-parsers, nor can
we add a botparser by string.

* Adds botparserabstract to implement discarDetails

Re-adds the discardDetails() that accidently got removed in the refactor.
To be able to do this on all added bots, an abstract is created. It could
have been an interface, but all other (client, device) do this with an
abstract, so that pattern is followed

Adds a test that catches the problem in previous commit, and passes now
  • Loading branch information
NanneHuiges authored and sgiehl committed Mar 5, 2018
1 parent bade470 commit 25e11d1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
44 changes: 36 additions & 8 deletions DeviceDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -151,6 +152,11 @@ class DeviceDetector
*/
protected $deviceParsers = array();

/**
* @var BotParserAbstract[]
*/
public $botParsers = array();

/**
* @var bool
*/
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -539,7 +560,7 @@ public function getBot()

/**
* Returns true, if userAgent was already parsed with parse()
*
*
* @return bool
*/
public function isParsed()
Expand Down Expand Up @@ -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();
}


Expand Down
2 changes: 1 addition & 1 deletion Parser/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @package DeviceDetector\Parser
*/
class Bot extends ParserAbstract
class Bot extends BotParserAbstract
{
protected $fixtureFile = 'regexes/bots.yml';
protected $parserName = 'bot';
Expand Down
23 changes: 23 additions & 0 deletions Parser/BotParserAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Device Detector - The Universal Device Detection library for parsing User Agents
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
*/
namespace DeviceDetector\Parser;

/**
* Class BotParserAbstract
*
* Abstract class for all bot parsers
*
* @package DeviceDetector\Parser
*/
abstract class BotParserAbstract extends ParserAbstract
{
/**
* Enables information discarding
*/
abstract public function discardDetails();
}
10 changes: 10 additions & 0 deletions Tests/DeviceDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ public function testGetInfoFromUABot()
$this->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';
Expand Down

0 comments on commit 25e11d1

Please sign in to comment.