From 597a20deea8649315bff1bb7451bd1cdce879761 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Fri, 16 Feb 2018 20:18:04 +0700 Subject: [PATCH] Refactored view/template. --- src/B8Framework/Form.php | 1 + src/B8Framework/Form/Element.php | 2 +- src/B8Framework/Form/Element/Button.php | 2 +- src/B8Framework/Form/Element/Checkbox.php | 2 +- src/B8Framework/Form/Element/Csrf.php | 2 +- src/B8Framework/Form/Element/Email.php | 2 +- src/B8Framework/Form/Element/Password.php | 2 +- src/B8Framework/Form/Element/Select.php | 2 +- src/B8Framework/Form/Element/Submit.php | 2 +- src/B8Framework/Form/Element/Text.php | 2 +- src/B8Framework/Form/Element/TextArea.php | 2 +- src/B8Framework/Form/Element/Url.php | 2 +- src/B8Framework/Form/FieldSet.php | 2 +- src/B8Framework/Form/Input.php | 2 +- src/B8Framework/View.php | 82 --- src/B8Framework/View/Template.php | 548 ------------------ src/PHPCensor/Application.php | 1 - src/PHPCensor/Controller.php | 14 +- src/PHPCensor/Controller/BuildController.php | 7 +- .../Controller/ProjectController.php | 11 +- src/PHPCensor/Controller/UserController.php | 9 +- .../WidgetAllProjectsController.php | 4 +- .../WidgetBuildErrorsController.php | 2 +- .../Controller/WidgetLastBuildsController.php | 2 +- src/PHPCensor/Helper/LoginIsDisabled.php | 31 - src/PHPCensor/Plugin/Email.php | 4 +- src/PHPCensor/View.php | 88 +++ src/PHPCensor/View/Group/index.phtml | 12 +- src/PHPCensor/View/Project/ajax-builds.phtml | 4 +- src/PHPCensor/View/Project/view.phtml | 4 +- src/PHPCensor/View/User/index.phtml | 10 +- src/PHPCensor/View/exception.phtml | 4 +- src/PHPCensor/View/layout.phtml | 24 +- tests/B8Framework/ViewTest.php | 92 +-- 34 files changed, 179 insertions(+), 801 deletions(-) delete mode 100644 src/B8Framework/View.php delete mode 100644 src/B8Framework/View/Template.php delete mode 100644 src/PHPCensor/Helper/LoginIsDisabled.php create mode 100644 src/PHPCensor/View.php diff --git a/src/B8Framework/Form.php b/src/B8Framework/Form.php index 39f59568..31ee1563 100644 --- a/src/B8Framework/Form.php +++ b/src/B8Framework/Form.php @@ -3,6 +3,7 @@ namespace b8; use b8\Form\FieldSet; +use PHPCensor\View; class Form extends FieldSet { diff --git a/src/B8Framework/Form/Element.php b/src/B8Framework/Form/Element.php index e485b9a3..2d89f80d 100644 --- a/src/B8Framework/Form/Element.php +++ b/src/B8Framework/Form/Element.php @@ -2,7 +2,7 @@ namespace b8\Form; -use b8\View; +use PHPCensor\View; use b8\Config; abstract class Element diff --git a/src/B8Framework/Form/Element/Button.php b/src/B8Framework/Form/Element/Button.php index 7f6948a4..b3c7774b 100644 --- a/src/B8Framework/Form/Element/Button.php +++ b/src/B8Framework/Form/Element/Button.php @@ -3,7 +3,7 @@ namespace b8\Form\Element; use b8\Form\Input; -use b8\View; +use PHPCensor\View; class Button extends Input { diff --git a/src/B8Framework/Form/Element/Checkbox.php b/src/B8Framework/Form/Element/Checkbox.php index e69349f7..d2352714 100644 --- a/src/B8Framework/Form/Element/Checkbox.php +++ b/src/B8Framework/Form/Element/Checkbox.php @@ -2,7 +2,7 @@ namespace b8\Form\Element; -use b8\View; +use PHPCensor\View; use b8\Form\Input; class Checkbox extends Input diff --git a/src/B8Framework/Form/Element/Csrf.php b/src/B8Framework/Form/Element/Csrf.php index 89b1e924..ee7fb3f5 100644 --- a/src/B8Framework/Form/Element/Csrf.php +++ b/src/B8Framework/Form/Element/Csrf.php @@ -2,7 +2,7 @@ namespace b8\Form\Element; -use b8\View; +use PHPCensor\View; class Csrf extends Hidden { diff --git a/src/B8Framework/Form/Element/Email.php b/src/B8Framework/Form/Element/Email.php index ccf10965..b0990eb8 100644 --- a/src/B8Framework/Form/Element/Email.php +++ b/src/B8Framework/Form/Element/Email.php @@ -2,7 +2,7 @@ namespace b8\Form\Element; -use b8\View; +use PHPCensor\View; class Email extends Text { diff --git a/src/B8Framework/Form/Element/Password.php b/src/B8Framework/Form/Element/Password.php index 6701a753..b63d3994 100644 --- a/src/B8Framework/Form/Element/Password.php +++ b/src/B8Framework/Form/Element/Password.php @@ -2,7 +2,7 @@ namespace b8\Form\Element; -use b8\View; +use PHPCensor\View; class Password extends Text { diff --git a/src/B8Framework/Form/Element/Select.php b/src/B8Framework/Form/Element/Select.php index 900ed742..d7d91853 100644 --- a/src/B8Framework/Form/Element/Select.php +++ b/src/B8Framework/Form/Element/Select.php @@ -2,7 +2,7 @@ namespace b8\Form\Element; -use b8\View; +use PHPCensor\View; use b8\Form\Input; class Select extends Input diff --git a/src/B8Framework/Form/Element/Submit.php b/src/B8Framework/Form/Element/Submit.php index a95bff0f..14e5ea7a 100644 --- a/src/B8Framework/Form/Element/Submit.php +++ b/src/B8Framework/Form/Element/Submit.php @@ -2,7 +2,7 @@ namespace b8\Form\Element; -use b8\View; +use PHPCensor\View; class Submit extends Button { diff --git a/src/B8Framework/Form/Element/Text.php b/src/B8Framework/Form/Element/Text.php index 56c88ca7..59f45ffd 100644 --- a/src/B8Framework/Form/Element/Text.php +++ b/src/B8Framework/Form/Element/Text.php @@ -3,7 +3,7 @@ namespace b8\Form\Element; use b8\Form\Input; -use b8\View; +use PHPCensor\View; class Text extends Input { diff --git a/src/B8Framework/Form/Element/TextArea.php b/src/B8Framework/Form/Element/TextArea.php index 6ec8b85a..b39d6466 100644 --- a/src/B8Framework/Form/Element/TextArea.php +++ b/src/B8Framework/Form/Element/TextArea.php @@ -2,7 +2,7 @@ namespace b8\Form\Element; -use b8\View; +use PHPCensor\View; class TextArea extends Text { diff --git a/src/B8Framework/Form/Element/Url.php b/src/B8Framework/Form/Element/Url.php index 4627eb79..e89a4578 100644 --- a/src/B8Framework/Form/Element/Url.php +++ b/src/B8Framework/Form/Element/Url.php @@ -2,7 +2,7 @@ namespace b8\Form\Element; -use b8\View; +use PHPCensor\View; class Url extends Text { diff --git a/src/B8Framework/Form/FieldSet.php b/src/B8Framework/Form/FieldSet.php index 49f485a3..fba88738 100644 --- a/src/B8Framework/Form/FieldSet.php +++ b/src/B8Framework/Form/FieldSet.php @@ -2,7 +2,7 @@ namespace b8\Form; -use b8\View; +use PHPCensor\View; class FieldSet extends Element { diff --git a/src/B8Framework/Form/Input.php b/src/B8Framework/Form/Input.php index b3763710..37813a52 100644 --- a/src/B8Framework/Form/Input.php +++ b/src/B8Framework/Form/Input.php @@ -2,7 +2,7 @@ namespace b8\Form; -use b8\View; +use PHPCensor\View; class Input extends Element { diff --git a/src/B8Framework/View.php b/src/B8Framework/View.php deleted file mode 100644 index 6d976265..00000000 --- a/src/B8Framework/View.php +++ /dev/null @@ -1,82 +0,0 @@ -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; - } -} diff --git a/src/B8Framework/View/Template.php b/src/B8Framework/View/Template.php deleted file mode 100644 index 717d020e..00000000 --- a/src/B8Framework/View/Template.php +++ /dev/null @@ -1,548 +0,0 @@ -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: - * - * - * {for myarray.items} - * {@item.title} - * {/for} - * - * - * Or: - * - * - * {for 0:pages.count; i++} - * {@i} - * {/for} - * - * - * @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}(); - } -} diff --git a/src/PHPCensor/Application.php b/src/PHPCensor/Application.php index a4c97605..f60fdbe9 100644 --- a/src/PHPCensor/Application.php +++ b/src/PHPCensor/Application.php @@ -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 diff --git a/src/PHPCensor/Controller.php b/src/PHPCensor/Controller.php index e25bd0cf..4ba7ea07 100644 --- a/src/PHPCensor/Controller.php +++ b/src/PHPCensor/Controller.php @@ -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}'); } } diff --git a/src/PHPCensor/Controller/BuildController.php b/src/PHPCensor/Controller/BuildController.php index a3652d3b..c3b25a5e 100644 --- a/src/PHPCensor/Controller/BuildController.php +++ b/src/PHPCensor/Controller/BuildController.php @@ -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(); diff --git a/src/PHPCensor/Controller/ProjectController.php b/src/PHPCensor/Controller/ProjectController.php index c2b0673d..a4f61d94 100644 --- a/src/PHPCensor/Controller/ProjectController.php +++ b/src/PHPCensor/Controller/ProjectController.php @@ -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; diff --git a/src/PHPCensor/Controller/UserController.php b/src/PHPCensor/Controller/UserController.php index 551a65d9..f598d130 100644 --- a/src/PHPCensor/Controller/UserController.php +++ b/src/PHPCensor/Controller/UserController.php @@ -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 */ 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; diff --git a/src/PHPCensor/Controller/WidgetAllProjectsController.php b/src/PHPCensor/Controller/WidgetAllProjectsController.php index f4013a45..8d45a836 100644 --- a/src/PHPCensor/Controller/WidgetAllProjectsController.php +++ b/src/PHPCensor/Controller/WidgetAllProjectsController.php @@ -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() diff --git a/src/PHPCensor/Controller/WidgetBuildErrorsController.php b/src/PHPCensor/Controller/WidgetBuildErrorsController.php index 7bde6c44..b0e1e73a 100644 --- a/src/PHPCensor/Controller/WidgetBuildErrorsController.php +++ b/src/PHPCensor/Controller/WidgetBuildErrorsController.php @@ -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; diff --git a/src/PHPCensor/Controller/WidgetLastBuildsController.php b/src/PHPCensor/Controller/WidgetLastBuildsController.php index 0e0f2ea0..a613a25f 100644 --- a/src/PHPCensor/Controller/WidgetLastBuildsController.php +++ b/src/PHPCensor/Controller/WidgetLastBuildsController.php @@ -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; diff --git a/src/PHPCensor/Helper/LoginIsDisabled.php b/src/PHPCensor/Helper/LoginIsDisabled.php deleted file mode 100644 index 70ed3486..00000000 --- a/src/PHPCensor/Helper/LoginIsDisabled.php +++ /dev/null @@ -1,31 +0,0 @@ - - */ -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; - } -} diff --git a/src/PHPCensor/Plugin/Email.php b/src/PHPCensor/Plugin/Email.php index c460bb45..c5a9c1b5 100644 --- a/src/PHPCensor/Plugin/Email.php +++ b/src/PHPCensor/Plugin/Email.php @@ -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 */ class Email extends Plugin diff --git a/src/PHPCensor/View.php b/src/PHPCensor/View.php new file mode 100644 index 00000000..8a648e42 --- /dev/null +++ b/src/PHPCensor/View.php @@ -0,0 +1,88 @@ +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; + } +} diff --git a/src/PHPCensor/View/Group/index.phtml b/src/PHPCensor/View/Group/index.phtml index 7bad7e89..182b4251 100644 --- a/src/PHPCensor/View/Group/index.phtml +++ b/src/PHPCensor/View/Group/index.phtml @@ -1,4 +1,10 @@ - +
@@ -22,7 +28,7 @@
- User()->getIsAdmin() && (!count($group['projects']))): ?> + getIsAdmin() && (!count($group['projects']))): ?> @@ -45,4 +51,4 @@ confirmDelete(e.target.href).onCloseConfirmed = function () {window.location = window.location.href}; }); }); - \ No newline at end of file + diff --git a/src/PHPCensor/View/Project/ajax-builds.phtml b/src/PHPCensor/View/Project/ajax-builds.phtml index a04a9787..7b74a738 100644 --- a/src/PHPCensor/View/Project/ajax-builds.phtml +++ b/src/PHPCensor/View/Project/ajax-builds.phtml @@ -6,6 +6,8 @@ use PHPCensor\Helper\Lang; +$user = $_SESSION['php-censor-user']; + ?> @@ -92,7 +94,7 @@ $branches = $build->getExtra('branches');
- User()->getIsAdmin()): ?> + getIsAdmin()): ?> diff --git a/src/PHPCensor/View/Project/view.phtml b/src/PHPCensor/View/Project/view.phtml index 50ed3dce..52ff981d 100644 --- a/src/PHPCensor/View/Project/view.phtml +++ b/src/PHPCensor/View/Project/view.phtml @@ -6,6 +6,8 @@ use PHPCensor\Helper\Lang; +$user = $_SESSION['php-censor-user']; + ?>