PHPackages                             wondernetwork/slim-kernel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. wondernetwork/slim-kernel

ActiveLibrary

wondernetwork/slim-kernel
=========================

v2.5.0(1mo ago)18.2k↑11.1%1MITPHPPHP &gt;=8.3CI passing

Since Oct 23Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/WonderNetwork/slim-kernel)[ Packagist](https://packagist.org/packages/wondernetwork/slim-kernel)[ RSS](/packages/wondernetwork-slim-kernel/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (36)Versions (27)Used By (1)

Wonder 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](#basic-usage) to get an overview how to use it.

Installation
------------

[](#installation)

```
composer require wondernetwork/slim-kernel

```

Basic Usage
-----------

[](#basic-usage)

```
use WonderNetwork\SlimKernel\KernelBuilder;
use WonderNetwork\SlimKernel\ServiceFactory\SymfonyConsoleServiceFactory;
use WonderNetwork\SlimKernel\StartupHook\RoutesStartupHook;

// configure the container:
$container = KernelBuilder::start(
        // all the paths will be calculated relative to this one
        __DIR__.'/..',
    )
    // just add inline definitions if you’d like
    ->add([
        'environment' => $environment
    ])
    // specify a directory or directories to search definition files in
    ->glob(
        // include all *.php files in some `services` folder
        __DIR__.'/../services/*.php',
        // overrides for specific environment, given we have $environment defined:
        __DIR__."/../services/{$environment}/*.php",
    )
    // register more advanced definition providers implementing the ServiceFactory interface
    ->register(
        new SymfonyConsoleServiceFactory(
            // for example, this provider registers a symfony application
            // and adds commands matching this glob pattern:
            __DIR__.'/../src/Cli/**/*Command.php',
        ),
    )
    ->onStartup(
        // automatically add routes
        new RoutesStartupHook(__DIR__.'/../app/routes/*.php'),
    )
    // only pass a path for prod environments, null otherwise
    ->useCache(__DIR__.'/../.cache')
    ->build();

// get the slim application from the container
$app = $container->get(Slim\App::class)
$app->run();
```

Declarative service definitions
-------------------------------

[](#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 => value` pairs. See [PHP DI documentation](https://php-di.org/doc/php-definitions.html#syntax) for details
- [A `ServiceFactory` instance (see below)](#service-factory)

Example:

```
// given it’s configured:
// $kernelBuilder->glob('app/services/*.php');
// next, in app/services/some.php:
use Psr\Container\ContainerInterface;

return [
  'foo.scalar.dependency' => 10,
  FooService::class => fn (ContainerInterface $container) => new FooService(
    $container->get(FooDependency::class),
    $container->get('foo.scalar.dependency'),
  ),
];
```

On Startup hooks
----------------

[](#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

[](#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.

```
$kernelBuilder->onStartup(
    new \WonderNetwork\SlimKernel\StartupHook\RoutesStartupHook(
        __DIR__.'/../routes/*.php',
    ),
);
// app/routes/some.php
use Psr\Http\Message\ResponseInterface;
return static function (Slim\App $app) {
  $app->get('/hello/{message}', function (ResponseInterface $response, string $message) {
    return $response->withHeader("X-Hello", $message);
  });
}
```

Error handling
--------------

[](#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:

```
// somewhere in app/services/test/errors.php
use WonderNetwork\SlimKernel\SlimExtension\ErrorMiddlewareConfiguration as Configuration;
return [
   Configuration::class => static fn () => Configuration::verbose();
];
```

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
---------------

[](#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

[](#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

[](#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.json` file

```
// app/services/cli.php
return new WonderNetwork\SlimKernel\ServiceFactory\SymfonyConsoleServiceFactory(
    // glob to find command files
    __DIR__.'/../../src/Cli/**/*Command.php',
    // name for your symfony console application
    'acme v1.0',
);
```

Convenience methods to access strongly typed input argumets
-----------------------------------------------------------

[](#convenience-methods-to-access-strongly-typed-input-argumets)

```
// HTTP
$requestParams = WonderNetwork\SlimKernel\Http\RequestParams::of($serverRequest);
$requestParams->post->requireString('userId');
$requestParams->query->int('limit', 10);
$requestParams->server->bool('HTTPS');
// route arguments are all string, but you can still cast them to ints
$routeArguments = WonderNetwork\SlimKernel\Http\RouteArgument::of($serverRequest);
$routeArguments->string('token');
$routeArguments->int('limit', 10);
// CLI
$cliParams = WonderNetwork\SlimKernel\Cli\InputParams::ofInput($input);
$cliParams->options
$cliParams->arguments
```

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 `RequestInterface` and represent request body, query and server params. Each field is returned as an [Array Accessor](#array-accessor)
- *Input Params*: created from `InputInterface` and represent command line arguments and options. Each field is returned as an [Array Accessor](#array-accessor)
- *Route Argument*: created from `RequestInterface` and represent the arguments matched by slim routing. Returns a single [Array Accessor](#array-accessor)

### 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 default
- `requireString(key)` see above, but throws as the default
- `int(key, default = 0)` similar to string, casts numeric strings to ints
- `maybeInt()` and `requireInt()` similarly to string methods
- `bool(key)` interpolates `1` and `0` to boolean
- `maybeBool()` and `requireBool()` see above
- `array(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 default
- `at(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
---------------------------

[](#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:

```
public function execute(InputInterface $input, OutputInterface $output): int {
    return FingersCrossedHandler::of($input, $output)->run(
        function (InputParams $input, FingersCrossedOutput $output) {
            $output->write("Hello world!")
            return 1;
        },
    );
}
```

The output will be silenced except for the following situatios:

- You increase the output verbosity using the `-v` flag or the `setVerbosity()` 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
-------------

[](#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:

```
public function __invoke(#[WonderNetwork\SlimKernel\Http\Serializer\Payload] MyDto $input) {}
```

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:

```
\WonderNetwork\SlimKernel\ServiceFactory\SlimServiceFactory::INPUT_DENORMALIZER

```

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance89

Actively maintained with recent releases

Popularity26

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 96.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~22 days

Recently: every ~7 days

Total

24

Last Release

56d ago

Major Versions

v1.7.0 → v2.0.02025-01-28

PHP version history (2 changes)v1.0.0PHP ^7.4|^8.0

v2.0.0PHP &gt;=8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/8572223899390b494d1282b615009bc2eea5ef1cd6bfc22f6e6b175034dadfe0?d=identicon)[mlebkowski](/maintainers/mlebkowski)

![](https://www.gravatar.com/avatar/341a28d185d58818b433a0447f4f97df322bd172f528879d24febfc075df8d35?d=identicon)[preinheimer](/maintainers/preinheimer)

---

Top Contributors

[![mlebkowski](https://avatars.githubusercontent.com/u/848731?v=4)](https://github.com/mlebkowski "mlebkowski (61 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/wondernetwork-slim-kernel/health.svg)

```
[![Health](https://phpackages.com/badges/wondernetwork-slim-kernel/health.svg)](https://phpackages.com/packages/wondernetwork-slim-kernel)
```

###  Alternatives

[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
