Skip to content

Commit

Permalink
Merge branch 'release/0.9.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Dec 12, 2014
2 parents 8991af1 + 14780c3 commit f284147
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 79 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# v0.9.10 beta
## 12/12/2014

1. [](#new)
* Added Facebook-style `nicetime` date Twig filter
2. [](#improved)
* Moved `clear-cache` functionality into Cache object required for Admin plugin
3. [](#bugfix)
* Fix for undefined index with previous/next buttons

# v0.9.9 beta
## 12/05/2014

Expand Down
2 changes: 1 addition & 1 deletion system/defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Some standard defines
define('GRAV', true);
define('GRAV_VERSION', '0.9.9');
define('GRAV_VERSION', '0.9.10');
define('DS', '/');

// Directories and Paths
Expand Down
94 changes: 94 additions & 0 deletions system/src/Grav/Common/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use \Doctrine\Common\Cache\Cache as DoctrineCache;
use Grav\Common\Config\Config;
use Grav\Common\Filesystem\Folder;

/**
* The GravCache object is used throughout Grav to store and retrieve cached data.
Expand Down Expand Up @@ -37,6 +38,33 @@ class Cache extends Getters

protected $cache_dir;

protected static $standard_remove = [
'cache/twig/',
'cache/doctrine/',
'cache/compiled/',
'cache/validated-',
'images/',
'assets/',
];

protected static $all_remove = [
'cache/',
'images/',
'assets/'
];

protected static $assets_remove = [
'assets/'
];

protected static $images_remove = [
'images/'
];

protected static $cache_remove = [
'cache/'
];

/**
* Constructor
*
Expand Down Expand Up @@ -169,4 +197,70 @@ public function getKey()
{
return $this->key;
}

/**
* Helper method to clear all Grav caches
*
* @param string $remove standard|all|assets-only|images-only|cache-only
*
* @return array
*/
public static function clearCache($remove = 'standard')
{

$output = [];
$user_config = USER_DIR . 'config/system.yaml';

switch($remove) {
case 'all':
$remove_paths = self::$all_remove;
break;
case 'assets-only':
$remove_paths = self::$assets_remove;
break;
case 'images-only':
$remove_paths = self::$images_remove;
break;
case 'cache-only':
$remove_paths = self::$cache_remove;
break;
default:
$remove_paths = self::$standard_remove;
}


foreach ($remove_paths as $path) {

$anything = false;
$files = glob(ROOT_DIR . $path . '*');

foreach ($files as $file) {
if (is_file($file)) {
if (@unlink($file)) {
$anything = true;
}
} elseif (is_dir($file)) {
if (@Folder::delete($file)) {
$anything = true;
}
}
}


if ($anything) {
$output[] = '<red>Cleared: </red>' . $path . '*';
}
}

$output[] = '';

if (($remove == 'all' || $remove == 'standard') && file_exists($user_config)) {
touch($user_config);

$output[] = '<red>Touched: </red>' . $user_config;
$output[] = '';
}

return $output;
}
}
22 changes: 13 additions & 9 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ public function children($type = Page::STANDARD_PAGES)
*/
public function isFirst()
{
$collection = $this->parent()->collection();
$collection = $this->parent()->collection('content', false);
return $collection->isFirst($this->path());
}

Expand All @@ -1197,7 +1197,7 @@ public function isFirst()
*/
public function isLast()
{
$collection = $this->parent()->collection();
$collection = $this->parent()->collection('content', false);
return $collection->isLast($this->path());
}

Expand Down Expand Up @@ -1229,7 +1229,7 @@ public function nextSibling()
*/
public function adjacentSibling($direction = 1)
{
$collection = $this->parent()->collection();
$collection = $this->parent()->collection('content', false);
return $collection->adjacentSibling($this->path(), $direction);
}

Expand Down Expand Up @@ -1318,10 +1318,11 @@ public function find($url)
* Get a collection of pages in the current context.
*
* @param string|array $params
* @param boolean $pagination
* @return Collection
* @throws \InvalidArgumentException
*/
public function collection($params = 'content')
public function collection($params = 'content', $pagination = true)
{
if (is_string($params)) {
$params = (array) $this->value('header.'.$params);
Expand Down Expand Up @@ -1378,13 +1379,16 @@ public function collection($params = 'content')
// New Custom event to handle things like pagination.
$grav->fireEvent('onCollectionProcessed', new Event(['collection' => $collection]));

$params = $collection->params();
// Slice and dice the collection if pagination is required
if ($pagination) {
$params = $collection->params();

$limit = isset($params['limit']) ? $params['limit'] : 0;
$start = !empty($params['pagination']) ? ($uri->currentPage() - 1) * $limit : 0;
$limit = isset($params['limit']) ? $params['limit'] : 0;
$start = !empty($params['pagination']) ? ($uri->currentPage() - 1) * $limit : 0;

if ($limit && $collection->count() > $limit) {
$collection->slice($start, $limit);
if ($limit && $collection->count() > $limit) {
$collection->slice($start, $limit);
}
}

return $collection;
Expand Down
63 changes: 62 additions & 1 deletion system/src/Grav/Common/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function getFilters()
new \Twig_SimpleFilter('md5', [$this,'md5Filter']),
new \Twig_SimpleFilter('sort_by_key', [$this,'sortByKeyFilter']),
new \Twig_SimpleFilter('ksort', [$this,'ksortFilter']),
new \Twig_SimpleFilter('contains', [$this, 'containsFilter'])
new \Twig_SimpleFilter('contains', [$this, 'containsFilter']),
new \Twig_SimpleFilter('nicetime', [$this, 'nicetimeFilter']),
];
}

Expand Down Expand Up @@ -255,6 +256,66 @@ public function containsFilter($haystack, $needle)
return (strpos($haystack, $needle) !== false);
}

/**
* displays a facebook style 'time ago' formatted date/time
*
* @param $date
* @param $long_strings
* @param String
*
* @return boolean
*/
public function nicetimeFilter($date, $long_strings = true)
{
if (empty($date)) {
return "No date provided";
}

if ($long_strings) {
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
} else {
$periods = array("sec", "min", "hr", "day", "wk", "mo", "yr", "dec");
}

$lengths = array("60","60","24","7","4.35","12","10");

$now = time();

// check if unix timestamp
if ((string)(int)$date == $date) {
$unix_date = $date;
} else {
$unix_date = strtotime($date);
}

// check validity of date
if (empty($unix_date)) {
return "Bad date";
}

// is it future date or past date
if ($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";

} else {
$difference = $unix_date - $now;
$tense = "from now";
}

for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}

$difference = round($difference);

if ($difference != 1) {
$periods[$j].= "s";
}

return "$difference $periods[$j] {$tense}";
}

/**
* Repeat given string x times.
*
Expand Down
77 changes: 9 additions & 68 deletions system/src/Grav/Console/Cli/ClearCacheCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Grav\Console\Cli;

use Grav\Console\ConsoleTrait;
use Grav\Common\Cache;
use Grav\Common\Filesystem\Folder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
Expand All @@ -17,33 +19,6 @@
class ClearCacheCommand extends Command
{

protected $standard_remove = [
'cache/twig/',
'cache/doctrine/',
'cache/compiled/',
'cache/validated-',
'images/',
'assets/',
];

protected $all_remove = [
'cache/',
'images/',
'assets/'
];

protected $assets_remove = [
'assets/'
];

protected $images_remove = [
'images/'
];

protected $cache_remove = [
'cache/'
];

/**
*
*/
Expand Down Expand Up @@ -87,55 +62,21 @@ private function cleanPaths(InputInterface $input, OutputInterface $output)
$output->writeln('<magenta>Clearing cache</magenta>');
$output->writeln('');

$user_config = USER_DIR . 'config/system.yaml';

$anything = false;

if ($input->getOption('all')) {
$remove_paths = $this->all_remove;
$remove = 'all';
} elseif ($input->getOption('assets-only')) {
$remove_paths = $this->assets_remove;
$remove = 'assets-only';
} elseif ($input->getOption('images-only')) {
$remove_paths = $this->images_remove;
$remove = 'images-only';
} elseif ($input->getOption('cache-only')) {
$remove_paths = $this->cache_remove;
$remove = 'cache-only';
} else {
$remove_paths = $this->standard_remove;
$remove = 'standard';
}

foreach ($remove_paths as $path) {

$files = glob(ROOT_DIR . $path . '*');

foreach ($files as $file) {
if (is_file($file)) {
if (@unlink($file)) {
$anything = true;
}
} elseif (is_dir($file)) {
if (@Folder::delete($file)) {
$anything = true;
}
}
}

if ($anything) {
$output->writeln('<red>Cleared: </red>' . $path . '*');
}
}

if (file_exists($user_config)) {
touch($user_config);
$output->writeln('');
$output->writeln('<red>Touched: </red>' . $user_config);
$output->writeln('');
foreach (Cache::clearCache($remove) as $result) {
$output->writeln($result);
}

if (!$anything) {
$output->writeln('<green>Nothing to clear...</green>');
$output->writeln('');
}

}
}

0 comments on commit f284147

Please sign in to comment.