Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DB
- {
- // объявление свойств
- protected $host = 'localhost';
- protected $db = 'db';
- protected $charset = 'utf8';
- protected $user = 'user';
- protected $pass = 'pwd';
- public function __construct() {
- // создание подключения к БД
- }
- // другие методы класса
- }
- $config = **parse_ini_file**('config.ini', true);
- // define DB configuration
- $configDb = $config['db'];
- define('DB_HOST', $configDb['host']);
- define('DB_USER', $configDb['user']);
- define('DB_PASSWORD', $configDb['password']);
- define('DB_NAME', $configDb['dbname']);
- define('DB_PORT', $configDb['port']);
- $link = new mysqli(DB_HOST, DB_USER, DB_PASSWORD);
- require_once 'config.php';
- require_once 'DB.php';
- namespace EngineCoreDatabase;
- use PDO;
- use EngineCoreConfigConfig;
- class Connection
- {
- private $link;
- /**
- * Connection constructor.
- */
- public function __construct()
- {
- $this->connect();
- }
- /**
- * @return $this
- */
- private function connect()
- {
- $config = Config::file('database');
- $dsn = 'mysql:host=' . $config['host'] . ';dbname=' . $config['db_name'] . ';charset=' . $config['charset'];
- $this->link = new PDO($dsn, $config['username'], $config['password']);
- return $this;
- }
- /**
- * @param $sql
- * @param array $values
- * @return mixed
- */
- public function execute($sql, $values = [])
- {
- $sth = $this->link->prepare($sql);
- return $sth->execute($values);
- }
- /**
- * @param $sql
- * @param array $values
- * @return array
- */
- public function query($sql, $values = [])
- {
- $sth = $this->link->prepare($sql);
- $sth->execute($values);
- $result = $sth->fetchAll(PDO::FETCH_ASSOC);
- if ($result === false) {
- return [];
- }
- return $result;
- }
- }
- namespace EngineCoreDatabase;
- class QueryBuilder
- {
- /**
- * @var array
- */
- protected $sql = [];
- /**
- * @var array
- */
- public $values = [];
- /**
- * @param string $fields
- * @return $this
- */
- public function select($fields = '*')
- {
- $this->reset();
- $this->sql['select'] = "SELECT {$fields} ";
- return $this;
- }
- /**
- * @param $table
- * @return $this
- */
- public function from($table)
- {
- $this->sql['from'] = "FROM {$table} ";
- return $this;
- }
- /**
- * @param $column
- * @param $value
- * @param string $operator
- * @return $this
- */
- public function where($column, $value, $operator = '=')
- {
- $this->sql['where'][] = "{$column} {$operator} ?";
- $this->values[] = $value;
- return $this;
- }
- /**
- * @param $field
- * @param $order
- * @return $this
- */
- public function orderBy($field, $order)
- {
- $this->sql['order_by'] = " ORDER BY {$field} {$order}";
- return $this;
- }
- /**
- * @param $number
- * @return $this
- */
- public function limit($number)
- {
- $this->sql['limit'] = " LIMIT {$number}";
- return $this;
- }
- public function update($table)
- {
- $this->reset();
- $this->sql['update'] = " UPDATE {$table} ";
- return $this;
- }
- public function insert($table)
- {
- $this->reset();
- $this->sql['insert'] = "INSERT INTO {$table} ";
- return $this;
- }
- public function set($data = [])
- {
- $this->sql['set'] .= " SET ";
- if (!empty($data)) {
- foreach ($data as $key => $value) {
- $this->sql['set'] .= "{$key} = ?";
- if (next($data)) {
- $this->sql['set'] .= ", ";
- }
- $this->values[] = $value;
- }
- }
- return $this;
- }
- /**
- * @return string
- */
- public function sql()
- {
- $sql = '';
- if (!empty($this->sql)) {
- foreach ($this->sql as $key => $value) {
- if ($key == 'where') {
- $sql .= " WHERE ";
- foreach ($value as $where) {
- $sql .= $where;
- if (count($value) > 1 and next($value)) {
- $sql .= ' AND ';
- }
- }
- } else {
- $sql .= $value;
- }
- }
- }
- return $sql;
- }
- public function reset()
- {
- $this->sql = [];
- $this->values = [];
- }
- }
- $this->db->execute(
- $this->queryBuilder->update($this->getTable())
- ->set($properties)
- ->where('id', $this->id)
- ->sql(),
- $this->queryBuilder->values
- );
- namespace Engine;
- use EngineDIDI;
- abstract class Controller
- {
- /**
- * @var DI
- */
- protected $di;
- protected $db;
- /**
- * Controller constructor.
- * @param DI $di
- */
- public function __construct(DI $di)
- {
- $this->di = $di;
- $this->db = $this->di->get('db');
- }
- }
- namespace EngineCoreConfig;
- class Config
- {
- /**
- * @param $key
- * @param string $group
- * @return null
- */
- public static function item($key, $group = 'main')
- {
- $groupItems = static::file($group);
- return isset($groupItems[$key]) ? $groupItems[$key] : null;
- }
- /**
- * @param $group
- * @return bool|mixed
- * @throws Exception
- */
- public static function file($group)
- {
- $path = $_SERVER['DOCUMENT_ROOT'] . '/' . mb_strtolower(ENV) . '/Config/' . $group . '.php';
- if(file_exists($path))
- {
- $items = require_once $path;
- if(!empty($items))
- {
- return $items;
- }
- else
- {
- throw new Exception(
- sprintf(
- 'Config file <b>%s</b> is not a valid array.', $path
- )
- );
- }
- }
- else
- {
- throw new Exception(
- sprintf('Cannot load config from file, file <b>%s</b> does not exist.', $path)
- );
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement