amorvan/responders-bundle

A bundle provide many responders according ADR architecture

v4.1.0 2021-03-17 14:14 UTC

README

  • CircleCI Status : Last version : CircleCI

Description

A library to use many responders according ADR architecture.

Installation

  • Require library
composer require amorvan/responders-bundle
  • For Symfony 3.* without Flex:

Adding bundle to app/AppKernel.php

//app/AppKernel.php
$bundles = [
        ...,
        new Morvan\Bundle\RespondersBundle\MorvanRespondersBundle(),
        new AppBundle\AppBundle(),
    ];
  • For Symfony 3.4 with Flex, Symfony 4.* or Symfony 5:

Bundle is automatically add to config/bundles.php. Check the bundle is allow for all environment.

//config/bundles.php
<?php

return [
    ...,
    Morvan\Bundle\RespondersBundle\MorvanRespondersBundle::class => ['all' => true],
];

Usage

  • All responders are autowiring.
  • To use one of these responders, inject responder and use the responder's with __invoke` method and pass the correct parameters.

ViewResponder

Description

This responder used to return a response with a view build with twig.

Usage

  • Example inside Symfony, consider following method for an action :
use Morvan\Bundle\RespondersBundle\Responders\ViewResponder;

public function listArticles(ViewResponder $viewResponder)
{
    return $viewResponder(
        'list.html.twig',
        [
            ".idea/articles" => $articles,
        ]
    );
}

RedirectResponder

Description

This responder used to return a redirect response with route parameters

Usage

  • Example inside Symfony, consider following method for an action :
use Morvan\Bundle\RespondersBundle\Responders\RedirectResponder;

public function addArticle(RedirectResponder $redirectResponder)
{
    return $redirectResponder(
       'show_article',
       [
            'id' => $article->getId(),
       ]
    );
}

JsonResponder

Description

This responder used to return a json response according many parameters

Usage

  • Example inside Symfony, consider following method for an action :
use Morvan\Bundle\RespondersBundle\Responders\JsonResponder;

public function getArticle(JsonResponder $jsonResponder)
{
    return $jsonResponder(
       $datas
    );
}

FileResponder

Description

This responder used to return a file.

Usage

  • Example inside Symfony, consider following method for an action :
use Morvan\Bundle\RespondersBundle\Responders\FileResponder;

public function getArticle(FileResponder $jsonResponder)
{
//$pathToFile accept string or SplFileObject
//$fileName optionnal parameter
    return $jsonResponder(
       $pathToFile,
       $fileName
    );
}

Quality

Many tools used for quality.

  • PHPCS : PSR 1 & 12.
  • PHPStan
  • All builds passed by CircleCI.