Update playground files for Vagrant
This commit is contained in:
parent
9dcb2502b7
commit
6b3f2dded1
|
|
@ -1,11 +1,54 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use pjdietz\WellRESTed\Router;
|
// Provide autoloading for the playground site.
|
||||||
|
use WellRESTed\Message\Stream;
|
||||||
|
use WellRESTed\Server;
|
||||||
|
|
||||||
$loader = require_once __DIR__. "/../vendor/autoload.php";
|
$loader = require_once __DIR__. "/../vendor/autoload.php";
|
||||||
$loader->addPsr4("", __DIR__ . "/../vagrant/src");
|
|
||||||
$loader->addPsr4("", __DIR__ . "/../autoload");
|
$loader->addPsr4("", __DIR__ . "/../autoload");
|
||||||
|
|
||||||
$router = new Router();
|
|
||||||
$router->add("/", "\\WellRESTedDev\\RootHandler");
|
// Build some middleware. We'll register these with a server below.
|
||||||
$router->respond();
|
// We're using callables to fit this all in one example, but these
|
||||||
|
// could also be classes implementing WellRESTed\MiddlewareInterface.
|
||||||
|
|
||||||
|
// Set the status code and provide the greeting as the response body.
|
||||||
|
$hello = function ($request, $response, $next) {
|
||||||
|
|
||||||
|
// Check for a "name" attribute which may have been provided as a
|
||||||
|
// path variable. Use "world" as a default.
|
||||||
|
$name = $request->getAttribute("name", "world");
|
||||||
|
|
||||||
|
// Set the response body to the greeting and the status code to 200 OK.
|
||||||
|
$response = $response->withStatus(200)
|
||||||
|
->withHeader("Content-type", "text/plain")
|
||||||
|
->withBody(new Stream("Hello, $name!"));
|
||||||
|
|
||||||
|
// Propagate to the next middleware, if any, and return the response.
|
||||||
|
return $next($request, $response);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add a header to the response.
|
||||||
|
$headerAdder = function ($request, $response, $next) {
|
||||||
|
// Add the header.
|
||||||
|
$response = $response->withHeader("X-example", "hello world");
|
||||||
|
// Propagate to the next middleware, if any, and return the response.
|
||||||
|
return $next($request, $response);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create a server
|
||||||
|
$server = new Server();
|
||||||
|
|
||||||
|
// Start each request-response cycle by dispatching the header adder.
|
||||||
|
$server->add($headerAdder);
|
||||||
|
|
||||||
|
// The header adder will propagate to this router, which will dispatch the
|
||||||
|
// $hello middleware, possibly with a {name} variable.
|
||||||
|
$server->add($server->createRouter()
|
||||||
|
->register("GET", "/hello", $hello)
|
||||||
|
->register("GET", "/hello/{name}", $hello)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Read the request from the client, dispatch middleware, and output.
|
||||||
|
$server->respond();
|
||||||
|
|
|
||||||
|
|
@ -1,50 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace WellRESTedDev;
|
|
||||||
|
|
||||||
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
|
|
||||||
use pjdietz\WellRESTed\Interfaces\RequestInterface;
|
|
||||||
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
|
|
||||||
use pjdietz\WellRESTed\Response;
|
|
||||||
|
|
||||||
class RootHandler implements HandlerInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Return the handled response.
|
|
||||||
*
|
|
||||||
* @param RequestInterface $request The request to respond to.
|
|
||||||
* @param array|null $args Optional additional arguments.
|
|
||||||
* @return ResponseInterface The handled response.
|
|
||||||
*/
|
|
||||||
public function getResponse(RequestInterface $request, array $args = null)
|
|
||||||
{
|
|
||||||
$view = <<<HTML
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<title>WellRESTed</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Welcome to the WellRESTed Test Site</h1>
|
|
||||||
<ul>
|
|
||||||
<li>View <a href="/docs/">Documentatation</a></li>
|
|
||||||
<li>View <a href="/coverage/">Code Coverage Report</a></li>
|
|
||||||
</ul>
|
|
||||||
<p>Run <code>vagrant ssh</code>, then:</p>
|
|
||||||
<dl>
|
|
||||||
<dt>To run unit tests</dt>
|
|
||||||
<dd><code>vendor/bin/phpunit</code></dd>
|
|
||||||
<dt>To generate documentation</dt>
|
|
||||||
<dd><code>make html -C docs</code></dd>
|
|
||||||
</dl>
|
|
||||||
<p>Use this site as a sandbox. Modify the router <code>/htdocs/index.php</code> however you like.</p>
|
|
||||||
<p>Any classes you create inside <code>/autoload</code> will be autoloaded with a PSR-4 autoloader.</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
HTML;
|
|
||||||
$response = new Response(200);
|
|
||||||
$response->setBody($view);
|
|
||||||
return $response;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue