Permalink
Browse files

Initial Commit

  • Loading branch information...
0 parents commit da59441fe5161f28eb0a0754db529d149c57bb61 @dongilbert dongilbert committed Nov 1, 2013
Showing with 159 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +19 −0 LICENSE
  3. +36 −0 README.md
  4. +25 −0 composer.json
  5. +55 −0 src/League/StackRobots/Robots.php
  6. +19 −0 tests/test.php
@@ -0,0 +1,5 @@
+/.idea
+/vendor
+/composer.phar
+/composer.lock
+.DS_Store
@@ -0,0 +1,19 @@
+Copyright © 2013 Don Gilbert (@dilbert4life)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
@@ -0,0 +1,36 @@
+# League\StackRobots
+
+StackRobots is a middleware for [StackPHP](http://stackphp.com), based heavily off of [Cylon](https://github.com/dmathieu/cylon) for Ruby.
+It provides a default robots.txt for non-production environments.
+
+## Install Via Composer
+
+```json
+{
+ "require": {
+ "league/stack-robots": "~1.0@dev"
+ }
+}
+```
+
+## Example
+
+```php
+include_once '../vendor/autoload.php';
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use League\StackRobots\Robots;
+
+$app = new Stack\CallableHttpKernel(function (Request $request) {
+ return new Response('Hello World!');
+});
+
+putenv('SERVER_ENV=dev');
+
+$app = (new Stack\Builder)
+ ->push('League\\StackRobots\\Robots', 'production')
+ ->resolve($app);
+
+Stack\run($app);
+```
@@ -0,0 +1,25 @@
+{
+ "name": "league/stack-robots",
+ "description": "StackRobots Middleware for StackPHP",
+ "license": "MIT",
+ "require": {
+ "php": ">=5.3.10",
+ "symfony/http-foundation": "~2.1",
+ "symfony/http-kernel": "~2.1"
+ },
+ "require-dev" : {
+ "stack/builder" : "1.0.*@dev",
+ "stack/callable-http-kernel": "~1.0@dev",
+ "stack/run": "~1.0@dev"
+ },
+ "autoload": {
+ "psr-0": {
+ "League\\Stack\\": "src/"
+ }
+ },
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.0-dev"
+ }
+ }
+}
@@ -0,0 +1,55 @@
+<?php
+
+namespace League\StackRobots;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\HttpKernelInterface;
+
+class Robots implements HttpKernelInterface
+{
+ /**
+ * @var \Symfony\Component\HttpKernel\HttpKernelInterface
+ */
+ private $app;
+
+ /**
+ * @var string
+ */
+ private $env;
+
+ /**
+ * @var string
+ */
+ private $envVar;
+
+ /**
+ * Constructor
+ *
+ * @param HttpKernelInterface $app
+ * @param string $env The environment variable to compare against.
+ * @param string $envVar The environment variable to inspect.
+ */
+ public function __construct(HttpKernelInterface $app, $env, $envVar = 'SERVER_ENV')
+ {
+ $this->app = $app;
+ $this->env = $env;
+ $this->envVar = $envVar;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
+ {
+ if (getenv($this->envVar) !== $this->env) {
+ // @TODO: Add support for X-Robots-Tag
+
+ if ($request->getPathInfo() === '/robots.txt') {
+ return new Response("User-Agent: *\nDisallow: /", 200, array('Content-Type' => 'text/plain'));
+ }
+ }
+
+ return $this->app->handle($request, $type, $catch);
+ }
+}
@@ -0,0 +1,19 @@
+<?php
+
+include_once '../vendor/autoload.php';
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use League\StackRobots\Robots;
+
+$app = new Stack\CallableHttpKernel(function (Request $request) {
+ return new Response('Hello World!');
+});
+
+putenv('SERVER_ENV=dev');
+
+$app = (new Stack\Builder)
+ ->push('League\\StackRobots\\Robots', 'production')
+ ->resolve($app);
+
+Stack\run($app);

0 comments on commit da59441

Please sign in to comment.