Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Ermashev committed Aug 7, 2020
1 parent 2b94154 commit d92af86
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 22 deletions.
134 changes: 114 additions & 20 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ HTTP example
<?php
use Tiny\Http\Request;
use Tiny\Http\RequestHttpParams;
use Tiny\Http\Request;
use Tiny\Http\RequestHttpParams;
$request = new Request(
$request = new Request(
new RequestHttpParams($_SERVER)
);
);
echo $request->getRequest(); // prints the current request like: "/", "/users", "/documents/view", etc
echo $request->getMethod(); // prints the current method like: "GET", "POST", "DELETE", etc
echo $request->getRequest(); // prints the current request like: "/", "/users", "/documents/view", etc
echo $request->getMethod(); // prints the current method like: "GET", "POST", "DELETE", etc
PS: The :code:`RequestHttpParams` ignores any http `GET` parameters and returns only the request's path.
E.g :code:`/users/?param=1` would be :code:`/users`, also it removes sub directory path - :code:`sub_directory/users/` would be just :code:`/users`, it very
Expand All @@ -51,17 +51,17 @@ CLI example
<?php
use Tiny\Http\Request;
use Tiny\Http\RequestCliParams;
use Tiny\Http\Request;
use Tiny\Http\RequestCliParams;
$request = new Request(
$request = new Request(
new RequestCliParams($_SERVER)
);
);
echo $request->getRequest(); // prints the current request like: "users", "user import 1", etc
echo $request->getMethod(); // prints the current method like: "CONSOLE"
echo $request->getRequest(); // prints the current request like: "users", "user import 1", etc
echo $request->getMethod(); // prints the current method like: "CONSOLE"
// example of calling: php index.php user import 1
// example of calling: php index.php user import 1
-----------------
Universal example
Expand All @@ -73,11 +73,105 @@ In this case we don't really care about the current mode, we just work with the
<?php
use Tiny\Http\Request;
use Tiny\Http\RequestCliParams;
use Tiny\Http\RequestHttpParams;
use Tiny\Http\Request;
use Tiny\Http\RequestCliParams;
use Tiny\Http\RequestHttpParams;
$request = new Request((php_sapi_name() === 'cli'
? new RequestCliParams($_SERVER)
: new RequestHttpParams($_SERVER)
));
// a good tip: the should be placed in a factory
$request = new Request((php_sapi_name() === 'cli'
? new RequestCliParams($_SERVER)
: new RequestHttpParams($_SERVER)
));
echo $request->getRequest();
echo $request->getMethod();
Response
--------

The :code:`Response` object holds and returns a server’s answer to your clients.
The response maybe changed in any time in any place in you application.
So it's good choice if you are going to use **Middlewares** and it also works good using in old **MVC paradigm**.

------------
HTTP example
------------

.. code-block:: php
<?php
use Tiny\Http\ResponseHttp;
use Tiny\Http\ResponseHttpUtils;
$response = new ResponseHttp(
new ResponseHttpUtils()
);
$response
->setCode(200)
->setResponse('Hello world')
->setResponseType('text/html');
// prints "Hello world" and send associated headers
echo $response->getResponseForDisplaying();
Take into account you don't need to run manually the :code:`header()` function. Because it triggers automatically
based on the provided response `code` and `type` whenever you call the :code:`getResponseForDisplaying()` method. In our case these headers are sent:

- HTTP/1.1 200 OK
- Content-Type: text/html; charset=utf-8

-----------
CLI example
-----------

.. code-block:: php
<?php
use Tiny\Http\ResponseCli;
$response = new ResponseCli();
// we even can pass an object as a response, which will be transformed in a string
$response->setResponse(new class() {
public function __toString() {
return 'Hello world';
}
});
// prints "Hello world"
echo $response->getResponseForDisplaying();
In comparison with the `HTTP` analog it doesn't send any headers, it only returns the request. Also you can skipp
the adding both "code" and "type" they are not used in the `CLI` mode.

-----------------
Universal example
-----------------

If we don't need to know about the current context we can build an abstraction layer.

.. code-block:: php
<?php
use Tiny\Http\ResponseCli;
use Tiny\Http\ResponseHttp;
use Tiny\Http\ResponseHttpUtils;
// a good tip: the should be placed in a factory
$response = (php_sapi_name() === 'cli'
? new ResponseCli()
: new ResponseHttp(
new ResponseHttpUtils()
);
$response
->setCode(201)
->setResponse('{"name": "test"}')
->setResponseType('application/json');
// we don't care about neither "CLI" nor "HTTP", we just print the value
echo $response->getResponseForDisplaying();
4 changes: 2 additions & 2 deletions test/ResponseHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public function responseProvider(): array
return [
[
200,
'{"name": "tes"}',
'{"name": "test"}',
'application/json',
'{"name": "tes"}',
'{"name": "test"}',
['Content-Type: application/json; charset=utf-8']
],
[
Expand Down

0 comments on commit d92af86

Please sign in to comment.