Skip to content

Instantly share code, notes, and snippets.

@gigablah
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save gigablah/9132063 to your computer and use it in GitHub Desktop.

Select an option

Save gigablah/9132063 to your computer and use it in GitHub Desktop.
<?php
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RequestMatcher;
require_once __DIR__.'/../vendor/autoload.php';
$app = new Durian\Application();
$app['middleware.response_time'] = $app->handler(function () {
$time = microtime(true);
yield;
$this->getResponse()->headers->set('X-Response-Time', sprintf('%fms', microtime(true) - $time));
});
$app['middleware.exception_handler'] = $app->handler(function () {
try {
yield;
} catch (\Exception $exception) {
if ($exception instanceof HttpException) {
$this->setResponse(Response::create(
$exception->getMessage() ?: Response::$statusTexts[$exception->getStatusCode()],
$exception->getStatusCode(),
$exception->getHeaders()
));
} else {
$this->setResponse(Response::create($exception->getMessage(), 500));
}
}
});
/*$app->handler(function () {
$this->setResponse(new Response('Hello World!'));
}, function () {
$matcher = new RequestMatcher('^/$');
return $matcher->matches($this->getRequest());
});*/
$app->route('/', function () {
$this->setResponse(new Response('Hello World!'));
});
$albums = $app->route('/albums', function () {
yield $this->last();
})->post(function () {
return 'POST '.$this->last();
})->get(function () {
return 'GET '.$this->last();
});
$album = $albums->route('/{albumId:[0-9]+}', function () {
yield $this->last().'/'.$this->param('albumId');
})->get(function () {
return 'GET '.$this->last();
})->put(function () {
return 'PUT '.$this->last();
})->delete(function () {
return 'DELETE '.$this->last();
});
$album->route('/tracks', function () {
yield $this->last().'/tracks';
})->get(function () {
return 'GET '.$this->last();
})->post(function () {
return 'POST '.$this->last();
})->route('/{trackId:[0-9]+}', function () {
yield $this->last().'/'.$this->param('trackId');
})->get(function () {
return 'GET '.$this->last();
})->put(function () {
return 'PUT '.$this->last();
})->delete(function () {
return 'DELETE '.$this->last();
});
$album->route('/artists', function () {
yield '/artists';
})->get(function () {
return 'GET '.$this->last();
});
$app->route('/artists', function () {
return '/artists';
});
$app->handlers(array(
'middleware.response_time',
'middleware.exception_handler',
new Durian\Middleware\RouterMiddleware()
));
$app->before(function () {
yield;
$response = $this->getResponse();
$response->prepare($this->getRequest());
echo sprintf(
'HTTP/%s %s %s',
$response->getProtocolVersion(),
$response->getStatusCode(),
Response::$statusTexts[$response->getStatusCode()]
)."\r\n";
echo $this->getResponse()->headers."\r\n";
}, function () {
return 'cli' === PHP_SAPI;
});
if ('cli' === PHP_SAPI) {
$method = isset($argv[1]) ? $argv[1] : 'GET';
$path = isset($argv[2]) ? $argv[2] : '/';
$app->run($method, $path);
} else {
$app->run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment