Skip to content

Commit

Permalink
BaseCrawlerDetect initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas MURE committed Nov 24, 2016
1 parent 07aa099 commit 1fb92fc
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 20 deletions.
32 changes: 32 additions & 0 deletions CrawlerDetect/CrawlerDetect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Nmure\CrawlerDetectBundle\CrawlerDetect;

use Jaybizzle\CrawlerDetect\CrawlerDetect as BaseCrawlerDetect;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Class extending the BaseCrawlerDetect to adapt it for Symfony.
*/
class CrawlerDetect extends BaseCrawlerDetect
{
/**
* Initialise the BaseCrawlerDetect using the master request
* from the $requestStack.
*
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack)
{
if ($request = $requestStack->getMasterRequest()) {
// the app is accessed by a HTTP request
$headers = $request->server->all();
$userAgent = $request->headers->get('User-Agent');
} else {
// the app is accessed by the CLI
$headers = $userAgent = null;
}

parent::__construct($headers, $userAgent);
}
}
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
[![Build Status](https://travis-ci.org/nicolasmure/CrawlerDetectBundle.svg?branch=master)](https://travis-ci.org/nicolasmure/CrawlerDetectBundle)
[![Coverage Status](https://coveralls.io/repos/github/nicolasmure/CrawlerDetectBundle/badge.svg?branch=master)](https://coveralls.io/github/nicolasmure/CrawlerDetectBundle?branch=master)

A Symfony bundle for the [Crawler-Detect](https://github.com/JayBizzle/Crawler-Detect)
A Symfony bundle for the [Crawler-Detect](https://github.com/JayBizzle/Crawler-Detect "JayBizzle/Crawler-Detect")
library (detects bots/crawlers/spiders via the user agent).

## Table of contents

- [Introduction](#introduction)
- [Installation](#installation)
- [Step 1 : Download the Bundle](#step-1--download-the-bundle)
- [Step 2 : Enable the Bundle](#step-2--enable-the-bundle)
- [Usage](#usage)
- [Testing](#testing)

## Introduction

This Bundle integrates the [Crawler-Detect](https://github.com/JayBizzle/Crawler-Detect) library into Symfony.
This Bundle integrates the [Crawler-Detect](https://github.com/JayBizzle/Crawler-Detect "JayBizzle/Crawler-Detect")
library into Symfony.
It is **recommended** to read the lib's documentation before continuing here.

The aim of this bundle is to expose the [`CrawlerDetect`](https://github.com/JayBizzle/Crawler-Detect/blob/master/src/CrawlerDetect.php "Jaybizzle\CrawlerDetect\CrawlerDetect")
Expand All @@ -26,23 +25,13 @@ class as a service (`crawler_detect`) to make it easier to use with Symfony

## Installation

### Step 1 : Download the Bundle

Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:
Download the bundle using composer :

```bash
$ composer require nmure/crawler-detect-bundle "dev-master"
```

This command requires you to have Composer installed globally, as explained
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
of the Composer documentation.

### Step 2 : Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles
in the `app/AppKernel.php` file of your project:
then enable the bundle in your AppKernel :

```php
// app/AppKernel.php
Expand All @@ -61,17 +50,29 @@ class AppKernel extends Kernel

## Usage

From a controller :
The `crawler_detect` service is initialized with the data from
the Symfony's master request.

To use this service from a controller :

```php
public function indexAction()
{
if ($this->get('crawler_detect')->isCrawler()) {
// crawler user-agent detected :)
// this request is from a crawler :)
}

// you can also specify an user agent if you don't want
// to use the one of the master request or if the app
// is accessed by the CLI :
$ua = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
if ($this->get('crawler_detect')->isCrawler($ua)) {
// this user agent belongs to a crawler :)
}
}
```

or you can also inject this service as a dependency
You can also inject this service as a dependency
using the `crawler_detect` service id.

## Testing
Expand Down
3 changes: 2 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="crawler_detect.class">Jaybizzle\CrawlerDetect\CrawlerDetect</parameter>
<parameter key="crawler_detect.class">Nmure\CrawlerDetectBundle\CrawlerDetect\CrawlerDetect</parameter>
</parameters>

<services>
<service id="crawler_detect" class="%crawler_detect.class%">
<argument type="service" id="request_stack" />
</service>
</services>
</container>
70 changes: 70 additions & 0 deletions Tests/Unit/CrawlerDetect/CrawlerDetectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Nmure\CrawlerDetectBundle\Tests\Unit\CrawlerDetect;

use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Nmure\CrawlerDetectBundle\CrawlerDetect\CrawlerDetect;
use Symfony\Component\HttpFoundation\Request;

class CrawlerDetectTest extends TestCase
{
private $browserUA = 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0';
private $crawlerUA = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';

public function testRequestNotFromACrawler()
{
$rsMock = $this->getRequestStackMockWithMasterRequest($this->browserUA);

$crawlerDetect = new CrawlerDetect($rsMock);
$this->assertFalse($crawlerDetect->isCrawler());

// overriding the determined UA
$this->assertTrue($crawlerDetect->isCrawler($this->crawlerUA));
}

public function testRequestFromACrawler()
{
$rsMock = $this->getRequestStackMockWithMasterRequest($this->crawlerUA);

$crawlerDetect = new CrawlerDetect($rsMock);
$this->assertTrue($crawlerDetect->isCrawler());

// overriding the determined UA
$this->assertFalse($crawlerDetect->isCrawler($this->browserUA));
}

public function testNoRequest()
{
$rsMock = $this->getRequestStackMock();
$rsMock->expects($this->once())
->method('getMasterRequest')
->willReturn(null);

$crawlerDetect = new CrawlerDetect($rsMock);
// when the app is accessed from the CLI
$this->assertFalse($crawlerDetect->isCrawler());

// specifying the UA
$this->assertFalse($crawlerDetect->isCrawler($this->browserUA));
$this->assertTrue($crawlerDetect->isCrawler($this->crawlerUA));
}

private function getRequestStackMockWithMasterRequest($userAgent)
{
$rsMock = $this->getRequestStackMock();
$rsMock->expects($this->once())
->method('getMasterRequest')
->willReturn(new Request(array(), array(), array(), array(), array(), array(
'HTTP_USER_AGENT' => $userAgent,
)));

return $rsMock;
}

private function getRequestStackMock()
{
return $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')
->disableOriginalConstructor()
->getMock();
}
}
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<testsuite name="functional">
<directory suffix="Test.php">./Tests/Functional</directory>
</testsuite>
<testsuite name="unit">
<directory suffix="Test.php">./Tests/Unit</directory>
</testsuite>
</testsuites>

<filter>
Expand Down

0 comments on commit 1fb92fc

Please sign in to comment.