Browse Source
Memcache, refactoring, View helpers, #16
Memcache, refactoring, View helpers, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@124 4cb57b5f-5bbd-dd11-951b-001d605cbbc5master
pzinovkin
15 years ago
19 changed files with 706 additions and 147 deletions
-
39app/ErrorAction.php
-
10app/FrontController.php
-
66app/PagerAction.php
-
72cache/CacheKey.php
-
48cache/CacheKeySet.php
-
54cache/MemcacheCache.php
-
24cache/iCacheable.php
-
78classes/Pager.class.php
-
4exception/ErrorHandler.php
-
3model/DbDriver.php
-
50model/DbStatement.php
-
135model/Model.php
-
23model/MySQLiDriver.php
-
16model/MySQLiStatement.php
-
82util/Profiler.php
-
39util/QueryProfiler.php
-
29view/PHPView.php
-
24view/helpers/ViewHelper.php
-
57view/helpers/ViewHelperGet.php
@ -0,0 +1,66 @@ |
|||
<?php |
|||
/** |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @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); |
|||
}*/ |
|||
} |
@ -0,0 +1,72 @@ |
|||
<?php |
|||
/** |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @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()); |
|||
} |
|||
} |
@ -0,0 +1,48 @@ |
|||
<?php |
|||
/** |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @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); |
|||
} |
|||
} |
@ -0,0 +1,24 @@ |
|||
<?php |
|||
/** |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @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); |
|||
} |
@ -1,78 +0,0 @@ |
|||
<?php |
|||
/** |
|||
* PagerAction |
|||
* |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @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); |
|||
} |
|||
} |
|||
?>
|
@ -0,0 +1,82 @@ |
|||
<?php |
|||
/** |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @link http://netmonsters.ru |
|||
* @package Majestic |
|||
* @subpackage util |
|||
* @since 2010-03-09 |
|||
* @version SVN: $Id$ |
|||
* @filesource $URL$ |
|||
*/ |
|||
|
|||
class Profiler |
|||
{ |
|||
|
|||
protected $start = null; |
|||
protected $end = null; |
|||
|
|||
protected $queries = array(); |
|||
|
|||
static protected $instance = null; |
|||
|
|||
private function __construct() |
|||
{ |
|||
if (DEBUG == false) { |
|||
throw new GeneralException('Need to turn on DEBUG before use.'); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Refuse cloning |
|||
*/ |
|||
private function __clone(){} |
|||
|
|||
/** |
|||
* @return Profiler |
|||
*/ |
|||
static public function getInstance() |
|||
{ |
|||
if (!isset(self::$instance)) { |
|||
self::$instance = new self(); |
|||
} |
|||
return self::$instance; |
|||
} |
|||
|
|||
/** |
|||
* @param string $query |
|||
* @return QueryProfiler |
|||
*/ |
|||
public function profilerQuery($query) |
|||
{ |
|||
$profiler = new QueryProfiler($query); |
|||
$this->queries[] = $profiler; |
|||
return $profiler; |
|||
} |
|||
|
|||
public function start() |
|||
{ |
|||
$this->start = microtime(true); |
|||
} |
|||
|
|||
public function end($html) |
|||
{ |
|||
$this->end = microtime(true); |
|||
if (stripos($html, '</body>') == False) { |
|||
return $html; |
|||
} |
|||
return str_ireplace('</body>', $this->getOutput() . '</body>', $html); |
|||
} |
|||
|
|||
protected function getOutput() |
|||
{ |
|||
$html = '<div style="clear:both; font:12px monospace; margin: 5px;">' |
|||
. 'Elapsed time: ' . round(($this->end- $this->start) * 1000) . 'ms.<br/>' |
|||
. 'Queries: ' . count($this->queries) . '<br/>'; |
|||
|
|||
foreach ($this->queries as $query) { |
|||
$html .= '[' . round($query->getElapsed() * 1000) . 'ms] ' . $query->getQuery() . '<br/>'; |
|||
} |
|||
$html .= '</div>'; |
|||
return $html; |
|||
} |
|||
} |
@ -0,0 +1,39 @@ |
|||
<?php |
|||
/** |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @link http://netmonsters.ru |
|||
* @package Majestic |
|||
* @subpackage util |
|||
* @since 2010-03-09 |
|||
* @version SVN: $Id$ |
|||
* @filesource $URL$ |
|||
*/ |
|||
|
|||
class QueryProfiler |
|||
{ |
|||
|
|||
protected $query = ''; |
|||
protected $start = null; |
|||
protected $end = null; |
|||
|
|||
public function __construct($query) |
|||
{ |
|||
$this->query = $query; |
|||
$this->start = microtime(true); |
|||
} |
|||
|
|||
public function end() |
|||
{ |
|||
$this->end = microtime(true); |
|||
} |
|||
|
|||
public function getQuery() |
|||
{ |
|||
return $this->query; |
|||
} |
|||
|
|||
public function getElapsed() |
|||
{ |
|||
return $this->end - $this->start; |
|||
} |
|||
} |
@ -0,0 +1,24 @@ |
|||
<?php |
|||
/** |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @link http://netmonsters.ru |
|||
* @package Majestic |
|||
* @subpackage View |
|||
* @since 2010-03-09 |
|||
* @version SVN: $Id$ |
|||
* @filesource $URL$ |
|||
*/ |
|||
|
|||
abstract class ViewHelper |
|||
{ |
|||
|
|||
/** |
|||
* @var PHPView |
|||
*/ |
|||
protected $view = null; |
|||
|
|||
public function __construct($view) |
|||
{ |
|||
$this->view = $view; |
|||
} |
|||
} |
@ -0,0 +1,57 @@ |
|||
<?php |
|||
/** |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @link http://netmonsters.ru |
|||
* @package Majestic |
|||
* @subpackage View |
|||
* @since 2010-03-09 |
|||
* @version SVN: $Id$ |
|||
* @filesource $URL$ |
|||
*/ |
|||
|
|||
class ViewHelperGet extends ViewHelper |
|||
{ |
|||
|
|||
protected $get; |
|||
|
|||
public function get($replace) |
|||
{ |
|||
$get = $this->getSanitizedRequest(); |
|||
if (!is_array($replace)) { |
|||
$replace = array($replace); |
|||
} |
|||
foreach ($replace as $key => $value) { |
|||
if (is_int($key)) { |
|||
unset($get[$value]); |
|||
} else { |
|||
$get[$key] = $this->impl($key, $value); |
|||
} |
|||
} |
|||
return '?' . $this->view->escape(implode('&', $get)); |
|||
} |
|||
|
|||
protected function getSanitizedRequest() |
|||
{ |
|||
if ($this->get === null) { |
|||
$get = Env::Get(); |
|||
foreach ($get as $key => $value) { |
|||
$this->get[$key] = $this->impl($key, $value); |
|||
} |
|||
} |
|||
return $this->get; |
|||
} |
|||
|
|||
protected function impl($name, $value) |
|||
{ |
|||
if (is_array($value)){ |
|||
$result = array(); |
|||
foreach ($value as $key => $val) { |
|||
$result[] = $name . '[' . $key . ']=' . $val; |
|||
} |
|||
$result = implode('&', $result); |
|||
} else { |
|||
$result = $name . '=' . $value; |
|||
} |
|||
return $result; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue