opencredit/badgeos — pull request #268 by karnold (2014-04-14 18:14:01+00:00)
Title: Introduction of ORM and new logging subsystem

This pull request incorporates the composer package manager and autoloading subsystem to introduce the Capsule ORM class as well as Monolog to replace the existing core logging.

All new classes introduced exist in the BadgeOS namespace and are loaded via the PSR-4 spec.

This initial pull is meant primarily to be a proof of concept on how these tools can be implemented. It would be my recommendation that it starts a 2.x branch of BadgeOS and not get included into the existing 1.x branch as these tools can improve other areas of badgeos as well.

Capsule:
Capsule is an ORM and Schema management class for php. Along with basic ORM functionality it also introduces basic model management. Documentation is available at https://github.com/illuminate/database.

I've created an update hook in badgeos.php to create the table, and this can be extendable to account for version changes.

This is an example of creating a table with Capsule:
/**
* Create a log schema
*/
function badgeos_upgrade_2_0() {
Capsule::schema()->create('badgeos_logs', function($table) {
$table->increments('id');
$table->string('site_id')->nullable();
$table->string('action');
$table->longText('message');
$table->integer('object_id')->default(0)->nullable();
$table->integer('points_earned')->default(0)->nullable();
$table->integer('total_points')->default(0)->nullable();
$table->timestamp('timestamp');
$table->string('timezone');
$table->integer('user_id');
$table->timestamp('user_registered');
$table->string('zip')->nullable();
});

}

The initial implementation only deals with log entries as seen in includes/LogEntry.php and include/BadgeOSLogHandler.php.

While this currently only handles logs this library would be extremely useful for rewriting any code that manually queries the database as the query object does not exist in a global namespace. This will also further performance improvements as flat database tables can more easily be generated and managed.

Monolog:
Monolog is a logging subsystem that has been implemented to replace logging as wordpress posts. By default a factory class is assigned via $badgeos_settings['log_factory']. This factory class is implemented as includes/Log.php. And a custom dispatcher has been created to dispatch the data to a flat table structure.

Any custom plugin can override the default logging factory by implementing an override to the $badgeos_settings['log_factory'] setting. Further more any of the existing log dispatchers available can be utilized to route the data to the developers preferred data storage. Many different dispatchers already exist, and the api is very easily extended.

You can see an example of how the DMA extended the logging class to include elastic search as a data source at https://github.com/DallasMuseumArt/DMA-Friends/blob/master/wp-content/plugins/badgeos-dma-platform/includes/DmaLog.php