php-censor/src/B8Framework/View.php

83 lines
1.8 KiB
PHP
Raw Normal View History

2016-04-12 19:31:39 +02:00
<?php
namespace b8;
2016-04-20 12:30:26 +02:00
2016-04-12 19:31:39 +02:00
class View
{
2016-04-20 12:30:26 +02:00
protected $_vars = [];
protected static $_helpers = [];
2016-04-12 19:31:39 +02:00
protected static $extension = 'phtml';
2016-04-20 12:30:26 +02:00
public function __construct($file, $path = null)
{
if (!self::exists($file, $path)) {
2017-01-13 16:35:41 +01:00
throw new \RuntimeException('View file does not exist: ' . $file);
2016-04-20 12:30:26 +02:00
}
2016-04-12 19:31:39 +02:00
2016-04-20 12:30:26 +02:00
$this->viewFile = self::getViewFile($file, $path);
}
2016-04-12 19:31:39 +02:00
2016-04-20 12:30:26 +02:00
protected static function getViewFile($file, $path = null)
{
$viewPath = is_null($path) ? Config::getInstance()->get('b8.view.path') : $path;
$fullPath = $viewPath . $file . '.' . static::$extension;
2016-04-12 19:31:39 +02:00
return $fullPath;
2016-04-20 12:30:26 +02:00
}
public static function exists($file, $path = null)
{
if (!file_exists(self::getViewFile($file, $path))) {
return false;
}
return true;
}
public function __isset($var)
{
return isset($this->_vars[$var]);
}
public function __get($var)
{
return $this->_vars[$var];
}
public function __set($var, $val)
{
$this->_vars[$var] = $val;
}
public function __call($method, $params = [])
{
if (!isset(self::$_helpers[$method])) {
$class = '\\' . Config::getInstance()->get('b8.app.namespace') . '\\Helper\\' . $method;
if (!class_exists($class)) {
$class = '\\b8\\View\\Helper\\' . $method;
}
if (!class_exists($class)) {
throw new \Exception('Helper class does not exist: ' . $class);
2016-04-20 12:30:26 +02:00
}
self::$_helpers[$method] = new $class();
}
return self::$_helpers[$method];
}
public function render()
{
extract($this->_vars);
ob_start();
require($this->viewFile);
$html = ob_get_contents();
ob_end_clean();
return $html;
}
}