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.

152 lines
4.8 KiB

<?php namespace Majestic\View;
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage View
* @since 2010-02-25
*/
/**
* @method Helpers\BreadcrumbViewHelper breadcrumb() breadcrumb(string $text = false, string $href = false) Append next link to breadcrumb
* @method Helpers\GetViewHelper get() get(array $replace) Replace some HTTP GET parameters with $replace
* @method Helpers\HeadViewHelper head() head(string $string = false) Append another string to HEAD section of Layout
* @method Helpers\MsgViewHelper msg() msg(string $msg = null, string $type = null) Set a message to display for user in Layout
* @method Helpers\TitleViewHelper title() title(string $string = false) Append another section for TITLE of Layout
* @method Helpers\FormViewHelper form() form(string $form = null) Get form values from session
* @method Helpers\LinkHeadViewHelper linkHead() linkHead($href = false, $rel = 'stylesheet', $type = 'text/css', $media = 'screen,projection,print') Append link tag (css file or other) into head section
* @method Helpers\ScriptFooterViewHelper scriptFooter() scriptFooter($src = false, $type = 'text/javascript') Append script src (javascript or other) into footer section
* @method Helpers\GroupedCssViewHelper groupedCss() groupedCss($relative_file_path = false) Append css into one file
* @method Helpers\GroupedJsViewHelper groupedJs() groupedJs($relative_file_path = false) Append js into one file
* @method Helpers\DescriptionViewHelper description() description(string $string = false) Set value of meta-tag description
* @method Helpers\KeywordsViewHelper keywords() keywords(string $string = false) Set value of meta-tag keywords
*
*/
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 \Majestic\Exception\InitializationException('Configuration must have a "path" set.');
}
$this->setPath($config['path']);
}
public function getPath()
{
return $this->path;
}
public function setPath($path)
{
$this->path = $path;
}
/**
* @param \Majestic\App\Action $object
*/
public function assignObject($object)
{
foreach (get_object_vars($object) as $name => $value) {
$this->assign($name, $value);
}
}
/**
* @param string $name
* @param mixed $value
*/
public function assign($name, $value = null)
{
$this->variables[$name] = $value;
}
/**
* @param string $name
* @param mixed $value
*/
public function prepend($name, $value)
{
if (isset($this->variables[$name])) {
$this->variables[$name] = $value . $this->variables[$name];
} else {
$this->variables[$name] = $value;
}
}
/**
* @param string $name
* @param mixed $value
*/
public function append($name, $value)
{
if (isset($this->variables[$name])) {
$this->variables[$name] .= $value;
} else {
$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 \Majestic\Exception\GeneralException('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 Helpers\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 = '\\' . ucfirst($name) . 'ViewHelper';
if (!class_exists($class)) {
$class = '\Majestic\View\Helpers\\' . ucfirst($name) . 'ViewHelper';
if (!class_exists($class)) {
throw new \Majestic\Exception\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;
}
}