You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.4 KiB

<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage View
* @since 2010-02-25
* @version SVN: $Id$
* @filesource $URL$
*/
/**
* @method ViewHelperGet get()
*/
class PHPView implements iView
{
protected $path = '';
protected $variables = array();
protected $helpers = array();
protected $extension = '.phtml';
protected $template = '';
public function __construct($config)
{
if (!isset($config['path'])) {
throw new Exception('Configuration must have a "path".');
}
$this->setPath($config['path']);
}
public function getPath()
{
return $this->path;
}
public function setPath($path)
{
$this->path = $path;
}
/**
* @param ViewAction $object
*/
public function assignObject($object)
{
foreach (get_object_vars($object) as $name => $value) {
$this->assign($name, $value);
}
}
public function assign($name, $value = null)
{
$this->variables[$name] = $value;
}
public function fetch($template)
{
$this->template = $this->getTemplatePath($template);
unset($template);
extract($this->variables);
ob_start();
if (!is_readable($this->template)) {
ob_clean();
throw new Exception('Template "' . $this->template .'" not found.');
}
include($this->template);
return ob_get_clean();
}
public function escape($var)
{
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;
}
}