-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fd08c1b
Showing
29 changed files
with
1,229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
vendor/ | ||
bin/ | ||
|
||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.