Skip to content

Commit 38386ae

Browse files
committed
Tidy documentation
1 parent a697ed2 commit 38386ae

File tree

1 file changed

+46
-63
lines changed

1 file changed

+46
-63
lines changed

README.md

+46-63
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
The **icanboogie/http** package provides a foundation to handle HTTP requests, with representations
88
for requests, request files, responses, and headers. The package also lay the foundation of
99

10-
The following example is an overview of a request processing:
10+
The following example is an overview of request processing:
1111

1212
```php
1313
<?php
@@ -78,8 +78,8 @@ $request = Request::from([
7878

7979
### Safe and idempotent requests
8080

81-
Safe methods are HTTP methods that do not modify resources. For instance, using `GET` or `HEAD` on a
82-
resource URL, should NEVER change the resource.
81+
Safe methods are HTTP methods that don't modify resources.
82+
For instance, using `GET` or `HEAD` on a resource URL, should NEVER change the resource.
8383

8484
The `is_safe` property may be used to check if a request is safe or not.
8585

@@ -93,9 +93,9 @@ Request::from([ Request::OPTION_METHOD => Request::METHOD_POST ])->is_safe; // f
9393
Request::from([ Request::OPTION_METHOD => Request::METHOD_DELETE ])->is_safe; // false
9494
```
9595

96-
An idempotent HTTP method is a HTTP method that can be called many times without different outcomes.
97-
It would not matter if the method is called only once, or ten times over. The result should be the
98-
same.
96+
An idempotent HTTP method is an HTTP method that can be called many times without different
97+
outcomes. It would not matter if the method is called only once, or ten times over. The result
98+
should be the same.
9999

100100
The `is_idempotent` property may be used to check if a request is idempotent or not.
101101

