php-censor/src/View.php

140 lines
2.6 KiB
PHP
Raw Normal View History

2018-02-16 14:18:04 +01:00
<?php
namespace PHPCensor;
2018-03-04 08:30:34 +01:00
use PHPCensor\Store\Factory;
2018-02-16 14:18:04 +01:00
use PHPCensor\Model\User;
2018-02-17 05:59:02 +01:00
use PHPCensor\Store\UserStore;
2018-02-16 14:18:04 +01:00
class View
{
2018-03-17 03:23:49 +01:00
/**
* @var array
*/
protected $data = [];
2018-02-16 14:18:04 +01:00
2018-03-17 03:23:49 +01:00
/**
* @var string
*/
protected $viewFile;
/**
* @var string
*/
2018-02-16 14:18:04 +01:00
protected static $extension = 'phtml';
2018-03-17 03:23:49 +01:00
/**
* @param string $file
* @param string|null $path
*/
2018-02-16 14:18:04 +01:00
public function __construct($file, $path = null)
{
2018-03-17 03:23:49 +01:00
if (!self::exists($file, $path)) {
throw new \RuntimeException('View file does not exist: ' . $file);
2018-02-16 14:18:04 +01:00
}
2018-03-17 03:23:49 +01:00
$this->viewFile = self::getViewFile($file, $path);
2018-02-16 14:18:04 +01:00
}
2018-03-17 03:23:49 +01:00
/**
* @param string $file
* @param string|null $path
*
* @return string
*/
2018-02-16 14:18:04 +01:00
protected static function getViewFile($file, $path = null)
{
2018-03-04 11:50:08 +01:00
$viewPath = is_null($path) ? (SRC_DIR . 'View/') : $path;
2018-02-16 14:18:04 +01:00
$fullPath = $viewPath . $file . '.' . static::$extension;
return $fullPath;
}
2018-03-17 03:23:49 +01:00
/**
* @param string $file
* @param string|null $path
*
* @return boolean
*/
2018-02-16 14:18:04 +01:00
public static function exists($file, $path = null)
{
if (!file_exists(self::getViewFile($file, $path))) {
return false;
}
return true;
}
2018-03-17 03:23:49 +01:00
/**
* @param string $key
*
* @return boolean
*/
public function __isset($key)
2018-02-16 14:18:04 +01:00
{
2018-03-17 03:23:49 +01:00
return isset($this->data[$key]);
2018-02-16 14:18:04 +01:00
}
2018-03-17 03:23:49 +01:00
/**
* @param string $key
*
* @return mixed
*/
public function __get($key)
2018-02-16 14:18:04 +01:00
{
2018-03-17 03:23:49 +01:00
return $this->data[$key];
2018-02-16 14:18:04 +01:00
}
2018-03-17 03:23:49 +01:00
/**
* @param string $key
* @param mixed $value
*/
public function __set($key, $value)
2018-02-16 14:18:04 +01:00
{
2018-03-17 03:23:49 +01:00
$this->data[$key] = $value;
2018-02-16 14:18:04 +01:00
}
2018-03-17 03:23:49 +01:00
/**
* @return string
*/
2018-02-16 14:18:04 +01:00
public function render()
{
2018-03-17 03:23:49 +01:00
extract($this->data);
2018-02-16 14:18:04 +01:00
2018-03-17 03:23:49 +01:00
ob_start();
2018-02-16 14:18:04 +01:00
2018-03-17 03:23:49 +01:00
require($this->viewFile);
2018-02-16 14:18:04 +01:00
2018-03-17 03:23:49 +01:00
$html = ob_get_contents();
ob_end_clean();
2018-02-16 14:18:04 +01:00
2018-03-17 03:23:49 +01:00
return $html;
2018-02-16 14:18:04 +01:00
}
/**
* @return boolean
*/
2018-02-23 17:05:06 +01:00
protected function loginIsDisabled()
2018-02-16 14:18:04 +01:00
{
$config = Config::getInstance();
$disableAuth = (boolean)$config->get('php-censor.security.disable_auth', false);
return $disableAuth;
}
2018-02-17 05:59:02 +01:00
/**
* @return User|null
*/
protected function getUser()
{
if (empty($_SESSION['php-censor-user-id'])) {
return null;
}
/** @var UserStore $userStore */
$userStore = Factory::getStore('User');
return $userStore->getById($_SESSION['php-censor-user-id']);
}
2018-02-16 14:18:04 +01:00
}