Browse Source

refactoring for Load::model removal

git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/trunk@56 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
master
pzinovkin 16 years ago
parent
commit
08db172f23
  1. 38
      classes/DBConnector.class.php
  2. 3
      classes/Env.class.php
  3. 6
      classes/Model.class.php
  4. 12
      classes/User.class.php

38
classes/DBConnector.class.php

@ -13,8 +13,28 @@
*/
class DBConnector
{
static private $handlers = array();
static public $queries = array();
private $handlers = array();
static private $instance = null;
static public $queries = array();
/**
* Запрещаем new и клонирование
*/
private function __construct(){}
private function __clone(){}
/**
* Единственный способ инициализации. Singletone как-никак.
* @return DBConnector
*/
static public function getInstance()
{
if (self::$instance == null) {
self::$instance = new DBConnector();
}
return self::$instance;
}
/**
* Получение соединения.
@ -23,12 +43,12 @@ class DBConnector
* @param array $db_settings - массив настроек
* @return resource - идентификатор соединения
*/
static function getConnect($db_settings)
public function getConnect($db_settings)
{
$handler_name = self::getConnectionName($db_settings);
$handler_name = $this->getConnectionName($db_settings);
if (isset(self::$handlers[$handler_name])) {
return self::$handlers[$handler_name];
if (isset($this->handlers[$handler_name])) {
return $this->handlers[$handler_name];
}
if (!$handler = mysqli_connect($db_settings['host'], $db_settings['user'], $db_settings['password'], $db_settings['database'])) {
@ -36,12 +56,12 @@ class DBConnector
}
mysqli_query($handler, "SET NAMES 'utf8'"); //cheat!!!
return self::$handlers[$handler_name] = $handler;
return $this->handlers[$handler_name] = $handler;
}
static function getConnectionName($db_settings)
protected function getConnectionName($db_settings)
{
return $db_settings['host'].'-'.$db_settings['database'];
return $db_settings['host'] . '-' . $db_settings['database'];
}
}
?>

3
classes/Env.class.php

@ -83,7 +83,8 @@ final class Env
*/
static public function getBidCost($dis = 0)
{
$cost = Load::model('Settings')->get('bid_base_price');
$settings_model = new SettingsModel();
$cost = $settings_model->get('bid_base_price');
if ($dis > 0 && $dis <= 100) {
return $cost - intval($dis);
}

6
classes/Model.class.php

@ -19,7 +19,7 @@ abstract class Model
function __construct()
{
$this->handler = DBConnector::getConnect(Env::getParam('db_settings'));
$this->handler = DBConnector::getInstance()->getConnect(Env::getParam('db_settings'));
}
/**
@ -30,7 +30,9 @@ abstract class Model
*/
function exec($sql)
{
$time = microtime(true);
if (DEBUG_ENABLE) {
$time = microtime(true);
}
$res = mysqli_query($this->handler, $sql);
if (mysqli_errno($this->handler)) {
throw new MJException("<b>Query Error:</b>\n".$sql."\n<b>Error:</b>\n".mysqli_error($this->handler), 1);

12
classes/User.class.php

@ -34,7 +34,8 @@ class User
}
self::setSession();
Load::model('UserData')->loginUpdate($login);
$model = new UserDataModel();
$model->loginUpdate($login);
return true;
}
@ -106,12 +107,14 @@ class User
static function getByLogin($login)
{
return Load::model('UserData')->getByLogin($login);
$model = new UserDataModel();
return $model->getByLogin($login);
}
static function getById($id)
{
return Load::model('UserData')->getById($id);
$model = new UserDataModel();
return $model->getById($id);
}
@ -124,7 +127,8 @@ class User
*/
static function sendActivateMail($mail, $login)
{
$link = 'http://'.Env::Server('SERVER_NAME').'/activate/?key='.Load::model('UserActivate')->generateKey($login, $mail);
$model = new UserActivateModel();
$link = 'http://'.Env::Server('SERVER_NAME').'/activate/?key=' . $model->generateKey($login, $mail);
$mailer = new Mailer('UserRegister');
return $mailer->send($mail, "Подтверждени E-mail ".Env::Server('SERVER_NAME'), array('link' => $link, 'site_name' => Env::Server('SERVER_NAME')));

Loading…
Cancel
Save