@@ -136,7 +136,7 @@ $request = Request::from($_SERVER)->with([
136136

137137
### Request parameters
138138

139-
Whether they are sent as part of the query string, the post body, or the path info, parameters sent
139+
Whether they're sent as part of the query string, the POST body, or the path info, parameters sent
140140
along a request are collected in arrays. The `query_params`, `request_params`, and `path_params`
141141
properties give you access to these parameters.
142142

@@ -237,9 +237,8 @@ $file = $files['uploaded']; // instanceof File
237237
$file = $files['undefined']; // null
238238
```
239239

240-
Uploaded files, and _pretend_ uploaded files, are represented by [File][] instances. The class
241-
tries its best to provide the same API for both. The `is_uploaded` property helps you set
242-
them apart.
240+
[File][] represents uploaded files, and _pretend_ uploaded files, with a single API.
241+
The `is_uploaded` property helps you set them apart.
243242

244243
The `is_valid` property is a simple way to check if a file is valid. The `move()` method
245244
lets you move the file out of the temporary folder or around the filesystem.
@@ -282,7 +281,7 @@ echo $file->match('image'); // false
282281
echo $file->match('.png'); // false
283282
```
284283

285-
The method also handles sets, and returns `true` if there's any match:
284+
The method also handles sets, and returns `true` if there is any match:
286285

287286
```php
288287
<?php
@@ -319,9 +318,9 @@ $file->to_array();
319318

320319
### Request context
321320

322-
Because requests may be nested the request context offers a safe place where you can store the state
323-
of your application that is relative to a request, for instance a request relative site, page,
324-
route, dispatcher… The context may be used as an array, but is also a prototyped instance.
321+
Because requests may be nested, the request context offers a safe place where you can store the
322+
state of your application that is relative to a request. For instance, the context can store the
323+
relative site, page, route, dispatcher…
325324

326325
The following example demonstrates how to store a value in a request context:
327326

@@ -330,32 +329,16 @@ The following example demonstrates how to store a value in a request context:
330329

331330
namespace ICanBoogie\HTTP;
332331

333-
$request = Request::from($_SERVER);
334-
$request->context['site'] = $app->models['sites']->one;
335-
```
336-
337-
The following example demonstrates how to use the prototype feature to provide a value when it is
338-
requested from the context:
332+
/** @var Request $request */
333+
/** @var \ICanBoogie\Routing\Route $route */
339334

340-
```php
341-
<?php
335+
$request->context->add($route);
342336

343-
namespace ICanBoogie\HTTP;
344-
345-
use ICanBoogie\HTTP\Request\Context;
346-
use ICanBoogie\Prototype;
347-
348-
Prototype::from(Context::class)['lazy_get_site'] = function(Context $context) use ($site_model) {
349-
350-
return $site_model->resolve_from_request($context->request);
351-
352-
};
337+
// …
353338

354-
$request = Request::from($_SERVER);
355-
356-
$site = $request->context['site'];
357-
# or
358-
$site = $request->context->site;
339+
$route = $request->conntext->find(Route::class);
340+
# or, if the value is required
341+
$route = $request->conntext->get(Route::class);
359342
```
360343

361344

@@ -364,23 +347,25 @@ $site = $request->context->site;
364347

365348
### Obtaining a response
366349

367-
A response is obtained from a request simply by invoking the request, or by invoking one of the
368-
available HTTP methods. The `dispatch()` helper is used to dispatch the request. A [Response][]
369-
instance is returned if the dispatching is successful, a [NotFound][] exception is
370-
thrown otherwise.
350+
A response is the result of a [Responder][]'s `respond()` method. An exception is thrown when a
351+
response can't be provided; for example, [NotFound][].
371352

372353
```php
373354
<?php
374355

375356
namespace ICanBoogie\HTTP;
376357

377358
/* @var $request Request */
359+
/* @var ResponderProvider $responder_provider */
378360

379-
$response = $request();
361+
// The Responder Provider matches a request with a Responder
362+
$responder = $responder_provider->responder_for_request($request);
380363

381-
# using the POST method and additional parameters
364+
// The Responder responds to the request with a Response, it might also throw an exception.
365+
$response = $responder->respond($request);
382366

383-
$response = $request->post([ 'param' => 'value' ]);
367+
// The response is sent to the client.
368+
$response();
384369
```
385370

386371

@@ -425,7 +410,7 @@ $response();
425410

426411
### Response status
427412

428-
The response status is represented by a [Status][] instance. It may be defined as a HTTP response
413+
The response status is represented by a [Status][] instance. It may be defined as an HTTP response
429414
code such as `200`, an array such as `[ 200, "Ok" ]`, or a string such as `"200 Ok"`.
430415

431416
```php
@@ -454,7 +439,7 @@ $response->status->is_not_found; // true
454439

455440
### Streaming the response body
456441

457-
When a large response body needs to be streamed, it is recommended to use a closure as response
442+
When a large response body needs to be streamed, it is recommended to use a closure as a response
458443
body instead of a huge string that would consume a lot of memory.
459444

460445
```php
@@ -488,10 +473,10 @@ $response = new Response($output, Response::STATUS_OK, [ 'Content-Type' => 'text
488473

489474
Before v2.3.2 the `Content-Length` header field was added automatically when it was computable,
490475
for instance when the body was a string or an instance implementing `__toString()`.
491-
Starting v2.3.2 this is no longer the case and the header field has to be defined when required.
476+
Starting v2.3.2, this is no longer the case, and the header field has to be defined when required.
492477
This was decided to prevent a bug with Apache+FastCGI+DEFLATE where the `Content-Length` field
493-
was not adjusted although the body was compressed. Also, in most cases it's not such a good idea
494-
to define that field for generated content because it prevents the response to be sent as
478+
wasn't adjusted although the body was compressed. Also, in most cases it is not such a good idea
479+
to define that field for generated content because it prevents the response from being sent as
495480
[compressed chunks](http://en.wikipedia.org/wiki/Chunked_transfer_encoding).
496481

497482

@@ -519,7 +504,7 @@ $response->status->is_redirect; // true
519504
### Delivering a file
520505

521506
A file may be delivered using a [FileResponse][] instance. Cache control and _range_ requests
522-
are handled automatically, you just need to provide the pathname of the file, or a `SplFileInfo`
507+
are handled automatically; you only have to provide the pathname of the file, or a `SplFileInfo`
523508
instance, and a request.
524509

525510
```php
@@ -533,7 +518,7 @@ $response = new FileResponse("/absolute/path/to/my/file", $request);
533518
$response();
534519
```
535520

536-
The `OPTION_FILENAME` option may be used to force downloading. Of course, utf-8 string are
521+
The `OPTION_FILENAME` option may be used to force downloading. Of course, utf-8 strings are
537522
supported:
538523

539524
```php
@@ -554,11 +539,11 @@ $response();
554539

555540
The following options are also available:
556541

557-
- `OPTION_ETAG`: Specifies the `ETag` header field of the response. If it is not defined the
542+
- `OPTION_ETAG`: Specifies the `ETag` header field of the response. If it is not defined, the
558543
[SHA-384][] of the file is used instead.
559544

560545
- `OPTION_EXPIRES`: Specifies the expiration date as a `DateTime` instance or a relative date
561-
such as "+3 month", which maps to the `Expires` header field. The `max-age` directive of the
546+
such as `+3 month`, which maps to the `Expires` header field. The `max-age` directive of the
562547
`Cache-Control` header field is computed from the current time. If it is not defined
563548
`DEFAULT_EXPIRES` is used instead ("+1 month").
564549

@@ -569,7 +554,7 @@ The following properties are available:
569554

570555
- `modified_time`: Returns the last modified timestamp of the file.
571556

572-
- `is_modified`: Whether the file was modified since the last response. The value is computed
557+
- `is_modified`: Whether the file has been modified since the last response. The value is computed
573558
using the request header fields `If-None-Match` and `If-Modified-Since`, and the properties
574559
`modified_time` and `etag`.
575560

@@ -579,7 +564,8 @@ using the request header fields `If-None-Match` and `If-Modified-Since`, and the
579564

580565
## Headers
581566

582-
Here's an overview of headers usage, details are available in the [Headers documentation](docs/Headers.md).
567+
Here is an overview of header usage.
568+
Details are available in the [Headers documentation](docs/Headers.md).
583569

584570
```php
585571
<?php
@@ -627,22 +613,22 @@ echo $headers['X-My-Header']; // 'Some value';
627613

628614
## Exceptions
629615

630-
The following exceptions are defined by the HTTP package:
616+
The HTTP package defines the following exceptions:
631617

632618
* [ClientError][]: thrown when a client error occurs.
633619
* [NotFound][]: thrown when a resource is not found. For instance, this exception is
634620
thrown by the dispatcher when it fails to resolve a request into a response.
635621
* [AuthenticationRequired][]: thrown when the authentication of the client is required. Implements [SecurityError][].
636622
* [PermissionRequired][]: thrown when the client lacks a required permission. Implements [SecurityError][].
637-
* [MethodNotAllowed][]: thrown when a HTTP method is not supported.
623+
* [MethodNotAllowed][]: thrown when an HTTP method is not supported.
638624
* [ServerError][]: throw when a server error occurs.
639625
* [ServiceUnavailable][]: thrown when a server is currently unavailable
640626
(because it is overloaded or down for maintenance).
641627
* [ForceRedirect][]: thrown when a redirect is absolutely required.
642-
* [StatusCodeNotValid][]: thrown when a HTTP status code is not valid.
628+
* [StatusCodeNotValid][]: thrown when an HTTP status code is not valid.
643629

644630
Exceptions defined by the package implement the `ICanBoogie\HTTP\Exception` interface.
645-
Using this interface one can easily catch HTTP related exceptions:
631+
Using this interface, one can easily catch HTTP-related exceptions:
646632

647633
```php
648634
<?php
@@ -669,10 +655,6 @@ catch (\Exception $e)
669655

670656
The following helpers are available:
671657

672-
* [`dispatch()`][]: Dispatches a request using the dispatcher returned by [`get_dispatcher()`][].
673-
* [`get_dispatcher()`][]: Returns the request dispatcher. If no dispatcher provider is defined,
674-
the method defines a new instance of [DispatcherProvider][] as provider and use it to retrieve the
675-
dispatcher.
676658
* [`get_initial_request()`][]: Returns the initial request.
677659

678660
```php
@@ -737,6 +719,7 @@ See [CONTRIBUTING](CONTRIBUTING.md) for details.
737719
[PermissionRequired]: lib/PermissionRequired.php
738720
[RedirectResponse]: lib/RedirectResponse.php
739721
[Request]: lib/Request.php
722+
[Responder]: lib/Responder.php
740723
[Response]: lib/Response.php
741724
[SecurityError]: lib/SecurityError.php
742725
[ServerError]: lib/ServerError.php

0 commit comments

Comments
 (0)