GithubHelp home page GithubHelp logo

carthage-software / elissa-bundle Goto Github PK

View Code? Open in Web Editor NEW
22.0 3.0 0.0 30 KB

Elissa Bundle by Carthage brings out-of-the-box PSR-7 and PSR-15 support to your Symfony project.

License: MIT License

PHP 100.00%
psr-15 psr-7 symfony symfony-bundle zero-config

elissa-bundle's Introduction

Elissa Bundle - PSR-7 & PSR-15 Made Easy!

Elissa Bundle by Carthage brings out-of-the-box PSR-7 and PSR-15 support to your Symfony project.

The package allows developers to write PSR-15 handlers and middleware, which are auto-tagged and ready to use as controllers.

It also provides ready access to all PSR-7 factories, and enables seamless integration of middleware from third-party vendor packages.

Installation ๐Ÿš€

Install the Elissa Bundle using Composer with the following command:

composer require carthage/elissa-bundle

Enabling the Bundle ๐ŸŽš

To enable the bundle in your Symfony project, add the following line in the config/bundles.php file:

return [
    //...
    Carthage\ElissaBundle\ElissaBundle::class => ['all' => true],
    //...
];

Usage ๐Ÿ’ผ

PSR-7 Factories ๐Ÿญ

The Elissa Bundle comes with autowired and pre-configured PSR-7 factories. You can conveniently create PSR-7 objects in your services without any extra configuration.

<?php

declare(strict_types=1);

namespace App\Service;

use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;

final class MyService
{
    public function __construct(
        private StreamFactoryInterface $streamFactory,
        private ResponseFactoryInterface $responseFactory,
        private RequestFactoryInterface $requestFactory,
        private ServerRequestFactoryInterface $serverRequestFactory,
        private UriFactoryInterface $uriFactory,
        private UploadedFileFactoryInterface $uploadedFileFactory,
    ) {}
}

PSR-15 Handlers ๐ŸŽ›

After installing and enabling the Elissa Bundle, writing PSR-15 handlers becomes a breeze. The handlers will automatically function as controllers in your Symfony project.

<?php

declare(strict_types=1);

namespace App\Handler;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\Routing\Annotation\Route;

final class HomeHandler implements RequestHandlerInterface
{
    public function __construct(
        private StreamFactoryInterface $streamFactory,
        private ResponseFactoryInterface $responseFactory,
    ) {}

    #[Route('/', name: 'home')]
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $body = $this->streamFactory->createStream('Hello World!');

        return $this->responseFactory->createResponse(200)
            ->withBody($body);
    }
}

PSR-15 Middleware ๐Ÿ› 

Just like handlers, you can write PSR-15 middleware, and they will automatically function.

<?php

declare(strict_types=1);

namespace App\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

final class MadeWithMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $response = $handler->handle($request);
        
        return $response->withHeader('X-Made-With', 'Love');
    }
}

Configurations โš™

The Elissa Bundle is designed to minimize configuration, but if you need to register additional PSR-15 middlewares from third-party vendors, you can use the elissa.middleware service tag.

# config/services.yaml

services:
    Some\External\ServiceMiddleware:
        tags:
            - { name: elissa.middleware }

Code Of Conduct ๐Ÿค

Our community is guided by a Code of Conduct, and we expect all contributors to respect it. See the CODE_OF_CONDUCT for more details.

Contributing ๐ŸŽ

The Elissa Bundle thrives on contributions from the open-source community. We value every contribution, no matter how small.

License ๐Ÿ“œ

The Elissa Bundle is distributed under the MIT License. See LICENSE for more information.


We hope you enjoy using the Elissa Bundle! For any queries or suggestions, don't hesitate to open an issue or submit a pull request. Happy coding!

elissa-bundle's People

Contributors

azjezz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.