Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cdaguerre committed Jun 1, 2016
0 parents commit fd08c1b
Show file tree
Hide file tree
Showing 29 changed files with 1,229 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cache:
mount:
- .git
- /drone/composer-cache

build:
image: worldia/php:cli
environment:
COMPOSER_CACHE_DIR: /drone/composer-cache
commands:
- composer install --prefer-dist --no-interaction --no-progress --ansi
- bin/phpspec run

notify:
slack_blame:
token: xoxp-2941314706-2941314708-20197447201-1ced90fcf4
success:
username: "Bob"
icon: ":bob:"
message: "The build is fixed!"
failure:
username: "Bob"
icon: ":Bob:"
message: "The build is broken!"
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor/
bin/

composer.lock
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## RobotsBundle

Add contextual robot meta headers.

### Installation

```sh
$ composer require cdaguerre/robots-bundle
```

### Configuration

```yml
dag_robots:
rules:
- { route: 'homepage', tags: ['noindex', 'nofollow'], hosts: ['www.example.com'] }
- { route: 'product_show', tags: ['noindex', 'nofollow'], hosts: ['www.example1.com', 'www.example2.com'] }
- { route: 'category_show', tags: ['noindex', 'nofollow'] }
```
### TODO
- [ ] Add route to serve robots.txt files dynamically.
- [ ] Add tests
56 changes: 56 additions & 0 deletions Robots/Checker/RequestChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Dag\Component\Robots\Checker;

use Symfony\Component\HttpFoundation\Request;

/**
* @author Christian Daguerre <[email protected]>
*/
class RequestChecker implements RequestCheckerInterface
{
/**
* @var array
*/
protected $crawlerUserAgents = array(
'googlebot',
'bingbot',
'msnbot',
'twitterbot',
'yahoo',
'baiduspider',
'facebookexternalhit',
'askjeeves',
'teomabar',
);

/**
* @param array $crawlerUserAgents Additionnal user agents.
*/
public function __construct(array $crawlerUserAgents = array())
{
$this->crawlerUserAgents = array_flip(array_merge($this->crawlerUserAgents, $crawlerUserAgents));
}

/**
* {@inheritdoc}
*/
public function isRobot(Request $request)
{
$userAgent = strtolower($request->headers->get('User-Agent'));

return isset($this->crawlerUserAgents[$userAgent]);
}

/**
* {@inheritdoc}
*/
public function isCrawler(Request $request)
{
if (null !== $request->query->get('_escaped_fragment_')) {
return true;
}

return $this->isRobot($request);
}
}
29 changes: 29 additions & 0 deletions Robots/Checker/RequestCheckerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Dag\Component\Robots\Checker;

use Symfony\Component\HttpFoundation\Request;

/**
* @author Christian Daguerre <[email protected]>
*/
interface RequestCheckerInterface
{
/**
* Check if the given request was made by a robot.
*
* @param Request $request
*
* @return bool
*/
public function isRobot(Request $request);

/**
* Check if the given request was made by a crawler.
*
* @param Request $request
*
* @return bool
*/
public function isCrawler(Request $request);
}
16 changes: 16 additions & 0 deletions Robots/Exception/RuleNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Dag\Component\Robots\Exception;

/**
* Exception should be thrown when a rule does not exist.
*
* @author Christian Daguerre <[email protected]>
*/
class RuleNotFoundException extends \Exception
{
public function __construct($route, $host)
{
parent::__construct(sprintf('There is no rule for host "%s" and route "%s".', $route, $host));
}
}
131 changes: 131 additions & 0 deletions Robots/Model/Rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace Dag\Component\Robots\Model;

/**
* @author Christian Daguerre <[email protected]>
*/
class Rule implements RuleInterface
{
/**
* @var int
*/
protected $id;

/**
* @var string
*/
protected $route;

/**
* @var string
*/
protected $host;

/**
* @var array
*/
protected $tags = array();

/**
* @var \DateTime
*/
protected $createdAt;

/**
* @var \DateTime
*/
protected $updatedAt;

/**
* Constructor.
*/
public function __construct()
{
$this->createdAt = new \DateTime();
}

/**
* {@inheritdoc}
*/
public function getRoute()
{
return $this->route;
}

/**
* {@inheritdoc}
*/
public function setRoute($route)
{
$this->route = $route;
}

/**
* {@inheritdoc}
*/
public function getHost()
{
return $this->host;
}

/**
* {@inheritdoc}
*/
public function setHost($host)
{
$this->host = $host;
}

/**
* {@inheritdoc}
*/
public function getTags()
{
return $this->tags;
}

/**
* {@inheritdoc}
*/
public function setTags(array $tags)
{
$this->tags = $tags;
}

/**
* {@inheritdoc}
*/
public function getCreatedAt()
{
return $this->createdAt;
}

/**
* {@inheritdoc}
*/
public function setCreatedAt(\DateTime $createdAt)
{
$this->createdAt = $createdAt;

return $this;
}

/**
* {@inheritdoc}
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}

/**
* {@inheritdoc}
*/
public function setUpdatedAt(\DateTime $updatedAt)
{
$this->updatedAt = $updatedAt;

return $this;
}
}
53 changes: 53 additions & 0 deletions Robots/Model/RuleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Dag\Component\Robots\Model;

use Sylius\Component\Resource\Model\TimestampableInterface;

/**
* @author Christian Daguerre <[email protected]>
*/
interface RuleInterface extends TimestampableInterface
{
/**
* Get route.
*
* @return string
*/
public function getRoute();

/**
* Set route.
*
* @param string $route
*/
public function setRoute($route);

/**
* Get host.
*
* @return string
*/
public function getHost();

/**
* Set host.
*
* @param string $host
*/
public function setHost($host);

/**
* Get tags.
*
* @return array
*/
public function getTags();

/**
* Set tags.
*
* @param string $tags
*/
public function setTags(array $tags);
}
40 changes: 40 additions & 0 deletions Robots/Provider/ArrayRuleProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Dag\Component\Robots\Provider;

use Dag\Component\Robots\Model\Rule;

/**
* @author Christian Daguerre <[email protected]>
*/
class ArrayRuleProvider implements RuleProviderInterface
{
/**
* @var array
*/
protected $rules;

/**
* @param array $rules
*/
public function __construct(array $rules)
{
$this->rules = $rules;
}

/**
* {@inheritdoc}
*/
public function findMatching($route, $host)
{
$rule = new Rule();
$rule->setHost($host);
$rule->setRoute($route);

if (isset($this->rules[$route][$host])) {
$rule->setTags($this->rules[$route][$host]);
}

return $rule;
}
}
Loading

0 comments on commit fd08c1b

Please sign in to comment.