Refactored View.
This commit is contained in:
parent
cec3823a3d
commit
cdc926c62a
69
src/View.php
69
src/View.php
|
@ -8,24 +8,40 @@ use PHPCensor\Store\UserStore;
|
||||||
|
|
||||||
class View
|
class View
|
||||||
{
|
{
|
||||||
protected $vars = [];
|
/**
|
||||||
protected $isContent = false;
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $data = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $viewFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
protected static $extension = 'phtml';
|
protected static $extension = 'phtml';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $file
|
||||||
|
* @param string|null $path
|
||||||
|
*/
|
||||||
public function __construct($file, $path = null)
|
public function __construct($file, $path = null)
|
||||||
{
|
{
|
||||||
if ('{@content}' === $file) {
|
|
||||||
$this->isContent = true;
|
|
||||||
} else {
|
|
||||||
if (!self::exists($file, $path)) {
|
if (!self::exists($file, $path)) {
|
||||||
throw new \RuntimeException('View file does not exist: ' . $file);
|
throw new \RuntimeException('View file does not exist: ' . $file);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->viewFile = self::getViewFile($file, $path);
|
$this->viewFile = self::getViewFile($file, $path);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $file
|
||||||
|
* @param string|null $path
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
protected static function getViewFile($file, $path = null)
|
protected static function getViewFile($file, $path = null)
|
||||||
{
|
{
|
||||||
$viewPath = is_null($path) ? (SRC_DIR . 'View/') : $path;
|
$viewPath = is_null($path) ? (SRC_DIR . 'View/') : $path;
|
||||||
|
@ -34,6 +50,12 @@ class View
|
||||||
return $fullPath;
|
return $fullPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $file
|
||||||
|
* @param string|null $path
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
public static function exists($file, $path = null)
|
public static function exists($file, $path = null)
|
||||||
{
|
{
|
||||||
if (!file_exists(self::getViewFile($file, $path))) {
|
if (!file_exists(self::getViewFile($file, $path))) {
|
||||||
|
@ -43,27 +65,41 @@ class View
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __isset($var)
|
/**
|
||||||
|
* @param string $key
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function __isset($key)
|
||||||
{
|
{
|
||||||
return isset($this->vars[$var]);
|
return isset($this->data[$key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __get($var)
|
/**
|
||||||
|
* @param string $key
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function __get($key)
|
||||||
{
|
{
|
||||||
return $this->vars[$var];
|
return $this->data[$key];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __set($var, $val)
|
/**
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
public function __set($key, $value)
|
||||||
{
|
{
|
||||||
$this->vars[$var] = $val;
|
$this->data[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function render()
|
public function render()
|
||||||
{
|
{
|
||||||
if ($this->isContent) {
|
extract($this->data);
|
||||||
return $this->vars['content'];
|
|
||||||
} else {
|
|
||||||
extract($this->vars);
|
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
|
|
||||||
|
@ -74,7 +110,6 @@ class View
|
||||||
|
|
||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return boolean
|
* @return boolean
|
||||||
|
|
|
@ -29,11 +29,4 @@ class ViewTest extends \PHPUnit\Framework\TestCase
|
||||||
self::assertFalse(isset($view->what));
|
self::assertFalse(isset($view->what));
|
||||||
self::assertTrue($view->render() == 'Hello World');
|
self::assertTrue($view->render() == 'Hello World');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUserViewVars()
|
|
||||||
{
|
|
||||||
$view = new View('{@content}');
|
|
||||||
$view->content = 'World';
|
|
||||||
self::assertTrue($view->render() == 'World');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue