Skip to content

Commit 1fb92fc

Browse files
author
Nicolas MURE
committed
BaseCrawlerDetect initialization
1 parent 07aa099 commit 1fb92fc

File tree

5 files changed

+127
-20
lines changed

5 files changed

+127
-20
lines changed

CrawlerDetect/CrawlerDetect.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Nmure\CrawlerDetectBundle\CrawlerDetect;
4+
5+
use Jaybizzle\CrawlerDetect\CrawlerDetect as BaseCrawlerDetect;
6+
use Symfony\Component\HttpFoundation\RequestStack;
7+
8+
/**
9+
* Class extending the BaseCrawlerDetect to adapt it for Symfony.
10+
*/
11+
class CrawlerDetect extends BaseCrawlerDetect
12+
{
13+
/**
14+
* Initialise the BaseCrawlerDetect using the master request
15+
* from the $requestStack.
16+
*
17+
* @param RequestStack $requestStack
18+
*/
19+
public function __construct(RequestStack $requestStack)
20+
{
21+
if ($request = $requestStack->getMasterRequest()) {
22+
// the app is accessed by a HTTP request
23+
$headers = $request->server->all();
24+
$userAgent = $request->headers->get('User-Agent');
25+
} else {
26+
// the app is accessed by the CLI
27+
$headers = $userAgent = null;
28+
}
29+
30+
parent::__construct($headers, $userAgent);
31+
}
32+
}

README.md

+20-19
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33
[![Build Status](https://travis-ci.org/nicolasmure/CrawlerDetectBundle.svg?branch=master)](https://travis-ci.org/nicolasmure/CrawlerDetectBundle)
44
[![Coverage Status](https://coveralls.io/repos/github/nicolasmure/CrawlerDetectBundle/badge.svg?branch=master)](https://coveralls.io/github/nicolasmure/CrawlerDetectBundle?branch=master)
55

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

99
## Table of contents
1010

1111
- [Introduction](#introduction)
1212
- [Installation](#installation)
13-
- [Step 1 : Download the Bundle](#step-1--download-the-bundle)
14-
- [Step 2 : Enable the Bundle](#step-2--enable-the-bundle)
1513
- [Usage](#usage)
1614
- [Testing](#testing)
1715

1816
## Introduction
1917

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

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

2726
## Installation
2827

29-
### Step 1 : Download the Bundle
30-
31-
Open a command console, enter your project directory and execute the
32-
following command to download the latest stable version of this bundle:
28+
Download the bundle using composer :
3329

3430
```bash
3531
$ composer require nmure/crawler-detect-bundle "dev-master"
3632
```
3733

38-
This command requires you to have Composer installed globally, as explained
39-
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
40-
of the Composer documentation.
41-
42-
### Step 2 : Enable the Bundle
43-
44-
Then, enable the bundle by adding it to the list of registered bundles
45-
in the `app/AppKernel.php` file of your project:
34+
then enable the bundle in your AppKernel :
4635

4736
```php
4837
// app/AppKernel.php
@@ -61,17 +50,29 @@ class AppKernel extends Kernel
6150

6251
## Usage
6352

64-
From a controller :
53+
The `crawler_detect` service is initialized with the data from
54+
the Symfony's master request.
55+
56+
To use this service from a controller :
57+
6558
```php
6659
public function indexAction()
6760
{
6861
if ($this->get('crawler_detect')->isCrawler()) {
69-
// crawler user-agent detected :)
62+
// this request is from a crawler :)
63+
}
64+
65+
// you can also specify an user agent if you don't want
66+
// to use the one of the master request or if the app
67+
// is accessed by the CLI :
68+
$ua = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
69+
if ($this->get('crawler_detect')->isCrawler($ua)) {
70+
// this user agent belongs to a crawler :)
7071
}
7172
}
7273
```
7374

74-
or you can also inject this service as a dependency
75+
You can also inject this service as a dependency
7576
using the `crawler_detect` service id.
7677

7778
## Testing

Resources/config/services.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<parameters>
8-
<parameter key="crawler_detect.class">Jaybizzle\CrawlerDetect\CrawlerDetect</parameter>
8+
<parameter key="crawler_detect.class">Nmure\CrawlerDetectBundle\CrawlerDetect\CrawlerDetect</parameter>
99
</parameters>
1010

1111
<services>
1212
<service id="crawler_detect" class="%crawler_detect.class%">
13+
<argument type="service" id="request_stack" />
1314
</service>
1415
</services>
1516
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Nmure\CrawlerDetectBundle\Tests\Unit\CrawlerDetect;
4+
5+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
6+
use Nmure\CrawlerDetectBundle\CrawlerDetect\CrawlerDetect;
7+
use Symfony\Component\HttpFoundation\Request;
8+
9+
class CrawlerDetectTest extends TestCase
10+
{
11+
private $browserUA = 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0';
12+
private $crawlerUA = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
13+
14+
public function testRequestNotFromACrawler()
15+
{
16+
$rsMock = $this->getRequestStackMockWithMasterRequest($this->browserUA);
17+
18+
$crawlerDetect = new CrawlerDetect($rsMock);
19+
$this->assertFalse($crawlerDetect->isCrawler());
20+
21+
// overriding the determined UA
22+
$this->assertTrue($crawlerDetect->isCrawler($this->crawlerUA));
23+
}
24+
25+
public function testRequestFromACrawler()
26+
{
27+
$rsMock = $this->getRequestStackMockWithMasterRequest($this->crawlerUA);
28+
29+
$crawlerDetect = new CrawlerDetect($rsMock);
30+
$this->assertTrue($crawlerDetect->isCrawler());
31+
32+
// overriding the determined UA
33+
$this->assertFalse($crawlerDetect->isCrawler($this->browserUA));
34+
}
35+
36+
public function testNoRequest()
37+
{
38+
$rsMock = $this->getRequestStackMock();
39+
$rsMock->expects($this->once())
40+
->method('getMasterRequest')
41+
->willReturn(null);
42+
43+
$crawlerDetect = new CrawlerDetect($rsMock);
44+
// when the app is accessed from the CLI
45+
$this->assertFalse($crawlerDetect->isCrawler());
46+
47+
// specifying the UA
48+
$this->assertFalse($crawlerDetect->isCrawler($this->browserUA));
49+
$this->assertTrue($crawlerDetect->isCrawler($this->crawlerUA));
50+
}
51+
52+
private function getRequestStackMockWithMasterRequest($userAgent)
53+
{
54+
$rsMock = $this->getRequestStackMock();
55+
$rsMock->expects($this->once())
56+
->method('getMasterRequest')
57+
->willReturn(new Request(array(), array(), array(), array(), array(), array(
58+
'HTTP_USER_AGENT' => $userAgent,
59+
)));
60+
61+
return $rsMock;
62+
}
63+
64+
private function getRequestStackMock()
65+
{
66+
return $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')
67+
->disableOriginalConstructor()
68+
->getMock();
69+
}
70+
}

phpunit.xml.dist

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<testsuite name="functional">
1111
<directory suffix="Test.php">./Tests/Functional</directory>
1212
</testsuite>
13+
<testsuite name="unit">
14+
<directory suffix="Test.php">./Tests/Unit</directory>
15+
</testsuite>
1316
</testsuites>
1417

1518
<filter>

0 commit comments

Comments
 (0)