The best kernel for simple apps

Overview

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

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.

Framework Req/s
Sunflower 2.548
Symfony 6.0 1.819
Symfony 5.4 1.804
Slim 4 1.380
Mezzio 3 985
Laravel 8 421

Using 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

composer require nyholm/sunflower

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

A short example using HTTP and a simple switch-router. This example is using 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');
    }
};

Use with Bref

To create apps that works with Bref you will need the 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
 # serverless.yml

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

History

The Sunflower project was open sourced in 2021. The very first version of the project was created back in 2015. A few private applications was created around the concept of using Symfony's Dependency Injection component but not use the FrameworkBundle or HttpKernel.

The first public version of the project was SuperSlim. That version was a opinionated framework to show what the FrameworkBundle actually did for you behind the scenes. With some more private iterations and many more applications created, we finally removed all unnecessary things and ended up with just the one Kernel.

You might also like...
mTube is a simple video sharing platform built with Laravel.
mTube is a simple video sharing platform built with Laravel.

mTube is a simple video sharing platform built with Laravel. Create personal channel share videos online with friends and family.

A simple PHP library for complex monetary prices management

PHP Prices 💸 Version 2.x This new major version is shifting the package towards more flexibility and configuration possibilities in general. One of t

TinyFileManager is web based file manager and it is a simple, fast and small file manager with a single file, multi-language ready web application
TinyFileManager is web based file manager and it is a simple, fast and small file manager with a single file, multi-language ready web application

TinyFileManager is web based file manager and it is a simple, fast and small file manager with a single file, multi-language ready web application for storing, uploading, editing and managing files and folders online via web browser. The Application runs on PHP 5.5+, It allows the creation of multiple users and each user can have its own directory and a build-in support for managing text files with cloud9 IDE and it supports syntax highlighting for over 150+ languages and over 35+ themes.

Simple and lightweight OOP wrapper for PHP's low-level sockets extension (ext-sockets)

clue/socket-raw Simple and lightweight OOP wrapper for PHP's low-level sockets extension (ext-sockets). PHP offers two networking APIs, the newer stre

A simple, beautiful, mobile-first instant messaging web application backend build with ThinkPHP6 and Swoole.

OnChat A simple, beautiful, mobile-first instant messaging progressive web application build with ThinkPHP6 and Swoole. You can click here to view the

A simple, not so bad looking Minecraft Server's website template

Minecraft Server 官网模板 本仓库为 Minecraft 服务器官网模板,主要通过 Bootstrap 和 Argon 组件库实现 本项目基于 https://github.com/nyancatda/mcserverweb 二开 config.json参数说明 参数 说明 备注 s

Project of Simple Blog using: HTML, CSS, PHP, MYSQL, and BOOTSTRAP
Project of Simple Blog using: HTML, CSS, PHP, MYSQL, and BOOTSTRAP

Project-Stormwind Project of Simple Blog using: HTML, CSS, PHP, MYSQL, and BOOTSTRAP Functions : A personal blog about Blizzard and their work Main Th

A simple blog project I built when learning Laravel 8
A simple blog project I built when learning Laravel 8

Harmonify Blog Disclaimer: All users data previewed on the image are fake data generated with FakerPHP. Introduction Harmonify Blog is a simple blog p

Simple and Lightweight PHP Class & Methods Annotations Reader

README Simple and Lightweight PHP Class & Methods Annotations Reader Forked from eriknyk/Annotations 项目地址 github https://github.com/phppkg/annotations

Releases(0.2.1)
Owner
Tobias Nyholm
Symfony Core team, certified Symfony developer. Speaker, writer, podcaster. Maintainer for many awesome libraries.
Tobias Nyholm
eMarket Online Store. It is a free online store engine. Make the best online shop with us.

eMarket Online Store. It is a free online store engine. Make the best online shop with us. Join our Open Source community. Together we will make the best free e-commerce solution.

Alexander 54 Oct 22, 2022
The API & platform builder, build your apps 10x faster even more, it's open source & 100% free !

The API & platform builder, build your apps 10x faster, even more. It's open source & 100% free ! Try live demo Why badaso ? 100% FREE - No need for e

Uasoft 1k Jan 2, 2023
Laravue - a sensible starting point for single-page apps

Laravue - a sensible starting point for single-page apps Note: The vast majority of the functionality of this project has essentially been added into

null 222 Oct 7, 2021
A simple wrapper for PHP Intervention Library to provide a more simple interface and convenient way to convert images to webp

This package is a simple wrapper for PHP Intervention Library to provide a more simple interface and convenient way to convert images to webp - next generation format - extension, and resize them to render only needed sizes.

eyad hamza 18 Jun 28, 2022
Simple-cache - PHP FIG Simple Cache PSR

PHP FIG Simple Cache PSR This repository holds all interfaces related to PSR-16. Note that this is not a cache implementation of its own. It is merely

PHP-FIG 8k Jan 3, 2023
Simple-podcast-generator - 👉 A very simple way to host your podcast files and generate the RSS Podcast feed 🎙

Podcast RSS Feed Generator A very simple way to host your podcast files and generate the RSS Podcast feed ?? ?? Contents Summary Requirements Installa

♚ PH⑦ de Soria™♛ 11 Dec 2, 2022
Simple web interface to manage Redis databases.

phpRedisAdmin phpRedisAdmin is a simple web interface to manage Redis databases. It is released under the Creative Commons Attribution 3.0 license. Th

Erik Dubbelboer 3k Dec 31, 2022
Instagram simple version.

.feed Getting started Clone project Go to the folder Install composer composer install Install npm package npm install Copy and edit .env file from .e

krido 4 Jan 22, 2022
Koel is a simple web-based personal audio streaming service written in Vue and Laravel

Koel (also stylized as koel, with a lowercase k) is a simple web-based personal audio streaming service written in Vue on the client side and Laravel on the server side. Targeting web developers, Koel embraces some of the more modern web technologies – CSS grid, audio, and drag-and-drop API to name a few – to do its job.

Koel 14.3k Jan 4, 2023