Skip to content

Commit

Permalink
Merge pull request #17 from krakphp/0.3
Browse files Browse the repository at this point in the history
0.3
  • Loading branch information
ragboyjr committed Mar 18, 2017
2 parents d166b5a + cf00f64 commit 3c9f3bd
Show file tree
Hide file tree
Showing 99 changed files with 848 additions and 2,766 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed

Completely re-tooled the entire package. This no longer holds any micro framework code. [Krak\\Lava](https://github.com/krakphp/lava) is the replacement for this.

- Converted functions for dispatching/server/response-factory into classes with proper interfaces since you typically won't need to make your own very frequently
- Removed all framework related code
- Removed all unneeded composer dependencies (only nikic/iter remains)
- Routing is now a lot simpler and not dependent on any libraries like `krak/mw`. They are simply value objects now.
- This package is now at `krak/http` instead of `krak/mw-http`

### Added

- This CHANGELOG
- ResponseFactoryStore for easily storing and utilizing response factories.

## [0.2.4] - 2016-12-05

Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

PERIDOT = ./vendor/bin/peridot

.PHONY: test doc
.PHONY: test doc inc

inc:
./vendor/bin/php-inc php-inc:generate -o src/inc.php src

test:
$(PERIDOT) test
Expand Down
114 changes: 95 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,116 @@
# Mw Http
# Http

[![Documentation Status](https://readthedocs.org/projects/mw-http/badge/?version=latest)](http://mw-http.readthedocs.io/en/latest/?badge=latest)

Middlewares written for http applications
The Krak Http package is a set of utilities for building Http applications. It comes with an implementation agnostic routing system, PSR-7 Response Factories, PSR-7 Server implementation, and a handful of useful middleware for Http applications.

## Installation

Install via composer at `krak/mw-http`
Install via composer at `krak/http`

## Usage

### Response Factories

```php
<?php

interface ResponseFactory {
public function createResponse($status = 200, array $headers = [], $body = null);
}
```

Every response factory must implement that interface.

```php
<?php

use Krak\Http\ResponseFactory;

$rf = new ResponseFactory\DiactorosResponseFactory();
$rf = new ResponseFactory\GuzzleResponseFactory();

// adds html content-type header
$html_rf = new ResponseFactory\HtmlResponseFactory($rf);
// json encodes the body and add json content-type header. Accepts json_encode_options as second parameter
$json_rf = new ResponseFactory\JsonResponseFactory($rf, JSON_PRETTY_PRINT);
// adds text content-type header
$text_rf = new ResponseFactory\TextResponseFactory($rf);

$json_rf->createResponse(200, [], [1,2,3]);
```

### Routes

```php
<?php

use Krak\Mw\Http;
use Krak\Http\Route;

$app = new Http\App();
$app->get('/', function($req) {
return "Hello World!";
$routes = new Route\RouteGroup();
$routes->get('/', function() {})->with('attribute', 'value');
$routes->group('/foo', function($foo) {
$sub->get('', 'handler');
$sub->group('/bar', function($bar) {
$bar->get('/baz', 'handler');
});
});
$app->with(Http\Package\std());
$app->serve();
$routes->with('attribute1', 'value');
```

More documentation coming soon! For now look over the examples and source code for information.
### Compiling Routes

## Documentation
Once you've created a set of routes, you can then compile them with a route compiler. These will traverse the hierarchy of routes and flatten them into an iterator with normalized paths.

View them at [http://mw-http.readthedocs.io/en/latest/](http://mw-http.readthedocs.io/en/latest/)
```php
<?php

Or build them locally:
use Krak\Http\Route;

```bash
make doc
$routes = new Route\RouteGroup();
// add routes to $routes

$compiler = new Route\RecursiveRouteCompiler();
// compile on a path
$routes = $compiler->compileRoutes($routes, '/');
```

### Dispatch

To dispatch a set of routes, you need to create dispatcher factory, which will create a dispatcher from a set of routes, then you can dispatch a PSR-7 request.

```php
<?php

use Krak\Http\Dispatcher;
$dispatch_factory = new Dispatcher\FastRoute\FastRouteDispatcherFactory();
$dispatch = $dispatch_factory->createDispatcher($routes);
$res = $dispatch->dispatch($req);

// $res->status_code
// $res->matched_route->route
// $res->matched_route->params
// $res->allowed_methods /* if status code is a 405 response */
```

### Server

The server is responsible for creating a request, and emitting a response. It's a simple interface:

```php
<?php

interface Server {
/** @param $handler resolves the request into a response object */
public function serve($handler);
}
```

```php
<?php

$server = new Krak\Http\Server\DiactorosServer();
$server->serve(function($req) {
return new Zend\Diactoros\Response();
});
```

## Tests and Examples
Expand All @@ -42,5 +120,3 @@ Run tests via:
```bash
make test
```

View the test folder or example dir for examples on how the code is used.
27 changes: 12 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"name": "krak/mw-http",
"description": "Http Middlewares and Application",
"name": "krak/http",
"description": "Http Utilities for building frameworks",
"type": "library",
"license": "MIT",
"homepage": "https://gitlab.bighead.net/krak-mw/http",
"homepage": "https://github.com/krakphp/http",
"keywords": [
"middleware",
"framework",
"utility",
"psr-7",
"http",
"micro"
"routing"
],
"authors": [
{
Expand All @@ -19,22 +18,20 @@
],
"autoload": {
"psr-4": {
"Krak\\Mw\\Http\\": "src"
"Krak\\Http\\": "src"
},
"files": ["src/inc.php"]
},
"require": {
"evenement/evenement": "^2.0",
"krak/mw": "^0.3.0",
"nikic/fast-route": "^1.0",
"nikic/iter": "^1.4",
"pimple/pimple": "^3.0",
"zendframework/zend-diactoros": "^1.3"
"nikic/iter": "^1.4"
},
"require-dev": {
"guzzlehttp/psr7": "^1.3",
"league/plates": "^3.1",
"krak/mw": "^0.5.0",
"krak/php-inc": "^0.1.3",
"nikic/fast-route": "^1.0",
"peridot-php/peridot": "^1.18",
"symfony/var-dumper": "^3.1"
"symfony/var-dumper": "^3.2",
"zendframework/zend-diactoros": "^1.3"
}
}

0 comments on commit 3c9f3bd

Please sign in to comment.