Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Class Pdo
- *
- * @package Won
- */
- class Db
- {
- /**
- * @var null|\Pdo
- */
- private $pdo = null;
- /**
- * @var array
- */
- private $queries = [];
- /**
- * @var null|Pdo
- */
- private static $instance = null;
- /**
- * @var string
- */
- private static $sql_host = 'localhost';
- /**
- * @var string
- */
- private static $sql_base = 'default';
- /**
- * @var string
- */
- private static $sql_user = 'root';
- /**
- * @var string
- */
- private static $sql_password = '';
- //-------------------------------------------------------------------------
- /**
- * Pdo constructor.
- *
- * @param void
- * @return self
- */
- private function __construct()
- {
- $options = [
- \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'",
- \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
- ];
- $this->pdo = new DB('mysql:host=' . self::$sql_host . ';dbname=' . self::$sql_base, self::$sql_user, self::$sql_password, $options);
- }
- //-------------------------------------------------------------------------
- /**
- * Retourne l'historique des requêtes.
- *
- * @param void
- * @return array
- */
- private function log($colors = true)
- {
- if ($colors === true)
- {
- $queries = $this->queries;
- foreach ($queries as &$query)
- {
- $this->colorizeQuery($query);
- }
- return $queries;
- }
- return $this->queries;
- }
- //-------------------------------------------------------------------------
- /**
- * Retourne la dernière requête.
- *
- * @param void
- * @return string
- */
- private function lastQuery($colors = true)
- {
- $query = end($this->queries);
- if ($colors === true)
- {
- $this->colorizeQuery($query);
- }
- return $query;
- }
- //-------------------------------------------------------------------------
- /**
- * Définit le moteur en UTF-8.
- *
- * @param void
- * @return self
- */
- private function utf8()
- {
- $this->query("SET NAMES 'utf8'");
- return $this;
- }
- //-------------------------------------------------------------------------
- /**
- * @param void
- * @return \PDOStatement|\PDO
- */
- public static function get()
- {
- if (is_null(self::$instance))
- {
- self::$instance = new self;
- }
- return self::$instance;
- }
- //-------------------------------------------------------------------------
- /**
- * @param array $params
- * @return null|Pdo
- */
- public static function configure(array $params = [])
- {
- self::$sql_host = !empty($params['host']) ? $params['host'] : 'localhost';
- self::$sql_base = !empty($params['base']) ? $params['base'] : 'base';
- self::$sql_user = !empty($params['user']) ? $params['user'] : 'root';
- self::$sql_password = !empty($params['pass']) ? $params['pass'] : 'root';
- return self::get();
- }
- //-------------------------------------------------------------------------
- /**
- * @param void
- * @return void
- */
- public function __clone()
- {
- throw new \RuntimeException('This instance of PDO use a Singleton');
- }
- //-------------------------------------------------------------------------
- /**
- * @param string $method
- * @param array $arguments
- * @return mixed
- */
- public function __call($method, $arguments)
- {
- if (method_exists($this->pdo, $method) === true)
- {
- $this->saveQuery($arguments[0]);
- return call_user_func_array([$this->pdo, $method], $arguments);
- }
- else if (method_exists($this, $method))
- {
- return self::$instance->$method();
- }
- else
- {
- throw new \RuntimeException('Invalid method called.');
- }
- }
- //-------------------------------------------------------------------------
- /**
- * Sauvegarde une requête.
- *
- * @param string &$query
- * @return self
- */
- private function saveQuery($query)
- {
- // Requête vide...
- if (empty($query))
- {
- return $this;
- }
- $query = str_replace(["\t"], '', $query);
- $this->queries[] = $query;
- return $this;
- }
- //-------------------------------------------------------------------------
- /**
- * Apply colors on a SQL string.
- *
- * @param string $query
- * @return string
- */
- private function colorizeQuery(&$query)
- {
- $keywords = ['select', 'update', 'delete', 'from', 'set', 'values', 'inner', 'outer', 'left', 'right', 'join', 'on', 'where', 'order by', 'limit', 'group by', 'having'];
- foreach ($keywords as $keyword)
- {
- $query = preg_replace("/($keyword\s)/i", '<span style="color:blue;font-weight:bold;">$1</span>', $query);
- }
- return $query;
- }
- //-------------------------------------------------------------------------
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement