Memcache, refactoring, View helpers, #16

git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@124 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
pzinovkin
2010-03-13 23:33:46 +00:00
parent 4a22759e3d
commit 8fc917dca2
19 changed files with 706 additions and 147 deletions

View File

@ -9,11 +9,15 @@
* @filesource $URL$
*/
/**
* @method ViewHelperGet get()
*/
class PHPView implements iView
{
protected $path = '';
protected $variables = array();
protected $helpers = array();
protected $extension = '.phtml';
protected $template = '';
@ -69,6 +73,31 @@ class PHPView implements iView
return htmlentities($var, ENT_QUOTES, 'UTF-8');
}
/**
* Helpers call
*
* @param mixed $name
* @param mixed $args
* @return ViewHelper
*/
public function __call($name, $args)
{
$helper = $this->getHelper($name);
return call_user_func_array(array($helper, $name), $args);
}
protected function getHelper($name)
{
if (!isset($this->helpers[$name])) {
$class = 'ViewHelper' . ucfirst($name);
if (!class_exists($class)) {
throw new GeneralException('View helper "' . $class . '" not found.');
}
$this->helpers[$name] = new $class($this);
}
return $this->helpers[$name];
}
protected function getTemplatePath($template)
{
return $this->path . $template . $this->extension;

View File

@ -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;
}
}

View File

@ -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;
}
}