debuss-a / piccolo-framework
The framework of the Piccolo skeleton: a PSR-15 application built on Laminas Stratigility and Mezzio components, with a scoped container, attribute-based routing, and FrankenPHP worker mode support.
Requires
- php: ^8.4
- borschphp/config: ^1.4
- debuss-a/attribute-routing: ^3.0
- debuss-a/awareness: ^1.2
- debuss-a/piccolo-container: ^0.2.1
- debuss-a/server-request-factory: ^1.0
- laminas/laminas-httphandlerrunner: ^2.13
- laminas/laminas-stratigility: ^4.3
- league/plates: ^3.6
- mezzio/mezzio-cors: ^1.16
- mezzio/mezzio-fastroute: ^3.14
- mezzio/mezzio-helpers: ^5.20
- mezzio/mezzio-platesrenderer: ^2.14
- mezzio/mezzio-problem-details: ^1.19
- mezzio/mezzio-router: ^4.2
- mezzio/mezzio-template: ^3.0
- monolog/monolog: ^3.10
- nyholm/psr7: ^1.8
- psr/container: ^2.0
- psr/http-factory: ^1.1
- psr/http-message: ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- psr/log: ^3.0
- symfony/console: ^8.1
Requires (Dev)
- pestphp/pest: ^5.1
- phpstan/phpstan: ^2.1
Suggests
- pestphp/pest: For the end-to-end tests with Piccolo\Testing\TestClient (or phpunit/phpunit)
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-27 21:45:12 UTC
README
The framework of the Piccolo skeleton: a PSR-15 application built on Laminas
Stratigility and Mezzio components (without mezzio/mezzio), with
a scoped container, attribute-based routing, and support for the
FrankenPHP worker mode.
composer create-project debuss-a/piccolo my-app # the skeleton, which requires this package
What it provides
| Namespace | Content |
|---|---|
Piccolo\Kernel |
Boots the application: paths, configuration, environment, PHP settings, container |
Piccolo\Application |
The PSR-15 application: middleware pipeline, routes, one container scope per request |
Piccolo\Runtime |
SapiRunner (PHP-FPM, built-in server, ...) and FrankenPhpWorkerRunner, chosen automatically |
Piccolo\ServiceProvider |
HTTP factories (Nyholm PSR-7), logger (Monolog), router (FastRoute), error handler, Problem Details, CORS, templates (Plates) |
Piccolo\Middleware |
RequestIdMiddleware, SecurityHeadersMiddleware, HttpCacheMiddleware, ApiAcceptHeaderMiddleware, LazyLoadingMiddleware |
Piccolo\Http |
Handler (base class of the request handlers: html(), text(), xml(), json(), redirect(), empty()), HTTP exceptions (NotFoundException, UnprocessableEntityException, ...) answered as Problem Details, error logging, request identifier |
Piccolo\Console |
about, cache:clear, cache:warmup, container:check, container:list, routes:list, routes:match |
Piccolo\Testing |
TestClient and TestResponse, for the end-to-end tests of the application |
Piccolo\Paths |
The directories of the application, and the helpers app_path(), config_path(), storage_path(), ... |
Application layout
The kernel expects this layout, from the root directory given to Kernel::boot():
config/
configuration.php optional: returns a Borsch\Config\Config (default: .env, then the environment variables)
container.php optional: static function (ContainerBuilder $builder, Config $config, Environment $environment): void
pipeline.php static function (Application $app, ContainerInterface $container): void
routes.php static function (Application $app, ContainerInterface $container): void
commands.php optional: returns ['name' => CommandClass::class, ...]
storage/
cache/ logs/ templates/
// public/index.php Kernel::boot(dirname(__DIR__))->http()->run(); // bin/console exit(Kernel::boot(dirname(__DIR__))->console()->run());
The definitions of config/container.php are registered after the providers of the framework, so they can replace
them (another logger, another PSR-7 implementation with new HttpFactoryServiceProvider(...), ...).
Lifetimes and worker mode
| Lifetime | Framework services |
|---|---|
| Singleton | configuration, environment, paths, PSR-17 factories, logger, router and routing middlewares, pipeline, application, error handler, Problem Details, CORS, template renderer |
| Scoped | none: for the services of the application with a per-request state (unit of work, current user, ...) |
| Transient | the classes that are not registered: request handlers, middlewares, ... |
Each request is handled in its own scope: the middlewares and handlers piped or routed as class names are resolved from it, and the scope is disposed once the response is created.
When the script is run by a FrankenPHP worker (frankenphp_handle_request() exists and $_SERVER['FRANKENPHP_WORKER']
is set), the kernel is booted once, and the requests are handled in a loop. After each request, the singletons holding a
state are reset (Monolog buffers and processors, the request identifier, ...) and the garbage collector runs. The worker
stops after MAX_REQUESTS requests (unlimited by default) so that FrankenPHP restarts it.
Rule of thumb in a worker: never keep a request state in a singleton: make the service scoped, or reset it (implement
Piccolo\Container\Resettable, or register a hook with $builder->onReset()).
Testing an application
Piccolo\Testing\TestClient sends requests to the application as its HTTP server would: it is booted once per
client, then each request is handled in its own scope (as in a FrankenPHP worker). The requests are created with the
PSR-17 factories of the container, so the tests do not depend on the PSR-7 implementation.
use Piccolo\Testing\TestClient; test('creates a post', function () { $client = new TestClient(dirname(__DIR__, 2)); $client->get('/api/ping')->assertOk()->assertHeader('X-Request-Id'); $client->post('/api/v1/posts', ['title' => 'Hello'])->assertStatus(201)->assertJson(['title' => 'Hello']); $client->form('POST', '/login', ['email' => 'alice@example.com'])->assertStatus(302); expect($client->get('/api/v1/posts/1')->json())->toHaveKey('id'); });
get(), head(), options(), delete(), post(), put(), patch() (JSON body), json(), form(), request(),
and createRequest() + handle() for a request to complete (cookies, attributes, uploaded files). The assertions of
TestResponse (assertStatus(), assertOk(), assertHeader(), assertHeaderMissing(), assertJson(),
assertBodyContains()) need PHPUnit or Pest; status(), header(), body() and json() work with any test tool.
Tests
composer test # Pest composer analyse # PHPStan, level max
stubs/ContainerInterface.stub types PSR-11 for PHPStan as the container of Piccolo (get(Foo::class) is a Foo): an
application analysed with PHPStan can add it to the stubFiles of its configuration.