From 08db172f23e37d31ae56a47a960de86b1c342e94 Mon Sep 17 00:00:00 2001 From: pzinovkin Date: Tue, 3 Feb 2009 10:49:08 +0000 Subject: [PATCH] refactoring for Load::model removal git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/trunk@56 4cb57b5f-5bbd-dd11-951b-001d605cbbc5 --- classes/DBConnector.class.php | 38 +++++++++++++++++++++++++++++--------- classes/Env.class.php | 3 ++- classes/Model.class.php | 6 ++++-- classes/User.class.php | 12 ++++++++---- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/classes/DBConnector.class.php b/classes/DBConnector.class.php index 245bb58..b505a6e 100644 --- a/classes/DBConnector.class.php +++ b/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']; } } ?> \ No newline at end of file diff --git a/classes/Env.class.php b/classes/Env.class.php index 45c326c..c196bb2 100644 --- a/classes/Env.class.php +++ b/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); } diff --git a/classes/Model.class.php b/classes/Model.class.php index e2b20a8..08a9583 100644 --- a/classes/Model.class.php +++ b/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("Query Error:\n".$sql."\nError:\n".mysqli_error($this->handler), 1); diff --git a/classes/User.class.php b/classes/User.class.php index e4a74e5..1463fc2 100644 --- a/classes/User.class.php +++ b/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')));