Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
Removing trait usage for PHP 5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed May 30, 2013
1 parent e64ae2a commit a2dd42e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Redis service class
* Abstract Service class
*
* PHP version 5
*
Expand All @@ -11,56 +11,67 @@
* @copyright Copyright 2012, Wan Qi Chen <[email protected]>
* @link http://resqueboard.kamisama.me
* @package resqueboard
* @subpackage resqueboard.lib
* @subpackage resqueboard.lib.service
* @since 1.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

namespace ResqueBoard\Lib\Service;

/**
* Redis service class
* Abstract Service class
*
* @subpackage resqueboard.lib.service
* @since 1.0.0
* Implements function to log all services activities
*
* @subpackage resqueboard.lib.service
* @since 2.0.0
* @author Wan Qi Chen <[email protected]>
*/
trait Loggable
abstract class AbstractService
{
protected static $_logs = array();

protected static $_totalTime = 0;
protected static $_totalTime = array();

protected static $_totalQueries = 0;
protected static $_totalQueries = array();

protected static $_maxLogs = 200;

public static $serviceInstance = array();

public function __call($name, $args)
{
$t = microtime(true);
$result = call_user_func_array(array(self::$serviceInstance, $name), $args);
$result = call_user_func_array(array(self::$serviceInstance[get_called_class()], $name), $args);
$queryTime = round((microtime(true) - $t) * 1000, 2);
self::logQuery(
array(
'command' => strtoupper($name) . ' ' . multiImplode(' ', $args),
'time' => $queryTime
)
);
self::$_totalTime += $queryTime;
self::$_totalQueries++;
self::$_totalTime[get_called_class()] += $queryTime;
self::$_totalQueries[get_called_class()]++;
return $result;
}

protected function bootstrap()
{
self::$_totalTime[get_called_class()] = 0;
self::$_totalQueries[get_called_class()] = 0;
self::$_logs[get_called_class()] = array();
}

protected static function logQuery($log)
{
$trace = debug_backtrace();

$log['trace']['file'] = $trace[1]['file'];
$log['trace']['line'] = $trace[1]['line'];

self::$_logs[] = $log;
if (count(self::$_logs) > self::$_maxLogs) {
array_shift(self::$_logs);
self::$_logs[get_called_class()][] = $log;
if (count(self::$_logs[get_called_class()]) > self::$_maxLogs) {
array_shift(self::$_logs[get_called_class()]);
}
}

Expand All @@ -72,13 +83,11 @@ protected static function logQuery($log)
public static function getLogs()
{
return array(
'count' => self::$_totalQueries,
'time' => self::$_totalTime,
'logs' => self::$_logs
'count' => self::$_totalQueries[get_called_class()],
'time' => self::$_totalTime[get_called_class()],
'logs' => self::$_logs[get_called_class()]
);
}


}

function multiImplode($glue, $pieces)
Expand Down
10 changes: 4 additions & 6 deletions src/ResqueBoard/Lib/Service/Cube.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,18 @@
* @since 1.0.0
* @author Wan Qi Chen <[email protected]>
*/
class Cube
class Cube extends AbstractService
{
use Loggable;

public static $instance = null;

public static $serviceInstance = null;

protected $settings = array();

public function __construct($settings)
{
parent::bootstrap();

$this->settings = $settings;
self::$serviceInstance = $this;
parent::$serviceInstance[get_class()] = $this;
}

public static function init($settings)
Expand Down
16 changes: 7 additions & 9 deletions src/ResqueBoard/Lib/Service/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,26 @@
* @since 1.0.0
* @author Wan Qi Chen <[email protected]>
*/
class Mongo
class Mongo extends AbstractService
{
use Loggable;

public static $instance = null;

public static $serviceInstance = null;

public function __construct($settings)
{
parent::bootstrap();

$t = microtime(true);
self::$serviceInstance = new \Mongo($settings['host'] . ':' . $settings['port']);
parent::$serviceInstance[get_class()] = new \Mongo($settings['host'] . ':' . $settings['port']);

$queryTime = round((microtime(true) - $t) * 1000, 2);
self::logQuery(
parent::logQuery(
array(
'command' => 'CONNECTION to ' . $settings['host'] . ':' . $settings['port'],
'time' => $queryTime
)
);
self::$_totalTime += $queryTime;
self::$_totalQueries++;
parent::$_totalTime[get_class()] += $queryTime;
parent::$_totalQueries[get_class()]++;
}

public static function init($settings)
Expand Down
20 changes: 9 additions & 11 deletions src/ResqueBoard/Lib/Service/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@
* @since 1.0.0
* @author Wan Qi Chen <[email protected]>
*/
class Redis
class Redis extends AbstractService
{
use Loggable;

public static $instance = null;

public static $serviceInstance = null;

public function __construct($settings)
{
parent::bootstrap();

$t = microtime(true);
$redis = new \Redis();
$redis->connect($settings['host'], $settings['port']);
Expand All @@ -43,7 +41,7 @@ public function __construct($settings)
$redis->setOption(\Redis::OPT_PREFIX, $settings['prefix'] . ':');
}

self::$serviceInstance = $redis;
parent::$serviceInstance[get_class()] = $redis;

$queryTime = round((microtime(true) - $t) * 1000, 2);
self::logQuery(
Expand All @@ -52,8 +50,8 @@ public function __construct($settings)
'time' => $queryTime
)
);
self::$_totalTime += $queryTime;
self::$_totalQueries++;
self::$_totalTime[get_class()] += $queryTime;
self::$_totalQueries[get_class()]++;
}

public static function init($settings)
Expand All @@ -68,7 +66,7 @@ public static function init($settings)
public function pipeline($commands, $type = \Redis::PIPELINE)
{
$t = microtime(true);
$redisPipeline = self::$serviceInstance->multi($type);
$redisPipeline = parent::$serviceInstance[get_class()]->multi($type);
foreach ($commands as $command) {
call_user_func_array(array($redisPipeline, $command[0]), (array)$command[1]);
}
Expand All @@ -80,8 +78,8 @@ public function pipeline($commands, $type = \Redis::PIPELINE)
'time' => $queryTime
)
);
self::$_totalTime += $queryTime;
self::$_totalQueries++;
self::$_totalTime[get_class()] += $queryTime;
self::$_totalQueries[get_class()]++;

return $results;
}
Expand Down

0 comments on commit a2dd42e

Please sign in to comment.