Code refactoring, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@114 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
@ -1,109 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Рутовый класс для любого действия.
|
||||
* Описывает основной функционал для работы с классами действий.
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @package Majestic
|
||||
* @subpackage Action
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
abstract class Action
|
||||
{
|
||||
public $template; //шаблон действия
|
||||
public $template_dir = '.'; //путь к шаблону действия
|
||||
public $class; //имя дочернего класса
|
||||
|
||||
protected $templater; //шаблонизатор
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->class = get_class($this);
|
||||
$this->template = substr($this->class, 0, -strlen(ACTION_POSTFIX));
|
||||
|
||||
if (CACHE_ENABLE && ($cache_name = $this->getCacheKey()) !== false) {
|
||||
$cache = new Cache($this->class.'_'.$cache_name, $this->getCacheTime());
|
||||
if ($cache->isCached()) {
|
||||
$this->restore($cache->load());
|
||||
} else {
|
||||
$this->init();
|
||||
$cache->save($this->store());
|
||||
}
|
||||
} else {
|
||||
$this->init();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Выдает результат действия.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function display()
|
||||
{
|
||||
$this->templater = Load::templater();
|
||||
$this->prepare();
|
||||
$this->templater->setPath(ACTION_TPL_PATH.'/'.$this->template_dir);
|
||||
return $this->templater->fetch($this->template.'.tpl');
|
||||
}
|
||||
|
||||
/**
|
||||
* Инициализация данных действия.
|
||||
* Тут должна быть ВСЯ работа с установкой данных,
|
||||
* дабы потом они нормально закешировались.
|
||||
* Этот метод НЕ исполняется при срабатывании кеша.
|
||||
*
|
||||
*/
|
||||
abstract protected function init();
|
||||
|
||||
/**
|
||||
* Подготовка данных для шаблона.
|
||||
* Этот метод выполняется ВСЕГДА
|
||||
*/
|
||||
protected function prepare() {}
|
||||
|
||||
/**
|
||||
* Возвращает имя файла для кеширования
|
||||
* Переопределяется в доченрих классах.
|
||||
*
|
||||
* @return false/string - имя файла кеша либо false если кеш запрещен
|
||||
*/
|
||||
protected function getCacheKey()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает время жизни кеша в секундах
|
||||
* При отрицательном значении кеш вечен.
|
||||
* Переопределяется в доченрих классах.
|
||||
*
|
||||
* @return false/integer - время жизни кеша либо false если кеш запрещен
|
||||
*/
|
||||
protected function getCacheTime()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Выдает строку для кеширования.
|
||||
*/
|
||||
protected function store()
|
||||
{
|
||||
return serialize(get_object_vars($this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Разбирает строку кеша, созданную методом store
|
||||
*/
|
||||
protected function restore($data)
|
||||
{
|
||||
foreach(unserialize($data) as $key => $val) {
|
||||
$this->$key = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
@ -1,159 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Класс базы данных.
|
||||
* Возвращает идентификатор соединения
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @package Majestic
|
||||
* @subpackage DB
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
class DBConnector
|
||||
{
|
||||
|
||||
/**
|
||||
* PDO constant values.
|
||||
*/
|
||||
const FETCH_ASSOC = 2;
|
||||
const FETCH_BOTH = 4;
|
||||
const FETCH_NUM = 3;
|
||||
const FETCH_OBJ = 5;
|
||||
|
||||
static private $handlers = array();
|
||||
static public $queries = array();
|
||||
|
||||
/**
|
||||
* Запрещаем new и клонирование
|
||||
*/
|
||||
private function __construct(){}
|
||||
|
||||
private function __clone(){}
|
||||
|
||||
/**
|
||||
* Получение соединения.
|
||||
* Если соединение с такими параметрами уже есть - новое не создается.
|
||||
*
|
||||
* @param array $db_settings - массив настроек
|
||||
* @return resource - идентификатор соединения
|
||||
*/
|
||||
static public function getConnect($db_settings)
|
||||
{
|
||||
$handler_name = self::getConnectionName($db_settings);
|
||||
|
||||
if (isset(self::$handlers[$handler_name])) {
|
||||
return self::$handlers[$handler_name];
|
||||
}
|
||||
|
||||
if (!$handler = mysqli_connect($db_settings['host'], $db_settings['user'], $db_settings['password'], $db_settings['database'])) {
|
||||
throw new MJException('Can\'t connect to DB '.mysqli_connect_error(), 2);
|
||||
}
|
||||
|
||||
self::query($handler, "SET NAMES 'utf8'"); //cheat!!!
|
||||
return self::$handlers[$handler_name] = $handler;
|
||||
}
|
||||
|
||||
static protected function getConnectionName($db_settings)
|
||||
{
|
||||
return $db_settings['host'] . '-' . $db_settings['database'];
|
||||
}
|
||||
|
||||
/////////
|
||||
|
||||
static public function query($handler, $sql)
|
||||
{
|
||||
return mysqli_query($handler, $sql);
|
||||
}
|
||||
|
||||
static public function escape($handler, $str)
|
||||
{
|
||||
return mysqli_real_escape_string($handler, $str);
|
||||
}
|
||||
|
||||
static public function error($handler)
|
||||
{
|
||||
return mysqli_error($handler);
|
||||
}
|
||||
|
||||
static public function free($result)
|
||||
{
|
||||
return mysqli_free_result($result);
|
||||
}
|
||||
|
||||
static public function fetchObject($result, $class_name = false)
|
||||
{
|
||||
return $class_name ? mysqli_fetch_object($result, $class_name) : mysqli_fetch_object($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a row from the result set.
|
||||
*
|
||||
* @param mysqli_result $result
|
||||
* @param int $style OPTIONAL Fetch mode for this fetch operation.
|
||||
* @return mixed Array, object, or scalar depending on fetch mode.
|
||||
* @throws Exception
|
||||
*/
|
||||
static public function fetchArray($result, $style = null)
|
||||
{
|
||||
if (!$result) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($style === null) {
|
||||
$style = self::FETCH_ASSOC;
|
||||
}
|
||||
|
||||
$row = false;
|
||||
switch ($style) {
|
||||
case self::FETCH_NUM:
|
||||
$row = mysqli_fetch_array($result, MYSQLI_NUM);
|
||||
break;
|
||||
case self::FETCH_ASSOC:
|
||||
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
|
||||
break;
|
||||
case self::FETCH_BOTH:
|
||||
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
|
||||
break;
|
||||
case self::FETCH_OBJ:
|
||||
$row = mysqli_fetch_object($result, MYSQLI_BOTH);
|
||||
break;
|
||||
default:
|
||||
throw new Exception('Invalid fetch mode "' . $style . '" specified');
|
||||
break;
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
|
||||
static public function numRows($result)
|
||||
{
|
||||
return mysqli_num_rows($result);
|
||||
}
|
||||
|
||||
static public function affectedRows($handler, $result)
|
||||
{
|
||||
return mysqli_affected_rows($handler);
|
||||
}
|
||||
|
||||
static public function getId($handler)
|
||||
{
|
||||
return mysqli_insert_id($handler);
|
||||
}
|
||||
|
||||
static public function autocommit($handler, $switch)
|
||||
{
|
||||
return mysqli_autocommit($handler, (bool) $switch);
|
||||
}
|
||||
|
||||
static public function commit($handler)
|
||||
{
|
||||
return mysqli_commit($handler);
|
||||
}
|
||||
|
||||
static public function rollback($handler)
|
||||
{
|
||||
return mysqli_rollback($handler);
|
||||
}
|
||||
}
|
||||
?>
|
@ -1,62 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Родительский класс для всех декораторов. Содержит основной функционал.
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @package Majestic
|
||||
* @subpackage Decorator
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
abstract class Decorator
|
||||
{
|
||||
protected $layout = false;
|
||||
protected $action = false;
|
||||
protected $action_name = 'Action';
|
||||
|
||||
protected $templater = false;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
if (!$this->layout) {
|
||||
throw new MJException('$layout not set in '.get_class($this));
|
||||
}
|
||||
$this->templater = Load::templater();
|
||||
}
|
||||
|
||||
/**
|
||||
* Основной метод вывода
|
||||
*
|
||||
*/
|
||||
function display(Action $action)
|
||||
{
|
||||
$this->action = $action;
|
||||
$this->templater->assign($this->action_name, $this->action->display(), $this->layout);
|
||||
|
||||
$this->exec();
|
||||
|
||||
$this->templater->setPath(WRAPPERS_TPL_PATH);
|
||||
return $this->templater->fetch($this->layout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Добавить данные на вывод
|
||||
*
|
||||
* @param string $var_name - имя переменной, в которую будет осуществлен вывод
|
||||
* @param Action $action - действие
|
||||
* @param Decorator $decorator - декоратор, в котором должно выполнится действие.
|
||||
*/
|
||||
function addOutput($var_name, Action $action, Decorator $decorator = null)
|
||||
{
|
||||
$this->templater->assign($var_name, $decorator ? $decorator->display($action) : $action->display(), $this->layout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Основной метод для дочерних декораторов
|
||||
*
|
||||
*/
|
||||
abstract function exec();
|
||||
}
|
||||
?>
|
@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Эксепшен для выводя результата работы класса
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @package Majestic
|
||||
* @subpackage Core
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
class DynamicPageException extends Exception
|
||||
{}
|
||||
?>
|
@ -3,38 +3,54 @@
|
||||
* Класс для работы с переменными окружения.
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage Core
|
||||
* @subpackage env
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
final class Env
|
||||
{
|
||||
static public $params = array();
|
||||
|
||||
static function Get($var, $default = false)
|
||||
class Env
|
||||
{
|
||||
|
||||
static protected $request = null;
|
||||
|
||||
static public function getRequestUri()
|
||||
{
|
||||
if (self::$request === null) {
|
||||
// removes get params
|
||||
list(self::$request, ) = explode('?', Env::Server('REQUEST_URI'));
|
||||
// removes base url
|
||||
$base = FrontController::getInstance()->getBaseUrl();
|
||||
if (($length = strlen($base)) > 0 && strpos(self::$request, $base) === 0) {
|
||||
self::$request = (string) substr(self::$request, $length);
|
||||
}
|
||||
}
|
||||
return self::$request;
|
||||
}
|
||||
|
||||
static public function Get($var, $default = false)
|
||||
{
|
||||
return isset($_GET[$var]) ? $_GET[$var] : $default;
|
||||
}
|
||||
|
||||
static function Post($var, $default = false)
|
||||
static public function Post($var, $default = false)
|
||||
{
|
||||
return isset($_POST[$var]) ? $_POST[$var] : $default;
|
||||
}
|
||||
|
||||
static function Server($var, $default = false)
|
||||
static public function Server($var, $default = false)
|
||||
{
|
||||
return isset($_SERVER[$var]) ? $_SERVER[$var] : $default;
|
||||
}
|
||||
|
||||
static function Session($var, $default = false)
|
||||
static public function Session($var, $default = false)
|
||||
{
|
||||
return isset($_SESSION[$var]) ? $_SESSION[$var] : $default;
|
||||
}
|
||||
|
||||
static function setSession($var, $value)
|
||||
static public function setSession($var, $value)
|
||||
{
|
||||
$_SESSION[$var] = $value;
|
||||
}
|
||||
@ -44,48 +60,32 @@ final class Env
|
||||
*
|
||||
* @param string $var
|
||||
*/
|
||||
static function unsetSession($var)
|
||||
static public function unsetSession($var)
|
||||
{
|
||||
if (isset($_SESSION[$var])) {
|
||||
unset($_SESSION[$var]);
|
||||
}
|
||||
}
|
||||
|
||||
static function getCookie($var, $default = false)
|
||||
static public function getCookie($var, $default = false)
|
||||
{
|
||||
return isset($_COOKIE[$var]) ? $_COOKIE[$var] : $default;
|
||||
}
|
||||
|
||||
static function setCookie($var, $value, $time = 0, $path = '/')
|
||||
static public function setCookie($var, $value, $time = 0, $path = '/')
|
||||
{
|
||||
return setcookie($var, $value, $time, $path);
|
||||
}
|
||||
|
||||
static function getParam($var, $default = false)
|
||||
{
|
||||
return isset(self::$params[$var]) ? self::$params[$var] : $default;
|
||||
}
|
||||
|
||||
static function setParam($var, $val)
|
||||
{
|
||||
self::$params[$var] = $val;
|
||||
}
|
||||
|
||||
static function setParams($params=array())
|
||||
{
|
||||
self::$params = self::$params + $params;
|
||||
}
|
||||
|
||||
static public function Files($name = '', $default = array(), $param = false)
|
||||
{
|
||||
if(!isset($_FILES)){
|
||||
if (!isset($_FILES)) {
|
||||
return $default;
|
||||
}
|
||||
if(empty($name)){
|
||||
if (empty($name)) {
|
||||
return $_FILES;
|
||||
}
|
||||
$res = isset($_FILES[$name]) ? $_FILES[$name] : $default;
|
||||
return $param ? $res[$param] : $res;
|
||||
}
|
||||
}
|
||||
?>
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @package Majestic
|
||||
* @subpackage Load
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
class Load
|
||||
{
|
||||
static $models = array();
|
||||
static $templater = false;
|
||||
static $router = false;
|
||||
|
||||
static function model($model_name)
|
||||
{
|
||||
if (isset(self::$models[$model_name])) {
|
||||
return self::$models[$model_name];
|
||||
}
|
||||
|
||||
$class_name = $model_name.MODEL_POSTFIX;
|
||||
return self::$models[$model_name] = new $class_name;
|
||||
}
|
||||
|
||||
static function templater($path = '')
|
||||
{
|
||||
if (self::$templater) {
|
||||
if ($path) {
|
||||
self::$templater->setPath($path);
|
||||
}
|
||||
return self::$templater;
|
||||
}
|
||||
return self::$templater = new Sublimer($path);
|
||||
}
|
||||
|
||||
static function router()
|
||||
{
|
||||
return self::$router ? self::$router : self::$router = new Router;
|
||||
}
|
||||
}
|
||||
?>
|
@ -1,262 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Класс модели данных
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @package Majestic
|
||||
* @subpackage DB
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
abstract class Model
|
||||
{
|
||||
private $handler;
|
||||
|
||||
protected $table = false;
|
||||
protected $primary_key = 'id';
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->handler = DBConnector::getConnect(Env::getParam('db_settings'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Выполняет запрос и возвращает сырой результат
|
||||
*
|
||||
* @param string $sql
|
||||
* @return resource
|
||||
*/
|
||||
function exec($sql)
|
||||
{
|
||||
if (DEBUG_ENABLE) {
|
||||
$time = microtime(true);
|
||||
}
|
||||
$res = DBConnector::query($this->handler, $sql);
|
||||
if ($error = DBConnector::error($this->handler)) {
|
||||
throw new MJException("<b>Query Error:</b>\n".$sql."\n<b>Error:</b>\n".$error, 1);
|
||||
}
|
||||
|
||||
if (DEBUG_ENABLE) {
|
||||
DBConnector::$queries[] = $sql.'; ('.round((microtime(true)-$time)*1000, 1).'ms)';
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Выполняет запрос и возвращает объект результата
|
||||
*
|
||||
* @param string $sql
|
||||
* @return object
|
||||
*/
|
||||
function query($sql)
|
||||
{
|
||||
$res = $this->exec($sql);
|
||||
|
||||
switch (strtolower(substr($sql, 0, 6))) {
|
||||
case 'select':
|
||||
case '(selec':
|
||||
return new ModelSelectResult($res);
|
||||
case 'insert':
|
||||
case 'replac':
|
||||
return new ModelInsertResult($this->handler, $res); //$res for postgreSQL
|
||||
default:
|
||||
return new ModelChangeResult($this->handler, $res); //$res for postgreSQL
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Экранирует строку
|
||||
*
|
||||
* @param mixed $data - строка для экранирования
|
||||
* @return mixed
|
||||
*/
|
||||
function escape($data)
|
||||
{
|
||||
if(is_array($data)){
|
||||
foreach($data as $id => $val){
|
||||
$data[$id] = DBConnector::escape($this->handler, $val);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
return DBConnector::escape($this->handler, $data);
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
function update($id, $data)
|
||||
{
|
||||
$sql = '';
|
||||
foreach ($data as $key => $val) {
|
||||
$sql .= $key."='".$this->escape($val)."', ";
|
||||
}
|
||||
return $this->query('UPDATE '.$this->table.' SET '.rtrim($sql, ', ').' WHERE '.$this->primary_key.'='.(int) $id);
|
||||
}
|
||||
|
||||
function insert($data, $postfix = '')
|
||||
{
|
||||
$keys = array();
|
||||
$values = array();
|
||||
|
||||
foreach ($data as $key => $val) {
|
||||
$keys[] = $key;
|
||||
$values[] = $this->escape($val);
|
||||
}
|
||||
return $this->query('INSERT INTO '.$this->table.' ('.implode(',', $keys).") VALUES('".implode("','", $values)."') ".$postfix);
|
||||
}
|
||||
|
||||
function delete($id)
|
||||
{
|
||||
return $this->query('DELETE FROM '.$this->table.' WHERE '.$this->primary_key.'='.(int) $id);
|
||||
}
|
||||
|
||||
function get($id)
|
||||
{
|
||||
return $this->query('SELECT * FROM '.$this->table.' WHERE '.$this->primary_key.'='.(int) $id);
|
||||
}
|
||||
|
||||
function getList($limit = false, $sort = 'ASC')
|
||||
{
|
||||
return $this->query('SELECT *
|
||||
FROM '.$this->table.'
|
||||
ORDER BY '.$this->primary_key.' '.($sort == 'ASC' ? 'ASC' : 'DESC')
|
||||
.($limit !== false ? ' LIMIT '.(int) $limit : ''))->fetchAll();
|
||||
}
|
||||
|
||||
function setAutocommit($set)
|
||||
{
|
||||
return DBConnector::autocommit($this->handler, (bool) $set);
|
||||
}
|
||||
|
||||
function commit()
|
||||
{
|
||||
return DBConnector::commit($this->handler);
|
||||
}
|
||||
|
||||
function rollback()
|
||||
{
|
||||
return DBConnector::rollback($this->handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает значение поля $table
|
||||
* @return string
|
||||
*/
|
||||
function getTableName()
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
}
|
||||
|
||||
class ModelResult
|
||||
{
|
||||
function __call($name, $args)
|
||||
{
|
||||
throw new MJException('Call undeclared method "'.$name.'" in "'.get_class($this).'" class', -1);
|
||||
}
|
||||
}
|
||||
|
||||
class ModelSelectResult extends ModelResult
|
||||
{
|
||||
public $result;
|
||||
|
||||
function __construct($res)
|
||||
{
|
||||
$this->result = $res;
|
||||
}
|
||||
|
||||
function fetch($class_name = false)
|
||||
{
|
||||
return DBConnector::fetchObject($this->result, $class_name);
|
||||
}
|
||||
|
||||
function fetchField($field, $default = false)
|
||||
{
|
||||
$row = $this->fetch();
|
||||
return isset($row->$field) ? $row->$field : $default;
|
||||
}
|
||||
|
||||
function fetchAll($key = false)
|
||||
{
|
||||
$array = array();
|
||||
if ($key) {
|
||||
while ($row = DBConnector::fetchObject($this->result)) {
|
||||
$array[$row->$key] = $row;
|
||||
}
|
||||
} else {
|
||||
while ($row = DBConnector::fetchObject($this->result)) {
|
||||
$array[] = $row;
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all SQL result rows as an array of key-value pairs.
|
||||
*
|
||||
* The first column is the key, the second column is the
|
||||
* value.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fetchPairs()
|
||||
{
|
||||
if (!method_exists('DBConnector', 'fetchArray')) {
|
||||
throw new Exception('Method not implemented yet.');
|
||||
}
|
||||
|
||||
$data = array();
|
||||
while ($row = DBConnector::fetchArray($this->result, DBConnector::FETCH_NUM)) {
|
||||
$data[$row[0]] = $row[1];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
function count()
|
||||
{
|
||||
return DBConnector::numRows($this->result);
|
||||
}
|
||||
|
||||
function free()
|
||||
{
|
||||
DBConnector::free($this->result);
|
||||
}
|
||||
|
||||
function __destruct() {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
class ModelChangeResult extends ModelResult
|
||||
{
|
||||
public $affected;
|
||||
|
||||
function __construct($resource, $result)
|
||||
{
|
||||
$this->affected = DBConnector::affectedRows($resource, $result);
|
||||
}
|
||||
|
||||
function count()
|
||||
{
|
||||
return $this->affected;
|
||||
}
|
||||
}
|
||||
|
||||
class ModelInsertResult extends ModelChangeResult
|
||||
{
|
||||
public $id;
|
||||
|
||||
function __construct($resource, $result)
|
||||
{
|
||||
parent::__construct($resource, $result);
|
||||
$this->id = DBConnector::getId($resource);
|
||||
}
|
||||
|
||||
function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
}
|
||||
?>
|
@ -1,60 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @package Majestic
|
||||
* @subpackage PageController
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
class PageController
|
||||
{
|
||||
|
||||
/**
|
||||
* Действия на инициализацию.
|
||||
*
|
||||
*/
|
||||
protected function init() {}
|
||||
|
||||
/**
|
||||
* Действия после разбора роута.
|
||||
*
|
||||
*/
|
||||
protected function onDispatch() {}
|
||||
|
||||
/**
|
||||
* Вывод в браузер всего сайта.
|
||||
*
|
||||
*/
|
||||
public function display()
|
||||
{
|
||||
try {
|
||||
$this->init();
|
||||
try{
|
||||
$this->route = Load::router()->proccess(MJ_PATH);
|
||||
$this->onDispatch();
|
||||
$action = new $this->route->action;
|
||||
$decorator = new $this->route->decorator;
|
||||
return $decorator->display($action);
|
||||
} catch (MJException $e) {
|
||||
return $e->terminate();
|
||||
}
|
||||
} catch (DynamicPageException $e) {
|
||||
$decorator_name = DEFAULT_DECORATOR;
|
||||
$decorator = new $decorator_name;
|
||||
$action_name = $e->getMessage();
|
||||
return $decorator->display(new $action_name);
|
||||
} catch (StaticPageException $e) {
|
||||
$decorator_name = DEFAULT_DECORATOR;
|
||||
$decorator = new $decorator_name;
|
||||
$action_name = DEFAULT_ACTION;
|
||||
return $decorator->display(new $action_name($e->getMessage()));
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
@ -1,131 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Класс для работы с роутерами
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @package Majestic
|
||||
* @subpackage Core
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
final class Router
|
||||
{
|
||||
|
||||
protected $routes = array();
|
||||
|
||||
static protected $rewrite_base = '';
|
||||
static protected $decorator = DEFAULT_DECORATOR;
|
||||
static protected $route_name = '';
|
||||
|
||||
|
||||
/**
|
||||
* Добавить роутер
|
||||
*
|
||||
* @param string $name - имя роутера
|
||||
* @param string $path - путь
|
||||
* @param string $action - имя действия
|
||||
* @param array $params - массив параметров
|
||||
*/
|
||||
function add($name, $path, $action, $params = array())
|
||||
{
|
||||
$this->routes[$name] = new Route($path, $action, $params);
|
||||
$this->routes[$name]->decorator = self::$decorator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Установить декоратор для роута (действия), отличный от стандартного
|
||||
*
|
||||
* @param string $name - имя роута (действия)
|
||||
* @param string $decorator - имя декоратора
|
||||
*/
|
||||
function setDecorator($name, $decorator)
|
||||
{
|
||||
if (isset($this->routes[$name])) {
|
||||
$this->routes[$name]->decorator = $decorator.DECORATOR_POSTFIX;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Найти роутер соответствующий заданному пути
|
||||
*
|
||||
* @param stirng $path - путь
|
||||
* @return Route - роутер
|
||||
*/
|
||||
function proccess($path)
|
||||
{
|
||||
$path = explode('/', $path);
|
||||
|
||||
foreach($this->routes as $name => $route) {
|
||||
if ($route->match($path)) {
|
||||
$route->action .= ACTION_POSTFIX;
|
||||
Env::setParams($route->params);
|
||||
self::$route_name = $name;
|
||||
return $route;
|
||||
}
|
||||
}
|
||||
throw new StaticPageException(E_404);
|
||||
}
|
||||
|
||||
static public function setDefaultDecorator($decorator)
|
||||
{
|
||||
self::$decorator = $decorator.DECORATOR_POSTFIX;
|
||||
}
|
||||
|
||||
static public function getRouteName()
|
||||
{
|
||||
return self::$route_name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Роутер
|
||||
*
|
||||
*/
|
||||
final class Route
|
||||
{
|
||||
const URL_VARIABLE = '&';
|
||||
|
||||
protected $path;
|
||||
|
||||
public $decorator = DEFAULT_DECORATOR;
|
||||
public $action;
|
||||
public $params;
|
||||
|
||||
function __construct($path, $action, $params=array())
|
||||
{
|
||||
$this->path = $path;
|
||||
$this->action = $action;
|
||||
$this->params = $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверяет соответствие роутера и пути
|
||||
*
|
||||
* @param string $path - путь для сравнения
|
||||
* @return boolean - соответствует или нет
|
||||
*/
|
||||
function match($path_arr)
|
||||
{
|
||||
$parts = explode('/', $this->path);
|
||||
$cnt = count($parts);
|
||||
if (end($parts) == self::URL_VARIABLE) {
|
||||
$cnt--;
|
||||
} elseif ($cnt != count($path_arr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for ($i=0; $i<$cnt; $i++) {
|
||||
if (substr($parts[$i], 0, 1) == self::URL_VARIABLE) {
|
||||
$this->params[substr($parts[$i], 1)] = $path_arr[$i];
|
||||
} elseif ($parts[$i] != $path_arr[$i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
?>
|
@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Эксепшен для вывода статических шаблонов
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @package Majestic
|
||||
* @subpackage Core
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
class StaticPageException extends Exception
|
||||
{
|
||||
|
||||
}
|
||||
?>
|
@ -1,175 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Простейший шаблонизатор.
|
||||
* Зато быстрый.
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @package Majestic
|
||||
* @subpackage Decorator
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
final class Sublimer
|
||||
{
|
||||
const GLOBAL_KEY = 0;
|
||||
|
||||
public $vars = array();
|
||||
public $path = '';
|
||||
public $template = '';
|
||||
|
||||
protected $head_array = array();
|
||||
|
||||
/**
|
||||
* Конструктор
|
||||
*
|
||||
* @param string $path - путь до шаблонов
|
||||
*/
|
||||
public function __construct($path = '')
|
||||
{
|
||||
$this->setPath($path);
|
||||
$this->vars[self::GLOBAL_KEY] = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Присвоить переменную только
|
||||
*
|
||||
* @param string $name - имя переменной
|
||||
* @param mixed $value - значение переменной
|
||||
* @param string $template - шаблон, если не указан, то переменная глобальная
|
||||
*/
|
||||
public function assign($name, $value, $template = self::GLOBAL_KEY)
|
||||
{
|
||||
$this->vars[$template][$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Очистить стек переменных
|
||||
* @param boolean $with_local - включая локальные переменные
|
||||
*/
|
||||
public function clear($template = self::GLOBAL_KEY)
|
||||
{
|
||||
$this->vars[$template] = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Обработать шаблон
|
||||
*
|
||||
* @param string $template - относительный путь до шаблона
|
||||
* @return string - обработанное содержимое шаблона
|
||||
*/
|
||||
public function fetch($template)
|
||||
{
|
||||
$this->template = $template; //дабы экстракт не перезатер нам переменную. Это важно! $this то он не перезатрет :)
|
||||
extract($this->vars[self::GLOBAL_KEY]);
|
||||
if (isset($this->vars[$this->template])) {
|
||||
extract($this->vars[$this->template], EXTR_OVERWRITE);
|
||||
}
|
||||
ob_start();
|
||||
if (!(include $this->path.'/'.$this->template) == 'OK') {
|
||||
throw new MJException('Template '.$this->path.'/'.$this->template.' not found');
|
||||
}
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Установать путь до шаблонов
|
||||
*
|
||||
* @param string $path - путь до шаблонов
|
||||
*/
|
||||
public function setPath($path)
|
||||
{
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
//Функции для вызова из шаблонов.
|
||||
|
||||
protected function addHead($str)
|
||||
{
|
||||
$this->head_array[] = $str;
|
||||
}
|
||||
|
||||
protected function getHead()
|
||||
{
|
||||
return $this->head_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* обрезает текст до заданной длинны
|
||||
*
|
||||
* @param string $text
|
||||
* @param int $count
|
||||
* @param string $postfix
|
||||
*/
|
||||
protected function trimString($text, $count = 15, $postfix = "...")
|
||||
{
|
||||
return (mb_strlen($text) > $count) ? mb_substr($text, 0, $count).$postfix : $text;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Выполняет разрыв строки на данное количество символов с использованием символа разрыва (wordwrap для utf)
|
||||
*
|
||||
* @param string $text
|
||||
* @param int $width
|
||||
* @param string $break
|
||||
* @return string
|
||||
*/
|
||||
protected function wrapString($text, $width = 15, $break = " ")
|
||||
{
|
||||
$words = explode(' ', $text);
|
||||
for ($i = 0; $i < count($words); $i++) {
|
||||
if (mb_strlen($words[$i]) > $width) {
|
||||
for ($j = $width; $j < mb_strlen($words[$i]); $j += $width + (mb_strlen($break))) {
|
||||
$words[$i] = mb_substr($words[$i], 0, $j) . $break . mb_substr($words[$i], $j);
|
||||
}
|
||||
}
|
||||
}
|
||||
return implode(' ', $words);
|
||||
}
|
||||
|
||||
/**
|
||||
* Формирует get-строку для запроса
|
||||
*
|
||||
* @param array/string $replace - Имя переменной для замены (либо массив ключ=значение) В случае false вернет весь get
|
||||
* @param mixed $value - значение переменной для замены, если $replace - массив, то не играет роли. В случае false переменная удаляется из get
|
||||
* @return string
|
||||
*/
|
||||
function formGet($replace = false, $value = false)
|
||||
{
|
||||
$chunk = true; //обрезать последний & (или ?)
|
||||
$get = $_GET; //дабы не менять дефолтный массив
|
||||
if (is_array($replace)) {
|
||||
foreach($replace as $key => $val) {
|
||||
if($val === false) {
|
||||
unset($get[$key]);
|
||||
$chunk = false;
|
||||
} else {
|
||||
$get[$key] = $val;
|
||||
}
|
||||
}
|
||||
} else if ($replace !== false) {
|
||||
if ($value === false) { //для получения строки БЕЗ параметра с именем $replace
|
||||
unset($get[$replace]);
|
||||
$chunk = false;
|
||||
} else {
|
||||
$get[$replace] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$str = '?';
|
||||
foreach($get as $key => $val) {
|
||||
$str .= $key.'='.$val.'&';
|
||||
}
|
||||
|
||||
if ($chunk) {
|
||||
$str = mb_substr($str, 0, -1);
|
||||
}
|
||||
|
||||
return htmlspecialchars($str);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -1,110 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Класс базы данных.
|
||||
* Возвращает идентификатор соединения
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link
|
||||
* @package Majestic
|
||||
* @subpackage DB
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
class DBConnector
|
||||
{
|
||||
static private $handlers = array();
|
||||
static public $queries = array();
|
||||
|
||||
/**
|
||||
* Запрещаем new и клонирование
|
||||
*/
|
||||
private function __construct(){}
|
||||
|
||||
private function __clone(){}
|
||||
|
||||
/**
|
||||
* Получение соединения.
|
||||
* Если соединение с такими параметрами уже есть - новое не создается.
|
||||
*
|
||||
* @param array $db_settings - массив настроек
|
||||
* @return resource - идентификатор соединения
|
||||
*/
|
||||
static public function getConnect($db_settings)
|
||||
{
|
||||
$handler_name = self::getConnectionName($db_settings);
|
||||
|
||||
if (isset(self::$handlers[$handler_name])) {
|
||||
return self::$handlers[$handler_name];
|
||||
}
|
||||
|
||||
if (!$handler = pg_connect("host='".$db_settings['host']."' dbname='".$db_settings['database']."' user='".$db_settings['user']."' password='".$db_settings['password']."'")) {
|
||||
throw new MJException('Can\'t connect to DB '.pg_last_error(), 2);
|
||||
}
|
||||
|
||||
return self::$handlers[$handler_name] = $handler;
|
||||
}
|
||||
|
||||
static protected function getConnectionName($db_settings)
|
||||
{
|
||||
return $db_settings['host'] . '-' . $db_settings['database'];
|
||||
}
|
||||
|
||||
/////////
|
||||
|
||||
static public function query($handler, $sql)
|
||||
{
|
||||
return pg_query($handler, $sql);
|
||||
}
|
||||
|
||||
static public function escape($handler, $str)
|
||||
{
|
||||
return pg_escape_string($str);
|
||||
}
|
||||
|
||||
static public function error($handler)
|
||||
{
|
||||
return pg_last_error($handler);
|
||||
}
|
||||
|
||||
static public function free($result)
|
||||
{
|
||||
return pg_free_result($result);
|
||||
}
|
||||
|
||||
static public function fetchObject($result, $class_name = false)
|
||||
{
|
||||
return $class_name ? pg_fetch_object($result, null, $class_name) : pg_fetch_object($result);
|
||||
}
|
||||
|
||||
static public function numRows($result)
|
||||
{
|
||||
return pg_num_rows($result);
|
||||
}
|
||||
|
||||
static public function affectedRows($handler, $result)
|
||||
{
|
||||
return pg_affected_rows($result);
|
||||
}
|
||||
|
||||
static public function getId($handler)
|
||||
{
|
||||
return -1; //DISABLED FORM postgreSQL
|
||||
}
|
||||
|
||||
static public function autocommit($handler, $switch)
|
||||
{
|
||||
throw new MJException('Autocommit disabled for postgreSQL Connector' ,1);
|
||||
}
|
||||
|
||||
static public function commit($handler)
|
||||
{
|
||||
throw new MJException('Commit disabled for postgreSQL Connector' ,1);
|
||||
}
|
||||
|
||||
static public function rollback($handler)
|
||||
{
|
||||
throw new MJException('Rollback disabled for postgreSQL Connector' ,1);
|
||||
}
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user