Merge branch 'develop' into feature/dependencies

This commit is contained in:
Flavio Copes
2016-03-01 11:28:20 +01:00
48 changed files with 353 additions and 315 deletions

View File

@@ -5,6 +5,7 @@ use Closure;
use Exception;
use FilesystemIterator;
use Grav\Common\Config\Config;
use Grav\Common\Grav;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RegexIterator;
@@ -23,8 +24,6 @@ define('JS_ASSET', false);
*/
class Assets
{
use GravTrait;
/** @const Regex to match CSS and JavaScript files */
const DEFAULT_REGEX = '/.\.(css|js)$/i';
@@ -167,7 +166,7 @@ class Assets
// Set timestamp
if (isset($config['enable_asset_timestamp']) && $config['enable_asset_timestamp'] === true) {
$this->timestamp = '?' . self::getGrav()['cache']->getKey();
$this->timestamp = '?' . Grav::instance()['cache']->getKey();
}
return $this;
@@ -179,12 +178,12 @@ class Assets
public function init()
{
/** @var Config $config */
$config = self::getGrav()['config'];
$base_url = self::getGrav()['base_url'];
$config = Grav::instance()['config'];
$base_url = Grav::instance()['base_url'];
$asset_config = (array)$config->get('system.assets');
/** @var UniformResourceLocator $locator */
$locator = self::$grav['locator'];
$locator = Grav::instance()['locator'];
$this->assets_dir = $locator->findResource('asset://') . DS;
$this->assets_url = $locator->findResource('asset://', false);
@@ -499,7 +498,7 @@ class Assets
}
// Sort array by priorities (larger priority first)
if (self::getGrav()) {
if (Grav::instance()) {
uasort($this->css, function ($a, $b) {
if ($a['priority'] == $b['priority']) {
return $b['order'] - $a['order'];
@@ -640,7 +639,7 @@ class Assets
protected function pipelineCss($group = 'head')
{
/** @var Cache $cache */
$cache = self::getGrav()['cache'];
$cache = Grav::instance()['cache'];
$key = '?' . $cache->getKey();
// temporary list of assets to pipeline
@@ -711,7 +710,7 @@ class Assets
protected function pipelineJs($group = 'head')
{
/** @var Cache $cache */
$cache = self::getGrav()['cache'];
$cache = Grav::instance()['cache'];
$key = '?' . $cache->getKey();
// temporary list of assets to pipeline
@@ -901,7 +900,7 @@ class Assets
// Check if $directory is a stream.
if (strpos($directory, '://')) {
$directory = self::$grav['locator']->findResource($directory, null);
$directory = Grav::instance()['locator']->findResource($directory, null);
}
// Get files
@@ -963,7 +962,7 @@ class Assets
protected function buildLocalLink($asset)
{
try {
$asset = self::getGrav()['locator']->findResource($asset, false);
$asset = Grav::instance()['locator']->findResource($asset, false);
} catch (\Exception $e) {
}

View File

@@ -1,7 +1,7 @@
<?php
namespace Grav\Common\Backup;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use Grav\Common\Filesystem\Folder;
use Grav\Common\Inflector;
@@ -13,8 +13,6 @@ use Grav\Common\Inflector;
*/
class ZipBackup
{
use GravTrait;
protected static $ignorePaths = [
'backup',
'cache',
@@ -40,7 +38,7 @@ class ZipBackup
public static function backup($destination = null, callable $messager = null)
{
if (!$destination) {
$destination = self::getGrav()['locator']->findResource('backup://', true);
$destination = Grav::instance()['locator']->findResource('backup://', true);
if (!$destination)
throw new \RuntimeException('The backup folder is missing.');
@@ -48,7 +46,7 @@ class ZipBackup
Folder::mkdir($destination);
}
$name = self::getGrav()['config']->get('site.title', basename(GRAV_ROOT));
$name = Grav::instance()['config']->get('site.title', basename(GRAV_ROOT));
$inflector = new Inflector();

View File

@@ -4,6 +4,7 @@ namespace Grav\Common;
use \Doctrine\Common\Cache as DoctrineCache;
use Grav\Common\Config\Config;
use Grav\Common\Filesystem\Folder;
use Grav\Common\Grav;
/**
* The GravCache object is used throughout Grav to store and retrieve cached data.
@@ -21,8 +22,6 @@ use Grav\Common\Filesystem\Folder;
*/
class Cache extends Getters
{
use GravTrait;
/**
* @var string Cache key.
*/
@@ -228,6 +227,32 @@ class Cache extends Getters
}
}
/**
* Deletes an item in the cache based on the id
*
* @param $id the id of the cached data entry
* @return bool true if the item was deleted successfully
*/
public function delete($id)
{
if ($this->enabled) {
return $this->driver->delete($id);
}
}
/**
* Returns a boolean state of whether or not the item exists in the cache based on id key
*
* @param $id the id of the cached data entry
* @return bool true if the cached items exists
*/
public function contains($id)
{
if ($this->enabled) {
return $this->driver->contains(($id));
}
}
/**
* Getter method to get the cache key
*/
@@ -245,7 +270,7 @@ class Cache extends Getters
*/
public static function clearCache($remove = 'standard')
{
$locator = self::getGrav()['locator'];
$locator = Grav::instance()['locator'];
$output = [];
$user_config = USER_DIR . 'config/system.yaml';

View File

@@ -1,7 +1,7 @@
<?php
namespace Grav\Common\Data;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use RocketTheme\Toolbox\ArrayTraits\Export;
use RocketTheme\Toolbox\ArrayTraits\ExportInterface;
use RocketTheme\Toolbox\ArrayTraits\NestedArrayAccessWithGetters;
@@ -14,7 +14,7 @@ use RocketTheme\Toolbox\ArrayTraits\NestedArrayAccessWithGetters;
*/
class Blueprint implements \ArrayAccess, ExportInterface
{
use Export, NestedArrayAccessWithGetters, GravTrait;
use Export, NestedArrayAccessWithGetters;
public $name;
@@ -78,7 +78,7 @@ class Blueprint implements \ArrayAccess, ExportInterface
try {
$this->validateArray($data, $this->nested);
} catch (\RuntimeException $e) {
$language = self::getGrav()['language'];
$language = Grav::instance()['language'];
$message = sprintf($language->translate('FORM.VALIDATION_FAIL', null, true) . ' %s', $e->getMessage());
throw new \RuntimeException($message);
}
@@ -404,7 +404,7 @@ class Blueprint implements \ArrayAccess, ExportInterface
elseif (substr($name, 0, 8) == '@config-') {
$property = substr($name, 8);
$default = isset($field[$property]) ? $field[$property] : null;
$config = self::getGrav()['config']->get($value, $default);
$config = Grav::instance()['config']->get($value, $default);
if (!is_null($config)) {
$field[$property] = $config;
@@ -467,7 +467,7 @@ class Blueprint implements \ArrayAccess, ExportInterface
&& $field['validate']['required'] === true
&& empty($data[$name])) {
$value = isset($field['label']) ? $field['label'] : $field['name'];
$language = self::getGrav()['language'];
$language = Grav::instance()['language'];
$message = sprintf($language->translate('FORM.MISSING_REQUIRED_FIELD', null, true) . ' %s', $value);
throw new \RuntimeException($message);
}

View File

@@ -2,7 +2,7 @@
namespace Grav\Common\Data;
use Grav\Common\File\CompiledYamlFile;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
/**
@@ -13,8 +13,6 @@ use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
*/
class Blueprints
{
use GravTrait;
protected $search;
protected $types;
protected $instances = array();
@@ -43,7 +41,7 @@ class Blueprints
// Check if search is a stream and resolve the path.
if (strpos($filename, '://')) {
$grav = static::getGrav();
$grav = Grav::instance();
/** @var UniformResourceLocator $locator */
$locator = $grav['locator'];
$parents = $locator->findResources($filename);
@@ -122,7 +120,7 @@ class Blueprints
// Check if search is a stream.
if (strpos($this->search, '://')) {
// Stream: use UniformResourceIterator.
$grav = static::getGrav();
$grav = Grav::instance();
/** @var UniformResourceLocator $locator */
$locator = $grav['locator'];
$iterator = $locator->getIterator($this->search, null);

View File

@@ -1,7 +1,7 @@
<?php
namespace Grav\Common\Data;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;
@@ -13,8 +13,6 @@ use Symfony\Component\Yaml\Parser;
*/
class Validation
{
use GravTrait;
/**
* Validate value against a blueprint field definition.
*
@@ -37,7 +35,7 @@ class Validation
}
// Get language class
$language = self::getGrav()['language'];
$language = Grav::instance()['language'];
// Validate type with fallback type text.
$type = (string) isset($field['validate']['type']) ? $field['validate']['type'] : $field['type'];
@@ -345,7 +343,7 @@ class Validation
protected static function filterDateTime($value, array $params, array $field)
{
$format = self::getGrav()['config']->get('system.pages.dateformat.default');
$format = Grav::instance()['config']->get('system.pages.dateformat.default');
if ($format) {
$converted = new \DateTime($value);
return $converted->format($format);

View File

@@ -1,7 +1,7 @@
<?php
namespace Grav\Common\Filesystem;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
/**
* Class RecursiveFolderFilterIterator
@@ -9,8 +9,6 @@ use Grav\Common\GravTrait;
*/
class RecursiveFolderFilterIterator extends \RecursiveFilterIterator
{
use GravTrait;
protected static $folder_ignores;
/**
@@ -22,7 +20,7 @@ class RecursiveFolderFilterIterator extends \RecursiveFilterIterator
{
parent::__construct($iterator);
if (empty($this::$folder_ignores)) {
$this::$folder_ignores = self::getGrav()['config']->get('system.pages.ignore_folders');
$this::$folder_ignores = Grav::instance()['config']->get('system.pages.ignore_folders');
}
}

View File

@@ -1,13 +1,11 @@
<?php
namespace Grav\Common\GPM;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use Grav\Common\Iterator;
abstract class AbstractCollection extends Iterator {
use GravTrait;
abstract class AbstractCollection extends Iterator
{
public function toJson()
{
$items = [];

View File

@@ -1,13 +1,11 @@
<?php
namespace Grav\Common\GPM\Common;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use Grav\Common\Iterator;
abstract class AbstractPackageCollection extends Iterator {
use GravTrait;
abstract class AbstractPackageCollection extends Iterator
{
protected $type;
public function toJson()

View File

@@ -20,7 +20,7 @@ class GPM extends Iterator
private $repository;
/**
* @var Remote\Grav
* @var Remote\GravCore
*/
public $grav;
@@ -42,7 +42,7 @@ class GPM extends Iterator
$this->installed = new Local\Packages();
try {
$this->repository = new Remote\Packages($refresh, $callback);
$this->grav = new Remote\Grav($refresh, $callback);
$this->grav = new Remote\GravCore($refresh, $callback);
} catch (\Exception $e) {
}
}

View File

@@ -1,6 +1,8 @@
<?php
namespace Grav\Common\GPM\Local;
use Grav\Common\Grav;
/**
* Class Plugins
* @package Grav\Common\GPM\Local
@@ -17,6 +19,6 @@ class Plugins extends AbstractPackageCollection
*/
public function __construct()
{
parent::__construct(self::getGrav()['plugins']->all());
parent::__construct(Grav::instance()['plugins']->all());
}
}

View File

@@ -1,6 +1,8 @@
<?php
namespace Grav\Common\GPM\Local;
use Grav\Common\Grav;
/**
* Class Themes
* @package Grav\Common\GPM\Local
@@ -17,6 +19,6 @@ class Themes extends AbstractPackageCollection
*/
public function __construct()
{
parent::__construct(self::getGrav()['themes']->all());
parent::__construct(Grav::instance()['themes']->all());
}
}

View File

@@ -1,10 +1,15 @@
<?php
namespace Grav\Common\GPM\Remote;
use Grav\Common\Grav;
use Grav\Common\GPM\Common\AbstractPackageCollection as BaseCollection;
use Grav\Common\GPM\Response;
use \Doctrine\Common\Cache\FilesystemCache;
/**
* Class AbstractPackageCollection
* @package Grav\Common\GPM\Remote
*/
class AbstractPackageCollection extends BaseCollection
{
/**
@@ -23,13 +28,20 @@ class AbstractPackageCollection extends BaseCollection
protected $cache;
/**
* AbstractPackageCollection constructor.
*
* @param null $repository
* @param bool $refresh
* @param null $callback
*/
public function __construct($repository = null, $refresh = false, $callback = null)
{
if ($repository === null) {
throw new \RuntimeException("A repository is required to indicate the origin of the remote collection");
}
$cache_dir = self::getGrav()['locator']->findResource('cache://gpm', true, true);
$cache_dir = Grav::instance()['locator']->findResource('cache://gpm', true, true);
$this->cache = new FilesystemCache($cache_dir);
$this->repository = $repository;

View File

@@ -1,9 +1,10 @@
<?php
namespace Grav\Common\GPM\Remote;
use Grav\Common\Grav;
use \Doctrine\Common\Cache\FilesystemCache;
class Grav extends AbstractPackageCollection
class GravCore extends AbstractPackageCollection
{
protected $repository = 'https://getgrav.org/downloads/grav.json';
private $data;
@@ -17,7 +18,7 @@ class Grav extends AbstractPackageCollection
*/
public function __construct($refresh = false, $callback = null)
{
$cache_dir = self::getGrav()['locator']->findResource('cache://gpm', true, true);
$cache_dir = Grav::instance()['locator']->findResource('cache://gpm', true, true);
$this->cache = new FilesystemCache($cache_dir);
$this->raw = $this->cache->fetch(md5($this->repository));

View File

@@ -2,7 +2,7 @@
namespace Grav\Common\GPM;
use Grav\Common\Utils;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
/**
* Class Response
@@ -10,8 +10,6 @@ use Grav\Common\GravTrait;
*/
class Response
{
use GravTrait;
/**
* The callback for the progress
*
@@ -199,7 +197,7 @@ class Response
$callback = $args[2];
// if proxy set add that
$proxy_url = self::getGrav()['config']->get('system.proxy_url');
$proxy_url = Grav::instance()['config']->get('system.proxy_url');
if ($proxy_url) {
$options['fopen']['proxy'] = $proxy_url;
}
@@ -267,7 +265,7 @@ class Response
}
// if proxy set add that
$proxy_url = self::getGrav()['config']->get('system.proxy_url');
$proxy_url = Grav::instance()['config']->get('system.proxy_url');
if ($proxy_url) {
$options['curl'][CURLOPT_PROXY] = $proxy_url;
}

View File

@@ -32,7 +32,7 @@ class Upgrader
*/
public function __construct($refresh = false, $callback = null)
{
$this->remote = new Remote\Grav($refresh, $callback);
$this->remote = new Remote\GravCore($refresh, $callback);
}
/**

View File

@@ -37,6 +37,16 @@ class Grav extends Container
*/
protected static $instance;
/**
* Reset the Grav instance.
*/
public static function resetInstance()
{
if (self::$instance) {
self::$instance = null;
}
}
/**
* Return the Grav instance. Create it if it's not already instanced
*
@@ -48,9 +58,6 @@ class Grav extends Container
{
if (!self::$instance) {
self::$instance = static::load($values);
GravTrait::setGrav(self::$instance);
} elseif ($values) {
$instance = self::$instance;
foreach ($values as $key => $value) {

View File

@@ -8,28 +8,12 @@ namespace Grav\Common;
*/
trait GravTrait
{
/**
* @var Grav
*/
protected static $grav;
/**
* @return Grav
*/
public static function getGrav()
{
if (!self::$grav) {
self::$grav = Grav::instance();
}
return self::$grav;
}
/**
* @param Grav $grav
*/
public static function setGrav(Grav $grav)
{
self::$grav = $grav;
return Grav::instance();
}
}

View File

@@ -1,6 +1,8 @@
<?php
namespace Grav\Common;
use Grav\Common\Grav;
/**
* This file was originally part of the Akelos Framework
*/
@@ -20,8 +22,6 @@ namespace Grav\Common;
class Inflector
{
use GravTrait;
protected $plural;
protected $singular;
protected $uncountable;
@@ -31,7 +31,7 @@ class Inflector
public function init()
{
if (empty($this->plural)) {
$language = self::getGrav()['language'];
$language = Grav::instance()['language'];
$this->plural = $language->translate('INFLECTOR_PLURALS', null, true);
$this->singular = $language->translate('INFLECTOR_SINGULAR', null, true);
$this->uncountable = $language->translate('INFLECTOR_UNCOUNTABLE', null, true);

View File

@@ -2,6 +2,7 @@
namespace Grav\Common\Language;
use Grav\Common\Grav;
use Grav\Common\Config\Config;
/**
* Language and translation functionality for Grav
@@ -15,7 +16,10 @@ class Language
protected $fallback_languages = [];
protected $default;
protected $active = null;
/** @var Config $config */
protected $config;
protected $http_accept_language;
protected $lang_in_url = false;
@@ -406,7 +410,7 @@ class Language
}
foreach ((array)$languages as $lang) {
$translation_array = (array)$this->config->getLanguages()->get($lang . '.' . $key, null);
$translation_array = (array)Grav::instance()['languages']->get($lang . '.' . $key, null);
if ($translation_array && array_key_exists($index, $translation_array)) {
return $translation_array[$index];
}
@@ -431,7 +435,7 @@ class Language
*/
public function getTranslation($lang, $key, $array_support = false)
{
$translation = $this->config->getLanguages()->get($lang . '.' . $key, null);
$translation = Grav::instance()['languages']->get($lang . '.' . $key, null);
if (!$array_support && is_array($translation)) {
return (string)array_shift($translation);
}

View File

@@ -1,7 +1,7 @@
<?php
namespace Grav\Common\Markdown;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
use Grav\Common\Uri;
@@ -12,8 +12,6 @@ use RocketTheme\Toolbox\Event\Event;
*/
trait ParsedownGravTrait
{
use GravTrait;
/** @var Page $page */
protected $page;
@@ -39,17 +37,17 @@ trait ParsedownGravTrait
*/
protected function init($page, $defaults)
{
$grav = self::getGrav();
$grav = Grav::instance();
$this->page = $page;
$this->pages = $grav['pages'];
$this->uri = $grav['uri'];
$this->BlockTypes['{'] [] = "TwigTag";
$this->pages_dir = self::getGrav()['locator']->findResource('page://');
$this->pages_dir = Grav::instance()['locator']->findResource('page://');
$this->special_chars = ['>' => 'gt', '<' => 'lt', '"' => 'quot'];
if ($defaults === null) {
$defaults = self::getGrav()['config']->get('system.pages.markdown');
$defaults = Grav::instance()['config']->get('system.pages.markdown');
}
$this->setBreaksEnabled($defaults['auto_line_breaks']);
@@ -217,7 +215,7 @@ trait ParsedownGravTrait
$media = $this->page->media();
} else {
// see if this is an external page to this one
$base_url = rtrim(self::getGrav()['base_url_relative'] . self::getGrav()['pages']->base(), '/');
$base_url = rtrim(Grav::instance()['base_url_relative'] . Grav::instance()['pages']->base(), '/');
$page_route = '/' . ltrim(str_replace($base_url, '', $path_parts['dirname']), '/');
$ext_page = $this->pages->dispatch($page_route, true);

View File

@@ -2,7 +2,7 @@
namespace Grav\Common\Page;
use Grav\Common\Getters;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use Grav\Common\Page\Medium\Medium;
use Grav\Common\Page\Medium\MediumFactory;
@@ -15,8 +15,6 @@ use Grav\Common\Page\Medium\MediumFactory;
*/
class Media extends Getters
{
use GravTrait;
protected $gettersVariable = 'instances';
protected $path;

View File

@@ -1,15 +1,14 @@
<?php
namespace Grav\Common\Page\Medium;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use Gregwar\Image\Exceptions\GenerationError;
use RocketTheme\Toolbox\Event\Event;
use Gregwar\Image\Image;
use RocketTheme\Toolbox\Event\Event;
class ImageFile extends Image
{
use GravTrait;
/**
* Clear previously applied operations
*/
@@ -75,12 +74,12 @@ class ImageFile extends Image
throw new GenerationError($result);
}
self::getGrav()->fireEvent('onImageMediumSaved', new Event(['image' => $target]));
Grav::instance()->fireEvent('onImageMediumSaved', new Event(['image' => $target]));
};
// Asking the cache for the cacheFile
try {
$perms = self::getGrav()['config']->get('system.images.cache_perms', '0755');
$perms = Grav::instance()['config']->get('system.images.cache_perms', '0755');
$perms = octdec($perms);
$file = $this->cache->setDirectoryMode($perms)->getOrCreateFile($cacheFile, $conditions, $generate, $actual);
} catch (GenerationError $e) {

View File

@@ -1,9 +1,14 @@
<?php
namespace Grav\Common\Page\Medium;
use Grav\Common\Utils;
use Grav\Common\Data\Blueprint;
use Grav\Common\Grav;
use Grav\Common\Utils;
/**
* Class ImageMedium
* @package Grav\Common\Page\Medium
*/
class ImageMedium extends Medium
{
/**
@@ -77,7 +82,7 @@ class ImageMedium extends Medium
{
parent::__construct($items, $blueprint);
$config = self::$grav['config'];
$config = Grav::instance()['config'];
$image_info = getimagesize($this->get('filepath'));
$this->def('width', $image_info[0]);
@@ -137,8 +142,8 @@ class ImageMedium extends Medium
*/
public function url($reset = true)
{
$image_path = self::$grav['locator']->findResource('cache://images', true);
$image_dir = self::$grav['locator']->findResource('cache://images', false);
$image_path = Grav::instance()['locator']->findResource('cache://images', true);
$image_dir = Grav::instance()['locator']->findResource('cache://images', false);
$saved_image_path = $this->saveImage();
$output = preg_replace('|^' . preg_quote(GRAV_ROOT) . '|', '', $saved_image_path);
@@ -151,7 +156,7 @@ class ImageMedium extends Medium
$this->reset();
}
return self::$grav['base_url'] . $output . $this->querystring() . $this->urlHash();
return Grav::instance()['base_url'] . $output . $this->querystring() . $this->urlHash();
}
/**
@@ -470,7 +475,7 @@ class ImageMedium extends Medium
*/
protected function image()
{
$locator = self::$grav['locator'];
$locator = Grav::instance()['locator'];
$file = $this->get('filepath');
$cacheDir = $locator->findResource('cache://images', true);
@@ -506,7 +511,7 @@ class ImageMedium extends Medium
$ratio = 1;
}
$locator = self::$grav['locator'];
$locator = Grav::instance()['locator'];
$overlay = $locator->findResource("system://assets/responsive-overlays/{$ratio}x.png") ?: $locator->findResource('system://assets/responsive-overlays/unknown.png');
$this->image->merge(ImageFile::open($overlay));
}

View File

@@ -1,11 +1,14 @@
<?php
namespace Grav\Common\Page\Medium;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
/**
* Class Link
* @package Grav\Common\Page\Medium
*/
class Link implements RenderableInterface
{
use GravTrait;
use ParsedownHtmlTrait;
/**

View File

@@ -2,7 +2,7 @@
namespace Grav\Common\Page\Medium;
use Grav\Common\File\CompiledYamlFile;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use Grav\Common\Data\Data;
use Grav\Common\Data\Blueprint;
@@ -15,7 +15,6 @@ use Grav\Common\Data\Blueprint;
*/
class Medium extends Data implements RenderableInterface
{
use GravTrait;
use ParsedownHtmlTrait;
/**
@@ -60,8 +59,8 @@ class Medium extends Data implements RenderableInterface
{
parent::__construct($items, $blueprint);
if (self::getGrav()['config']->get('system.media.enable_media_timestamp', true)) {
$this->querystring('&' . self::getGrav()['cache']->getKey());
if (Grav::instance()['config']->get('system.media.enable_media_timestamp', true)) {
$this->querystring('&' . Grav::instance()['cache']->getKey());
}
$this->def('mime', 'application/octet-stream');
@@ -143,7 +142,7 @@ class Medium extends Data implements RenderableInterface
$this->reset();
}
return self::$grav['base_url'] . $output . $this->querystring() . $this->urlHash();
return Grav::instance()['base_url'] . $output . $this->querystring() . $this->urlHash();
}
/**

View File

@@ -1,7 +1,7 @@
<?php
namespace Grav\Common\Page\Medium;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use Grav\Common\Data\Blueprint;
/**
@@ -14,8 +14,6 @@ use Grav\Common\Data\Blueprint;
*/
class MediumFactory
{
use GravTrait;
/**
* Create Medium from a file
*
@@ -35,7 +33,7 @@ class MediumFactory
$ext = array_pop($parts);
$basename = implode('.', $parts);
$config = self::getGrav()['config'];
$config = Grav::instance()['config'];
$media_params = $config->get("media.".strtolower($ext));
if (!$media_params) {
@@ -59,7 +57,7 @@ class MediumFactory
'thumbnails' => []
];
$locator = self::getGrav()['locator'];
$locator = Grav::instance()['locator'];
$lookup = $locator->findResources('image://');
foreach ($lookup as $lookupPath) {

View File

@@ -2,19 +2,18 @@
namespace Grav\Common\Page;
use Exception;
use Grav\Common\Filesystem\Folder;
use Grav\Common\Config\Config;
use Grav\Common\GravTrait;
use Grav\Common\Language\Language;
use Grav\Common\Utils;
use Grav\Common\Cache;
use Grav\Common\Twig;
use Grav\Common\Uri;
use Grav\Common\Config\Config;
use Grav\Common\Data\Blueprint;
use Grav\Common\Filesystem\Folder;
use Grav\Common\Grav;
use Grav\Common\Taxonomy;
use Grav\Common\Language\Language;
use Grav\Common\Markdown\Parsedown;
use Grav\Common\Markdown\ParsedownExtra;
use Grav\Common\Data\Blueprint;
use Grav\Common\Taxonomy;
use Grav\Common\Twig;
use Grav\Common\Uri;
use Grav\Common\Utils;
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\File\MarkdownFile;
use Symfony\Component\Yaml\Exception\ParseException;
@@ -33,8 +32,6 @@ define('PAGE_ORDER_PREFIX_REGEX', '/^[0-9]+\./u');
*/
class Page
{
use GravTrait;
/**
* @var string Filename. Leave as null if page is folder.
*/
@@ -109,7 +106,7 @@ class Page
public function __construct()
{
/** @var Config $config */
$config = self::getGrav()['config'];
$config = Grav::instance()['config'];
$this->taxonomy = [];
$this->process = $config->get('system.pages.process');
@@ -128,7 +125,7 @@ class Page
*/
public function init(\SplFileInfo $file, $extension = null)
{
$config = self::getGrav()['config'];
$config = Grav::instance()['config'];
$this->hide_home_route = $config->get('system.home.hide_in_urls', false);
$this->home_route = $config->get('system.home.alias');
@@ -167,7 +164,7 @@ class Page
public function translatedLanguages()
{
$filename = substr($this->name, 0, -(strlen($this->extension())));
$config = self::getGrav()['config'];
$config = Grav::instance()['config'];
$languages = $config->get('system.languages.supported', []);
$translatedLanguages = [];
@@ -196,7 +193,7 @@ class Page
public function untranslatedLanguages()
{
$filename = substr($this->name, 0, -(strlen($this->extension())));
$config = self::getGrav()['config'];
$config = Grav::instance()['config'];
$languages = $config->get('system.languages.supported', []);
$untranslatedLanguages = [];
@@ -298,7 +295,7 @@ class Page
$this->frontmatter = $file->frontmatter();
$this->header = (object)$file->header();
} catch (ParseException $e) {
$file->raw(self::getGrav()['language']->translate([
$file->raw(Grav::instance()['language']->translate([
'FRONTMATTER_ERROR_PAGE',
$this->slug(),
$file->filename(),
@@ -434,7 +431,7 @@ class Page
*/
public function summary($size = null)
{
$config = (array)self::getGrav()['config']->get('site.summary');
$config = (array)Grav::instance()['config']->get('site.summary');
if (isset($this->header->summary)) {
$config = array_merge($config, $this->header->summary);
}
@@ -516,11 +513,11 @@ class Page
$this->media();
/** @var Config $config */
$config = self::getGrav()['config'];
$config = Grav::instance()['config'];
// Load cached content
/** @var Cache $cache */
$cache = self::getGrav()['cache'];
$cache = Grav::instance()['cache'];
$cache_id = md5('page' . $this->id());
$content_obj = $cache->fetch($cache_id);
@@ -543,7 +540,7 @@ class Page
// if no cached-content run everything
if ($this->content === false || $cache_enable === false) {
$this->content = $this->raw_content;
self::getGrav()->fireEvent('onPageContentRaw', new Event(['page' => $this]));
Grav::instance()->fireEvent('onPageContentRaw', new Event(['page' => $this]));
if ($twig_first) {
if ($process_twig) {
@@ -554,7 +551,7 @@ class Page
}
// Content Processed but not cached yet
self::getGrav()->fireEvent('onPageContentProcessed', new Event(['page' => $this]));
Grav::instance()->fireEvent('onPageContentProcessed', new Event(['page' => $this]));
} else {
if ($process_markdown) {
@@ -562,7 +559,7 @@ class Page
}
// Content Processed but not cached yet
self::getGrav()->fireEvent('onPageContentProcessed', new Event(['page' => $this]));
Grav::instance()->fireEvent('onPageContentProcessed', new Event(['page' => $this]));
if ($process_twig) {
$this->processTwig();
@@ -638,7 +635,7 @@ class Page
protected function processMarkdown()
{
/** @var Config $config */
$config = self::getGrav()['config'];
$config = Grav::instance()['config'];
$defaults = (array)$config->get('system.pages.markdown');
if (isset($this->header()->markdown)) {
@@ -666,7 +663,7 @@ class Page
*/
private function processTwig()
{
$twig = self::getGrav()['twig'];
$twig = Grav::instance()['twig'];
$this->content = $twig->processPage($this, $this->content);
}
@@ -675,7 +672,7 @@ class Page
*/
private function cachePageContent()
{
$cache = self::getGrav()['cache'];
$cache = Grav::instance()['cache'];
$cache_id = md5('page' . $this->id());
$cache->save($cache_id, ['content' => $this->content, 'content_meta' => $this->content_meta]);
}
@@ -855,7 +852,7 @@ class Page
if ($parent->route()) {
$this->route($parent->route() . '/' . $this->slug());
} else {
$this->route(self::getGrav()['pages']->root()->route() . '/' . $this->slug());
$this->route(Grav::instance()['pages']->root()->route() . '/' . $this->slug());
}
return $this;
@@ -887,11 +884,11 @@ class Page
public function blueprints()
{
/** @var Pages $pages */
$pages = self::getGrav()['pages'];
$pages = Grav::instance()['pages'];
$blueprint = $pages->blueprints($this->blueprintName());
$fields = $blueprint->fields();
$edit_mode = isset(self::getGrav()['admin']) ? self::getGrav()['config']->get('plugins.admin.edit_mode') : null;
$edit_mode = isset(Grav::instance()['admin']) ? Grav::instance()['config']->get('plugins.admin.edit_mode') : null;
// override if you only want 'normal' mode
if (empty($fields) && ($edit_mode == 'auto' || $edit_mode == 'normal')) {
@@ -996,7 +993,7 @@ class Page
public function media($var = null)
{
/** @var Cache $cache */
$cache = self::getGrav()['cache'];
$cache = Grav::instance()['cache'];
if ($var) {
$this->media = $var;
@@ -1093,7 +1090,7 @@ class Page
// if not set in the page get the value from system config
if (empty($this->url_extension)) {
$this->url_extension = trim(isset($this->header->append_url_extension) ? $this->header->append_url_extension : self::getGrav()['config']->get('system.pages.append_url_extension', false));
$this->url_extension = trim(isset($this->header->append_url_extension) ? $this->header->append_url_extension : Grav::instance()['config']->get('system.pages.append_url_extension', false));
}
return $this->url_extension;
@@ -1112,7 +1109,7 @@ class Page
$this->expires = $var;
}
return empty($this->expires) ? self::getGrav()['config']->get('system.pages.expires') : $this->expires;
return empty($this->expires) ? Grav::instance()['config']->get('system.pages.expires') : $this->expires;
}
/**
@@ -1294,7 +1291,7 @@ class Page
// if not metadata yet, process it.
if (null === $this->metadata) {
$header_tag_http_equivs = ['content-type', 'default-style', 'refresh'];
$header_tag_http_equivs = ['content-type', 'default-style', 'refresh', 'x-ua-compatible'];
$this->metadata = [];
@@ -1303,7 +1300,7 @@ class Page
$metadata['generator'] = 'GravCMS';
// Get initial metadata for the page
$metadata = array_merge($metadata, self::getGrav()['config']->get('site.metadata'));
$metadata = array_merge($metadata, Grav::instance()['config']->get('site.metadata'));
if (isset($this->header->metadata)) {
// Merge any site.metadata settings in with page metadata
@@ -1324,6 +1321,8 @@ class Page
if ($value) {
if (in_array($key, $header_tag_http_equivs)) {
$this->metadata[$key] = ['http_equiv' => $key, 'content' => htmlspecialchars($value, ENT_QUOTES)];
} elseif ($key == 'charset') {
$this->metadata[$key] = ['charset' => htmlspecialchars($value, ENT_QUOTES)];
} else {
// if it's a social metadata with separator, render as property
$separator = strpos($key, ':');
@@ -1421,16 +1420,16 @@ class Page
public function url($include_host = false, $canonical = false, $include_lang = true)
{
/** @var Pages $pages */
$pages = self::getGrav()['pages'];
$pages = Grav::instance()['pages'];
/** @var Config $config */
$config = self::getGrav()['config'];
$config = Grav::instance()['config'];
/** @var Language $language */
$language = self::getGrav()['language'];
$language = Grav::instance()['language'];
/** @var Uri $uri */
$uri = self::getGrav()['uri'];
$uri = Grav::instance()['uri'];
// get pre-route
if ($include_lang && $language->enabled()) {
@@ -1639,7 +1638,7 @@ class Page
$this->etag = $var;
}
if (!isset($this->etag)) {
$this->etag = (bool)self::getGrav()['config']->get('system.pages.etag');
$this->etag = (bool)Grav::instance()['config']->get('system.pages.etag');
}
return $this->etag;
@@ -1658,7 +1657,7 @@ class Page
$this->last_modified = $var;
}
if (!isset($this->last_modified)) {
$this->last_modified = (bool)self::getGrav()['config']->get('system.pages.last_modified');
$this->last_modified = (bool)Grav::instance()['config']->get('system.pages.last_modified');
}
return $this->last_modified;
@@ -1851,7 +1850,7 @@ class Page
}
if (empty($this->max_count)) {
/** @var Config $config */
$config = self::getGrav()['config'];
$config = Grav::instance()['config'];
$this->max_count = (int)$config->get('system.pages.list.count');
}
@@ -1939,7 +1938,7 @@ class Page
}
/** @var Pages $pages */
$pages = self::getGrav()['pages'];
$pages = Grav::instance()['pages'];
return $pages->get($this->parent);
}
@@ -1977,7 +1976,7 @@ class Page
public function children()
{
/** @var Pages $pages */
$pages = self::getGrav()['pages'];
$pages = Grav::instance()['pages'];
return $pages->children($this->path());
}
@@ -2057,8 +2056,8 @@ class Page
*/
public function active()
{
$uri_path = rtrim(self::getGrav()['uri']->path(), '/') ?: '/';
$routes = self::getGrav()['pages']->routes();
$uri_path = rtrim(Grav::instance()['uri']->path(), '/') ?: '/';
$routes = Grav::instance()['pages']->routes();
if (isset($routes[$uri_path])) {
if ($routes[$uri_path] == $this->path()) {
@@ -2078,10 +2077,10 @@ class Page
*/
public function activeChild()
{
$uri = self::getGrav()['uri'];
$pages = self::getGrav()['pages'];
$uri = Grav::instance()['uri'];
$pages = Grav::instance()['pages'];
$uri_path = rtrim($uri->path(), '/');
$routes = self::getGrav()['pages']->routes();
$routes = Grav::instance()['pages']->routes();
if (isset($routes[$uri_path])) {
/** @var Page $child_page */
@@ -2134,7 +2133,7 @@ class Page
public function find($url, $all = false)
{
/** @var Pages $pages */
$pages = self::getGrav()['pages'];
$pages = Grav::instance()['pages'];
return $pages->dispatch($url, $all);
}
@@ -2167,9 +2166,9 @@ class Page
$collection->setParams($params);
/** @var Uri $uri */
$uri = self::getGrav()['uri'];
$uri = Grav::instance()['uri'];
/** @var Config $config */
$config = self::getGrav()['config'];
$config = Grav::instance()['config'];
$process_taxonomy = isset($params['url_taxonomy_filters']) ? $params['url_taxonomy_filters'] : $config->get('system.pages.url_taxonomy_filters');
@@ -2211,7 +2210,7 @@ class Page
}
/** @var Grav $grav */
$grav = self::getGrav()['grav'];
$grav = Grav::instance()['grav'];
// New Custom event to handle things like pagination.
$grav->fireEvent('onCollectionProcessed', new Event(['collection' => $collection]));
@@ -2268,7 +2267,7 @@ class Page
}
/** @var Pages $pages */
$pages = self::getGrav()['pages'];
$pages = Grav::instance()['pages'];
$parts = explode('.', $cmd);
$current = array_shift($parts);
@@ -2362,7 +2361,7 @@ class Page
// @taxonomy: { category: [ blog, featured ], level: 1 }
/** @var Taxonomy $taxonomy_map */
$taxonomy_map = self::getGrav()['taxonomy'];
$taxonomy_map = Grav::instance()['taxonomy'];
if (!empty($parts)) {
$params = [implode('.', $parts) => $params];
@@ -2445,7 +2444,7 @@ class Page
// Do reordering.
if ($reorder && $this->order() != $this->_original->order()) {
/** @var Pages $pages */
$pages = self::getGrav()['pages'];
$pages = Grav::instance()['pages'];
$parent = $this->parent();
@@ -2508,20 +2507,20 @@ class Page
protected function setPublishState()
{
// Handle publishing dates if no explict published option set
if (self::getGrav()['config']->get('system.pages.publish_dates') && !isset($this->header->published)) {
if (Grav::instance()['config']->get('system.pages.publish_dates') && !isset($this->header->published)) {
// unpublish if required, if not clear cache right before page should be unpublished
if ($this->unpublishDate()) {
if ($this->unpublishDate() < time()) {
$this->published(false);
} else {
$this->published();
self::getGrav()['cache']->setLifeTime($this->unpublishDate());
Grav::instance()['cache']->setLifeTime($this->unpublishDate());
}
}
// publish if required, if not clear cache right before page is published
if ($this->publishDate() && $this->publishDate() && $this->publishDate() > time()) {
$this->published(false);
self::getGrav()['cache']->setLifeTime($this->publishDate());
Grav::instance()['cache']->setLifeTime($this->publishDate());
}
}
}

View File

@@ -1,6 +1,7 @@
<?php
namespace Grav\Common;
use Grav\Common\Grav;
use Grav\Common\Config\Config;
use Grav\Common\Data\Blueprints;
use Grav\Common\Data\Data;
@@ -17,8 +18,6 @@ use RocketTheme\Toolbox\Event\EventSubscriberInterface;
*/
class Plugins extends Iterator
{
use GravTrait;
/**
* Recurses through the plugins directory creating Plugin objects for each plugin it finds.
*
@@ -28,13 +27,13 @@ class Plugins extends Iterator
public function init()
{
/** @var Config $config */
$config = self::getGrav()['config'];
$config = Grav::instance()['config'];
$plugins = (array)$config->get('plugins');
$inflector = self::getGrav()['inflector'];
$inflector = Grav::instance()['inflector'];
/** @var EventDispatcher $events */
$events = self::getGrav()['events'];
$events = Grav::instance()['events'];
foreach ($plugins as $plugin => $data) {
if (empty($data['enabled'])) {
@@ -42,10 +41,10 @@ class Plugins extends Iterator
continue;
}
$locator = self::getGrav()['locator'];
$locator = Grav::instance()['locator'];
$filePath = $locator->findResource('plugins://' . $plugin . DS . $plugin . PLUGIN_EXT);
if (!is_file($filePath)) {
self::getGrav()['log']->addWarning(sprintf("Plugin '%s' enabled but not found! Try clearing cache with `bin/grav clear-cache`", $plugin));
Grav::instance()['log']->addWarning(sprintf("Plugin '%s' enabled but not found! Try clearing cache with `bin/grav clear-cache`", $plugin));
continue;
}
@@ -69,7 +68,7 @@ class Plugins extends Iterator
$plugin));
}
$instance = new $pluginClassName($plugin, self::getGrav(), $config);
$instance = new $pluginClassName($plugin, Grav::instance(), $config);
if ($instance instanceof EventSubscriberInterface) {
$events->addSubscriber($instance);
}
@@ -147,7 +146,7 @@ class Plugins extends Iterator
$obj = new Data($file->content(), $blueprint);
// Override with user configuration.
$obj->merge(self::getGrav()['config']->get('plugins.' . $name) ?: []);
$obj->merge(Grav::instance()['config']->get('plugins.' . $name) ?: []);
// Save configuration always to user/config.
$file = CompiledYamlFile::instance("config://plugins/{$name}.yaml");

View File

@@ -90,7 +90,7 @@ class ConfigServiceProvider implements ServiceProviderInterface
public static function languages(Container $container)
{
/** Setup $setup */
/** @var Setup $setup */
$setup = $container['setup'];
/** @var Config $config */

View File

@@ -75,6 +75,7 @@ class TwigExtension extends \Twig_Extension
new \Twig_SimpleFilter('modulus', [$this, 'modulusFilter']),
new \Twig_SimpleFilter('rtrim', [$this, 'rtrimFilter']),
new \Twig_SimpleFilter('pad', [$this, 'padFilter']),
new \Twig_SimpleFilter('regex_replace', [$this, 'regexReplace']),
new \Twig_SimpleFilter('safe_email', [$this, 'safeEmailFilter']),
new \Twig_SimpleFilter('safe_truncate', ['\Grav\Common\Utils', 'safeTruncate']),
new \Twig_SimpleFilter('safe_truncate_html', ['\Grav\Common\Utils', 'safeTruncateHTML']),
@@ -105,6 +106,7 @@ class TwigExtension extends \Twig_Extension
new \Twig_SimpleFunction('nonce_field', [$this, 'nonceFieldFunc']),
new \Twig_simpleFunction('random_string', [$this, 'randomStringFunc']),
new \Twig_SimpleFunction('repeat', [$this, 'repeatFunc']),
new \Twig_SimpleFunction('regex_replace', [$this, 'regexReplace']),
new \Twig_SimpleFunction('string', [$this, 'stringFunc']),
new \Twig_simpleFunction('t', [$this, 'translate']),
new \Twig_simpleFunction('ta', [$this, 'translateArray']),
@@ -754,4 +756,19 @@ class TwigExtension extends \Twig_Extension
return $string;
}
/**
* Twig wrapper for PHP's preg_replace method
*
* @param mixed $subject the content to perform the replacement on
* @param mixed $pattern the regex pattern to use for matches
* @param mixed $replace the replacement value either as a string or an array of replacements
* @param int $limit the maximum possible replacements for each pattern in each subject
* @return mixed the resulting content
*/
public function regexReplace($subject, $pattern, $replace, $limit = -1)
{
return preg_replace($pattern, $replace, $subject, $limit);
}
}

View File

@@ -1,15 +1,13 @@
<?php
namespace Grav\Common\Twig;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
/**
* A trait to add some custom processing to the identifyLink() method in Parsedown and ParsedownExtra
*/
trait WriteCacheFileTrait
{
use GravTrait;
protected static $umask;
/**
@@ -26,7 +24,7 @@ trait WriteCacheFileTrait
}
if (!isset(self::$umask)) {
self::$umask = self::getGrav()['config']->get('system.twig.umask_fix', false);
self::$umask = Grav::instance()['config']->get('system.twig.umask_fix', false);
}
if (self::$umask) {

View File

@@ -746,18 +746,18 @@ class Uri
$url_path = $url;
}
$external = false;
$base = $grav['base_url_relative'];
$base_url = rtrim($base . $grav['pages']->base(), '/') . $language_append;
$pages_dir = $grav['locator']->findResource('page://');
$external = false;
$base = $grav['base_url_relative'];
$base_url = rtrim($base . $grav['pages']->base(), '/') . $language_append;
$pages_dir = $grav['locator']->findResource('page://');
// if absolute and starts with a base_url move on
if (isset($url['scheme']) && Utils::startsWith($url['scheme'], 'http')) {
$external = true;
} elseif (($base_url != '' && Utils::startsWith($url_path, $base_url)) ||
$url_path == '/' ||
Utils::startsWith($url_path, '#')) {
$url_path = $base_url . $url_path;
} elseif ($url_path == '' && isset($url['fragment'])) {
$external = true;
} elseif (($base_url != '' && Utils::startsWith($url_path, $base_url)) || $url_path == '/') {
$url_path = $base_url . $url_path;
} else {
// see if page is relative to this or absolute

View File

@@ -4,7 +4,7 @@ namespace Grav\Common\User;
use Grav\Common\Data\Blueprints;
use Grav\Common\Data\Data;
use Grav\Common\File\CompiledYamlFile;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use Grav\Common\Utils;
/**
@@ -15,8 +15,6 @@ use Grav\Common\Utils;
*/
class Group extends Data
{
use GravTrait;
/**
* Get the groups list
*
@@ -24,7 +22,7 @@ class Group extends Data
*/
private static function groups()
{
$groups = self::getGrav()['config']->get('groups');
$groups = Grav::instance()['config']->get('groups');
return $groups;
}
@@ -76,13 +74,13 @@ class Group extends Data
$fields = $blueprint->fields();
self::getGrav()['config']->set("groups.$this->groupname", []);
Grav::instance()['config']->set("groups.$this->groupname", []);
foreach ($fields as $field) {
if ($field['type'] == 'text') {
$value = $field['name'];
if (isset($this->items[$value])) {
self::getGrav()['config']->set("groups.$this->groupname.$value", $this->items[$value]);
Grav::instance()['config']->set("groups.$this->groupname.$value", $this->items[$value]);
}
}
if ($field['type'] == 'array') {
@@ -91,7 +89,7 @@ class Group extends Data
if ($arrayValues) {
foreach ($arrayValues as $arrayIndex => $arrayValue) {
self::getGrav()['config']->set("groups.$this->groupname.$value.$arrayIndex", $arrayValue);
Grav::instance()['config']->set("groups.$this->groupname.$value.$arrayIndex", $arrayValue);
}
}
}
@@ -99,8 +97,8 @@ class Group extends Data
$type = 'groups';
$blueprints = $this->blueprints("config/{$type}");
$obj = new Data(self::getGrav()['config']->get($type), $blueprints);
$file = CompiledYamlFile::instance(self::getGrav()['locator']->findResource("config://{$type}.yaml"));
$obj = new Data(Grav::instance()['config']->get($type), $blueprints);
$file = CompiledYamlFile::instance(Grav::instance()['locator']->findResource("config://{$type}.yaml"));
$obj->file($file);
$obj->save();
}
@@ -117,13 +115,13 @@ class Group extends Data
$blueprints = new Blueprints('blueprints://');
$blueprint = $blueprints->get('user/group');
$groups = self::getGrav()['config']->get("groups");
$groups = Grav::instance()['config']->get("groups");
unset($groups[$groupname]);
self::getGrav()['config']->set("groups", $groups);
Grav::instance()['config']->set("groups", $groups);
$type = 'groups';
$obj = new Data(self::getGrav()['config']->get($type), $blueprint);
$file = CompiledYamlFile::instance(self::getGrav()['locator']->findResource("config://{$type}.yaml"));
$obj = new Data(Grav::instance()['config']->get($type), $blueprint);
$file = CompiledYamlFile::instance(Grav::instance()['locator']->findResource("config://{$type}.yaml"));
$obj->file($file);
$obj->save();

View File

@@ -4,7 +4,7 @@ namespace Grav\Common\User;
use Grav\Common\Data\Blueprints;
use Grav\Common\Data\Data;
use Grav\Common\File\CompiledYamlFile;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use Grav\Common\Utils;
/**
@@ -18,8 +18,6 @@ use Grav\Common\Utils;
*/
class User extends Data
{
use GravTrait;
/**
* Load user account.
*
@@ -31,7 +29,7 @@ class User extends Data
*/
public static function load($username)
{
$locator = self::getGrav()['locator'];
$locator = Grav::instance()['locator'];
// force lowercase of username
$username = strtolower($username);
@@ -62,7 +60,7 @@ class User extends Data
*/
public static function remove($username)
{
$file_path = self::getGrav()['locator']->findResource('account://' . $username . YAML_EXT);
$file_path = Grav::instance()['locator']->findResource('account://' . $username . YAML_EXT);
if (file_exists($file_path) && unlink($file_path)) {
return true;
}
@@ -91,7 +89,7 @@ class User extends Data
// the result
Authentication::verify(
$password,
self::getGrav()['config']->get('system.security.default_hash')
Grav::instance()['config']->get('system.security.default_hash')
);
return false;
@@ -163,7 +161,7 @@ class User extends Data
$groups = $this->get('groups');
if ($groups) {
foreach ((array)$groups as $group) {
$permission = self::getGrav()['config']->get("groups.{$group}.access.{$action}");
$permission = Grav::instance()['config']->get("groups.{$group}.access.{$action}");
$return = Utils::isPositive($permission);
if ($return === true) {
break;

View File

@@ -3,6 +3,7 @@ namespace Grav\Common;
use DateTime;
use DateTimeZone;
use Grav\Common\Grav;
use Grav\Common\Helpers\Truncator;
use RocketTheme\Toolbox\Event\Event;
@@ -13,8 +14,6 @@ use RocketTheme\Toolbox\Event\Event;
*/
abstract class Utils
{
use GravTrait;
protected static $nonces = [];
/**
@@ -126,7 +125,7 @@ abstract class Utils
'H:i d-m-Y' => 'H:i d-m-Y (e.g. '.$now->format('H:i d-m-Y').')',
'h:i a m/d/Y' => 'h:i a m/d/Y (e.g. '.$now->format('h:i a m/d/Y').')',
];
$default_format = self::getGrav()['config']->get('system.pages.dateformat.default');
$default_format = Grav::instance()['config']->get('system.pages.dateformat.default');
if ($default_format) {
$date_formats = array_merge([$default_format => $default_format.' (e.g. '.$now->format($default_format).')'], $date_formats);
}
@@ -226,7 +225,7 @@ abstract class Utils
{
if (file_exists($file)) {
// fire download event
self::getGrav()->fireEvent('onBeforeDownload', new Event(['file' => $file]));
Grav::instance()->fireEvent('onBeforeDownload', new Event(['file' => $file]));
$file_parts = pathinfo($file);
$filesize = filesize($file);
@@ -285,7 +284,7 @@ abstract class Utils
public static function getMimeType($extension)
{
$extension = strtolower($extension);
$config = self::getGrav()['config']->get('media');
$config = Grav::instance()['config']->get('media');
if (isset($config[$extension])) {
return $config[$extension]['mime'];
@@ -402,7 +401,7 @@ abstract class Utils
return false;
}
$languages_enabled = self::getGrav()['config']->get('system.languages.supported', []);
$languages_enabled = Grav::instance()['config']->get('system.languages.supported', []);
if ($string[0] == '/' && $string[3] == '/' && in_array(substr($string, 1, 2), $languages_enabled)) {
return true;
@@ -421,7 +420,7 @@ abstract class Utils
*/
public static function date2timestamp($date, $format = null)
{
$config = self::getGrav()['config'];
$config = Grav::instance()['config'];
$dateformat = $format ?: $config->get('system.pages.dateformat.default');
// try to use DateTime and default format
@@ -489,8 +488,8 @@ abstract class Utils
private static function generateNonceString($action, $plusOneTick = false)
{
$username = '';
if (isset(self::getGrav()['user'])) {
$user = self::getGrav()['user'];
if (isset(Grav::instance()['user'])) {
$user = Grav::instance()['user'];
$username = $user->username;
}
@@ -501,14 +500,14 @@ abstract class Utils
$i++;
}
return ($i . '|' . $action . '|' . $username . '|' . $token . '|' . self::getGrav()['config']->get('security.salt'));
return ($i . '|' . $action . '|' . $username . '|' . $token . '|' . Grav::instance()['config']->get('security.salt'));
}
//Added in version 1.0.8 to ensure that existing nonces are not broken.
private static function generateNonceStringOldStyle($action, $plusOneTick = false)
{
if (isset(self::getGrav()['user'])) {
$user = self::getGrav()['user'];
if (isset(Grav::instance()['user'])) {
$user = Grav::instance()['user'];
$username = $user->username;
if (isset($_SERVER['REMOTE_ADDR'])) {
$username .= $_SERVER['REMOTE_ADDR'];
@@ -522,7 +521,7 @@ abstract class Utils
$i++;
}
return ($i . '|' . $action . '|' . $username . '|' . $token . '|' . self::getGrav()['config']->get('security.salt'));
return ($i . '|' . $action . '|' . $username . '|' . $token . '|' . Grav::instance()['config']->get('security.salt'));
}
/**

View File

@@ -1,6 +1,7 @@
<?php
namespace Grav\Console\Cli;
use Grav\Common\Grav;
use Grav\Common\Backup\ZipBackup;
use Grav\Console\ConsoleCommand;
use RocketTheme\Toolbox\File\JsonFile;
@@ -46,10 +47,10 @@ class BackupCommand extends ConsoleCommand
$this->progress = new ProgressBar($this->output);
$this->progress->setFormat('Archiving <cyan>%current%</cyan> files [<green>%bar%</green>] %elapsed:6s% %memory:6s%');
self::getGrav()['config']->init();
Grav::instance()['config']->init();
$destination = ($this->input->getArgument('destination')) ? $this->input->getArgument('destination') : null;
$log = JsonFile::instance(self::getGrav()['locator']->findResource("log://backup.log", true, true));
$log = JsonFile::instance(Grav::instance()['locator']->findResource("log://backup.log", true, true));
$backup = ZipBackup::backup($destination, [$this, 'output']);
$log->content([

View File

@@ -1,7 +1,7 @@
<?php
namespace Grav\Console;
use Grav\Common\GravTrait;
use Grav\Common\Grav;
use Grav\Common\Composer;
use Grav\Console\Cli\ClearCacheCommand;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
@@ -15,8 +15,6 @@ use Symfony\Component\Console\Output\OutputInterface;
*/
trait ConsoleTrait
{
use GravTrait;
/**
* @var
*/
@@ -36,8 +34,8 @@ trait ConsoleTrait
*/
public function setupConsole(InputInterface $input, OutputInterface $output)
{
if (self::getGrav()) {
self::getGrav()['config']->set('system.cache.driver', 'default');
if (Grav::instance()) {
Grav::instance()['config']->set('system.cache.driver', 'default');
}
$this->argv = $_SERVER['argv'][0];

View File

@@ -3,6 +3,7 @@ namespace Grav\Console\Gpm;
use Grav\Common\GPM\GPM;
use Grav\Common\GPM\Installer;
use Grav\Common\Grav;
use Grav\Console\ConsoleCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
@@ -180,7 +181,7 @@ class UninstallCommand extends ConsoleCommand
exit;
}
$path = self::getGrav()['locator']->findResource($package->package_type . '://' .$slug);
$path = Grav::instance()['locator']->findResource($package->package_type . '://' .$slug);
Installer::uninstall($path);
$errorCode = Installer::lastErrorCode();
@@ -238,7 +239,7 @@ class UninstallCommand extends ConsoleCommand
private function checkDestination($slug, $package)
{
$path = self::getGrav()['locator']->findResource($package->package_type . '://' . $slug);
$path = Grav::instance()['locator']->findResource($package->package_type . '://' . $slug);
$questionHelper = $this->getHelper('question');
$skipPrompt = $this->input->getOption('all-yes');

View File

@@ -5,51 +5,58 @@ namespace Grav;
use Codeception\Util\Fixtures;
use Faker\Factory;
use Grav\Common\Grav;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
ini_set('error_log', __DIR__ . '/error.log');
// Ensure vendor libraries exist
$autoload = __DIR__ . '/../vendor/autoload.php';
$grav = function() {
// Ensure vendor libraries exist
$autoload = __DIR__ . '/../vendor/autoload.php';
if (!is_file($autoload)) {
throw new \RuntimeException("Please run: <i>bin/grav install</i>");
}
if (!is_file($autoload)) {
throw new \RuntimeException("Please run: <i>bin/grav install</i>");
}
use Grav\Common\Grav;
// Register the auto-loader.
$loader = require_once $autoload;
// Register the auto-loader.
$loader = require_once $autoload;
if (version_compare($ver = PHP_VERSION, $req = GRAV_PHP_MIN, '<')) {
throw new \RuntimeException(sprintf('You are running PHP %s, but Grav needs at least <strong>PHP %s</strong> to run.', $ver, $req));
}
if (version_compare($ver = PHP_VERSION, $req = GRAV_PHP_MIN, '<')) {
throw new \RuntimeException(sprintf('You are running PHP %s, but Grav needs at least <strong>PHP %s</strong> to run.', $ver, $req));
}
// Set timezone to default, falls back to system if php.ini not set
date_default_timezone_set(@date_default_timezone_get());
// Set timezone to default, falls back to system if php.ini not set
date_default_timezone_set(@date_default_timezone_get());
// Set internal encoding if mbstring loaded
if (!extension_loaded('mbstring')) {
throw new \RuntimeException("'mbstring' extension is not loaded. This is required for Grav to run correctly");
}
mb_internal_encoding('UTF-8');
// Set internal encoding if mbstring loaded
if (!extension_loaded('mbstring')) {
throw new \RuntimeException("'mbstring' extension is not loaded. This is required for Grav to run correctly");
}
mb_internal_encoding('UTF-8');
Grav::resetInstance();
// Get the Grav instance
$grav = Grav::instance(['loader' => $loader]);
// Get the Grav instance
$grav = Grav::instance(['loader' => $loader]);
$grav['uri']->init();
$grav['debugger']->init();
$grav['assets']->init();
$grav['uri']->init();
$grav['debugger']->init();
$grav['assets']->init();
$grav['config']->set('system.cache.enabled', false);
$grav['config']->set('system.cache.enabled', false);
/** @var UniformResourceLocator $locator */
$locator = $grav['locator'];
$locator->addPath('tests', '', 'tests', false);
/** @var UniformResourceLocator $locator */
$locator = $grav['locator'];
$locator->addPath('tests', '', 'tests', false);
// Set default $_SERVER value used for nonces
empty( $_SERVER['HTTP_CLIENT_IP'] ) && $_SERVER['HTTP_CLIENT_IP'] = '127.0.0.1';
// Set default $_SERVER value used for nonces
empty( $_SERVER['HTTP_CLIENT_IP'] ) && $_SERVER['HTTP_CLIENT_IP'] = '127.0.0.1';
return $grav;
};
Fixtures::add('grav', $grav);
$fake = Factory::create();
Fixtures::add('grav', $grav);
Fixtures::add('fake', $fake);
Fixtures::add('fake', $fake);

View File

@@ -17,7 +17,8 @@ class AssetsTest extends \Codeception\TestCase\Test
protected function _before()
{
$this->grav = Fixtures::get('grav');
$grav = Fixtures::get('grav');
$this->grav = $grav();
$this->assets = $this->grav['assets'];
}

View File

@@ -13,7 +13,8 @@ class BrowserTest extends \Codeception\TestCase\Test
protected function _before()
{
$this->grav = Fixtures::get('grav');
$grav = Fixtures::get('grav');
$this->grav = $grav();
}
protected function _after()

View File

@@ -18,7 +18,8 @@ class InflectorTest extends \Codeception\TestCase\Test
protected function _before()
{
$this->grav = Fixtures::get('grav');
$grav = Fixtures::get('grav');
$this->grav = $grav();
$this->inflector = $this->grav['inflector'];
}

View File

@@ -34,11 +34,10 @@ class ParsedownTest extends \Codeception\TestCase\Test
protected $old_home;
static $run = false;
protected function _before()
{
$this->grav = Fixtures::get('grav');
$grav = Fixtures::get('grav');
$this->grav = $grav();
$this->pages = $this->grav['pages'];
$this->config = $this->grav['config'];
$this->uri = $this->grav['uri'];
@@ -51,13 +50,10 @@ class ParsedownTest extends \Codeception\TestCase\Test
unset($this->grav['language']);
$this->grav['language'] = new Language($this->grav);
if (!self::$run) {
/** @var UniformResourceLocator $locator */
$locator = $this->grav['locator'];
$locator->addPath('page', '', 'tests/fake/nested-site/user/pages', false);
$this->pages->init();
self::$run = true;
}
/** @var UniformResourceLocator $locator */
$locator = $this->grav['locator'];
$locator->addPath('page', '', 'tests/fake/nested-site/user/pages', false);
$this->pages->init();
$defaults = [
'extra' => false,
@@ -228,7 +224,7 @@ class ParsedownTest extends \Codeception\TestCase\Test
$this->parsedown->text('[With Query](?foo=bar)'));
$this->assertSame('<p><a href="/foo:bar">With Param</a></p>',
$this->parsedown->text('[With Param](/foo:bar)'));
$this->assertSame('<p><a href="/#foo">With Anchor</a></p>',
$this->assertSame('<p><a href="#foo">With Anchor</a></p>',
$this->parsedown->text('[With Anchor](#foo)'));
$this->config->set('system.languages.supported', ['fr','en']);
@@ -244,7 +240,7 @@ class ParsedownTest extends \Codeception\TestCase\Test
$this->parsedown->text('[With Query](?foo=bar)'));
$this->assertSame('<p><a href="/fr/foo:bar">With Param</a></p>',
$this->parsedown->text('[With Param](/foo:bar)'));
$this->assertSame('<p><a href="/fr/#foo">With Anchor</a></p>',
$this->assertSame('<p><a href="#foo">With Anchor</a></p>',
$this->parsedown->text('[With Anchor](#foo)'));
}
@@ -256,7 +252,7 @@ class ParsedownTest extends \Codeception\TestCase\Test
$this->grav['language'] = new Language($this->grav);
$this->uri->initializeWithURL('http://testing.dev/fr/item2/item2-2')->init();
$this->assertSame('<p><a href="/fr/item2/item2-2#foo">Current Anchor</a></p>',
$this->assertSame('<p><a href="#foo">Current Anchor</a></p>',
$this->parsedown->text('[Current Anchor](#foo)'));
$this->assertSame('<p><a href="/fr/#foo">Root Anchor</a></p>',
$this->parsedown->text('[Root Anchor](/#foo)'));
@@ -275,12 +271,12 @@ class ParsedownTest extends \Codeception\TestCase\Test
$this->grav['language'] = new Language($this->grav);
$this->uri->initializeWithURL('http://testing.dev/fr/item2/item2-2')->init();
$this->assertSame('<p><a href="#foo">Current Anchor</a></p>',
$this->parsedown->text('[Current Anchor](#foo)'));
$this->assertSame('<p><a href="http://testing.dev/fr/item2/item2-1#foo">Peer Anchor</a></p>',
$this->parsedown->text('[Peer Anchor](../item2-1#foo)'));
$this->assertSame('<p><a href="http://testing.dev/fr/item2/item2-1#foo">Peer Anchor 2</a></p>',
$this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)'));
$this->assertSame('<p><a href="http://testing.dev/fr/item2/item2-2#foo">Current Anchor</a></p>',
$this->parsedown->text('[Current Anchor](#foo)'));
$this->assertSame('<p><a href="http://testing.dev/fr/#foo">Root Anchor</a></p>',
$this->parsedown->text('[Root Anchor](/#foo)'));
@@ -320,7 +316,7 @@ class ParsedownTest extends \Codeception\TestCase\Test
{
$this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
$this->assertSame('<p><a href="/item2/item2-2#foo">Current Anchor</a></p>',
$this->assertSame('<p><a href="#foo">Current Anchor</a></p>',
$this->parsedown->text('[Current Anchor](#foo)'));
$this->assertSame('<p><a href="/#foo">Root Anchor</a></p>',
$this->parsedown->text('[Root Anchor](/#foo)'));
@@ -335,19 +331,16 @@ class ParsedownTest extends \Codeception\TestCase\Test
$this->config->set('system.absolute_urls', true);
$this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
$this->assertSame('<p><a href="#foo">Current Anchor</a></p>',
$this->parsedown->text('[Current Anchor](#foo)'));
$this->assertSame('<p><a href="http://testing.dev/item2/item2-1#foo">Peer Anchor</a></p>',
$this->parsedown->text('[Peer Anchor](../item2-1#foo)'));
$this->assertSame('<p><a href="http://testing.dev/item2/item2-1#foo">Peer Anchor 2</a></p>',
$this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)'));
$this->assertSame('<p><a href="http://testing.dev/item2/item2-2#foo">Current Anchor</a></p>',
$this->parsedown->text('[Current Anchor](#foo)'));
$this->assertSame('<p><a href="http://testing.dev/#foo">Root Anchor</a></p>',
$this->parsedown->text('[Root Anchor](/#foo)'));
}
public function testAnchorLinksWithPortAbsoluteUrls()
{
$this->config->set('system.absolute_urls', true);
@@ -357,7 +350,7 @@ class ParsedownTest extends \Codeception\TestCase\Test
$this->parsedown->text('[Peer Anchor](../item2-1#foo)'));
$this->assertSame('<p><a href="http://testing.dev:8080/item2/item2-1#foo">Peer Anchor 2</a></p>',
$this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)'));
$this->assertSame('<p><a href="http://testing.dev:8080/item2/item2-2#foo">Current Anchor</a></p>',
$this->assertSame('<p><a href="#foo">Current Anchor</a></p>',
$this->parsedown->text('[Current Anchor](#foo)'));
$this->assertSame('<p><a href="http://testing.dev:8080/#foo">Root Anchor</a></p>',
$this->parsedown->text('[Root Anchor](/#foo)'));
@@ -371,7 +364,7 @@ class ParsedownTest extends \Codeception\TestCase\Test
$this->parsedown->text('[Peer Anchor](../item2-1#foo)'));
$this->assertSame('<p><a href="/subdir/item2/item2-1#foo">Peer Anchor 2</a></p>',
$this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)'));
$this->assertSame('<p><a href="/subdir/item2/item2-2#foo">Current Anchor</a></p>',
$this->assertSame('<p><a href="#foo">Current Anchor</a></p>',
$this->parsedown->text('[Current Anchor](#foo)'));
$this->assertSame('<p><a href="/subdir/#foo">Root Anchor</a></p>',
$this->parsedown->text('[Root Anchor](/#foo)'));
@@ -386,7 +379,7 @@ class ParsedownTest extends \Codeception\TestCase\Test
$this->parsedown->text('[Peer Anchor](../item2-1#foo)'));
$this->assertSame('<p><a href="http://testing.dev/subdir/item2/item2-1#foo">Peer Anchor 2</a></p>',
$this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)'));
$this->assertSame('<p><a href="http://testing.dev/subdir/item2/item2-2#foo">Current Anchor</a></p>',
$this->assertSame('<p><a href="#foo">Current Anchor</a></p>',
$this->parsedown->text('[Current Anchor](#foo)'));
$this->assertSame('<p><a href="http://testing.dev/subdir/#foo">Root Anchor</a></p>',
$this->parsedown->text('[Root Anchor](/#foo)'));

View File

@@ -21,23 +21,18 @@ class PagesTest extends \Codeception\TestCase\Test
/** @var Page $root_page */
protected $root_page;
static $run = false;
protected function _before()
{
$this->grav = Fixtures::get('grav');
$grav = Fixtures::get('grav');
$this->grav = $grav();
$this->pages = $this->grav['pages'];
$this->grav['config']->set('system.home.alias', '/home');
if (!self::$run) {
/** @var UniformResourceLocator $locator */
$locator = $this->grav['locator'];
$locator->addPath('page', '', 'tests/fake/simple-site/user/pages', false);
$this->pages->init();
self::$run = true;
}
/** @var UniformResourceLocator $locator */
$locator = $this->grav['locator'];
$locator->addPath('page', '', 'tests/fake/simple-site/user/pages', false);
$this->pages->init();
}
public function testBase()

View File

@@ -137,6 +137,11 @@ class TwigExtensionTest extends \Codeception\TestCase\Test
}
public function testRegexReplace()
{
}
public function testUrlFunc()
{
@@ -169,7 +174,10 @@ class TwigExtensionTest extends \Codeception\TestCase\Test
public function testArrayFunc()
{
$this->assertSame('this is my text',
$this->twig_ext->regexReplace('<p>this is my text</p>', '(<\/?p>)', ''));
$this->assertSame('<i>this is my text</i>',
$this->twig_ext->regexReplace('<p>this is my text</p>', ['(<p>)','(<\/p>)'], ['<i>','</i>']));
}
public function testArrayKeyValue()

View File

@@ -18,7 +18,8 @@ class UriTest extends \Codeception\TestCase\Test
protected function _before()
{
$this->grav = Fixtures::get('grav');
$grav = Fixtures::get('grav');
$this->grav = $grav();
$this->uri = $this->grav['uri'];
}

View File

@@ -14,7 +14,8 @@ class UtilsTest extends \Codeception\TestCase\Test
protected function _before()
{
$this->grav = Fixtures::get('grav');
$grav = Fixtures::get('grav');
$this->grav = $grav();
}
protected function _after()