diff --git a/.gitignore b/.gitignore index 57a654d7..4c882368 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ /vendor /composer.phar /runtime + /app/config.yml /public/assets/vendor + /public/artifacts !/public/artifacts/.gitkeep diff --git a/composer.json b/composer.json index 113147cb..71a9790e 100644 --- a/composer.json +++ b/composer.json @@ -56,6 +56,7 @@ "symfony/debug": "~3.4.0", "symfony/dependency-injection": "~3.4.0", "symfony/event-dispatcher": "~3.4.0", + "symfony/cache": "~3.4.0", "psr/log": "~1.0.0", "monolog/monolog": "~1.22.0", "pimple/pimple": "~3.0.0", @@ -94,6 +95,9 @@ "mremi/flowdock": "For FlowdockNotify plugin" }, "extra": { + "platform": { + "php": "5.6.*" + }, "installer-paths": { "public/assets/vendor/sprintf-js": ["npm-asset/sprintf-js"], "public/assets/vendor/codemirror": ["npm-asset/codemirror"], diff --git a/composer.lock b/composer.lock index 1048f470..ac6cba9d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "68c69d9aed8a6c9ef73856c6ddc9ab23", + "content-hash": "acf206f49431b95f0fadc3a765e6930a", "packages": [ { "name": "behat/gherkin", @@ -1602,16 +1602,16 @@ }, { "name": "phpunit/phpunit", - "version": "5.7.26", + "version": "5.7.27", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "7fbc25c13309de0c4c9bb48b7361f1eca34c7fbd" + "reference": "b7803aeca3ccb99ad0a506fa80b64cd6a56bbc0c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/7fbc25c13309de0c4c9bb48b7361f1eca34c7fbd", - "reference": "7fbc25c13309de0c4c9bb48b7361f1eca34c7fbd", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/b7803aeca3ccb99ad0a506fa80b64cd6a56bbc0c", + "reference": "b7803aeca3ccb99ad0a506fa80b64cd6a56bbc0c", "shasum": "" }, "require": { @@ -1635,7 +1635,7 @@ "sebastian/global-state": "^1.1", "sebastian/object-enumerator": "~2.0", "sebastian/resource-operations": "~1.0", - "sebastian/version": "~1.0.3|~2.0", + "sebastian/version": "^1.0.6|^2.0.1", "symfony/yaml": "~2.1|~3.0|~4.0" }, "conflict": { @@ -1680,7 +1680,7 @@ "testing", "xunit" ], - "time": "2017-12-17T06:14:38+00:00" + "time": "2018-02-01T05:50:59+00:00" }, { "name": "phpunit/phpunit-mock-objects", @@ -1787,6 +1787,52 @@ ], "time": "2015-09-11T15:10:35+00:00" }, + { + "name": "psr/cache", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/cache.git", + "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/cache/zipball/d11b50ad223250cf17b86e38383413f5a6764bf8", + "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Cache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for caching libraries", + "keywords": [ + "cache", + "psr", + "psr-6" + ], + "time": "2016-08-06T20:24:11+00:00" + }, { "name": "psr/container", "version": "1.0.0", @@ -1933,6 +1979,54 @@ ], "time": "2016-10-10T12:19:37+00:00" }, + { + "name": "psr/simple-cache", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/simple-cache.git", + "reference": "753fa598e8f3b9966c886fe13f370baa45ef0e24" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/753fa598e8f3b9966c886fe13f370baa45ef0e24", + "reference": "753fa598e8f3b9966c886fe13f370baa45ef0e24", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\SimpleCache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interfaces for simple caching", + "keywords": [ + "cache", + "caching", + "psr", + "psr-16", + "simple-cache" + ], + "time": "2017-01-02T13:31:39+00:00" + }, { "name": "robmorgan/phinx", "version": "v0.8.1", @@ -2880,6 +2974,76 @@ "homepage": "https://symfony.com", "time": "2018-01-03T07:37:34+00:00" }, + { + "name": "symfony/cache", + "version": "v3.4.4", + "source": { + "type": "git", + "url": "https://github.com/symfony/cache.git", + "reference": "8dee9ec2c9824c3f4039960d679a6689ee1cbdc1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/cache/zipball/8dee9ec2c9824c3f4039960d679a6689ee1cbdc1", + "reference": "8dee9ec2c9824c3f4039960d679a6689ee1cbdc1", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8", + "psr/cache": "~1.0", + "psr/log": "~1.0", + "psr/simple-cache": "^1.0", + "symfony/polyfill-apcu": "~1.1" + }, + "conflict": { + "symfony/var-dumper": "<3.3" + }, + "provide": { + "psr/cache-implementation": "1.0", + "psr/simple-cache-implementation": "1.0" + }, + "require-dev": { + "cache/integration-tests": "dev-master", + "doctrine/cache": "~1.6", + "doctrine/dbal": "~2.4", + "predis/predis": "~1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.4-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Cache\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Cache component with PSR-6, PSR-16, and tags", + "homepage": "https://symfony.com", + "keywords": [ + "caching", + "psr6" + ], + "time": "2018-01-18T22:16:57+00:00" + }, { "name": "symfony/config", "version": "v3.4.4", @@ -3409,17 +3573,73 @@ "time": "2018-01-03T07:37:34+00:00" }, { - "name": "symfony/polyfill-mbstring", - "version": "v1.6.0", + "name": "symfony/polyfill-apcu", + "version": "v1.7.0", "source": { "type": "git", - "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296" + "url": "https://github.com/symfony/polyfill-apcu.git", + "reference": "e8ae2136ddb53dea314df56fcd88e318ab936c00" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296", - "reference": "2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296", + "url": "https://api.github.com/repos/symfony/polyfill-apcu/zipball/e8ae2136ddb53dea314df56fcd88e318ab936c00", + "reference": "e8ae2136ddb53dea314df56fcd88e318ab936c00", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.7-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Apcu\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting apcu_* functions to lower PHP versions", + "homepage": "https://symfony.com", + "keywords": [ + "apcu", + "compatibility", + "polyfill", + "portable", + "shim" + ], + "time": "2018-01-30T19:27:44+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.7.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "78be803ce01e55d3491c1397cf1c64beb9c1b63b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/78be803ce01e55d3491c1397cf1c64beb9c1b63b", + "reference": "78be803ce01e55d3491c1397cf1c64beb9c1b63b", "shasum": "" }, "require": { @@ -3431,7 +3651,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.6-dev" + "dev-master": "1.7-dev" } }, "autoload": { @@ -3465,7 +3685,7 @@ "portable", "shim" ], - "time": "2017-10-11T12:05:26+00:00" + "time": "2018-01-30T19:27:44+00:00" }, { "name": "symfony/process", diff --git a/runtime/cache/.gitkeep b/runtime/cache/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/src/B8Framework/Cache.php b/src/B8Framework/Cache.php deleted file mode 100644 index 0fc477cd..00000000 --- a/src/B8Framework/Cache.php +++ /dev/null @@ -1,31 +0,0 @@ -isEnabled()) { - return $default; - } - - $success = false; - $rtn = apc_fetch($key, $success); - if (!$success) { - $rtn = $default; - } - - return $rtn; - } - - /** - * Add an item to the cache: - * - * @param string $key - * @param mixed $value - * @param integer $ttl - * - * @return array|bool - */ - public function set($key, $value = null, $ttl = 0) - { - if (!$this->isEnabled()) { - return false; - } - - return apc_store($key, $value, $ttl); - } - - /** - * Remove an item from the cache: - * - * @param string $key - * - * @return bool|string[] - */ - public function delete($key) - { - if (!$this->isEnabled()) { - return false; - } - - return apc_delete($key); - } - - /** - * Check if an item is in the cache: - * - * @param string $key - * - * @return bool|string[] - */ - public function contains($key) - { - if (!$this->isEnabled()) { - return false; - } - - return apc_exists($key); - } - - /** - * Short-hand syntax for get() - * - * @see Config::get() - * - * @param string $key - * - * @return mixed - */ - public function __get($key) - { - return $this->get($key, null); - } - - /** - * Short-hand syntax for set() - * - * @see Config::set() - * - * @param string $key - * @param mixed $value - * - * @return array|bool - */ - public function __set($key, $value = null) - { - return $this->set($key, $value); - } - - /** - * Is set - * - * @param string $key - * - * @return bool|string[] - */ - public function __isset($key) - { - return $this->contains($key); - } - - /** - * Unset - * - * @param string $key - */ - public function __unset($key) - { - $this->delete($key); - } -} diff --git a/src/B8Framework/Cache/RequestCache.php b/src/B8Framework/Cache/RequestCache.php deleted file mode 100644 index 97b5a77c..00000000 --- a/src/B8Framework/Cache/RequestCache.php +++ /dev/null @@ -1,90 +0,0 @@ -contains($key) ? $this->data[$key] : $default; - } - - /** - * Add an item to the cache: - */ - public function set($key, $value = null, $ttl = 0) - { - $this->data[$key] = $value; - - return $this; - } - - /** - * Remove an item from the cache: - */ - public function delete($key) - { - if ($this->contains($key)) { - unset($this->data[$key]); - } - - return $this; - } - - /** - * Check if an item is in the cache: - */ - public function contains($key) - { - return array_key_exists($key, $this->data); - } - - /** - * Short-hand syntax for get() - * @see Config::get() - */ - public function __get($key) - { - return $this->get($key, null); - } - - /** - * Short-hand syntax for set() - * @see Config::set() - */ - public function __set($key, $value = null) - { - return $this->set($key, $value); - } - - /** - * Is set - */ - public function __isset($key) - { - return $this->contains($key); - } - - /** - * Unset - */ - public function __unset($key) - { - $this->delete($key); - } -} 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 90e62645..37813a52 100644 --- a/src/B8Framework/Form/Input.php +++ b/src/B8Framework/Form/Input.php @@ -2,24 +2,24 @@ namespace b8\Form; -use b8\View; +use PHPCensor\View; class Input extends Element { /** * @var boolean */ - protected $_required = false; + protected $required = false; /** * @var string */ - protected $_pattern; + protected $pattern; /** * @var callable */ - protected $_validator; + protected $validator; /** * @var mixed @@ -29,12 +29,12 @@ class Input extends Element /** * @var string */ - protected $_error; + protected $error; /** * @var boolean */ - protected $_customError = false; + protected $customError = false; /** * @param string $name @@ -78,7 +78,7 @@ class Input extends Element */ public function getRequired() { - return $this->_required; + return $this->required; } /** @@ -88,7 +88,7 @@ class Input extends Element */ public function setRequired($required) { - $this->_required = (bool)$required; + $this->required = (bool)$required; return $this; } @@ -98,7 +98,7 @@ class Input extends Element */ public function getValidator() { - return $this->_validator; + return $this->validator; } /** @@ -109,7 +109,7 @@ class Input extends Element public function setValidator($validator) { if (is_callable($validator) || $validator instanceof \Closure) { - $this->_validator = $validator; + $this->validator = $validator; } return $this; @@ -120,7 +120,7 @@ class Input extends Element */ public function getPattern() { - return $this->_pattern; + return $this->pattern; } /** @@ -130,7 +130,7 @@ class Input extends Element */ public function setPattern($pattern) { - $this->_pattern = $pattern; + $this->pattern = $pattern; return $this; } @@ -141,12 +141,12 @@ class Input extends Element public function validate() { if ($this->getRequired() && empty($this->value)) { - $this->_error = $this->getLabel() . ' is required.'; + $this->error = $this->getLabel() . ' is required.'; return false; } if ($this->getPattern() && !preg_match('/' . $this->getPattern() . '/', $this->value)) { - $this->_error = 'Invalid value entered.'; + $this->error = 'Invalid value entered.'; return false; } @@ -157,13 +157,13 @@ class Input extends Element try { call_user_func_array($validator, [$this->value]); } catch (\Exception $ex) { - $this->_error = $ex->getMessage(); + $this->error = $ex->getMessage(); return false; } } - if ($this->_customError) { + if ($this->customError) { return false; } @@ -177,8 +177,8 @@ class Input extends Element */ public function setError($message) { - $this->_customError = true; - $this->_error = $message; + $this->customError = true; + $this->error = $message; return $this; } @@ -189,8 +189,8 @@ class Input extends Element protected function onPreRender(View &$view) { $view->value = $this->getValue(); - $view->error = $this->_error; - $view->pattern = $this->_pattern; - $view->required = $this->_required; + $view->error = $this->error; + $view->pattern = $this->pattern; + $view->required = $this->required; } } diff --git a/src/B8Framework/Model.php b/src/B8Framework/Model.php index 6b4c3d3d..a269c346 100644 --- a/src/B8Framework/Model.php +++ b/src/B8Framework/Model.php @@ -3,6 +3,7 @@ namespace b8; use b8\Exception\HttpException; +use Symfony\Component\Cache\Simple\ArrayCache; class Model { @@ -23,7 +24,7 @@ class Model $this->data = array_merge($this->data, $initialData); } - $this->cache = Cache::getCache(Cache::TYPE_REQUEST); + $this->cache = new ArrayCache(); } /** diff --git a/src/B8Framework/Type/CacheInterface.php b/src/B8Framework/Type/CacheInterface.php deleted file mode 100644 index 99097c78..00000000 --- a/src/B8Framework/Type/CacheInterface.php +++ /dev/null @@ -1,24 +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..92e2e1bb 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 @@ -33,11 +32,8 @@ class Application extends b8\Application $user = b8\Store\Factory::getStore('User')->getByPrimaryKey($_SESSION['php-censor-user-id']); if ($user) { - $_SESSION['php-censor-user'] = $user; return true; } - - unset($_SESSION['php-censor-user-id']); } return false; @@ -159,11 +155,9 @@ class Application extends b8\Application $defaultUserId = (integer)$config->get('php-censor.security.default_user_id', 1); if ($disableAuth && $defaultUserId) { - $user = b8\Store\Factory::getStore('User') - ->getByPrimaryKey($defaultUserId); + $user = b8\Store\Factory::getStore('User')->getByPrimaryKey($defaultUserId); if ($user) { - $_SESSION['php-censor-user'] = $user; return true; } } diff --git a/src/PHPCensor/Controller.php b/src/PHPCensor/Controller.php index e25bd0cf..02276580 100644 --- a/src/PHPCensor/Controller.php +++ b/src/PHPCensor/Controller.php @@ -6,22 +6,29 @@ use b8\Config; use b8\Exception\HttpException\ForbiddenException; use b8\Http\Request; use b8\Http\Response; -use b8\View; +use b8\Store\Factory; +use PHPCensor\Model\User; +use PHPCensor\Store\UserStore; 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; @@ -45,6 +52,8 @@ class Controller extends \b8\Controller $class = explode('\\', get_class($this)); $this->className = substr(array_pop($class), 0, -10); $this->setControllerView(); + + unset($_SESSION['php-censor-user']); } /** @@ -55,7 +64,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}'); } } @@ -118,6 +127,26 @@ class Controller extends \b8\Controller */ protected function currentUserIsAdmin() { - return $_SESSION['php-censor-user']->getIsAdmin(); + $user = $this->getUser(); + if (!$user) { + return false; + } + + return $this->getUser()->getIsAdmin(); + } + + /** + * @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']); } } diff --git a/src/PHPCensor/Controller/BuildController.php b/src/PHPCensor/Controller/BuildController.php index a3652d3b..29382310 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. @@ -72,7 +73,7 @@ class BuildController extends Controller } /** @var User $user */ - $user = $_SESSION['php-censor-user']; + $user = $this->getUser(); $perPage = $user->getFinalPerPage(); $data = $this->getBuildData($build, $plugin, $severity, $isNew, (($page - 1) * $perPage), $perPage); $pages = ($data['errors'] === 0) @@ -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/GroupController.php b/src/PHPCensor/Controller/GroupController.php index 739ee9ce..12664d71 100644 --- a/src/PHPCensor/Controller/GroupController.php +++ b/src/PHPCensor/Controller/GroupController.php @@ -74,7 +74,7 @@ class GroupController extends Controller $group->setTitle($this->getParam('title')); if (is_null($groupId)) { /** @var User $user */ - $user = $_SESSION['php-censor-user']; + $user = $this->getUser(); $group->setCreateDate(new \DateTime()); $group->setUserId($user->getId()); diff --git a/src/PHPCensor/Controller/ProjectController.php b/src/PHPCensor/Controller/ProjectController.php index c2b0673d..7bb5a75f 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. @@ -95,7 +96,7 @@ class ProjectController extends PHPCensor\Controller } /** @var PHPCensor\Model\User $user */ - $user = $_SESSION['php-censor-user']; + $user = $this->getUser(); $perPage = $user->getFinalPerPage(); $builds = $this->getLatestBuildsHtml($projectId, $branch, $environment, (($page - 1) * $perPage), $perPage); $pages = ($builds[1] === 0) @@ -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); @@ -208,7 +209,7 @@ class ProjectController extends PHPCensor\Controller } /** @var PHPCensor\Model\User $user */ - $user = $_SESSION['php-censor-user']; + $user = $this->getUser(); $build = $this->buildService->createBuild( $project, $environment, @@ -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; @@ -336,7 +337,7 @@ class ProjectController extends PHPCensor\Controller ]; /** @var PHPCensor\Model\User $user */ - $user = $_SESSION['php-censor-user']; + $user = $this->getUser(); $project = $this->projectService->createProject($title, $type, $reference, $user->getId(), $options); $response = new RedirectResponse(); @@ -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/SessionController.php b/src/PHPCensor/Controller/SessionController.php index fb60230d..122ad509 100644 --- a/src/PHPCensor/Controller/SessionController.php +++ b/src/PHPCensor/Controller/SessionController.php @@ -11,7 +11,7 @@ use PHPCensor\Store\UserStore; /** * Session Controller - Handles user login / logout. - * + * * @author Dan Cryer */ class SessionController extends Controller @@ -53,7 +53,7 @@ class SessionController extends Controller return $response; } } - + $isLoginFailure = false; if ($this->request->getMethod() == 'POST') { @@ -92,7 +92,7 @@ class SessionController extends Controller if ($rememberMe) { $rememberKey = md5(microtime(true)); - + $user->setRememberKey($rememberKey); $this->userStore->save($user); @@ -161,7 +161,6 @@ class SessionController extends Controller */ public function logout() { - unset($_SESSION['php-censor-user']); unset($_SESSION['php-censor-user-id']); session_destroy(); @@ -233,7 +232,8 @@ class SessionController extends Controller $hash = password_hash($this->getParam('password'), PASSWORD_DEFAULT); $user->setHash($hash); - $_SESSION['php-censor-user'] = $this->userStore->save($user); + $this->userStore->save($user); + $_SESSION['php-censor-user-id'] = $user->getId(); $response = new b8\Http\Response\RedirectResponse(); diff --git a/src/PHPCensor/Controller/UserController.php b/src/PHPCensor/Controller/UserController.php index 551a65d9..c4603aca 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 @@ -55,13 +56,13 @@ class UserController extends Controller public function profile() { /** @var User $user */ - $user = $_SESSION['php-censor-user']; + $user = $this->getUser(); if ($this->request->getMethod() == 'POST') { $name = $this->getParam('name', null); $email = $this->getParam('email', null); $password = $this->getParam('password', null); - + $language = $this->getParam('language', null); if (!$language) { $language = null; @@ -72,8 +73,7 @@ class UserController extends Controller $perPage = null; } - $_SESSION['php-censor-user'] = $this->userService->updateUser($user, $name, $email, $password, null, $language, $perPage); - $user = $_SESSION['php-censor-user']; + $user = $this->userService->updateUser($user, $name, $email, $password, null, $language, $perPage); $this->view->updated = 1; } @@ -166,7 +166,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 +208,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/Github.php b/src/PHPCensor/Helper/Github.php index 3e19b848..0f8874a3 100644 --- a/src/PHPCensor/Helper/Github.php +++ b/src/PHPCensor/Helper/Github.php @@ -2,9 +2,9 @@ namespace PHPCensor\Helper; -use b8\Cache; use b8\Config; use GuzzleHttp\Client; +use Symfony\Component\Cache\Simple\FilesystemCache; /** * The Github Helper class provides some Github API call functionality. @@ -26,10 +26,10 @@ class Github $response = $client->get(('https://api.github.com' . $url), [ 'query' => $params, ]); - + $body = json_decode($response->getBody(), true); $headers = $response->getHeaders(); - + foreach ($body as $item) { $results[] = $item; } @@ -62,8 +62,8 @@ class Github return []; } - $cache = Cache::getCache(Cache::TYPE_APC); - $rtn = $cache->get('php-censor-github-repos'); + $cache = new FilesystemCache('', 0, RUNTIME_DIR . 'cache'); + $rtn = $cache->get('php-censor.github-repos'); if (!$rtn) { $client = new Client(); @@ -90,7 +90,7 @@ class Github } } - $cache->set('php-censor-github-repos', $rtn); + $cache->set('php-censor.github-repos', $rtn, 3600); } return $rtn; diff --git a/src/PHPCensor/Helper/Lang.php b/src/PHPCensor/Helper/Lang.php index 106b6ca5..300ee1b4 100644 --- a/src/PHPCensor/Helper/Lang.php +++ b/src/PHPCensor/Helper/Lang.php @@ -3,7 +3,9 @@ namespace PHPCensor\Helper; use b8\Config; +use b8\Store\Factory; use PHPCensor\Model\User; +use PHPCensor\Store\UserStore; /** * Languages Helper Class - Handles loading strings files and the strings within them. @@ -11,7 +13,7 @@ use PHPCensor\Model\User; class Lang { const DEFAULT_LANGUAGE = 'en'; - + /** * @var string */ @@ -99,7 +101,7 @@ class Lang $languages = []; foreach (self::$languages as $language) { $strings = include(SRC_DIR . 'Languages' . DIRECTORY_SEPARATOR . 'lang.' . $language . '.php'); - $languages[$language] = !empty($strings['language_name']) + $languages[$language] = !empty($strings['language_name']) ? $strings['language_name'] . ' (' . $language . ')' : $language; } @@ -132,11 +134,11 @@ class Lang return; } - /** @var User $user */ - $user = !empty($_SESSION['php-censor-user']) ? $_SESSION['php-censor-user'] : null; - - if ((!is_object($user) || get_class($user) == '__PHP_Incomplete_Class') && gettype($user) == 'object') { - $user = unserialize(serialize($_SESSION['php-censor-user'])); + $user = null; + if (!empty($_SESSION['php-censor-user-id'])) { + /** @var UserStore $userStore */ + $userStore = Factory::getStore('User'); + $user = $userStore->getById($_SESSION['php-censor-user-id']); } if ($user) { @@ -173,7 +175,7 @@ class Lang if (is_null($strings) || !is_array($strings) || !count($strings)) { return null; } - + return $strings; } 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/Helper/User.php b/src/PHPCensor/Helper/User.php deleted file mode 100644 index 6ca3d25f..00000000 --- a/src/PHPCensor/Helper/User.php +++ /dev/null @@ -1,32 +0,0 @@ - - */ -class User -{ - /** - * Proxies method calls through to the current active user model. - * @param $method - * @param array $params - * @return mixed|null - */ - public function __call($method, $params = []) - { - if (empty($_SESSION['php-censor-user'])) { - return null; - } - - $user = $_SESSION['php-censor-user']; - - if (!is_object($user)) { - return null; - } - - return call_user_func_array([$user, $method], $params); - } -} diff --git a/src/PHPCensor/Model/BuildError.php b/src/PHPCensor/Model/BuildError.php index bfb0bc20..5b7bbf1c 100644 --- a/src/PHPCensor/Model/BuildError.php +++ b/src/PHPCensor/Model/BuildError.php @@ -397,8 +397,8 @@ class BuildError extends Model return null; } - $cacheKey = 'Cache.Build.' . $key; - $rtn = $this->cache->get($cacheKey, null); + $cacheKey = 'php-censor.build-' . $key; + $rtn = $this->cache->get($cacheKey); if (empty($rtn)) { $rtn = Factory::getStore('Build', 'PHPCensor')->getById($key); diff --git a/src/PHPCensor/Model/BuildMeta.php b/src/PHPCensor/Model/BuildMeta.php index 0efa761c..1c2881a2 100644 --- a/src/PHPCensor/Model/BuildMeta.php +++ b/src/PHPCensor/Model/BuildMeta.php @@ -181,8 +181,8 @@ class BuildMeta extends Model return null; } - $cacheKey = 'Cache.Build.' . $key; - $rtn = $this->cache->get($cacheKey, null); + $cacheKey = 'php-censor.build-' . $key; + $rtn = $this->cache->get($cacheKey); if (empty($rtn)) { $rtn = Factory::getStore('Build', 'PHPCensor')->getById($key); diff --git a/src/PHPCensor/Model/Project.php b/src/PHPCensor/Model/Project.php index bf461b7a..c6ad79be 100644 --- a/src/PHPCensor/Model/Project.php +++ b/src/PHPCensor/Model/Project.php @@ -453,8 +453,8 @@ class Project extends Model return null; } - $cacheKey = 'Cache.ProjectGroup.' . $key; - $rtn = $this->cache->get($cacheKey, null); + $cacheKey = 'php-censor.project-group-' . $key; + $rtn = $this->cache->get($cacheKey); if (empty($rtn)) { $rtn = Factory::getStore('ProjectGroup', 'PHPCensor')->getById($key); @@ -741,8 +741,8 @@ class Project extends Model return null; } - $cacheKey = 'Cache.ProjectEnvironments.' . $key; - $rtn = $this->cache->get($cacheKey, null); + $cacheKey = 'php-censor.project-environments-' . $key; + $rtn = $this->cache->get($cacheKey); if (empty($rtn)) { $store = $this->getEnvironmentStore(); 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..fe2fc428 --- /dev/null +++ b/src/PHPCensor/View.php @@ -0,0 +1,105 @@ +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; + } + + /** + * @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']); + } +} diff --git a/src/PHPCensor/View/Group/index.phtml b/src/PHPCensor/View/Group/index.phtml index 7bad7e89..7d20dde3 100644 --- a/src/PHPCensor/View/Group/index.phtml +++ b/src/PHPCensor/View/Group/index.phtml @@ -1,4 +1,8 @@ - +
@@ -22,7 +26,7 @@
- User()->getIsAdmin() && (!count($group['projects']))): ?> + getUser()->getIsAdmin() && (!count($group['projects']))): ?> @@ -45,4 +49,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..940aeeda 100644 --- a/src/PHPCensor/View/Project/ajax-builds.phtml +++ b/src/PHPCensor/View/Project/ajax-builds.phtml @@ -92,7 +92,7 @@ $branches = $build->getExtra('branches');
- User()->getIsAdmin()): ?> + getUser()->getIsAdmin()): ?> diff --git a/src/PHPCensor/View/Project/view.phtml b/src/PHPCensor/View/Project/view.phtml index 50ed3dce..919aba25 100644 --- a/src/PHPCensor/View/Project/view.phtml +++ b/src/PHPCensor/View/Project/view.phtml @@ -29,7 +29,7 @@ use PHPCensor\Helper\Lang; ?>
getArchived()): ?> - User()->getIsAdmin()): ?> + getUser()->getIsAdmin()): ?> diff --git a/src/PHPCensor/View/User/index.phtml b/src/PHPCensor/View/User/index.phtml index 6eb77906..0c113a0e 100644 --- a/src/PHPCensor/View/User/index.phtml +++ b/src/PHPCensor/View/User/index.phtml @@ -1,4 +1,10 @@ - +getUser(); + +?>
@@ -39,7 +45,7 @@ getName()); ?> - User()->getIsAdmin()): ?> + getIsAdmin()): ?>
- User()->getIsAdmin()): ?> + getUser()->getIsAdmin()): ?>
Message: getMessage(); ?>
File: getFile(); ?>
diff --git a/src/PHPCensor/View/layout.phtml b/src/PHPCensor/View/layout.phtml index e5fba66b..75d1f5de 100644 --- a/src/PHPCensor/View/layout.phtml +++ b/src/PHPCensor/View/layout.phtml @@ -1,4 +1,10 @@ - +getUser(); + +?> @@ -87,19 +93,19 @@ - LoginIsDisabled()->check()): ?> + LoginIsDisabled()): ?>