Download the PHP package wondernetwork/slim-kernel without Composer
On this page you can find all versions of the php package wondernetwork/slim-kernel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package slim-kernel
Wonder Slim Kernel
This package aims to provide a set of configuration helpers for Slim v4 microframework.
By using the KernelBuilder, one can easily bootstrap the Slim\App service,
along with a DI\Container dependency injection container, declaratively define
a bunch of services, register symfony console commands, middlewares, routes, etc.
See basic usage to get an overview how to use it.
Installation
Basic Usage
Declarative service definitions
As mentioned in the basic usage section, the KernelBuilder has a glob() method,
which accepts a list of glob patterns that will be used to build the container. Each
of the matched php files needs to return either of:
- An array or other iterable of
key => valuepairs. See PHP DI documentation for details - A
ServiceFactoryinstance (see below)
Example:
On Startup hooks
You might want some initialization code to run on each request and each invocation
of a CLI command. This is best place to setup some global error handlers, boot some
static properties, etc. To do so, pass an object implementing StartupHook to the
onStartup() method
Routes Startup Hook
WonderNetwork\SlimKernel\StartupHook\RouteStartupHook
If you would like to get your routes registered in a declarative manner,
use this startup hook to point to files containing your route definitions.
Each of the files matched by the glob pattern needs to return a closure,
which takes Slim\App, or more precisely Slim\Interfaces\RouteCollectorProxyInterface.
This means you can use it for more than routes: for example global middlewares.
The closures will be evaluated on a Slim Application fetched from the container.
Error handling
The default error handling middleware is added. It’s configured to silently log
errors with details, but do not display the details in the response. You can change
that behavour, for example if you’d like to have more verbose logging in certain
environments, by overriding the ErrorMiddlewareConfiguration service:
Beside the factory methods (either silent() or verbose()) you can also just create() and
set each individual setting using with*() and without*() methods.
Service Factory
WonderNetwork\SlimKernel\ServiceFactory
This represents a more advanced service definition than a simple array,
by passing in a ServiceBuilder instance. It’s helpful to use it when
you’d like to autowire a bunch of files matching a pattern, or handle
a list of configuration files with some post-processing.
You can also add service factories directly using the KernelBuilder::register()
method.
See the following list of provided Service Factories for inspiration:
Slim Applcation Service Factory
This service factory is built-in and gets registered automatically.
It defines the Slim\App service by using the PHP DI Slim Bridge.
Symfony Command Service Factory
WonderNetwork\SlimKernel\ServiceFactory\SymfonyConsoleServiceFactory
This service factory will find all files matching a specified glob pattern, autowire them in the container, and then register a Symfony console application with all these commands added to it.
[!NOTE] Make sure that your command classes are autoloaded using a valid PSR-4 configuration in your
composer.jsonfile
Convenience methods to access strongly typed input argumets
This package is aimed at accessing trusted payloads. In other words, it does not aim at validating an unknown payload, but rather asserts that the structure is correct, so that we can use semantic methods to get strong type guarantees. The package does not try to handle any situation -- except for basic type casting, it throws if it encounters some unexpected input.
-
Request Params: created from
RequestInterfaceand represent request body, query and server params. Each field is returned as an Array Accessor -
Input Params: created from
InputInterfaceand represent command line arguments and options. Each field is returned as an Array Accessor - Route Argument: created from
RequestInterfaceand represent the arguments matched by slim routing. Returns a single Array Accessor
Array Accessor
string(key, default = '')- gets the interpolated string value of given field
- returns default on missing and null values
- throws on non-scalar values
maybeString(key)see above, but returns null as the defaultrequireString(key)see above, but throws as the defaultint(key, default = 0)similar to string, casts numeric strings to intsmaybeInt()andrequireInt()similarly to string methodsbool(key)interpolates1and0to booleanmaybeBool()andrequireBool()see abovearray(key)- returns a mixed array on given key
- returns an empty array if key does not exist
- throws if key exists but does not explicitly contain an array
maybeArray(key)see above, but returns null by defaultat(key)- returns an array accessor representing a nested structure
- uses null object pattern, so returns an empty accessor if the key does not exist or is not an array
allString(),allInt(),allBool()- ensures all items of payload match a given type
- returns an array of scalars of that type (
string[],int[],bool[])
Fingers Crossed CLI handler
For all those pesky cron jobs, where on one hand you’d like to silence the output
because it causes noisy emails for no reason, but at the same time you don’t want
to lose context when something bad happens. Wrap your commands in a
FingersCrossedHandler like so:
The output will be silenced except for the following situatios:
- You increase the output verbosity using the
-vflag or thesetVerbosity()method - The command returns a non-zero exit code
- The command throws
In the former case, the output will be written in realtime, and in the two latter ones you can expect it writtein in bulk at the end.
Input parsing
In order to simplify input parsing, you can use the Symfony Serializer component to
automatically denormalize parsed request body or get params to a data transfer object
of your choice. In your controller, simply mark one of the parameters with #[Payload]
attribute and typehint it with your desired class:
If the serializer fails to denormalize the input, a HttpBadRequestException will be thrown with a semi-helpful message attached. If you require custom serializer setup, you can register your own instance using the following container key:
All versions of slim-kernel with dependencies
ext-json Version *
slim/slim Version ^4.14
slim/psr7 Version ^1.6
php-di/php-di Version ^7
php-di/slim-bridge Version ^3.4
symfony/console Version ^5.4 | ^6 | ^7
symfony/finder Version ^5.4 | ^6 | ^7
symfony/cache Version ^6 | ^7
symfony/messenger Version ^6 | ^7
symfony/property-access Version ^7.2
symfony/serializer Version ^7.2
phpstan/phpdoc-parser Version ^2.0
phpdocumentor/reflection-docblock Version ^5.6
wondernetwork/php-collection-library Version ^10.0
monolog/monolog Version ^3.10
symfony/event-dispatcher Version ^7.4