Refactored view/template.

This commit is contained in:
Dmitry Khomutov 2018-02-16 20:18:04 +07:00
parent 09cee5a5df
commit 597a20deea
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
34 changed files with 179 additions and 801 deletions

View file

@ -3,6 +3,7 @@
namespace b8;
use b8\Form\FieldSet;
use PHPCensor\View;
class Form extends FieldSet
{

View file

@ -2,7 +2,7 @@
namespace b8\Form;
use b8\View;
use PHPCensor\View;
use b8\Config;
abstract class Element

View file

@ -3,7 +3,7 @@
namespace b8\Form\Element;
use b8\Form\Input;
use b8\View;
use PHPCensor\View;
class Button extends Input
{

View file

@ -2,7 +2,7 @@
namespace b8\Form\Element;
use b8\View;
use PHPCensor\View;
use b8\Form\Input;
class Checkbox extends Input

View file

@ -2,7 +2,7 @@
namespace b8\Form\Element;
use b8\View;
use PHPCensor\View;
class Csrf extends Hidden
{

View file

@ -2,7 +2,7 @@
namespace b8\Form\Element;
use b8\View;
use PHPCensor\View;
class Email extends Text
{

View file

@ -2,7 +2,7 @@
namespace b8\Form\Element;
use b8\View;
use PHPCensor\View;
class Password extends Text
{

View file

@ -2,7 +2,7 @@
namespace b8\Form\Element;
use b8\View;
use PHPCensor\View;
use b8\Form\Input;
class Select extends Input

View file

@ -2,7 +2,7 @@
namespace b8\Form\Element;
use b8\View;
use PHPCensor\View;
class Submit extends Button
{

View file

@ -3,7 +3,7 @@
namespace b8\Form\Element;
use b8\Form\Input;
use b8\View;
use PHPCensor\View;
class Text extends Input
{

View file

@ -2,7 +2,7 @@
namespace b8\Form\Element;
use b8\View;
use PHPCensor\View;
class TextArea extends Text
{

View file

@ -2,7 +2,7 @@
namespace b8\Form\Element;
use b8\View;
use PHPCensor\View;
class Url extends Text
{

View file

@ -2,7 +2,7 @@
namespace b8\Form;
use b8\View;
use PHPCensor\View;
class FieldSet extends Element
{

View file

@ -2,7 +2,7 @@
namespace b8\Form;
use b8\View;
use PHPCensor\View;
class Input extends Element
{

View file

@ -1,82 +0,0 @@
<?php
namespace b8;
class View
{
protected $vars = [];
protected static $helpers = [];
protected static $extension = 'phtml';
public function __construct($file, $path = null)
{
if (!self::exists($file, $path)) {
throw new \RuntimeException('View file does not exist: ' . $file);
}
$this->viewFile = self::getViewFile($file, $path);
}
protected static function getViewFile($file, $path = null)
{
$viewPath = is_null($path) ? Config::getInstance()->get('b8.view.path') : $path;
$fullPath = $viewPath . $file . '.' . static::$extension;
return $fullPath;
}
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);
}
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;
}
}

View file

@ -1,548 +0,0 @@
<?php
namespace b8\View;
use b8\View;
class Template extends View
{
public static $templateFunctions = [];
protected static $extension = 'html';
public function __construct($viewCode)
{
$this->viewCode = $viewCode;
if (!count(self::$templateFunctions)) {
self::$templateFunctions = [
'include' => [$this, 'includeTemplate'],
'call' => [$this, 'callHelperFunction']
];
}
}
public static function createFromFile($file, $path = null)
{
if (!static::exists($file, $path)) {
throw new \Exception('View file does not exist: ' . $file);
}
$viewFile = static::getViewFile($file, $path);
return new static(file_get_contents($viewFile));
}
public static function createFromString($string)
{
return new static($string);
}
public function addFunction($name, $handler)
{
self::$templateFunctions[$name] = $handler;
}
public function removeFunction($name)
{
unset(self::$templateFunctions[$name]);
}
public function render()
{
return $this->parse($this->viewCode);
}
protected function parse($string)
{
$keywords = ['ifnot', 'if', 'else', 'for', 'loop', '@', '/ifnot', '/if', '/for', '/loop'];
foreach (self::$templateFunctions as $function => $handler) {
$keywords[] = $function;
}
$stack = ['children' => [['type' => 'string', 'body' => '']]];
$stack['children'][0]['parent'] =& $stack;
$current =& $stack['children'][0];
while (!empty($string)) {
$current['body'] .= $this->readUntil('{', $string);
if (!empty($string)) {
$gotKeyword = false;
foreach ($keywords as $keyword) {
$kwLen = strlen($keyword) + 1;
if (substr($string, 0, $kwLen) == '{' . $keyword) {
$gotKeyword = true;
$item = ['type' => $keyword, 'cond' => '', 'children' => ''];
$string = substr($string, $kwLen);
$cond = trim($this->readUntil('}', $string));
$item['cond'] = $cond;
$string = substr($string, 1);
if (array_key_exists($keyword, self::$templateFunctions)) {
$item['function_name'] = $keyword;
$item['type'] = 'function';
}
$str = ['type' => 'string', 'body' => ''];
$parent =& $current['parent'];
if (substr($current['body'], (0 - strlen(PHP_EOL))) === PHP_EOL) {
$current['body'] = substr($current['body'], 0, strlen($current['body']) - strlen(PHP_EOL));
}
$item['parent'] =& $parent;
$parent['children'][] = $item;
if ($keyword == '@' || $item['type'] == 'function') {
// If we're processing a variable, add a string to the parent and move up to that as current.
$parent['children'][] = $str;
$current =& $parent['children'][count($parent['children']) - 1];
$current['parent'] =& $parent;
} elseif (substr($keyword, 0, 1) == '/') {
// If we're processing the end of a block (if/loop), add a string to the parent's parent and move up to that.
$parent =& $parent['parent'];
$parent['children'][] = $str;
$current =& $parent['children'][count($parent['children']) - 1];
$current['parent'] =& $parent;
} else {
if (!is_array($parent['children'][count($parent['children']) - 1]['children'])) {
$parent['children'][count($parent['children']) - 1]['children'] = [];
}
$parent['children'][count($parent['children']) - 1]['children'][] = $str;
$current =& $parent['children'][count($parent['children']) - 1]['children'][0];
$current['parent'] =& $parent['children'][count($parent['children']) - 1];
}
break;
}
}
if (!$gotKeyword) {
$current['body'] .= substr($string, 0, 1);
$string = substr($string, 1);
}
}
}
return $this->processStack($stack);
}
protected function processStack($stack)
{
$res = '';
while (count($stack['children'])) {
$current = array_shift($stack['children']);
switch ($current['type']) {
case 'string':
$res .= $current['body'];
break;
case '@':
$res .= $this->doParseVar($current['cond']);
break;
case 'if':
$res .= $this->doParseIf($current['cond'], $current);
break;
case 'ifnot':
$res .= $this->doParseIfNot($current['cond'], $current);
break;
case 'loop':
$res .= $this->doParseLoop($current['cond'], $current);
break;
case 'for':
$res .= $this->doParseFor($current['cond'], $current);
break;
case 'function':
$res .= $this->doParseFunction($current);
break;
}
}
return $res;
}
protected function readUntil($until, &$string)
{
$read = '';
while (!empty($string)) {
$char = substr($string, 0, 1);
if ($char == $until) {
break;
}
$read .= $char;
$string = substr($string, 1);
}
return $read;
}
protected function doParseVar($var)
{
if ($var == 'year') {
return date('Y');
}
$val = $this->processVariableName($var);
return $val;
}
protected function doParseIf($condition, $stack)
{
if ($this->ifConditionIsTrue($condition)) {
return $this->processStack($stack);
} else {
return '';
}
}
protected function doParseIfNot($condition, $stack)
{
if (!$this->ifConditionIsTrue($condition)) {
return $this->processStack($stack);
} else {
return '';
}
}
protected function ifConditionIsTrue($condition)
{
$matches = [];
if (preg_match(
'/([a-zA-Z0-9_\-\(\):\s.\"]+)\s+?([\!\=\<\>]+)?\s+?([a-zA-Z0-9\(\)_\-:\s.\"]+)?/',
$condition,
$matches
)) {
$left = is_numeric($matches[1])
? intval($matches[1])
: $this->processVariableName($matches[1]);
$right = is_numeric($matches[3])
? intval($matches[3])
: $this->processVariableName($matches[3]);
$operator = $matches[2];
switch ($operator) {
case '==':
case '=':
return ($left == $right);
case '!=':
return ($left != $right);
case '>=':
return ($left >= $right);
case '<=':
return ($left <= $right);
case '>':
return ($left > $right);
case '<':
return ($left < $right);
}
} elseif (preg_match('/([a-zA-Z0-9_\-\(\):\s.]+)/', $condition, $matches)) {
return $this->processVariableName($condition) ? true : false;
}
}
protected function doParseLoop($var, $stack)
{
$working = $this->processVariableName($var);
if (is_null($working)) {
return '';
}
if (!is_array($working)) {
$working = [$working];
}
$rtn = '';
foreach ($working as $key => $val) {
// Make sure we support nesting loops:
$keyWas = isset($this->key) ? $this->key : null;
$valWas = isset($this->value) ? $this->value : null;
$itemWas = isset($this->item) ? $this->item : null;
// Set up the necessary variables within the stack:
$this->parent = $this;
$this->item = $val;
$this->key = $key;
$this->value = $val;
$rtn .= $this->processStack($stack);
// Restore state for any parent nested loops:
$this->item = $itemWas;
$this->key = $keyWas;
$this->value = $valWas;
}
return $rtn;
}
/**
* Processes loops in templates, of the following styles:
*
* <code>
* {for myarray.items}
* {@item.title}
* {/for}
* </code>
*
* Or:
*
* <code>
* {for 0:pages.count; i++}
* <a href="/item/{@i}">{@i}</a>
* {/for}
* </code>
*
* @param $cond string The condition string for the loop, to be parsed (e.g. "myarray.items" or "0:pages.count; i++")
* @param $stack string The child stack for this loop, to be processed for each item.
* @return string
* @throws \Exception
*/
protected function doParseFor($cond, $stack)
{
// If this is a simple foreach loop, jump over to parse loop:
if (strpos($cond, ';') === false) {
return $this->doParseLoop($cond, $stack);
}
// Otherwise, process as a for loop:
$parts = explode(';', $cond);
$range = explode(':', trim($parts[0]));
// Process range:
$rangeLeft = $this->getForRangePart($range[0]);
$rangeRight = $this->getForRangePart($range[1]);
// Process variable & incrementor / decrementor:
$parts[1] = trim($parts[1]);
$matches = [];
if (preg_match('/([a-zA-Z0-9_]+)(\+\+|\-\-)/', $parts[1], $matches)) {
$varName = $matches[1];
$direction = $matches[2] == '++' ? 'increment' : 'decrement';
} else {
throw new \Exception('Syntax error in for loop: ' . $cond);
}
$rtn = '';
if ($direction == 'increment') {
for ($i = $rangeLeft; $i < $rangeRight; $i++) {
$this->parent = $this;
$this->{$varName} = $i;
$rtn .= $this->processStack($stack);
}
} else {
for ($i = $rangeLeft; $i > $rangeRight; $i--) {
$this->parent = $this;
$this->{$varName} = $i;
$rtn .= $this->processStack($stack);
}
}
return $rtn;
}
protected function getForRangePart($part)
{
if (is_numeric($part)) {
return intval($part);
}
$varPart = $this->processVariableName($part);
if (is_numeric($varPart)) {
return intval($varPart);
}
throw new \Exception('Invalid range in for loop: ' . $part);
}
public function processVariableName($varName)
{
// Case one - Test for function calls:
if (substr($varName, 0, 1) == '(' && substr($varName, -1) == ')') {
$functionCall = substr($varName, 1, -1);
$parts = explode(' ', $functionCall, 2);
$functionName = $parts[0];
$arguments = isset($parts[1]) ? $parts[1] : null;
return $this->executeTemplateFunction($functionName, $arguments);
}
// Case two - Test if it is just a string:
if (substr($varName, 0, 1) == '"' && substr($varName, -1) == '"') {
return substr($varName, 1, -1);
}
// Case three - Test if it is just a number:
if (is_numeric($varName)) {
return $varName;
}
// Case four - Test for helper calls:
if (strpos($varName, ':') !== false) {
list($helper, $property) = explode(':', $varName);
$helper = $this->{$helper}();
if (property_exists($helper, $property) || method_exists($helper, '__get')) {
return $helper->{$property};
}
return null;
}
// Case five - Process as a variable:
$varPart = explode('.', $varName);
$thisPart = array_shift($varPart);
if (!array_key_exists($thisPart, $this->vars)) {
return null;
}
$working = $this->{$thisPart};
while (count($varPart)) {
$thisPart = array_shift($varPart);
if (is_object($working)) {
// Check if we're working with an actual property:
if (property_exists($working, $thisPart)) {
$working = $working->{$thisPart};
continue;
}
// Check if the object has a magic __get method:
if (method_exists($working, '__get')) {
$working = $working->{$thisPart};
continue;
}
}
if (is_array($working) && array_key_exists($thisPart, $working)) {
$working = $working[$thisPart];
continue;
}
if ($thisPart == 'toLowerCase') {
$working = strtolower($working);
continue;
}
if ($thisPart == 'toUpperCase') {
$working = strtoupper($working);
continue;
}
if ($thisPart == 'isNumeric') {
return is_numeric($working);
}
return null;
}
return $working;
}
protected function doParseFunction($stack)
{
return $this->executeTemplateFunction($stack['function_name'], $stack['cond']);
}
protected function executeTemplateFunction($function, $args)
{
if (array_key_exists($function, self::$templateFunctions)) {
$handler = self::$templateFunctions[$function];
$args = $this->processFunctionArguments($args);
return $handler($args, $this);
}
return null;
}
protected function processFunctionArguments($args)
{
$rtn = [];
$args = explode(';', $args);
foreach ($args as $arg) {
$arg = explode(':', $arg);
if (count($arg) == 2) {
$key = trim($arg[0]);
$val = trim($arg[1]);
if (strpos($val, ',') !== false) {
$val = explode(',', $val);
}
$rtn[$key] = $val;
}
}
return $rtn;
}
public function getVariable($variable)
{
return $this->processVariableName($variable);
}
protected function includeTemplate($args, $view)
{
$template = static::createFromFile($view->getVariable($args['template']));
if (isset($args['variables'])) {
if (!is_array($args['variables'])) {
$args['variables'] = [$args['variables']];
}
foreach ($args['variables'] as $variable) {
$variable = explode('=>', $variable);
$variable = array_map('trim', $variable);
if (count($variable) == 1) {
$template->{$variable[0]} = $view->getVariable($variable[0]);
} else {
$template->{$variable[1]} = $view->getVariable($variable[0]);
}
}
}
return $template->render();
}
protected function callHelperFunction($args)
{
$helper = $args['helper'];
$function = $args['method'];
return $this->{$helper}()->{$function}();
}
}

View file

@ -6,7 +6,6 @@ use b8;
use b8\Exception\HttpException;
use b8\Http\Response;
use b8\Http\Response\RedirectResponse;
use b8\View;
/**
* @author Dan Cryer <dan@block8.co.uk>

View file

@ -6,22 +6,26 @@ use b8\Config;
use b8\Exception\HttpException\ForbiddenException;
use b8\Http\Request;
use b8\Http\Response;
use b8\View;
class Controller extends \b8\Controller
{
/**
* @var \b8\View
* @var View
*/
protected $controllerView;
/**
* @var \b8\View
* @var View
*/
protected $view;
/**
* @var \b8\View
* @var string
*/
protected $className;
/**
* @var View
*/
public $layout;
@ -55,7 +59,7 @@ class Controller extends \b8\Controller
if (View::exists($this->className)) {
$this->controllerView = new View($this->className);
} else {
$this->controllerView = new View\Template('{@content}');
$this->controllerView = new View('{@content}');
}
}

View file

@ -14,6 +14,7 @@ use PHPCensor\Model\Project;
use PHPCensor\Model\User;
use PHPCensor\Service\BuildService;
use PHPCensor\Controller;
use PHPCensor\View;
/**
* Build Controller - Allows users to run and view builds.
@ -189,7 +190,7 @@ class BuildController extends Controller
$errorStore = b8\Store\Factory::getStore('BuildError');
$errors = $errorStore->getByBuildId($build->getId(), $perPage, $start, $plugin, $severity, $isNew);
$errorView = new b8\View('Build/errors');
$errorView = new View('Build/errors');
$errorView->build = $build;
$errorView->errors = $errors['items'];
@ -213,7 +214,7 @@ class BuildController extends Controller
*/
protected function getPaginatorHtml($buildId, $plugin, $severity, $isNew, $total, $perPage, $page)
{
$view = new b8\View('pagination');
$view = new View('pagination');
$urlPattern = APP_URL . 'build/view/' . $buildId;
$params = [];
@ -306,7 +307,7 @@ class BuildController extends Controller
foreach ($builds['items'] as $build) {
$item = $build->toArray(1);
$header = new b8\View('Build/header-row');
$header = new View('Build/header-row');
$header->build = $build;
$item['header_row'] = $header->render();

View file

@ -16,6 +16,7 @@ use PHPCensor\Service\BuildService;
use PHPCensor\Service\ProjectService;
use PHPCensor\Model\Build;
use b8\Http\Response\RedirectResponse;
use PHPCensor\View;
/**
* Project Controller - Allows users to create, edit and view projects.
@ -141,7 +142,7 @@ class ProjectController extends PHPCensor\Controller
*/
protected function getPaginatorHtml($projectId, $branch, $environment, $total, $perPage, $page)
{
$view = new b8\View('pagination');
$view = new View('pagination');
$urlPattern = APP_URL . 'project/view/' . $projectId;
$params = [];
@ -178,7 +179,7 @@ class ProjectController extends PHPCensor\Controller
if (empty($project) || $project->getArchived()) {
throw new NotFoundException(Lang::get('project_x_not_found', $projectId));
}
$type = $this->getParam('type', 'branch');
$id = $this->getParam('id');
$debug = (boolean)$this->getParam('debug', false);
@ -273,7 +274,7 @@ class ProjectController extends PHPCensor\Controller
$order = ['id' => 'DESC'];
$builds = $this->buildStore->getWhere($criteria, $perPage, $start, [], $order);
$view = new b8\View('Project/ajax-builds');
$view = new View('Project/ajax-builds');
foreach ($builds['items'] as &$build) {
$build = BuildFactory::getBuild($build);
@ -312,7 +313,7 @@ class ProjectController extends PHPCensor\Controller
$form = $this->projectForm($values);
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$view = new b8\View('Project/edit');
$view = new View('Project/edit');
$view->type = 'add';
$view->project = null;
$view->form = $form;
@ -381,7 +382,7 @@ class ProjectController extends PHPCensor\Controller
$form = $this->projectForm($values, 'edit/' . $projectId);
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$view = new b8\View('Project/edit');
$view = new View('Project/edit');
$view->type = 'edit';
$view->project = $project;
$view->form = $form;

View file

@ -9,10 +9,11 @@ use PHPCensor\Controller;
use PHPCensor\Helper\Lang;
use PHPCensor\Model\User;
use PHPCensor\Service\UserService;
use PHPCensor\View;
/**
* User Controller - Allows an administrator to view, add, edit and delete users.
*
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class UserController extends Controller
@ -61,7 +62,7 @@ class UserController extends Controller
$name = $this->getParam('name', null);
$email = $this->getParam('email', null);
$password = $this->getParam('password', null);
$language = $this->getParam('language', null);
if (!$language) {
$language = null;
@ -166,7 +167,7 @@ class UserController extends Controller
$form = $this->userForm($values);
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$view = new b8\View('User/edit');
$view = new View('User/edit');
$view->type = 'add';
$view->user = null;
$view->form = $form;
@ -208,7 +209,7 @@ class UserController extends Controller
$form = $this->userForm($values, 'edit/' . $userId);
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$view = new b8\View('User/edit');
$view = new View('User/edit');
$view->type = 'edit';
$view->user = $user;
$view->form = $form;

View file

@ -5,7 +5,7 @@ namespace PHPCensor\Controller;
use PHPCensor\Model\Build;
use PHPCensor\Controller;
use b8\Store\Factory;
use b8\View;
use PHPCensor\View;
use PHPCensor\Model\Project;
use b8\Http\Response;
use PHPCensor\Store\BuildStore;
@ -101,7 +101,7 @@ class WidgetAllProjectsController extends Controller
/**
* Get a summary of the project groups we have, and what projects they have in them.
*
*
* @return array
*/
protected function getGroupInfo()

View file

@ -3,7 +3,7 @@
namespace PHPCensor\Controller;
use b8\Store\Factory;
use b8\View;
use PHPCensor\View;
use b8\Http\Response;
use PHPCensor\Controller;
use PHPCensor\Store\BuildStore;

View file

@ -3,7 +3,7 @@
namespace PHPCensor\Controller;
use b8\Store\Factory;
use b8\View;
use PHPCensor\View;
use b8\Http\Response;
use PHPCensor\BuildFactory;
use PHPCensor\Controller;

View file

@ -1,31 +0,0 @@
<?php
namespace PHPCensor\Helper;
use b8\Config;
/**
* Login Is Disabled Helper - Checks if login is disabled in the view
*
* @author Stephen Ball <phpci@stephen.rebelinblue.com>
*/
class LoginIsDisabled
{
/**
* Checks if
*
* @param $method
* @param array $params
*
* @return mixed|null
*/
public function __call($method, $params = [])
{
unset($method, $params);
$config = Config::getInstance();
$disableAuth = (boolean)$config->get('php-censor.security.disable_auth', false);
return $disableAuth;
}
}

View file

@ -2,14 +2,14 @@
namespace PHPCensor\Plugin;
use b8\View;
use PHPCensor\View;
use PHPCensor\Helper\Email as EmailHelper;
use Psr\Log\LogLevel;
use PHPCensor\Plugin;
/**
* Email Plugin - Provides simple email capability.
*
*
* @author Steve Brazier <meadsteve@gmail.com>
*/
class Email extends Plugin

88
src/PHPCensor/View.php Normal file
View file

@ -0,0 +1,88 @@
<?php
namespace PHPCensor;
use b8\Config;
use PHPCensor\Model\User;
class View
{
protected $vars = [];
protected $isContent = false;
protected static $extension = 'phtml';
public function __construct($file, $path = null)
{
if ('{@content}' === $file) {
$this->isContent = true;
} else {
if (!self::exists($file, $path)) {
throw new \RuntimeException('View file does not exist: ' . $file);
}
$this->viewFile = self::getViewFile($file, $path);
}
}
protected static function getViewFile($file, $path = null)
{
$viewPath = is_null($path) ? Config::getInstance()->get('b8.view.path') : $path;
$fullPath = $viewPath . $file . '.' . static::$extension;
return $fullPath;
}
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 render()
{
if ($this->isContent) {
return $this->vars['content'];
} else {
extract($this->vars);
ob_start();
require($this->viewFile);
$html = ob_get_contents();
ob_end_clean();
return $html;
}
}
/**
* @return boolean
*/
protected function LoginIsDisabled()
{
$config = Config::getInstance();
$disableAuth = (boolean)$config->get('php-censor.security.disable_auth', false);
return $disableAuth;
}
}

View file

@ -1,4 +1,10 @@
<?php use PHPCensor\Helper\Lang; ?>
<?php
use PHPCensor\Helper\Lang;
$user = $_SESSION['php-censor-user'];
?>
<div class="clearfix" style="margin-bottom: 20px;">
<a class="btn btn-success pull-right" href="<?php print APP_URL . 'group/edit'; ?>">
<?php Lang::out('group_add'); ?>
@ -22,7 +28,7 @@
<td>
<div class="btn-group btn-group-right">
<a class="btn btn-default btn-sm" href="<?php echo APP_URL ?>group/edit/<?php print $group['id']; ?>"><?php Lang::out('group_edit'); ?></a>
<?php if($this->User()->getIsAdmin() && (!count($group['projects']))): ?>
<?php if($user->getIsAdmin() && (!count($group['projects']))): ?>
<button class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
@ -45,4 +51,4 @@
confirmDelete(e.target.href).onCloseConfirmed = function () {window.location = window.location.href};
});
});
</script>
</script>

View file

@ -6,6 +6,8 @@
use PHPCensor\Helper\Lang;
$user = $_SESSION['php-censor-user'];
?>
<?php if(empty($builds) || !count($builds)): ?>
@ -92,7 +94,7 @@ $branches = $build->getExtra('branches');
<td>
<div class="btn-group btn-group-right">
<a class="btn btn-default btn-sm" href="<?php echo APP_URL ?>build/view/<?php print $build->getId(); ?>"><?php Lang::out('view'); ?></a>
<?php if($this->User()->getIsAdmin()): ?>
<?php if($user->getIsAdmin()): ?>
<button class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>

View file

@ -6,6 +6,8 @@
use PHPCensor\Helper\Lang;
$user = $_SESSION['php-censor-user'];
?>
<script>
var PROJECT_ID = <?= $project->getId(); ?>;
@ -29,7 +31,7 @@ use PHPCensor\Helper\Lang;
?>
<div class="pull-right btn-group">
<?php if (!$project->getArchived()): ?>
<?php if ($this->User()->getIsAdmin()): ?>
<?php if ($user->getIsAdmin()): ?>
<?php if (!empty($environment)): ?>
<a class="btn btn-danger" href="<?= $build_url . '?' . http_build_query(['type' => 'environment', 'id' => $environment, 'debug' => 1]); ?>">
<?php Lang::out('build_now_debug'); ?>

View file

@ -1,4 +1,10 @@
<?php use PHPCensor\Helper\Lang; ?>
<?php
use PHPCensor\Helper\Lang;
$user = $_SESSION['php-censor-user'];
?>
<div class="clearfix" style="margin-bottom: 20px;">
<div class="pull-right btn-group">
<a class="btn btn-success" href="<?php print APP_URL; ?>user/add"><?php Lang::out('add_user'); ?></a>
@ -39,7 +45,7 @@
<td><?php print htmlspecialchars($user->getName()); ?></td>
<td><?php print $status; ?></td>
<td>
<?php if($this->User()->getIsAdmin()): ?>
<?php if($user->getIsAdmin()): ?>
<div class="btn-group btn-group-right">
<a class="btn btn-default btn-sm" href="<?php echo APP_URL ?>user/edit/<?php print $user->getId(); ?>"><?php Lang::out('edit'); ?></a>
<button class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">

View file

@ -4,13 +4,15 @@
* @var $exception \Exception
*/
$user = $_SESSION['php-censor-user'];
?>
<div class="panel panel-danger">
<div class="box-header">
<h2 class="box-title">Sorry, there was a problem</h2>
</div>
<?php if ($this->User()->getIsAdmin()): ?>
<?php if ($user->getIsAdmin()): ?>
<div class="box-body">
<strong>Message</strong>: <?= $exception->getMessage(); ?><br />
<strong>File</strong>: <?= $exception->getFile(); ?><br />

View file

@ -1,4 +1,10 @@
<?php use PHPCensor\Helper\Lang; ?>
<?php
use PHPCensor\Helper\Lang;
$user = $_SESSION['php-censor-user'];
?>
<!DOCTYPE html>
<html>
<head>
@ -87,19 +93,19 @@
</ul>
</li>
<?php if (!$this->LoginIsDisabled()->check()): ?>
<?php if (!$this->LoginIsDisabled()): ?>
<!-- User Account: style can be found in dropdown.less -->
<li class="dropdown user user-menu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="glyphicon glyphicon-user"></i>
<span><?php print $this->User()->getName(); ?> <i class="caret"></i></span>
<span><?= $user->getName(); ?> <i class="caret"></i></span>
</a>
<ul class="dropdown-menu">
<!-- User image -->
<li class="user-header">
<img src="https://www.gravatar.com/avatar/<?php print md5($this->User()->getEmail()); ?>?d=mm" class="img-circle" alt="<?php print $this->User()->getName(); ?>" />
<img src="https://www.gravatar.com/avatar/<?php print md5($user->getEmail()); ?>?d=mm" class="img-circle" alt="<?php print $user->getName(); ?>" />
<p>
<?php print $this->User()->getName(); ?>
<?php print $user->getName(); ?>
</p>
</li>
@ -124,14 +130,14 @@
<!-- sidebar: style can be found in sidebar.less -->
<section class="sidebar">
<?php if (!$this->LoginIsDisabled()->check()): ?>
<?php if (!$this->LoginIsDisabled()): ?>
<!-- Sidebar user panel -->
<div class="user-panel">
<div class="pull-left image">
<img src="https://www.gravatar.com/avatar/<?php print md5($this->User()->getEmail()); ?>?d=mm" class="img-circle" alt="<?php print $this->User()->getName(); ?>" />
<img src="https://www.gravatar.com/avatar/<?php print md5($user->getEmail()); ?>?d=mm" class="img-circle" alt="<?php print $user->getName(); ?>" />
</div>
<div class="pull-left info">
<p><?php Lang::out('hello_name', $this->User()->getName()); ?></p>
<p><?php Lang::out('hello_name', $user->getName()); ?></p>
</div>
</div>
<?php endif; ?>
@ -144,7 +150,7 @@
</a>
</li>
<?php if ($this->User()->getIsAdmin()): ?>
<?php if ($user->getIsAdmin()): ?>
<li class="treeview">
<a href="#">
<i class="fa fa-edit"></i>

View file

@ -2,10 +2,10 @@
namespace Tests\b8;
use b8\View;
use b8\View\Template;
use PHPCensor\View;
use PHPUnit\Framework\TestCase;
class ViewTest extends \PHPUnit\Framework\TestCase
class ViewTest extends TestCase
{
public function testSimpleView()
{
@ -31,90 +31,10 @@ class ViewTest extends \PHPUnit\Framework\TestCase
self::assertTrue($view->render() == 'Hello World');
}
/**
* @expectedException \Exception
*/
public function testInvalidHelper()
{
$view = new Template('{@Invalid:test}');
$view->render();
}
public function testSimpleUserView()
{
$view = new Template('Hello');
self::assertTrue($view->render() == 'Hello');
}
public function testUserViewYear()
{
$view = new Template('{@year}');
self::assertTrue($view->render() == date('Y'));
}
public function testUserViewVars()
{
$view = new Template('Hello {@who}');
$view->who = 'World';
self::assertTrue($view->render() == 'Hello World');
$view = new Template('Hello {@who}');
self::assertTrue($view->render() == 'Hello ');
$view = new Template('Hello {@who.name}');
$view->who = ['name' => 'Dan'];
self::assertTrue($view->render() == 'Hello Dan');
$tmp = new Template('Hello');
$tmp->who = 'World';
$view = new Template('Hello {@tmp.who}');
$view->tmp = $tmp;
self::assertTrue($view->render() == 'Hello World');
try {
$tmp = new Template('Hello');
$view = new Template('Hello {@tmp.who}');
$view->tmp = $tmp;
self::assertTrue($view->render() == 'Hello ');
} catch (\Exception $e) {
self::assertInstanceOf('\PHPUnit_Framework_Error_Notice', $e);
}
$view = new Template('Hello {@who.toUpperCase}');
$view->who = 'World';
self::assertTrue($view->render() == 'Hello WORLD');
$view = new Template('Hello {@who.toLowerCase}');
$view->who = 'World';
self::assertTrue($view->render() == 'Hello world');
}
public function testUserViewIf()
{
$view = new Template('Hello{if who} World{/if}');
$view->who = true;
self::assertTrue($view->render() == 'Hello World');
$view = new Template('Hello{if who} World{/if}');
$view->who = false;
self::assertTrue($view->render() == 'Hello');
$view = new Template('Hello{ifnot who} World{/ifnot}');
$view->who = true;
self::assertTrue($view->render() == 'Hello');
}
public function testUserViewLoop()
{
$view = new Template('Hello {loop who}{@item}{/loop}');
$view->who = ['W', 'o', 'r', 'l', 'd'];
self::assertTrue($view->render() == 'Hello World');
$view = new Template('Hello {loop who}{@item}{/loop}');
self::assertTrue($view->render() == 'Hello ');
$view = new Template('Hello {loop who}{@item}{/loop}');
$view->who = 'World';
self::assertTrue($view->render() == 'Hello World');
$view = new View('{@content}');
$view->content = 'World';
self::assertTrue($view->render() == 'World');
}
}