PHPackages                             nyholm/nano-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. [Framework](/categories/framework)
4. /
5. nyholm/nano-kernel

Abandoned → [nyholm/sunflower](/?search=nyholm%2Fsunflower)Library[Framework](/categories/framework)

nyholm/nano-kernel
==================

The best Kernel for simple applications.

0.2.1(4y ago)20201MITPHPPHP &gt;=7.4

Since Aug 31Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Nyholm/sunflower)[ Packagist](https://packagist.org/packages/nyholm/nano-kernel)[ GitHub Sponsors](https://github.com/Nyholm)[ RSS](/packages/nyholm-nano-kernel/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (3)Dependencies (6)Versions (7)Used By (0)

Sunflower
=========

[](#sunflower)

Sunflower is a super small application kernel that is used to build a dependency injection container. This kernel is useful for microservices and applications that dont use HTTP. Say; reading from a queue or application invoked by AWS Lambda.

With this kernel you can use normal Symfony service definition with auto wiring and all. It even supports Symfony bundles!

The main difference from using `symfony/http-kernel` and Symfony FrameworkBundle is that Sunflower does not use `symfony/event-dispatcher`, `symfony/console`, `symfony/security`, `symfony/cache` and `symfony/router`.

Performance
-----------

[](#performance)

Below is a table of requests per second using a "hello world" application with different frameworks. The exact numbers are not relevant, they depend on the machine the tests was running on. But one should consider how the numbers change between frameworks since all test ran on the same machine.

FrameworkReq/sSunflower2.548Symfony 6.01.819Symfony 5.41.804Slim 41.380Mezzio 3985Laravel 8421Using a "hello world" comparison has some drawbacks. It does show performance for small applications with only a few hundreds lines of code, but it does not tell how large applications preform. It also does not give you any indication how fast you can write and maintain your application.

The table above is interesting if you are planning to build a small microservice that are similar to "hello world". Using the Sunflower Kernel is also very interesting if you are familiar with Symfony dependency injections, config and third party bundles.

Install
-------

[](#install)

```
composer require nyholm/sunflower

```

Use
---

[](#use)

```
// src/Kernel.php
namespace App;

use Nyholm\SunflowerKernel;

class Kernel extends SunflowerKernel
{
   /**
    * Optionally override the configureContainer()
    */
   protected function configureContainer(ContainerConfigurator $container): void
    {
        $container->import('../config/{packages}/*.yaml');
        $container->import('../config/{packages}/'.$this->environment.'/*.yaml');
        $container->import('../config/{packages}/'.$this->environment.'/*.php');

        if (\is_file(\dirname(__DIR__).'/config/services.yaml')) {
            $container->import('../config/services.yaml');
            $container->import('../config/{services}_'.$this->environment.'.yaml');
        } else {
            $container->import('../config/{services}.php');
        }
    }
}
```

```
use App\Kernel;
use App\Service\MyService;

require_once dirname(__DIR__).'/vendor/autoload.php';

$kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
$kernel->getContainer()->get(MyService::class)->run();
```

Use with HTTP
-------------

[](#use-with-http)

A short example using HTTP and a simple switch-router. This example is using [runtime/psr-nyholm](https://github.com/php-runtime/psr-nyholm).

```
// public/index.php

use Nyholm\Psr7;

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return function (array $context) {
    $kernel = new \App\Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
    $container = $kernel->getContainer();

    // This is an example router
    $urlPath = $context['REQUEST_URI'];
    switch ($urlPath) {
        case '/':
        case '':
            // This is an RequestHandlerInterface
            return $container->get(\App\Controller\Startpage::class);
        case '/foobar':
            return $container->get(\App\Controller\Foobar::class);
        default:
            return new Psr7\Response(404, [], 'The route does not exist');
    }
};
```

```
# config/services.yaml

services:
    _defaults:
        autowire: true
        autoconfigure: true

    _instanceof:
        Psr\Http\Server\RequestHandlerInterface:
            public: true

    App\:
        resource: '../src/'
        exclude:
            - '../src/Kernel.php'
```

Use with Bref
-------------

[](#use-with-bref)

To create apps that works with [Bref](https://bref.sh/) you will need the [runtime/bref](https://github.com/php-runtime/bref) package. Create microservices, SQS readers or react to S3 events etc.

```
// src/Kernel.php

namespace App;

use Nyholm\SunflowerKernel;

class Kernel extends SunflowerKernel
{
    public function isLambda(): bool
    {
        return false !== \getenv('LAMBDA_TASK_ROOT');
    }

    public function getCacheDir(): string
    {
        if ($this->isLambda()) {
            return '/tmp/cache/'.$this->environment;
        }

        return parent::getCacheDir();
    }

    public function getLogDir(): string
    {
        if ($this->isLambda()) {
            return '/tmp/log/';
        }

        return parent::getLogDir();
    }

    public function getProjectDir(): string
    {
        return \dirname(__DIR__);
    }
}
```

```
// bin/container.php

use App\Kernel;

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return function (array $context) {
    $kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);

    return $kernel->getContainer();
};
```

```
# config/services.yaml

services:
    _defaults:
        autowire: true
        autoconfigure: true

    _instanceof:
        Bref\Event\Handler:
            public: true

    App\:
        resource: '../src/'
        exclude:
            - '../src/Kernel.php'
```

```
 # serverless.yml

  functions:
      app:
          handler: bin/container.php:App\Service\MyHandler
```

Use with CLI
------------

[](#use-with-cli)

```
// bin/console
#!/usr/bin/env php
