/** * Initialize * * @param array credential, string table_prefix */ public static function initialize(array $credentials, Telegram $telegram, $table_prefix = null) { self::$telegram = $telegram; if (empty($credentials)) { throw new TelegramException('MySQL credentials not provided!'); } self::$mysql_credentials = $credentials; self::$table_prefix = $table_prefix; $dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database']; $options = array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); try { $pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options); $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING); //Define table if (!defined('TB_MESSAGES')) { define('TB_MESSAGES', self::$table_prefix . 'messages'); } if (!defined('TB_USERS')) { define('TB_USERS', self::$table_prefix . 'users'); } if (!defined('TB_CHATS')) { define('TB_CHATS', self::$table_prefix . 'chats'); } if (!defined('TB_USERS_CHATS')) { define('TB_USERS_CHATS', self::$table_prefix . 'users_chats'); } } catch (\PDOException $e) { throw new TelegramException($e->getMessage()); } self::$pdo = $pdo; return self::$pdo; }
/** * External Initialize * * Let you use the class with an external already existing Pdo Mysql connection. * * @param \PDO $external_pdo_connection PDO database object * @param Telegram $telegram Telegram object to connect with this object * @param string $table_prefix Table prefix * * @return \PDO PDO database object * @throws TelegramException */ public static function externalInitialize($external_pdo_connection, Telegram $telegram, $table_prefix = null) { if (empty($external_pdo_connection)) { throw new TelegramException('MySQL external connection not provided!'); } self::$pdo = $external_pdo_connection; self::$telegram = $telegram; self::$mysql_credentials = null; self::$table_prefix = $table_prefix; self::defineTable(); return self::$pdo; }