Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Class Pdo
  5. *
  6. * @package Won
  7. */
  8. class Db
  9. {
  10. /**
  11. * @var null|\Pdo
  12. */
  13. private $pdo = null;
  14.  
  15. /**
  16. * @var array
  17. */
  18. private $queries = [];
  19.  
  20. /**
  21. * @var null|Pdo
  22. */
  23. private static $instance = null;
  24.  
  25. /**
  26. * @var string
  27. */
  28. private static $sql_host = 'localhost';
  29.  
  30. /**
  31. * @var string
  32. */
  33. private static $sql_base = 'default';
  34.  
  35. /**
  36. * @var string
  37. */
  38. private static $sql_user = 'root';
  39.  
  40. /**
  41. * @var string
  42. */
  43. private static $sql_password = '';
  44.  
  45. //-------------------------------------------------------------------------
  46.  
  47. /**
  48. * Pdo constructor.
  49. *
  50. * @param void
  51. * @return self
  52. */
  53. private function __construct()
  54. {
  55. $options = [
  56. \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'",
  57. \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
  58. ];
  59.  
  60. $this->pdo = new DB('mysql:host=' . self::$sql_host . ';dbname=' . self::$sql_base, self::$sql_user, self::$sql_password, $options);
  61. }
  62.  
  63. //-------------------------------------------------------------------------
  64.  
  65. /**
  66. * Retourne l'historique des requêtes.
  67. *
  68. * @param void
  69. * @return array
  70. */
  71. private function log($colors = true)
  72. {
  73. if ($colors === true)
  74. {
  75. $queries = $this->queries;
  76.  
  77. foreach ($queries as &$query)
  78. {
  79. $this->colorizeQuery($query);
  80. }
  81.  
  82. return $queries;
  83. }
  84.  
  85. return $this->queries;
  86. }
  87.  
  88. //-------------------------------------------------------------------------
  89.  
  90. /**
  91. * Retourne la dernière requête.
  92. *
  93. * @param void
  94. * @return string
  95. */
  96. private function lastQuery($colors = true)
  97. {
  98. $query = end($this->queries);
  99.  
  100. if ($colors === true)
  101. {
  102. $this->colorizeQuery($query);
  103. }
  104.  
  105. return $query;
  106. }
  107.  
  108. //-------------------------------------------------------------------------
  109.  
  110. /**
  111. * Définit le moteur en UTF-8.
  112. *
  113. * @param void
  114. * @return self
  115. */
  116. private function utf8()
  117. {
  118. $this->query("SET NAMES 'utf8'");
  119.  
  120. return $this;
  121. }
  122.  
  123. //-------------------------------------------------------------------------
  124.  
  125. /**
  126. * @param void
  127. * @return \PDOStatement|\PDO
  128. */
  129. public static function get()
  130. {
  131. if (is_null(self::$instance))
  132. {
  133. self::$instance = new self;
  134. }
  135.  
  136. return self::$instance;
  137. }
  138.  
  139. //-------------------------------------------------------------------------
  140.  
  141. /**
  142. * @param array $params
  143. * @return null|Pdo
  144. */
  145. public static function configure(array $params = [])
  146. {
  147. self::$sql_host = !empty($params['host']) ? $params['host'] : 'localhost';
  148. self::$sql_base = !empty($params['base']) ? $params['base'] : 'base';
  149. self::$sql_user = !empty($params['user']) ? $params['user'] : 'root';
  150. self::$sql_password = !empty($params['pass']) ? $params['pass'] : 'root';
  151.  
  152. return self::get();
  153. }
  154.  
  155. //-------------------------------------------------------------------------
  156.  
  157. /**
  158. * @param void
  159. * @return void
  160. */
  161. public function __clone()
  162. {
  163. throw new \RuntimeException('This instance of PDO use a Singleton');
  164. }
  165.  
  166. //-------------------------------------------------------------------------
  167.  
  168. /**
  169. * @param string $method
  170. * @param array $arguments
  171. * @return mixed
  172. */
  173. public function __call($method, $arguments)
  174. {
  175. if (method_exists($this->pdo, $method) === true)
  176. {
  177. $this->saveQuery($arguments[0]);
  178. return call_user_func_array([$this->pdo, $method], $arguments);
  179. }
  180. else if (method_exists($this, $method))
  181. {
  182. return self::$instance->$method();
  183. }
  184. else
  185. {
  186. throw new \RuntimeException('Invalid method called.');
  187. }
  188. }
  189.  
  190. //-------------------------------------------------------------------------
  191.  
  192. /**
  193. * Sauvegarde une requête.
  194. *
  195. * @param string &$query
  196. * @return self
  197. */
  198. private function saveQuery($query)
  199. {
  200. // Requête vide...
  201. if (empty($query))
  202. {
  203. return $this;
  204. }
  205.  
  206. $query = str_replace(["\t"], '', $query);
  207.  
  208. $this->queries[] = $query;
  209.  
  210. return $this;
  211. }
  212.  
  213. //-------------------------------------------------------------------------
  214.  
  215. /**
  216. * Apply colors on a SQL string.
  217. *
  218. * @param string $query
  219. * @return string
  220. */
  221. private function colorizeQuery(&$query)
  222. {
  223. $keywords = ['select', 'update', 'delete', 'from', 'set', 'values', 'inner', 'outer', 'left', 'right', 'join', 'on', 'where', 'order by', 'limit', 'group by', 'having'];
  224.  
  225. foreach ($keywords as $keyword)
  226. {
  227. $query = preg_replace("/($keyword\s)/i", '<span style="color:blue;font-weight:bold;">$1</span>', $query);
  228. }
  229.  
  230. return $query;
  231. }
  232.  
  233. //-------------------------------------------------------------------------
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement