Skip to content

Commit

Permalink
Adhere to Psr7 amid other things...
Browse files Browse the repository at this point in the history
  • Loading branch information
nbish11 committed Apr 28, 2016
1 parent dd3c152 commit f0e317f
Show file tree
Hide file tree
Showing 42 changed files with 2,984 additions and 892 deletions.
1,033 changes: 1,033 additions & 0 deletions build/logs/clover.xml

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
}
],
"require": {
"php": "^5.5 || ^7.0"
"php": "^5.5 || ^7.0",
"psr/http-message": "^1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -22,7 +23,7 @@
},
"autoload-dev": {
"psr-4": {
"Meek\\Http\\Tests\\": "tests/"
"Meek\\Http\\": "tests/"
}
}
}
68 changes: 59 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions example/FictionalRouter.php

This file was deleted.

32 changes: 32 additions & 0 deletions example/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* Gets the current HTTP request as sent by the client.
*
* @return Psr\Http\Message\RequestInterface The current request.
*/
function get_current_request() {
static $request;

if (!$request) {
$request = Meek\Http\ServerRequest::createFromGlobals();
}

return $request;
}

/**
* Sends a HTTP response to the client.
*
* @param string $body The response body to send to the client.
* @param integer $code The HTTP status code.
* @param array $headers A list of optional headers to add to the response.
*
* @return Psr\Http\Message\RequestInterface The sent response.
*/
function send_response($body = '', $code = 200, array $headers = []) {
$response = new Meek\Http\Response(null, $code, $headers);
$response->getBody()->write($body);

return $response->prepare(get_current_request())->send();
}
78 changes: 12 additions & 66 deletions example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,15 @@

// include external libraries
require_once '../vendor/autoload.php';
require_once 'FictionalRouter.php';

// use statements
use Meek\Http\Request;
use Meek\Http\Session\PdoStorageDriver;
use Meek\Http\Session;
use Meek\Http\Response;
use Meek\Http\RedirectedResponse;
use Meek\Http\Exception as HttpException;

// get the current request
$request = Request::createFromGlobals();

// manipulate the current URI
$uri = $request->getUri();
$uri->setPath(substr($uri->getPath(), strlen('/meek-http')));

// initialize a session
$dbh = new PDO('mysql:host=localhost;dbname=test', 'root', '', [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
]);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$handler = new PdoStorageDriver($dbh);
$session = new Session($handler);

$request->setSession($session);
$request->session->start();

// setup and initalize our application's routes
$router = new FictionalRouter();

// basic usage
$router->map('GET', '/', function ($request) {
$username = $request->session->get('username', 'World');

return new Response(sprintf('Hello, %s!', $username));
});

// working with headers and redirections
$router->map('GET', '/login', function ($request) {
$username = $request->server->get('PHP_AUTH_USER');
$password = $request->server->get('PHP_AUTH_PW');

if ($username === 'admin' && $password === 'password') {
$request->session->set('username', $username);
return new RedirectedResponse('/meek-http/account');
}

return new Response('Please sign in.', 401, [
'WWW-Authenticate' => sprintf('Basic realm=%s', 'site_login')
]);
});

// working with JSON responses
$router->map('GET', '/api/v1', function () {
$user = new stdClass();
$user->name = 'Nathan';
$user->dob = '24-02-1991';

return new Meek\Http\JsonResponse($user);
});

// save session to store and dispatch request
$request->session->save();
$router->dispatch($request);
require_once 'functions.php';

// dispatch a response based on the current request
send_response('<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<video src="video.mp4" controls="controls"></video>
</body>
</html>');
10 changes: 0 additions & 10 deletions src/Collections/FileList.php

This file was deleted.

31 changes: 0 additions & 31 deletions src/Collections/ServerData.php

This file was deleted.

Loading

0 comments on commit f0e317f

Please sign in to comment.