Using PSR Interfaces - rougin/slytherin GitHub Wiki
โ Automagic Resolutions | Rendering Templates โ
What are PSRs?
PHP Standard Recommendations (PSRs) is a specification published by the PHP Framework Interoperability Group (PHP-FIG) and serves the standardization of programming concepts in PHP. Its aim is to enable interoperability of components and to provide a common technical basis for implementation of proven concepts for optimal programming and testing practices.
As Slytherin tries to be compatible and extensible with other PHP packages, its internal code is being written based on the following PSRs:
| Number | Name | Description |
|---|---|---|
| PSR-04 | Autoloading Standard | It describes a specification for autoloading classes from file paths. It is fully interoperable and can be used in addition to any other autoloading specification, including PSR-0. This also describes where to place files that will be auto loaded according to the specification. |
| PSR-07 | HTTP Message Interface | It describes common interfaces for representing HTTP messages as described in RFC 7230 and RFC 7231, and URIs for use with HTTP messages as described in RFC 3986. |
| PSR-11 | Container Interface | It describes a common interface for dependency injection containers. The goal is to standardize how frameworks and libraries make use of a container to obtain objects and parameters. |
| PSR-12 | Extended Coding Style Guide | It extends, expands and replaces PSR-02, the coding style guide and requires adherence to PSR-01, the basic coding standard. |
| PSR-15 | HTTP Server Request Handlers | It describes common interfaces for HTTP server request handlers and HTTP server middleware components that use HTTP messages. |
Accessing the PSR Interfaces
Using Slytherin, there is no need to instantiate the specified PSR interfaces as they will be created once used inside the HTTP routeโs action. With this, just simply add the specified interface (e.g., ServerRequestInterface from PSR-07) inside the specified HTTP route:
// app/web/index.php
use Psr\Http\Message\ServerRequestInterface;
// ...
$app->get('/', function (ServerRequestInterface $request)
{
$query = $request->getQueryParams();
$name = $query['name'] ?? 'world';
return 'Hello ' . $name . '!';
});
// ...
From the web browser, open the link below and it should show a text of Hello rougin!:
http://localhost:8000/?name=rougin
If the name query parameter is not specified, it should show a Hello world! text instead.
[!NOTE] The code responsible for autoloading the class instances inside the HTTP route are also implemented using the
PSR-11standard.
Custom Implementations
Since Slytherin is implemented under PSR interfaces, it is now easy to swap PSR implementations. Slytherin also provides its own PSR implementations mentioned above so that it can work directly without outside dependencies. However, it is possible and easy to replace those PSR implementations with a third-party implementation.
For this example, the goal is to replace the ServerRequestInterface of Slytherin. To start, kindly download a third-party package (e.g., httpsoft/http-message) using Composer:
$ composer require httpsoft/http-message
After downloading the specified third-party package, proceed to instantiate the ServerRequest class using the code below:
// app/web/index.php
use HttpSoft\Message\ServerRequestFactory;
// ...
// Create the ServerRequest from the third-party package ---------
$factory = new ServerRequestFactory;
$uri = $_SERVER['REQUEST_URI'];
$method = $_SERVER['REQUEST_METHOD'];
$request = $factory->createServerRequest($method, $uri, $_SERVER);
// ---------------------------------------------------------------
// Add the third-party ServerRequest class to the container ---
$container->set(ServerRequestInterface::class, $request);
// ------------------------------------------------------------
// ...
$app->get('/request', function (ServerRequestInterface $request)
{
return get_class($request);
});
// ...
Opening the link below from the web browser should return the ServerRequest class name from the third-party package (e.g., HttpSoft\Message\ServerRequest):
http://localhost:8000/request