Skip to content

Commit f2348d8

Browse files
committed
Enhance documentation for FastForward HTTP Factory
- Added detailed descriptions and examples for StreamFactoryInterface and StreamFactory. - Introduced a new compatibility section outlining package metadata and compatibility expectations. - Expanded FAQ section to clarify differences between PSR and Fast Forward factories, container usage, and service registration. - Updated getting started guide with clearer installation instructions and usage examples. - Improved quickstart guide to demonstrate common workflows for new users. - Added use cases for various response types including JSON, HTML, text, and redirect responses. - Clarified stream usage with examples for creating JSON streams and handling payloads. - Documented dependencies and their roles in the package architecture. Signed-off-by: Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
1 parent 7c2f672 commit f2348d8

29 files changed

Lines changed: 1161 additions & 331 deletions

README.md

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# 🚀 FastForward HTTP Factory
1+
# FastForward HTTP Factory
22

3-
[![PHP Version](https://img.shields.io/badge/PHP-^8.1-8892BF?logo=php)](https://www.php.net/)
3+
[![PHP Version](https://img.shields.io/badge/PHP-^8.3-8892BF?logo=php)](https://www.php.net/)
44
[![License](https://img.shields.io/github/license/php-fast-forward/http-factory)](https://opensource.org/licenses/MIT)
55
[![CI](https://github.com/php-fast-forward/http-factory/actions/workflows/tests.yml/badge.svg)](https://github.com/php-fast-forward/http-factory/actions)
66

7-
A [PSR-11](https://www.php-fig.org/psr/psr-11/) compatible service provider that registers a fully functional set of [PSR-17](https://www.php-fig.org/psr/psr-17/) and [PSR-7](https://www.php-fig.org/psr/psr-7/) HTTP factories using [Nyholm PSR-7](https://github.com/Nyholm/psr7) and [Nyholm ServerRequestCreator](https://github.com/Nyholm/psr7-server).
7+
A Fast Forward service provider and helper-factory package for [PSR-17](https://www.php-fig.org/psr/psr-17/) and [PSR-7](https://www.php-fig.org/psr/psr-7/) HTTP objects, built on top of [Nyholm PSR-7](https://github.com/Nyholm/psr7) and [Nyholm ServerRequestCreator](https://github.com/Nyholm/psr7-server).
88

99
Designed to work out of the box with the [`php-fast-forward/container`](https://github.com/php-fast-forward/container) autowiring system.
1010

@@ -16,19 +16,19 @@ Designed to work out of the box with the [`php-fast-forward/container`](https://
1616
composer require fast-forward/http-factory
1717
```
1818

19-
## ✅ Features
20-
- Registers the Psr17Factory as the base implementation for all PSR-17 interfaces
21-
- Registers the ServerRequestCreator using InvokableFactory
22-
- Provides ServerRequestInterface::class using fromGlobals() via MethodFactory
23-
- Aliases:
24-
- RequestFactoryInterface
25-
- ResponseFactoryInterface
26-
- ServerRequestFactoryInterface
27-
- StreamFactoryInterface
28-
- UploadedFileFactoryInterface
29-
- UriFactoryInterface
19+
## Features
20+
- Reuses one `Nyholm\Psr7\Factory\Psr17Factory` instance for the standard PSR-17 interfaces
21+
- Registers `Nyholm\Psr7Server\ServerRequestCreator` and exposes `ServerRequestInterface::class` via `fromGlobals()`
22+
- Exposes `FastForward\Http\Message\Factory\ResponseFactoryInterface` for HTML, JSON, text, redirect, and no-content helpers
23+
- Exposes `FastForward\Http\Message\Factory\StreamFactoryInterface` for payload-aware JSON stream helpers
24+
- Keeps returned objects PSR-7 compatible
3025

31-
## 🛠️ Usage
26+
## Usage
27+
28+
There are two similarly named response and stream factory interfaces:
29+
30+
- `Psr\Http\Message\ResponseFactoryInterface` and `Psr\Http\Message\StreamFactoryInterface` for plain PSR-17 behavior
31+
- `FastForward\Http\Message\Factory\ResponseFactoryInterface` and `FastForward\Http\Message\Factory\StreamFactoryInterface` for Fast Forward helper methods
3232

3333
If you’re using `fast-forward/container`:
3434
```php
@@ -47,9 +47,23 @@ $container = container($config);
4747

4848
$requestFactory = $container->get(Psr\Http\Message\RequestFactoryInterface::class);
4949
$serverRequest = $container->get(Psr\Http\Message\ServerRequestInterface::class);
50+
$responseFactory = $container->get(FastForward\Http\Message\Factory\ResponseFactoryInterface::class);
51+
$streamFactory = $container->get(FastForward\Http\Message\Factory\StreamFactoryInterface::class);
52+
53+
$request = $requestFactory->createRequest('GET', '/health');
54+
55+
$jsonResponse = $responseFactory->createResponseFromPayload(['ok' => true]);
56+
$htmlResponse = $responseFactory->createResponseFromHtml('<h1>Hello</h1>');
57+
$redirectResponse = $responseFactory->createResponseRedirect('/login');
58+
$noContentResponse = $responseFactory->createResponseNoContent();
59+
60+
$acceptedResponse = $responseFactory
61+
->createResponse(202)
62+
->withHeader('Content-Type', 'application/json; charset=utf-8')
63+
->withBody($streamFactory->createStreamFromPayload(['queued' => true]));
5064
```
5165

52-
## 🔧 Services Registered
66+
## Services Registered
5367

5468
The following services will be automatically registered in your container when using `HttpMessageFactoryServiceProvider`:
5569

@@ -61,19 +75,29 @@ The following services will be automatically registered in your container when u
6175
| `Psr\Http\Message\StreamFactoryInterface` | `Nyholm\Psr7\Factory\Psr17Factory` (via alias) |
6276
| `Psr\Http\Message\UploadedFileFactoryInterface` | `Nyholm\Psr7\Factory\Psr17Factory` (via alias) |
6377
| `Psr\Http\Message\UriFactoryInterface` | `Nyholm\Psr7\Factory\Psr17Factory` (via alias) |
78+
| `Nyholm\Psr7Server\ServerRequestCreatorInterface` | `Nyholm\Psr7Server\ServerRequestCreator` (via alias) |
79+
| `FastForward\Http\Message\Factory\ResponseFactoryInterface` | `FastForward\Http\Message\Factory\ResponseFactory` (via alias) |
80+
| `FastForward\Http\Message\Factory\StreamFactoryInterface` | `FastForward\Http\Message\Factory\StreamFactory` (via alias) |
6481
| `Nyholm\Psr7\Factory\Psr17Factory` | Registered via `InvokableFactory` |
6582
| `Nyholm\Psr7Server\ServerRequestCreator` | Registered via `InvokableFactory`, with dependencies |
83+
| `FastForward\Http\Message\Factory\ResponseFactory` | Registered via `InvokableFactory` |
84+
| `FastForward\Http\Message\Factory\StreamFactory` | Registered via `InvokableFactory` |
6685
| `Psr\Http\Message\ServerRequestInterface` | Created by calling `fromGlobals()` on `ServerRequestCreator` via `MethodFactory` |
6786

68-
---
87+
## Documentation
6988

70-
## 📂 License
89+
The Sphinx documentation under [`docs/`](docs/) covers:
7190

72-
This package is open-source software licensed under the [MIT License](https://opensource.org/licenses/MIT).
91+
- beginner installation and quickstart flows
92+
- concrete `ResponseFactory` and `StreamFactory` classes
93+
- common response and payload-stream scenarios
94+
- alias mapping, compatibility, dependencies, and troubleshooting
7395

74-
---
96+
## License
97+
98+
This package is open-source software licensed under the [MIT License](https://opensource.org/licenses/MIT).
7599

76-
## 🤝 Contributing
100+
## Contributing
77101

78102
Contributions, issues, and feature requests are welcome!
79103
Feel free to open a [GitHub Issue](https://github.com/php-fast-forward/http-factory/issues) or submit a Pull Request.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"ergebnis/composer-normalize": true,
4141
"fast-forward/dev-tools": true,
4242
"phpdocumentor/shim": true,
43-
"phpro/grumphp": true
43+
"phpro/grumphp": true,
44+
"pyrech/composer-changelogs": true
4445
},
4546
"platform": {
4647
"php": "8.3.0"

docs/advanced/aliases.rst

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
1-
Aliases and Factories
2-
=====================
1+
Aliases and Service Mapping
2+
===========================
33

4-
All PSR-17 aliases are resolved to a single instance of `Nyholm\Psr7\Factory\Psr17Factory`, ensuring consistency and resource efficiency.
4+
The service provider uses aliases so that multiple interfaces can reuse the same underlying object.
5+
This keeps the container setup compact and avoids duplicate factory instances.
56

6-
Details:
7-
--------
7+
PSR-17 Alias Group
8+
------------------
89

9-
- **Singleton Pattern**: The same instance is reused for all PSR-17 interfaces, reducing memory usage and initialization overhead.
10-
- **Custom Implementations**: You can override any alias by registering your own implementation in the container.
11-
- **ServerRequestInterface**: Resolved using the static `fromGlobals()` method from `Nyholm\Psr7Server\ServerRequestCreator`, providing easy access to the current HTTP request.
12-
- **Extensibility**: The provider is designed to be extensible, allowing you to add or replace factories as needed for your application.
10+
All of the following service ids resolve to the same ``Nyholm\Psr7\Factory\Psr17Factory`` instance:
11+
12+
- ``Psr\Http\Message\RequestFactoryInterface``
13+
- ``Psr\Http\Message\ResponseFactoryInterface``
14+
- ``Psr\Http\Message\ServerRequestFactoryInterface``
15+
- ``Psr\Http\Message\StreamFactoryInterface``
16+
- ``Psr\Http\Message\UploadedFileFactoryInterface``
17+
- ``Psr\Http\Message\UriFactoryInterface``
18+
19+
Fast Forward Alias Group
20+
------------------------
21+
22+
The package also exposes convenience aliases:
23+
24+
- ``FastForward\Http\Message\Factory\ResponseFactoryInterface`` resolves to ``FastForward\Http\Message\Factory\ResponseFactory``
25+
- ``FastForward\Http\Message\Factory\StreamFactoryInterface`` resolves to ``FastForward\Http\Message\Factory\StreamFactory``
26+
- ``Nyholm\Psr7Server\ServerRequestCreatorInterface`` resolves to ``Nyholm\Psr7Server\ServerRequestCreator``
27+
28+
Why This Matters
29+
----------------
30+
31+
The alias setup creates a clean separation:
32+
33+
- PSR-17 interfaces give you low-level factory behavior
34+
- Fast Forward interfaces give you convenience methods for common application responses
35+
36+
Overriding A Service
37+
--------------------
38+
39+
If you want different behavior, replace the alias target in your container configuration with your own implementation.
40+
Typical examples include:
41+
42+
- a custom response factory that always adds application-specific headers
43+
- a custom stream factory that uses another payload encoding strategy
44+
- a different request bootstrap process in long-running servers
45+
46+
Before overriding, check your container's precedence rules so you know whether your registration replaces or is replaced by the service-provider definition.

docs/advanced/index.rst

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
Advanced Topics
22
===============
33

4-
Overview
5-
--------
4+
These pages focus on the details that become important once the package is already working in your project:
65

7-
This section covers advanced usage and internal details of FastForward HTTP Factory. Each topic below provides in-depth documentation and examples:
8-
9-
- **Integration with PSR-11 Containers**: How to use the service provider with different containers and best practices for configuration.
10-
- **Aliases and Factories**: Details on how PSR-17 aliases are resolved, singleton patterns, and customization.
11-
- **Automated Tests**: How the package is tested, what is covered, and how to extend tests for your own use cases.
12-
- **Code Coverage**: How to access and interpret the coverage report, and tips for maintaining high test coverage.
13-
14-
Select a topic from the list above for more details.
6+
- how the service provider fits into the container
7+
- how aliases are mapped
8+
- how to replace or override pieces safely
9+
- what to check when something behaves differently than expected
1510

1611
.. toctree::
17-
:maxdepth: 2
18-
:caption: Advanced Topics
12+
:maxdepth: 2
13+
:caption: Advanced Topics
1914

20-
integration
21-
aliases
15+
integration
16+
aliases
17+
troubleshooting

docs/advanced/integration.rst

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,67 @@
1-
Integration with PSR-11 Containers
2-
==================================
1+
Integration
2+
===========
33

4-
FastForward HTTP Factory is designed to work seamlessly with any PSR-11 compatible container, such as `fast-forward/container` or other popular PHP containers.
4+
Preferred Integration: ``fast-forward/container``
5+
-------------------------------------------------
56

6-
Key Points:
7-
-----------
7+
The service provider in this package is designed first for ``fast-forward/container``.
8+
That is the most direct way to consume the aliases and factory definitions provided by ``HttpMessageFactoryServiceProvider``.
89

9-
- **Automatic Registration**: All required factories and aliases are registered for immediate use.
10-
- **Configuration Example**:
10+
.. code-block:: php
1111
12-
.. code-block:: php
12+
use FastForward\Config\ArrayConfig;
13+
use FastForward\Container\ContainerInterface;
14+
use FastForward\Container\container;
15+
use FastForward\Http\Message\Factory\ServiceProvider\HttpMessageFactoryServiceProvider;
1316
14-
use FastForward\Container\container;
15-
use FastForward\Config\ArrayConfig;
16-
use FastForward\Container\ContainerInterface;
17-
use FastForward\Http\Message\Factory\ServiceProvider\HttpMessageFactoryServiceProvider;
17+
$config = new ArrayConfig([
18+
ContainerInterface::class => [
19+
HttpMessageFactoryServiceProvider::class,
20+
],
21+
]);
1822
19-
$config = new ArrayConfig([
20-
ContainerInterface::class => [
21-
HttpMessageFactoryServiceProvider::class,
22-
],
23-
]);
23+
$container = container($config);
2424
25-
$container = container($config);
25+
Important Clarification
26+
----------------------
2627

27-
- **Custom Containers**: You can use the service provider with any container that implements PSR-11.
28-
- **Best Practices**: Register the provider as early as possible in your configuration to ensure all HTTP factories are available for dependency injection.
28+
The services exposed by this package are PSR-7 and PSR-17 compatible once resolved, but the service provider itself uses Fast Forward container helper classes such as:
29+
30+
- ``AliasFactory``
31+
- ``InvokableFactory``
32+
- ``MethodFactory``
33+
34+
Because of that, you should not assume that any generic PSR-11 container can consume this provider directly.
35+
If your container does not support Fast Forward service-provider definitions, register equivalent services manually.
36+
37+
Manual Wiring Example
38+
---------------------
39+
40+
.. code-block:: php
41+
42+
use FastForward\Http\Message\Factory\ResponseFactory;
43+
use FastForward\Http\Message\Factory\StreamFactory;
44+
use Nyholm\Psr7\Factory\Psr17Factory;
45+
use Nyholm\Psr7Server\ServerRequestCreator;
46+
47+
$psr17Factory = new Psr17Factory();
48+
49+
$responseFactory = new ResponseFactory($psr17Factory);
50+
$streamFactory = new StreamFactory($psr17Factory);
51+
52+
$serverRequestCreator = new ServerRequestCreator(
53+
$psr17Factory,
54+
$psr17Factory,
55+
$psr17Factory,
56+
$streamFactory,
57+
);
58+
59+
$serverRequest = $serverRequestCreator->fromGlobals();
60+
61+
Integration Checklist
62+
---------------------
63+
64+
- register the provider once
65+
- inject PSR-17 interfaces where generic factories are enough
66+
- inject Fast Forward interfaces where helper methods improve readability
67+
- resolve ``ServerRequestInterface`` only in the context of an actual HTTP request

docs/advanced/troubleshooting.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Troubleshooting
2+
===============
3+
4+
I resolved ``Psr\\Http\\Message\\ResponseFactoryInterface`` but the helper methods do not exist
5+
-----------------------------------------------------------------------------------------------
6+
7+
You resolved the PSR-17 interface, not the Fast Forward convenience interface.
8+
Use ``FastForward\Http\Message\Factory\ResponseFactoryInterface`` when you need methods such as ``createResponseFromPayload()`` or ``createResponseRedirect()``.
9+
10+
I resolved ``Psr\\Http\\Message\\StreamFactoryInterface`` but ``createStreamFromPayload()`` is missing
11+
------------------------------------------------------------------------------------------------------
12+
13+
The same rule applies to streams.
14+
Resolve ``FastForward\Http\Message\Factory\StreamFactoryInterface`` for payload-aware helpers.
15+
16+
``createResponseNoContent()`` does not let me choose another status code
17+
------------------------------------------------------------------------
18+
19+
That method is intentionally specialized for ``204 No Content``.
20+
For ``202 Accepted`` or other empty responses, call the standard PSR-17 ``createResponse()`` method instead.
21+
22+
I need a redirect status other than ``301`` or ``302``
23+
------------------------------------------------------
24+
25+
``createResponseRedirect()`` only models the common temporary and permanent redirect cases.
26+
Build the response manually if you need ``303``, ``307``, or ``308``.
27+
28+
My JSON payload throws an exception
29+
----------------------------------
30+
31+
The payload must be JSON-encodable.
32+
Resources are not supported, and invalid data will surface as an exception from ``JsonStream``.
33+
34+
The current request looks stale in a long-running process
35+
---------------------------------------------------------
36+
37+
``ServerRequestInterface`` is created from PHP globals when it is resolved.
38+
In long-running servers or workers, resolve it for each request and avoid keeping it as a singleton.
39+
40+
I use another container and the service provider does not plug in automatically
41+
-------------------------------------------------------------------------------
42+
43+
That can happen because the provider uses Fast Forward container helper factories.
44+
In non-Fast-Forward containers, manually register the equivalent services.

docs/api/index.rst

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
API Reference
22
=============
33

4-
Main Interfaces and Classes
5-
--------------------------
4+
This package is intentionally small, so the API surface is easy to learn.
5+
There are five main pieces to understand:
66

7-
.. contents:: Table of Contents
8-
:local:
9-
:depth: 2
7+
.. list-table::
8+
:header-rows: 1
109

11-
ResponseFactoryInterface
12-
-----------------------
13-
14-
API Reference
15-
=============
10+
* - Type
11+
- Role
12+
* - ``HttpMessageFactoryServiceProvider``
13+
- Registers aliases and concrete services in the container
14+
* - ``ResponseFactoryInterface``
15+
- Defines the response helper methods on top of PSR-17
16+
* - ``ResponseFactory``
17+
- Concrete decorator that implements the response helper methods
18+
* - ``StreamFactoryInterface``
19+
- Defines the payload-stream helper method on top of PSR-17
20+
* - ``StreamFactory``
21+
- Concrete decorator that implements the stream helper methods
1622

1723
.. toctree::
1824
:maxdepth: 2
1925
:caption: API Reference
2026

2127
response-factory-interface
28+
response-factory
2229
stream-factory-interface
30+
stream-factory
2331
service-provider
24-
25-
This section provides detailed documentation for the main interfaces and classes of FastForward HTTP Factory. Select a topic above for in-depth reference and examples.
26-
- **createResponseFromPayload**: Creates a JSON response from an associative array.

0 commit comments

Comments
 (0)