Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. class DB
  2. {
  3. // объявление свойств
  4. protected $host = 'localhost';
  5. protected $db = 'db';
  6. protected $charset = 'utf8';
  7. protected $user = 'user';
  8. protected $pass = 'pwd';
  9.  
  10. public function __construct() {
  11. // создание подключения к БД
  12. }
  13.  
  14. // другие методы класса
  15. }
  16.  
  17. $config = **parse_ini_file**('config.ini', true);
  18.  
  19. // define DB configuration
  20. $configDb = $config['db'];
  21. define('DB_HOST', $configDb['host']);
  22. define('DB_USER', $configDb['user']);
  23. define('DB_PASSWORD', $configDb['password']);
  24. define('DB_NAME', $configDb['dbname']);
  25. define('DB_PORT', $configDb['port']);
  26.  
  27. $link = new mysqli(DB_HOST, DB_USER, DB_PASSWORD);
  28.  
  29. require_once 'config.php';
  30. require_once 'DB.php';
  31.  
  32. namespace EngineCoreDatabase;
  33.  
  34. use PDO;
  35. use EngineCoreConfigConfig;
  36. class Connection
  37. {
  38. private $link;
  39.  
  40. /**
  41. * Connection constructor.
  42. */
  43. public function __construct()
  44. {
  45. $this->connect();
  46. }
  47.  
  48. /**
  49. * @return $this
  50. */
  51. private function connect()
  52. {
  53. $config = Config::file('database');
  54.  
  55. $dsn = 'mysql:host=' . $config['host'] . ';dbname=' . $config['db_name'] . ';charset=' . $config['charset'];
  56.  
  57. $this->link = new PDO($dsn, $config['username'], $config['password']);
  58.  
  59. return $this;
  60.  
  61. }
  62. /**
  63. * @param $sql
  64. * @param array $values
  65. * @return mixed
  66. */
  67. public function execute($sql, $values = [])
  68. {
  69. $sth = $this->link->prepare($sql);
  70. return $sth->execute($values);
  71. }
  72. /**
  73. * @param $sql
  74. * @param array $values
  75. * @return array
  76. */
  77. public function query($sql, $values = [])
  78. {
  79.  
  80. $sth = $this->link->prepare($sql);
  81.  
  82. $sth->execute($values);
  83. $result = $sth->fetchAll(PDO::FETCH_ASSOC);
  84. if ($result === false) {
  85. return [];
  86. }
  87.  
  88. return $result;
  89. }
  90. }
  91.  
  92. namespace EngineCoreDatabase;
  93. class QueryBuilder
  94. {
  95. /**
  96. * @var array
  97. */
  98. protected $sql = [];
  99. /**
  100. * @var array
  101. */
  102. public $values = [];
  103.  
  104. /**
  105. * @param string $fields
  106. * @return $this
  107. */
  108. public function select($fields = '*')
  109. {
  110. $this->reset();
  111. $this->sql['select'] = "SELECT {$fields} ";
  112.  
  113. return $this;
  114. }
  115.  
  116. /**
  117. * @param $table
  118. * @return $this
  119. */
  120. public function from($table)
  121. {
  122. $this->sql['from'] = "FROM {$table} ";
  123.  
  124. return $this;
  125. }
  126.  
  127. /**
  128. * @param $column
  129. * @param $value
  130. * @param string $operator
  131. * @return $this
  132. */
  133. public function where($column, $value, $operator = '=')
  134. {
  135. $this->sql['where'][] = "{$column} {$operator} ?";
  136. $this->values[] = $value;
  137.  
  138. return $this;
  139. }
  140.  
  141. /**
  142. * @param $field
  143. * @param $order
  144. * @return $this
  145. */
  146. public function orderBy($field, $order)
  147. {
  148. $this->sql['order_by'] = " ORDER BY {$field} {$order}";
  149.  
  150. return $this;
  151. }
  152.  
  153. /**
  154. * @param $number
  155. * @return $this
  156. */
  157. public function limit($number)
  158. {
  159. $this->sql['limit'] = " LIMIT {$number}";
  160.  
  161. return $this;
  162. }
  163.  
  164. public function update($table)
  165. {
  166. $this->reset();
  167. $this->sql['update'] = " UPDATE {$table} ";
  168.  
  169. return $this;
  170. }
  171.  
  172. public function insert($table)
  173. {
  174. $this->reset();
  175. $this->sql['insert'] = "INSERT INTO {$table} ";
  176.  
  177. return $this;
  178. }
  179.  
  180. public function set($data = [])
  181. {
  182. $this->sql['set'] .= " SET ";
  183.  
  184. if (!empty($data)) {
  185. foreach ($data as $key => $value) {
  186. $this->sql['set'] .= "{$key} = ?";
  187. if (next($data)) {
  188. $this->sql['set'] .= ", ";
  189. }
  190. $this->values[] = $value;
  191. }
  192. }
  193. return $this;
  194. }
  195.  
  196. /**
  197. * @return string
  198. */
  199. public function sql()
  200. {
  201. $sql = '';
  202. if (!empty($this->sql)) {
  203. foreach ($this->sql as $key => $value) {
  204. if ($key == 'where') {
  205. $sql .= " WHERE ";
  206. foreach ($value as $where) {
  207. $sql .= $where;
  208. if (count($value) > 1 and next($value)) {
  209. $sql .= ' AND ';
  210. }
  211. }
  212. } else {
  213. $sql .= $value;
  214. }
  215. }
  216. }
  217. return $sql;
  218. }
  219.  
  220. public function reset()
  221. {
  222. $this->sql = [];
  223. $this->values = [];
  224. }
  225.  
  226. }
  227.  
  228. $this->db->execute(
  229. $this->queryBuilder->update($this->getTable())
  230. ->set($properties)
  231. ->where('id', $this->id)
  232. ->sql(),
  233. $this->queryBuilder->values
  234. );
  235.  
  236. namespace Engine;
  237. use EngineDIDI;
  238.  
  239. abstract class Controller
  240. {
  241. /**
  242. * @var DI
  243. */
  244. protected $di;
  245. protected $db;
  246.  
  247.  
  248. /**
  249. * Controller constructor.
  250. * @param DI $di
  251. */
  252. public function __construct(DI $di)
  253. {
  254. $this->di = $di;
  255. $this->db = $this->di->get('db');
  256.  
  257. }
  258. }
  259.  
  260. namespace EngineCoreConfig;
  261. class Config
  262. {
  263. /**
  264. * @param $key
  265. * @param string $group
  266. * @return null
  267. */
  268. public static function item($key, $group = 'main')
  269. {
  270. $groupItems = static::file($group);
  271. return isset($groupItems[$key]) ? $groupItems[$key] : null;
  272. }
  273.  
  274. /**
  275. * @param $group
  276. * @return bool|mixed
  277. * @throws Exception
  278. */
  279. public static function file($group)
  280. {
  281. $path = $_SERVER['DOCUMENT_ROOT'] . '/' . mb_strtolower(ENV) . '/Config/' . $group . '.php';
  282.  
  283. if(file_exists($path))
  284. {
  285. $items = require_once $path;
  286.  
  287. if(!empty($items))
  288. {
  289. return $items;
  290. }
  291. else
  292. {
  293. throw new Exception(
  294. sprintf(
  295. 'Config file <b>%s</b> is not a valid array.', $path
  296. )
  297. );
  298. }
  299. }
  300. else
  301. {
  302. throw new Exception(
  303. sprintf('Cannot load config from file, file <b>%s</b> does not exist.', $path)
  304. );
  305. }
  306.  
  307. return false;
  308. }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement