Created
August 15, 2014 15:28
-
-
Save dhrrgn/c2dad1906073ff82ba29 to your computer and use it in GitHub Desktop.
A MySQLi Proxy with Query logging.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Core\Database; | |
use Logger; | |
use mysqli; | |
use Psr\Log\LoggerInterface; | |
/** | |
* Class MySQLiProxy | |
* @package Core\Database | |
*/ | |
class MySQLiProxy | |
{ | |
/** | |
* @var mysqli The MySQLi Object to proxy | |
*/ | |
private $instance; | |
/** | |
* @var bool Debug Mode | |
*/ | |
private $debug = false; | |
/** | |
* Set it up. | |
* @param mysqli $instance | |
* @param LoggerInterface $logger | |
*/ | |
public function __construct(mysqli $instance, LoggerInterface $logger) | |
{ | |
$this->instance = $instance; | |
$this->logger = $logger; | |
} | |
/** | |
* Gets a parameter from MySQLi | |
* @param $param | |
* @return mixed | |
*/ | |
public function __get($param) | |
{ | |
$return = $this->instance->{$param}; | |
if ($this->debug) { | |
$this->logger->debug("Get MySQLi->{$param} = {$return}"); | |
} | |
return $return; | |
} | |
/** | |
* Sets a value on the MySQLi instance. | |
* @param string $param | |
* @param mixed $value | |
*/ | |
public function __set($param, $value) | |
{ | |
$this->instance->{$param} = $value; | |
if ($this->debug) { | |
$this->logger->debug("Set MySQLi->{$param} = {$value}"); | |
} | |
} | |
/** | |
* Proxies the call to MySQLi | |
* @param string $method | |
* @param array $arguments | |
* @return mixed | |
*/ | |
public function __call($method, $arguments) | |
{ | |
if ($this->debug && in_array($method, ['query', 'prepare'])) { | |
$this->logger->debug("MySQLi::{$method}", $arguments); | |
} | |
switch (count($arguments)) { | |
case 0: | |
return $this->instance->{$method}(); | |
case 1: | |
return $this->instance->{$method}($arguments[0]); | |
case 2: | |
return $this->instance->{$method}($arguments[0], $arguments[1]); | |
case 3: | |
return $this->instance->{$method}($arguments[0], $arguments[1], $arguments[2]); | |
case 4: | |
return $this->instance->{$method}($arguments[0], $arguments[1], $arguments[2], $arguments[3]); | |
case 5: | |
return $this->instance->{$method}($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4]); | |
case 6: | |
return $this->instance->{$method}($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5]); | |
case 7: | |
return $this->instance->{$method}($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6]); | |
case 8: | |
return $this->instance->{$method}($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7]); | |
} | |
return call_user_func_array(array($this->instance, $method), $arguments); | |
} | |
/** | |
* Sets debug mode. | |
* @param bool $debug | |
* @return $this | |
*/ | |
public function setDebug($debug) | |
{ | |
$this->debug = $debug; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment