alvinios/miel

dev-main 2024-08-01 12:34 UTC

This package is auto-updated.

Last update: 2024-10-31 13:04:55 UTC


README

About The Project

Miel is an object-oriented PHP web development micro-framework (MIEL stands for Micro + Elegant, it also means "honey" in French). It' intend is to be designed based on elegant objects principles of Yegor Bugayenko. It means :

  • No mutable classes
  • No nulls
  • No type-checking or reflexion
  • No public static methods or constants
  • No configuration files
  • No DI container

It was inspired by the framework Takes For being operational it requires PSR-7 and PSR-17 implementations/libraries of your choice. For example Guzzle PSR-7

Getting Started

composer require ..

Quick Start

Create this index.php file:

use Alvinios\Miel\Http\Emit;
use Alvinios\Miel\Response\Text;
use Alvinios\Miel\Fork\{Regex, Routes};
use GuzzleHttp\Psr7\{HttpFactory,ServerRequest};

(new Emit())(
    (new Routes(
        new Regex('/', new Text('Hello world!'))
    ))->response(ServerRequest::fromGlobals(), new HttpFactory())
);

Cd to your index.php folder and run php local server

php -S localhost:8000

A Bigger Example

use Alvinios\Miel\Http\Emit;
use Alvinios\Miel\Endpoint\Base;
use Alvinios\Miel\Request\WithRegex;
use Alvinios\Miel\Response\{Json, Response, Text, Twig}; 
use Alvinios\Miel\Fork\{Routes, Regex, Methods};
use GuzzleHttp\Psr7\{HttpFactory, ServerRequest};
use Psr\Http\Message\ServerRequestInterface;

(new Emit())(
  (new Routes(
        new Regex('/', new Text('Hello world!')),
        new Regex(
            '/users/(?P<id>[\d]+)',
            new Methods(
                ['post'],
                new class() extends Base {
                    public function act(ServerRequestInterface $request): Response {
                        return new Json(
                            json_decode($request->getBody()->getContents())
                        );
                    }
                }         
            ),
            new Methods(
                ['get'],
                new class() extends Base {
                    public function act(ServerRequestInterface|WithRegex $request): Response {
                        return new Text(sprintf(
                            '<html><body>User %s</body></html>',
                            $request->regex()->group('id')
                        ));
                    }
                }      
            )
        )
  ))->response(ServerRequest::fromGlobals(), new HttpFactory())
);

Generators

Routes can be composed as variadic argument of Routes or with Generators using Append wrapper.

use Alvinios\Miel\Response\{Text, Twig}; 
use Alvinios\Miel\Fork\{Append, Routes, Regex};

 new Routes(
     new Append(
         call_user_func(function() : \Iterator {
             yield new Regex('^(/|/home)$', new Twig($this->twig, 'index.html.twig', []));
         }),
         call_user_func(function() : \Iterator {
             yield new Regex('/foo', new Text('Foo'));
             yield new Regex('/bar', new Text('Bar'));
         })
     )
)

Middleware Support

You can shield a route/routes behind PSR-15 Middleware(s). This is how it can be done:

use Alvinios\Miel\Response\Text; 
use Alvinios\Miel\Fork\{Fork, Shield};
use Psr\Http\Server\MiddlewareInterface;

new Shield(
    new Regex('/foo', new Text('Behind middleware')),
    new class() implements MiddlewareInterface {
       ...
    },
    new class() implements MiddlewareInterface {
       ...
    }
)

Shields can be nested.

Note

Today it is in a conceptual state and has not been tested in production environment.

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)