2017 © Pedro Peláez
 

library laravel-finalizer

Simple package with helpers to handle execution after closed connections

image

tuupke/laravel-finalizer

Simple package with helpers to handle execution after closed connections

  • Wednesday, November 22, 2017
  • by tuupke
  • Repository
  • 2 Watchers
  • 0 Stars
  • 1,505 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 21 % Grown

The README.md

Introduction

Laravel's implementation of finalizers (through middleware) are still synchronous with the request and will therefor increase the response-time of your application. There are many use-cases - logging for instance - for which this is not desired., (*1)

Normally speaking, standard Laravel practices dictate that a job should be used in these cases. When this is overkill, this package can be used. It provides a small wrapper around PHP's register_shutdown_function, to help with these use-cases., (*2)

Installation

Installation of this packages is done through composer., (*3)

composer require "tuupke/laravel-finalizer", (*4)

Then open config/app.php and append to the providers array:, (*5)

Tuupke\Finalizer\FinalizerServiceProvider::class,, (*6)

and add to the aliasses array:, (*7)

'Finalizer' => Tuupke\Finalizer\FinalizerFacade::class,, (*8)

Usage

From somewhere within your application you can now call: Finalizer::register($closure)) with $closure some closure executing some action. Optionally, you can provide an integer as the second parameter, which will server as the priority. The lower this priority, the earlier it is executed. When 2 closures are registered with the same priority. The closure which was registered first, will be executed first., (*9)

Example

A minimalist example which stores some contents in a file is listed below. Note that this should not be used in an actual application. When logging is required, use the Log facade., (*10)

Finalizer::register(function(){
    file_put_contents('/tmp/finalizer-test', "Second String", FILE_APPEND);
}, 2);

Finalizer::register(function(){
    file_put_contents('/tmp/finalizer-test', "First String\n", FILE_APPEND);
}, 1);

After a request has been executed which contained this code-block, the contents of /tmp/finalizer-test is:, (*11)

First String
Second String

Use in non-Laravel applications

Strictly speaking, this package can also be used in non-Laravel PHP applications which use composer. But it is not recommended and defeats the purpose of this package. If you want, you can still use it. A minimal example is provided below., (*12)

// include composer autoload
require 'vendor/autoload.php';

// import the Finalizer Class
use Tuupke\Finalizer;

// Create a new instance of the Finalizer. The class itself does not provide late static binding (yet).
$finalizer = new Finalizer;

// register some closure.
$finalizer->register(function(){
    ... <YOUR CODE GOES HERE> ...
});

The Versions

22/11 2017

dev-master

9999999-dev

Simple package with helpers to handle execution after closed connections

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

by Mart Pluijmaekers

22/11 2017

1.0.1

1.0.1.0

Simple package with helpers to handle execution after closed connections

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

by Mart Pluijmaekers

28/11 2016

1.0.0

1.0.0.0

Simple package with helpers to handle execution after closed connections

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

by Mart Pluijmaekers