Applications

Table of Contents

  1. Introduction
  2. Kernels
  3. Tasks
    1. Pre-Start Tasks
    2. Start Task
    3. Post-Start Tasks
    4. Pre-Shutdown Tasks
    5. Shutdown Task
    6. Post-Shutdown Tasks

Introduction

An Opulence application is started up through the Application class. You can register pre-start, post-start, pre-shutdown, and post-shutdown tasks to be run on every request. Once the application is started, a Kernel is instantiated and actually handles the request.

Kernels

A kernel is something that takes input, performs processing on it, and returns output. In Opulence, there are two kernels:

  1. Opulence\Framework\Http\Kernel
  2. Opulence\Console\Kernel

Having these two kernels allows Opulence to function as both a traditional HTTP web application and a console application.

Tasks

To start and shutdown an application, simply call the start() and shutDown() methods, respectively, on the application object. If you'd like to do some tasks before or after startup, you may do so with the Opulence\Applications\Tasks\Dispatchers\TaskDispatcher, which is injected into the Application object. Tasks are handy places to do any setting up that your application requires or any housekeeping after start/shutdown.

Pre-Start Tasks

Pre-start tasks are performed before the application is started.

use Opulence\Applications\Tasks\TaskTypes;

$dispatcher->registerTask(TaskTypes::PRE_START, function () {
    error_log('Application issued start command at ' . date('Y-m-d H:i:s'));
});

Start Task

If you'd like to perform a certain task after all the pre-start tasks have been completed, pass in a callable to the start() method. This is commonly used to instantiate a Kernel and handle a request. If the task returns anything, then start() returns that value. Otherwise, it returns null.

$application->start(function () {
    error_log('Application actually started at ' . date('Y-m-d H:i:s'));
});

Note: Passing a callable to start() is optional.

Post-Start Tasks

Post-start tasks are performed after the application has started.

use Opulence\Applications\Tasks\TaskTypes;

$dispatcher->registerTask(TaskTypes::POST_START, function () {
    error_log('Application finished starting at ' . date('Y-m-d H:i:s'));
});

Pre-Shutdown Tasks

Pre-shutdown tasks are performed before the application shuts down.

use Opulence\Applications\Tasks\TaskTypes;

$dispatcher->registerTask(TaskTypes::PRE_SHUTDOWN, function () {
    error_log('Application issued shutdown command at ' . date('Y-m-d H:i:s'));
});

Shutdown Task

If you'd like to perform a certain task after all the pre-shutdown tasks have been completed, pass in a callable to the shutDown() method. If the task returns anything, then shutDown() returns that value. Otherwise, it returns null.

$application->shutDown(function () {
    error_log('Application actually shut down at ' . date('Y-m-d H:i:s'));
});

Note: Passing a callable to shutDown() is optional.

Post-Shutdown Tasks

Post-shutdown tasks are performed after the application has shut down.

use Opulence\Applications\Tasks\TaskTypes;

$dispatcher->registerTask(TaskTypes::POST_SHUTDOWN, function () {
    error_log('Application finished shutting down at ' . date('Y-m-d H:i:s'));
});