Rework a bit the organization of the code. Added cache for comments

This commit is contained in:
Flavio Copes 2016-06-29 18:56:06 +02:00
parent 04078ba7e0
commit 60c8d091e6
1 changed files with 83 additions and 56 deletions

View File

@ -17,6 +17,7 @@ class CommentsPlugin extends Plugin
{ {
protected $route = 'comments'; protected $route = 'comments';
protected $enable = false; protected $enable = false;
protected $comments_cache_id;
/** /**
* @return array * @return array
@ -24,10 +25,7 @@ class CommentsPlugin extends Plugin
public static function getSubscribedEvents() public static function getSubscribedEvents()
{ {
return [ return [
'onPluginsInitialized' => ['onPluginsInitialized', 0], 'onPluginsInitialized' => ['onPluginsInitialized', 0]
'onFormProcessed' => ['onFormProcessed', 0],
'onPageInitialized' => ['onPageInitialized', 10],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
]; ];
} }
@ -36,7 +34,6 @@ class CommentsPlugin extends Plugin
*/ */
public function onPageInitialized() public function onPageInitialized()
{ {
if (!$this->isAdmin()) {
/** @var Page $page */ /** @var Page $page */
$page = $this->grav['page']; $page = $this->grav['page'];
if (!$page) { if (!$page) {
@ -51,17 +48,11 @@ class CommentsPlugin extends Plugin
} }
} }
} }
}
public function onTwigSiteVariables() { public function onTwigSiteVariables() {
if (!$this->isAdmin()) {
$this->grav['twig']->enable = $this->enable; $this->grav['twig']->enable = $this->enable;
if ($this->enable) {
$this->grav['twig']->comments = $this->fetchComments(); $this->grav['twig']->comments = $this->fetchComments();
} }
}
}
/** /**
* Determine if $haystack starts with $needle. Credit: http://stackoverflow.com/a/10473026/205039 * Determine if $haystack starts with $needle. Credit: http://stackoverflow.com/a/10473026/205039
@ -96,21 +87,34 @@ class CommentsPlugin extends Plugin
} }
/** /**
* Frontend side initialization
*/ */
public function onPluginsInitialized() public function initializeFrontend()
{ {
if (!$this->isAdmin()) {
$this->calculateEnable(); $this->calculateEnable();
if ($this->enable) {
$this->enable([ $this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0], 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onFormProcessed' => ['onFormProcessed', 0],
'onPageInitialized' => ['onPageInitialized', 10],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
]); ]);
}
} else { $cache = $this->grav['cache'];
//init cache id
$this->comments_cache_id = md5('comments-data' . $cache->getKey());
}
/**
* Admin side initialization
*/
public function initializeAdmin()
{
/** @var Uri $uri */ /** @var Uri $uri */
$uri = $this->grav['uri']; $uri = $this->grav['uri'];
//Admin
$this->enable([ $this->enable([
'onTwigTemplatePaths' => ['onTwigAdminTemplatePaths', 0], 'onTwigTemplatePaths' => ['onTwigAdminTemplatePaths', 0],
'onAdminMenu' => ['onAdminMenu', 0], 'onAdminMenu' => ['onAdminMenu', 0],
@ -132,6 +136,16 @@ class CommentsPlugin extends Plugin
$this->grav['twig']->comments = $comments; $this->grav['twig']->comments = $comments;
$this->grav['twig']->pages = $this->fetchPages(); $this->grav['twig']->pages = $this->fetchPages();
} }
/**
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->initializeAdmin();
} else {
$this->initializeFrontend();
}
} }
/** /**
@ -192,6 +206,10 @@ class CommentsPlugin extends Plugin
} }
$file->save(Yaml::dump($data)); $file->save(Yaml::dump($data));
//clear cache
$this->grav['cache']->delete($this->comments_cache_id);
break; break;
} }
} }
@ -294,11 +312,20 @@ class CommentsPlugin extends Plugin
* Return the comments associated to the current route * Return the comments associated to the current route
*/ */
private function fetchComments() { private function fetchComments() {
$cache = $this->grav['cache'];
//search in cache
if ($comments = $cache->fetch($this->comments_cache_id)) {
return $comments;
}
$lang = $this->grav['language']->getLanguage(); $lang = $this->grav['language']->getLanguage();
$filename = $lang ? '/' . $lang : ''; $filename = $lang ? '/' . $lang : '';
$filename .= $this->grav['uri']->path() . '.yaml'; $filename .= $this->grav['uri']->path() . '.yaml';
return $this->getDataFromFilename($filename)['comments']; $comments = $this->getDataFromFilename($filename)['comments'];
//save to cache if enabled
$cache->save($this->comments_cache_id, $comments);
return $comments;
} }
/** /**