Skip to content

Commit

Permalink
Merge pull request #1 from nicolasmure/lib-wrap
Browse files Browse the repository at this point in the history
Lib wrap
  • Loading branch information
Nicolas MURE authored Nov 23, 2016
2 parents 5e81fd8 + a3657c2 commit 07aa099
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/
composer.lock
vendor/
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: php
php:
- '5.3'
- '7.0'

env:
- SYMFONY_VERSION="2.3.*"
- SYMFONY_VERSION="3.0.*"

before_install:
- mkdir -p build/logs
- composer self-update

install:
- composer require satooshi/php-coveralls '~1.0'

after_success:
- php vendor/bin/coveralls
9 changes: 9 additions & 0 deletions CrawlerDetectBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Nmure\CrawlerDetectBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class CrawlerDetectBundle extends Bundle
{
}
20 changes: 20 additions & 0 deletions DependencyInjection/CrawlerDetectExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Nmure\CrawlerDetectBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class CrawlerDetectExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
}
}
81 changes: 80 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,81 @@
# CrawlerDetectBundle
A Symfony bundle for the Crawler-Detect library (detects bots/crawlers/spiders via the user agent)

[![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)
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.
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")
class as a service (`crawler_detect`) to make it easier to use with Symfony
(dependency injection, usable from a controller, etc...).

## 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:

```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:

```php
// app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Nmure\CrawlerDetectBundle\CrawlerDetectBundle(),
// ...
);
}
}
```

## Usage

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

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

## Testing

```bash
$ docker run --rm -v `pwd`:/app phpunit/phpunit -c /app
```
15 changes: 15 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
</parameters>

<services>
<service id="crawler_detect" class="%crawler_detect.class%">
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Nmure\CrawlerDetectBundle\Tests\Functional\DependencyInjection;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class CrawlerDetectExtensionTest extends KernelTestCase
{
private $container;

protected function setUp()
{
self::bootKernel();
$this->container = static::$kernel->getContainer();
}

protected function tearDown()
{
unset($this->container);
}

public function testServiceIsDefined()
{
$this->assertInstanceOf('Jaybizzle\\CrawlerDetect\\CrawlerDetect', $this->container->get('crawler_detect'));
}
}
26 changes: 26 additions & 0 deletions Tests/Functional/Fixtures/app/AppKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
/**
* {@inheritdoc}
*/
public function registerBundles()
{
return array(
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \Nmure\CrawlerDetectBundle\CrawlerDetectBundle(),
);
}

/**
* {@inheritdoc}
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config.yml');
}
}
4 changes: 4 additions & 0 deletions Tests/Functional/Fixtures/app/config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
framework:
secret: crawler_detect
session:
storage_id: session.storage.mock_file
7 changes: 7 additions & 0 deletions Tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

if (!is_file($autoloadFile = __DIR__ . '/../vendor/autoload.php')) {
throw new \LogicException('Could not find autoload.php in vendor/. Try to run "composer install"');
}

require $autoloadFile;
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "nmure/crawler-detect-bundle",
"type": "symfony-bundle",
"description": "A Symfony bundle for the Crawler-Detect library (detects bots/crawlers/spiders via the user agent)",
"keywords": ["crawler", "bot", "detect", "user-agent", "symfony", "bundle"],
"license": "MIT",
"authors": [
{
"name": "Nicolas MURE",
"email": "[email protected]",
"homepage": "https://github.com/nicolasmure"
}
],
"require": {
"php": ">=5.3.0",
"symfony/framework-bundle": "~2.3|~3.0",
"jaybizzle/crawler-detect": "1.*"
},
"autoload": {
"psr-4": { "Nmure\\CrawlerDetectBundle\\": "" }
}
}
30 changes: 30 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./Tests/bootstrap.php" colors="true">

<logging>
<log type="coverage-clover" target="build/logs/clover.xml" />
</logging>

<testsuites>
<testsuite name="functional">
<directory suffix="Test.php">./Tests/Functional</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./build</directory>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>

<php>
<server name="KERNEL_DIR" value="./Tests/Functional/Fixtures/app" />
</php>
</phpunit>

0 comments on commit 07aa099

Please sign in to comment.