2017 © Pedro Peláez
 

library shutdownhandler

Shutdown Handler.

image

gielfeldt/shutdownhandler

Shutdown Handler.

  • Sunday, September 20, 2015
  • by gielfeldt
  • Repository
  • 1 Watchers
  • 1 Stars
  • 56 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Shutdown Handler

Build Status Test Coverage Scrutinizer Code Quality Code Climate, (*1)

Latest Stable Version Latest Unstable Version [Dependency Status][11] License Total Downloads, (*2)

[Documentation Status][12] [Documentation Status][12], (*3)

Installation

To install the ShutdownHandler library in your project using Composer, first add the following to your composer.json config file., (*4)

{
    "require": {
        "gielfeldt/shutdownhandler": "~1.0"
    }
}

Then run Composer's install or update commands to complete installation. Please visit the Composer homepage for more information about how to use Composer., (*5)

Shutdown handler

This shutdown handler class allows you to create advanced shutdown handlers, that can be manipulated after being created., (*6)

Motivation

  1. Destructors are not run on fatal errors. In my particular case, I needed a lock class that was robust wrt to cleaning up after itself. See "Example 2" below or examples/fatal.php for an example of this., (*7)

  2. PHP shutdown handlers cannot be manipulated after registration (unregister, execute, etc.)., (*8)

Example 1 - using Shutdown Handler

namespace Gielfeldt\ShutdownHandler\Example;

require 'vendor/autoload.php';

use Gielfeldt\ShutdownHandler\ShutdownHandler;

/**
 * Simple shutdown handler callback.
 *
 * @param string $message
 *   Message to display during shutdown.
 */
function myshutdownhandler($message = '')
{
    echo "Goodbye $message\n";
}

// Register shutdown handler to be run during PHP shutdown phase.
$handler = new ShutdownHandler('\Gielfeldt\ShutdownHandler\Example\myshutdownhandler', array('cruel world'));

echo "Hello world\n";

// Register shutdown handler.
$handler2 = new ShutdownHandler('\Gielfeldt\ShutdownHandler\Example\myshutdownhandler', array('for now'));

// Don't wait for shutdown phase, just run now.
$handler2->run();

Example 2 - Ensuring object destruction

namespace Gielfeldt\ShutdownHandler\Example;

require 'vendor/autoload.php';

use Gielfeldt\ShutdownHandler\ShutdownHandler;

/**
 * Test class with destructor via Gielfeldt\ShutdownHandler\ShutdownHandler.
 */
class MyClass
{
    /**
     * Reference to the shutdown handler object.
     * @var ShutdownHandler
     */
    protected $shutdown;

    /**
     * Constructor.
     *
     * @param string $message
     *   Message to display during destruction.
     */
    public function __construct($message = '')
    {
        // Register our shutdown handler.
        $this->shutdown = new ShutdownHandler(array(get_class($this), 'shutdown'), array($message));
    }

    /**
     * Run our shutdown handler upon object destruction.
     */
    public function __destruct()
    {
        $this->shutdown->run();
    }

    /**
     * Our shutdown handler.
     *
     * @param string $message
     *   The message to display.
     */
    public static function shutdown($message = '')
    {
        echo "Destroy $message\n";
    }
}

// Instantiate object.
$obj = new MyClass("world");

// Destroy object. The object's shutdown handler will be run.
unset($obj);

// Instantiate new object.
$obj = new MyClass("universe");

// Object's shutdown handler will be run on object's destruction or when PHP's
// shutdown handlers are executed. Whichever comes first.

For more examples see the examples/ folder., (*9)

Features

  • Unregister a shutdown handler
  • Run a shutdown handler prematurely
  • Improve object destructors by ensuring destruction via PHP shutdown handlers
  • Keyed shutdown handlers, allowing to easily deduplicate multiple shutdown handlers

Caveats

  1. Lots probably.

The Versions

20/09 2015

dev-master

9999999-dev https://github.com/gielfeldt/shutdownhandler

Shutdown Handler.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thomas Gielfeldt

handler shutdown destructor

20/09 2015

dev-develop

dev-develop https://github.com/gielfeldt/shutdownhandler

Shutdown Handler.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thomas Gielfeldt

handler shutdown destructor

20/09 2015

1.2.0

1.2.0.0 https://github.com/gielfeldt/shutdownhandler

Shutdown Handler.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thomas Gielfeldt

handler shutdown destructor

11/08 2015

dev-feature/documentation

dev-feature/documentation https://github.com/gielfeldt/shutdownhandler

Shutdown Handler.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thomas Gielfeldt

handler shutdown destructor

08/08 2015

1.1.0

1.1.0.0 https://github.com/gielfeldt/shutdownhandler

Shutdown Handler.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thomas Gielfeldt

handler shutdown destructor

07/08 2015

1.0.0

1.0.0.0 https://github.com/gielfeldt/shutdownhandler

Shutdown Handler.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thomas Gielfeldt

handler shutdown destructor