diff --git a/app/ErrorAction.php b/app/ErrorAction.php index d6370b0..d71d8f9 100644 --- a/app/ErrorAction.php +++ b/app/ErrorAction.php @@ -13,7 +13,7 @@ class ErrorAction extends ViewAction { /** - * @var Exception + * @var ErrorException */ protected $exception; @@ -36,12 +36,12 @@ class ErrorAction extends ViewAction return '/static/' . $this->template; } - protected function sendHttpCode($code) + protected function sendHttpCode() { if (headers_sent()) { return; } - switch ($code) { + switch ($this->template) { case 404: header('HTTP/1.0 404 Not Found'); break; @@ -50,9 +50,40 @@ class ErrorAction extends ViewAction } } + protected function logError() + { + if ($this->template = 500) { + + $error = 0; + $ex = $this->exception; + if ($ex instanceof ErrorException) { + $error = $ex->getSeverity(); + } + + switch ($error) { + case E_NOTICE: + $error = 'Notice'; + break; + case E_WARNING: + $error = 'Warning'; + break; + case E_ERROR: + $error = 'Fatal Error'; + break; + default: + $error = 'Unknown Error'; + break; + } + $message = 'PHP ' . $error . ': ' . $ex->getMessage() . ' in ' . $ex->getFile() + . ' on line ' . $ex->getLine(); + error_log($message); + } + } + public function fetch() { - $this->sendHTTPCode($this->template); + $this->logError(); + $this->sendHTTPCode(); return $this->view->fetch($this->getTemplate()); } } \ No newline at end of file diff --git a/app/FrontController.php b/app/FrontController.php index 2ea3154..1cc33e7 100644 --- a/app/FrontController.php +++ b/app/FrontController.php @@ -32,6 +32,9 @@ class FrontController private function __construct() { ErrorHandler::init(); + if (DEBUG == true) { + Profiler::getInstance()->start(); + } $this->router = new Router(); } @@ -96,7 +99,6 @@ class FrontController public function execute() { try { - $request = Env::getRequestUri(); $route = $this->getRouter()->route($request); if (!$route) { @@ -107,14 +109,16 @@ class FrontController if (!class_exists($action_class)) { throw new GeneralException('Action class "' . $action_class . '" not found.'); } + $action = new $action_class(); $layout_class = $route->getLayout(); if (!class_exists($layout_class)) { throw new GeneralException('Layout class "' . $layout_class . '" not found.'); } - $layout = new $layout_class(); - return $layout->fetch($action); + $layout = new $layout_class(); + $html = $layout->fetch($action); + return (!DEBUG) ? $html : Profiler::getInstance()->end($html); } catch(Exception $e) { if (DEBUG == true) { return ErrorHandler::showDebug($e); diff --git a/app/PagerAction.php b/app/PagerAction.php new file mode 100644 index 0000000..44bf076 --- /dev/null +++ b/app/PagerAction.php @@ -0,0 +1,66 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage app + * @since 2010-03-07 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +class PagerAction extends ViewAction +{ + public $page; + public $last_page; + + //protected $num_rows; + + protected $offset = 0; + protected $count = 0; + protected $limit; + + public function __construct($count, $limit = 20) + { + $this->count = $count; + $this->limit = $limit; + parent::__construct(); + } + + protected function execute() + { + $page = (int) Env::Get('p'); + $this->last_page = ceil($this->count/$this->limit); + $this->page = ($page <= $this->last_page && $page > 0) ? $page : 1; + $this->offset = $this->limit * ($this->page - 1); + //$this->num_rows = ($this->limit + $this->offset) <= $this->count ? ($this->limit + $this->offset) : $this->count; + } + + public function getOffset() + { + return $this->offset; + } + + public function getLimit() + { + return $this->limit; + } + + protected function getTemplate() + { + $template = ($this->template) ? $this->template : substr(get_class($this), 0, -6/*strlen('Action')*/); + return '/actions/' . $template; + } + + +/* public function setNumRows($num_rows) + { + $this->num_rows = $num_rows; + } +*/ + /*function prepare() + { + $this->templater->assign('page', $this->page); + $this->templater->assign('page_max', $this->max_page_num); + }*/ +} \ No newline at end of file diff --git a/cache/CacheKey.php b/cache/CacheKey.php new file mode 100644 index 0000000..9271208 --- /dev/null +++ b/cache/CacheKey.php @@ -0,0 +1,72 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage Cache + * @since 2010-03-10 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +class CacheKey +{ + + protected $key; + protected $params = ''; + protected $expire = 0; + + /** + * @param string $key + * @param mixed $params + * @param iCacheable $cacheable + * @return CacheKey + */ + public function __construct($key, $params = array(), $cacheable) + { + $this->key = $key; + if (!$cacheable instanceof iCacheable) { + throw new GeneralException('CacheKey depends on iCacheable instance'); + } + $this->cache = $cacheable->getCache(); + $this->expire = $cacheable->getKeyExpire($this->key); + $this->params = (is_array($params)) ? implode('', $params) : $params; + } + + protected function getCacheKey() + { + $params = ($this->params) ? ('_' . $this->params) : ''; + return $this->key . $params; + } + + protected function getExpire() + { + return $this->expire; + } + + /** + * @param int $expire + */ + public function setExpire($expire) + { + $this->expire = $expire; + } + + public function get() + { + return $this->cache->get($this->getCacheKey()); + } + + /** + * @param mixed $value + */ + public function set($value) + { + return $this->cache->set($this->getCacheKey(), $value, $this->expire); + } + + public function del() + { + return $this->cache->del($this->getCacheKey()); + } +} \ No newline at end of file diff --git a/cache/CacheKeySet.php b/cache/CacheKeySet.php new file mode 100644 index 0000000..c470f78 --- /dev/null +++ b/cache/CacheKeySet.php @@ -0,0 +1,48 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage Cache + * @since 2010-03-10 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +class CacheKeySet extends CacheKey +{ + public function get() + { + $set = $this->cache->get($this->key); + $item_key = $this->getCacheKey(); + + if (!is_array($set) || !array_key_exists($item_key, $set)) { + return false; + } + return $this->cache->get($item_key); + } + + /** + * @param mixed $value + */ + public function set($value) + { + $set = $this->cache->get($this->key); + if (!is_array($set)) { + $set = array(); + } + + $item_key = $this->getCacheKey(); + if (!$this->cache->set($item_key, $value, $this->expire)) { + return false; + } + + $set[$item_key] = $this->cache->getExpire($this->expire); + return $this->cache->set($this->key, $set, $this->expire); + } + + public function del() + { + return $this->cache->del($this->key); + } +} \ No newline at end of file diff --git a/cache/MemcacheCache.php b/cache/MemcacheCache.php index 2dda1a1..8daabf2 100644 --- a/cache/MemcacheCache.php +++ b/cache/MemcacheCache.php @@ -17,10 +17,26 @@ class MemcacheCache extends Cache */ protected $connection = null; + protected $key_salt = null; + + /** + * One hour to live default + * + * @var int + */ + protected $expire = 3600; + + protected $keys = array(); + public function __construct($config) { $this->connection = new Memcache(); + if (isset($config['key_salt'])) { + $this->key_salt = $config['key_salt']; + unset($config['key_salt']); + } + $required = array('hostname', 'port'); foreach ($config as $c) { foreach ($required as $option) { @@ -42,7 +58,7 @@ class MemcacheCache extends Cache */ public function add($key, $value, $expire = 0) { - return $this->connection->add($key, $value, null, $expire); + return $this->connection->add($this->getKey($key), $value, null, $this->getExpire($expire)); } /** @@ -54,7 +70,7 @@ class MemcacheCache extends Cache */ public function decrement($key, $decrement = 1) { - return $this->connection->decrement($key, $decrement); + return $this->connection->decrement($this->getKey($key), $decrement); } /** @@ -66,7 +82,7 @@ class MemcacheCache extends Cache */ public function del($key) { - return $this->connection->delete($key); + return $this->connection->delete($this->getKey($key)); } /** @@ -82,12 +98,12 @@ class MemcacheCache extends Cache /** * Retrieve item from the cache * - * @param mixed $key + * @param string $key * @return mixed */ public function get($key) { - return $this->connection->get($key); + return $this->connection->get($this->getKey($key)); } /** @@ -99,7 +115,7 @@ class MemcacheCache extends Cache */ public function increment($key, $increment = 1) { - return $this->connection->increment($key, $increment); + return $this->connection->increment($this->getKey($key), $increment); } /** @@ -112,7 +128,7 @@ class MemcacheCache extends Cache */ public function replace($key, $value, $expire = 0) { - return $this->connection->replace($key, $value, null, $expire); + return $this->connection->replace($this->getKey($key), $value, null, $this->getExpire($expire)); } /** @@ -125,6 +141,28 @@ class MemcacheCache extends Cache */ public function set($key, $value, $expire = 0) { - return $this->connection->set($key, $value, null, $expire); + return $this->connection->set($this->getKey($key), $value, null, $this->getExpire($expire)); + } + + /** + * @param string $key + * @return string + */ + protected function getKey($key) + { + if (!isset($this->keys[$key])) { + $this->keys[$key] = md5($this->key_salt . $key); + } + return $this->keys[$key]; + } + + public function getExpire($expire) + { + return ($expire > 0) ? $expire : $this->expire; + } + + public function cleanKeys() + { + $this->keys = array(); } } \ No newline at end of file diff --git a/cache/iCacheable.php b/cache/iCacheable.php new file mode 100644 index 0000000..4f6b48b --- /dev/null +++ b/cache/iCacheable.php @@ -0,0 +1,24 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage cache + * @since 2010-03-12 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +interface iCacheable +{ + /** + * @return Cache + */ + public function getCache(); + + /** + * @param string $key + * @return Expiration time in seconds + */ + public function getKeyExpire($key); +} \ No newline at end of file diff --git a/classes/Pager.class.php b/classes/Pager.class.php deleted file mode 100644 index d6f227b..0000000 --- a/classes/Pager.class.php +++ /dev/null @@ -1,78 +0,0 @@ - - * @link - * @package Nakon - * @subpackage face - * @since 04.02.2008 - * @version SVN$ - * @filesource $URL$ - */ - - -class Pager extends Action -{ - public $template_dir = '.'; - - protected $current_page = 1; - protected $max_page_num; - protected $num_rows; - protected $limit; - protected $start_offset = 0; - protected $count = 0; - - public function __construct($count, $current_page = 1, $records_limit = 20) - { - parent::__construct(); - - $this->count = $count; - $this->limit = $records_limit; - - $this->max_page_num = ceil($this->count/$this->limit); - $this->current_page = ((int)$current_page <= $this->max_page_num && (int)$current_page > 0) ? (int)$current_page : 1; - - $this->start_offset = $this->limit * ($this->current_page - 1); - $this->num_rows = ($this->limit + $this->start_offset) <= $this->count ? ($this->limit + $this->start_offset) : $this->count; - } - - /** - * Возвращает текущую страницу - * - * @return int - */ - public function getPage() - { - return $this->current_page; - } - - public function startOffset() - { - return $this->start_offset; - } - - public function limit() - { - return $this->limit; - } - - public function setNumRows($num_rows) - { - $this->num_rows = $num_rows; - } - - function init() - { - $this->template = "Pager"; - } - - function prepare() - { - $this->templater->assign('page', $this->current_page); - $this->templater->assign('page_max', $this->max_page_num); - $this->templater->assign('limit', $this->limit); - $this->templater->assign('count', $this->count); - } -} -?> \ No newline at end of file diff --git a/exception/ErrorHandler.php b/exception/ErrorHandler.php index 1bd7fc7..69dd8ad 100644 --- a/exception/ErrorHandler.php +++ b/exception/ErrorHandler.php @@ -51,6 +51,9 @@ class ErrorHandler } $text = '
Variable | Value |
---|---|
' . $key . ' | ' . $value . ' |