Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/getgrav/grav into 2.0
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
  • Loading branch information
mahagr committed Aug 14, 2017
2 parents fb0e086 + 4b948e2 commit ad5cddf
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 8 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@
1. [](#improved)
* Make it possible to include debug bar also into non-HTML responses

# v1.3.2
## mm/dd/2017

1. [](#new)
* Added a new `cache_control` system and page level property [#1591](https://github.com/getgrav/grav/issues/1591)
* Added a new `clear_images_by_default` system property to stop cache clear events from removing processed images [#1481](https://github.com/getgrav/grav/pull/1481)
* Added new `onTwigLoader()` event to enable utilization of loader methods
* Added new `Twig::addPath()` and `Twig::prependPath()` methods to wrap loader methods and support namespacing [#1604](https://github.com/getgrav/grav/issues/1604)
* Added new `array_key_exists()` Twig function wrapper
* Added a new `Collection::intersect()` method [#1605](github.com/getgrav/grav/issues/1605)
1. [](#bugfix)
* Allow `session.timetout` field to be set to `0` via blueprints [#1598](https://github.com/getgrav/grav/issues/1598)
* Fixed `Data::exists()` and `Data::raw()` functions breaking if `Data::file()` hasn't been called with non-null value

# v1.3.1
## 07/19/2017

Expand Down
19 changes: 18 additions & 1 deletion system/blueprints/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ form:
validate:
type: number
min: 1
pages.cache_control:
type: text
size: medium
label: PLUGIN_ADMIN.CACHE_CONTROL
help: PLUGIN_ADMIN.CACHE_CONTROL_HELP
placeholder: 'e.g. public, max-age=31536000'
pages.last_modified:
type: toggle
label: PLUGIN_ADMIN.LAST_MODIFIED
Expand Down Expand Up @@ -508,6 +514,17 @@ form:
help: PLUGIN_ADMIN.CACHE_PREFIX_HELP
placeholder: PLUGIN_ADMIN.CACHE_PREFIX_PLACEHOLDER

cache.clear_images_by_default:
type: toggle
label: PLUGIN_ADMIN.CLEAR_IMAGES_BY_DEFAULT
help: PLUGIN_ADMIN.CLEAR_IMAGES_BY_DEFAULT_HELP
highlight: 1
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool

cache.cli_compatibility:
type: toggle
label: PLUGIN_ADMIN.CLI_COMPATIBILITY
Expand Down Expand Up @@ -986,7 +1003,7 @@ form:
help: PLUGIN_ADMIN.TIMEOUT_HELP
validate:
type: number
min: 1
min: 0

session.name:
type: text
Expand Down
2 changes: 2 additions & 0 deletions system/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pages:
types: [txt,xml,html,htm,json,rss,atom] # list of valid page types
append_url_extension: '' # Append page's extension in Page urls (e.g. '.html' results in /path/page.html)
expires: 604800 # Page expires time in seconds (604800 seconds = 7 days)
cache_control: # Can be blank for no setting, or a valid `cache-control` text value
last_modified: false # Set the last modified date header based on file modification timestamp
etag: false # Set the etag header tag
vary_accept_encoding: false # Add `Vary: Accept-Encoding` header
Expand All @@ -73,6 +74,7 @@ cache:
method: file # Method to check for updates in pages: file|folder|hash|none
driver: auto # One of: auto|file|apc|xcache|memcache|wincache
prefix: 'g' # Cache prefix string (prevents cache conflicts)
clear_images_by_default: true # By default grav will include processed images in cache clear, this can be disabled
cli_compatibility: false # Ensures only non-volatile drivers are used (file, redis, memcache, etc.)
lifetime: 604800 # Lifetime of cached data in seconds (0 = infinite)
gzip: false # GZip compress the page output
Expand Down
15 changes: 14 additions & 1 deletion system/src/Grav/Common/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ class Cache extends Getters
'asset://',
];

protected static $standard_remove_no_images = [
'cache://twig/',
'cache://doctrine/',
'cache://compiled/',
'cache://validated-',
'asset://',
];

protected static $all_remove = [
'cache://',
'cache://images',
Expand Down Expand Up @@ -360,7 +368,12 @@ public static function clearCache($remove = 'standard')
$remove_paths = self::$tmp_remove;
break;
default:
$remove_paths = self::$standard_remove;
if (Grav::instance()['config']->get('system.cache.clear_images_by_default')) {
$remove_paths = self::$standard_remove;
} else {
$remove_paths = self::$standard_remove_no_images;
}

}

// Clearing cache event to add paths to clear
Expand Down
8 changes: 6 additions & 2 deletions system/src/Grav/Common/Data/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ public function save()
*/
public function exists()
{
return $this->file()->exists();
$file = $this->file();

return $file && $file->exists();
}

/**
Expand All @@ -263,7 +265,9 @@ public function exists()
*/
public function raw()
{
return $this->file()->raw();
$file = $this->file();

return $file ? $file->raw() : '';
}

/**
Expand Down
11 changes: 10 additions & 1 deletion system/src/Grav/Common/Grav.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,24 @@ public function header()

header('Content-type: ' . Utils::getMimeByExtension($format, 'text/html'));

$cache_control = $page->cacheControl();

// Calculate Expires Headers if set to > 0
$expires = $page->expires();

if ($expires > 0) {
$expires_date = gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT';
header('Cache-Control: max-age=' . $expires);
if (!$cache_control) {
header('Cache-Control: max-age=' . $expires);
}
header('Expires: ' . $expires_date);
}

// Set cache-control header
if ($cache_control) {
header('Cache-Control: ' . strtolower($cache_control));
}

// Set the last modified time
if ($page->lastModified()) {
$last_modified_date = gmdate('D, d M Y H:i:s', $page->modified()) . ' GMT';
Expand Down
31 changes: 31 additions & 0 deletions system/src/Grav/Common/Page/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ public function addPage(Page $page)
return $this;
}

/**
* Add a page with path and slug
*
* @param $path
* @param $slug
* @return $this
*/
public function add($path, $slug)
{
$this->items[$path] = ['slug' => $slug];

return $this;
}

/**
*
* Create a copy of this collection
Expand All @@ -89,6 +103,23 @@ public function merge(Collection $collection)
return $this;
}

/**
* Intersect another collection with the current collection
*
* @param Collection $collection
* @return $this
*/
public function intersect(Collection $collection)
{
$array1 = $this->items;
$array2 = $collection->toArray();

$this->items = array_uintersect($array1, $array2, function($val1, $val2) {
return strcmp($val1['slug'], $val2['slug']);
});
return $this;
}

/**
* Set parameters to the Collection
*
Expand Down
2 changes: 1 addition & 1 deletion system/src/Grav/Common/Page/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected function init()
*
* @return mixed
*/
function path()
public function path()
{
return $this->path;
}
Expand Down
20 changes: 20 additions & 0 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Page
protected $parent;
protected $template;
protected $expires;
protected $cache_control;
protected $visible;
protected $published;
protected $publish_date;
Expand Down Expand Up @@ -429,6 +430,9 @@ public function header($var = null)
if (isset($this->header->expires)) {
$this->expires = intval($this->header->expires);
}
if (isset($this->header->cache_control)) {
$this->cache_control = $this->header->cache_control;
}
if (isset($this->header->etag)) {
$this->etag = (bool)$this->header->etag;
}
Expand Down Expand Up @@ -1250,6 +1254,22 @@ public function expires($var = null)
return !isset($this->expires) ? Grav::instance()['config']->get('system.pages.expires') : $this->expires;
}

/**
* Gets and sets the cache-control property. If not set it will return the default value (null)
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control for more details on valid options
*
* @param null $var
* @return null
*/
public function cacheControl($var = null)
{
if ($var !== null) {
$this->cache_control = $var;
}

return !isset($this->cache_control) ? Grav::instance()['config']->get('system.pages.cache_control') : $this->cache_control;
}

/**
* Gets and sets the title for this Page. If no title is set, it will use the slug() to get a name
*
Expand Down
4 changes: 2 additions & 2 deletions system/src/Grav/Common/Page/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public function inherited($route, $field = null)

$ancestorField = $page->parent()->value('header.' . $field);

if ($ancestorField != null) {
if ($ancestorField !== null) {
return $page->parent();
} elseif (!$page->parent()->root()) {
return $this->inherited($page->parent()->route(), $field);
Expand Down Expand Up @@ -670,7 +670,7 @@ public function getList(Page $current = null, $level = 0, $rawRoutes = false, $s

}

if ($limitLevels == false || ($level+1 < $limitLevels)) {
if ($limitLevels === false || ($level+1 < $limitLevels)) {
foreach ($current->children() as $next) {
if ($showAll || $next->routable() || ($next->modular() && $showModular)) {
$list = array_merge($list, $this->getList($next, $level + 1, $rawRoutes, $showAll, $showFullpath, $showSlug, $showModular, $limitLevels));
Expand Down
23 changes: 23 additions & 0 deletions system/src/Grav/Common/Twig/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public function init()
$this->grav->fireEvent('onTwigTemplatePaths');

$this->loader = new \Twig_Loader_Filesystem($this->twig_paths);

$this->grav->fireEvent('onTwigLoader');

$this->loaderArray = new \Twig_Loader_Array([]);
$loader_chain = new \Twig_Loader_Chain([$this->loaderArray, $this->loader]);

Expand Down Expand Up @@ -359,6 +362,26 @@ public function processSite($format = null, array $vars = [])
return $output;
}

/**
* Wraps the Twig_Loader_Filesystem addPath method (should be used only in `onTwigLoader()` event
* @param $template_path
* @param null $namespace
*/
public function addPath($template_path, $namespace = null)
{
$this->loader->addPath($template_path, $namespace);
}

/**
* Wraps the Twig_Loader_Filesystem prependPath method (should be used only in `onTwigLoader()` event
* @param $template_path
* @param null $namespace
*/
public function prependPath($template_path, $namespace = null)
{
$this->loader->prependPath($template_path, $namespace);
}

/**
* Simple helper method to get the twig template if it has already been set, else return
* the one being passed in
Expand Down
34 changes: 34 additions & 0 deletions system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Grav\Common\Twig;

use Grav\Common\Grav;
use Grav\Common\Page\Collection;
use Grav\Common\Page\Media;
use Grav\Common\Utils;
use Grav\Common\Markdown\Parsedown;
Expand Down Expand Up @@ -95,6 +96,7 @@ public function getFilters()
new \Twig_SimpleFilter('truncate_html', ['\Grav\Common\Utils', 'truncateHTML']),
new \Twig_SimpleFilter('json_decode', [$this, 'jsonDecodeFilter']),
new \Twig_SimpleFilter('array_unique', 'array_unique'),

];
}

Expand All @@ -108,6 +110,8 @@ public function getFunctions()
return [
new \Twig_SimpleFunction('array', [$this, 'arrayFunc']),
new \Twig_SimpleFunction('array_key_value', [$this, 'arrayKeyValueFunc']),
new \Twig_SimpleFunction('array_key_exists', [$this, 'arrayKeyExistsFunc']),
new \Twig_SimpleFunction('array_intersect', [$this, 'arrayIntersectFunc']),
new \Twig_simpleFunction('authorize', [$this, 'authorize']),
new \Twig_SimpleFunction('debug', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]),
new \Twig_SimpleFunction('dump', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]),
Expand Down Expand Up @@ -819,6 +823,36 @@ public function arrayKeyValueFunc($key, $val, $current_array = null)
}
}

/**
* Check to see if an array key exists
*
* @param string $key key of item
* @param string $current_array optional array to add to
*
* @return array
*/
public function arrayKeyExistsFunc($key, $current_array = null)
{
return array_key_exists($key, $current_array);
}

/**
* Wrapper for array_intersect() method
*
* @param $array1
* @param $array2
* @return array
*/
public function arrayIntersectFunc($array1, $array2)
{
if ($array1 instanceof Collection && $array2 instanceof Collection) {
return $array1->intersect($array2);
} else {
return array_intersect($array1, $array2);
}

}

/**
* Returns a string from a value. If the value is array, return it json encoded
*
Expand Down

0 comments on commit ad5cddf

Please sign in to comment.