Giter Club home page Giter Club logo

utils's Introduction

middlewares/utils

Latest Version on Packagist Software License Build Status Quality Score Total Downloads SensioLabs Insight

Common utilities used by the middlewares' packages:

Factory

Used to create psr-7 instances of ServerRequestInterface, ResponseInterface, StreamInterface and UriInterface. Detects automatically Diactoros, Guzzle and Slim but you can register a different factory using the http-interop/http-factory interface.

use Middlewares\Utils\Factory;

$request = Factory::createServerRequest();
$response = Factory::createResponse();
$stream = Factory::createStream();
$uri = Factory::createUri('http://example.com');

//Register other factory
Factory::setResponseFactory(new FooResponseFactory());

$fooResponse = Factory::createResponse();

Dispatcher

Minimalist PSR-15 compatible dispatcher. Used for testing purposes.

use Middlewares\Utils\Factory;
use Middlewares\Utils\Dispatcher;

$dispatcher = new Dispatcher([
    new Middleware1(),
    new Middleware2(),
    new Middleware3(),
    function ($request, $next) {
        $response = $next->handle($request);
        return $response->withHeader('X-Foo', 'Bar');
    }
]);

$response = $dispatcher->dispatch(Factory::createServerRequest());

CallableHandler

To resolve and execute a callable. It can be used as a middleware, server request handler or a callable:

use Middlewares\Utils\CallableHandler;

$callable = new CallableHandler(function () {
    return 'Hello world';
});

$response = $callable();

echo $response->getBody(); //Hello world

The constructor accepts the following arguments:

  • mixed $callable It can be callable or a string that can be resolved as callable, for example MyClass::method.
  • array $arguments Extra arguments passed when it's invoked.
  • CallableResolverInterface $resolver Used to resolve the $callable argument to generate something really callable. Currently there are two available resolvers: ReflectionResolver (used by default) and ContainerResolver (to use a PSR-11 container)

Helpers

A collection of helpers for psr-7 message manipulation. It contains the following methods:

Helpers::fixContentLength(MessageInterface $response): MessageInterface

Fixes the Content-Length header based in the body size.


Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.

The MIT License (MIT). Please see LICENSE for more information.

utils's People

Contributors

oscarotero avatar sagikazarmark avatar

Watchers

James Cloos avatar David Négrier 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.