mirror of
https://github.com/Mibew/mibew.git
synced 2024-11-15 16:44:11 +03:00
Use cache for Handlebars AST
This commit is contained in:
parent
d92b88b65a
commit
0d1efa5a87
@ -25,6 +25,7 @@ use Mibew\Asset\Generator\UrlGeneratorInterface as AssetUrlGeneratorInterface;
|
||||
use Mibew\Authentication\AuthenticationManagerAwareInterface;
|
||||
use Mibew\Authentication\AuthenticationManagerInterface;
|
||||
use Mibew\Cache\CacheAwareInterface;
|
||||
use Mibew\Handlebars\CacheAdapter as HandlebarsCacheAdapter;
|
||||
use Mibew\Handlebars\HandlebarsAwareInterface;
|
||||
use Mibew\Handlebars\Helper\AssetHelper;
|
||||
use Mibew\Handlebars\Helper\CsrfProtectedRouteHelper;
|
||||
@ -160,6 +161,14 @@ abstract class AbstractController implements
|
||||
public function setCache(PoolInterface $cache)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
|
||||
// Update cache inside the handlebars template engine if needed.
|
||||
if ($this->getStyle() instanceof HandlebarsAwareInterface) {
|
||||
$hbs = $this->getStyle()->getHandlebars();
|
||||
if ($hbs->getCache() instanceof CacheAwareInterface) {
|
||||
$hbs->getCache()->setCache($cache);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -275,27 +284,32 @@ abstract class AbstractController implements
|
||||
protected function prepareStyle(StyleInterface $style)
|
||||
{
|
||||
if ($style instanceof HandlebarsAwareInterface) {
|
||||
$hbs = $style->getHandlebars();
|
||||
|
||||
// Use mibew cache to store Handlebars AST
|
||||
$hbs->setCache(new HandlebarsCacheAdapter($this->getCache()));
|
||||
|
||||
// Add more helpers to template engine
|
||||
$style->getHandlebars()->addHelper(
|
||||
$hbs->addHelper(
|
||||
'route',
|
||||
new RouteHelper($this->getRouter())
|
||||
);
|
||||
$style->getHandlebars()->addHelper(
|
||||
$hbs->addHelper(
|
||||
'csrfProtectedRoute',
|
||||
new CsrfProtectedRouteHelper($this->getRouter())
|
||||
);
|
||||
$style->getHandlebars()->addHelper(
|
||||
$hbs->addHelper(
|
||||
'asset',
|
||||
new AssetHelper(
|
||||
$this->getAssetManager()->getUrlGenerator(),
|
||||
array('CurrentStyle' => $style->getFilesPath())
|
||||
)
|
||||
);
|
||||
$style->getHandlebars()->addHelper(
|
||||
$hbs->addHelper(
|
||||
'jsAssets',
|
||||
new JsAssetsHelper($this->getAssetManager())
|
||||
);
|
||||
$style->getHandlebars()->addHelper(
|
||||
$hbs->addHelper(
|
||||
'cssAssets',
|
||||
new CssAssetsHelper($this->getAssetManager())
|
||||
);
|
||||
|
107
src/mibew/libs/classes/Mibew/Handlebars/CacheAdapter.php
Normal file
107
src/mibew/libs/classes/Mibew/Handlebars/CacheAdapter.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is a part of Mibew Messenger.
|
||||
*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Handlebars;
|
||||
|
||||
use Handlebars\Cache as HandlebarsCacheInterface;
|
||||
use Mibew\Cache\CacheAwareInterface;
|
||||
use Stash\Interfaces\PoolInterface;
|
||||
use Stash\Invalidation;
|
||||
|
||||
/**
|
||||
* An adapter to use Mibew cache with Handlebars template engine.
|
||||
*/
|
||||
class CacheAdapter implements HandlebarsCacheInterface, CacheAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var PoolInterface;
|
||||
*/
|
||||
protected $cache = null;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param PoolInterface $cache An instance of cache pool
|
||||
*/
|
||||
public function __construct(PoolInterface $cache)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCache()
|
||||
{
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setCache(PoolInterface $cache)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
$item = $this->getCacheItem($name);
|
||||
$cached_data = $item->get(Invalidation::NONE);
|
||||
|
||||
if ($item->isMiss()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $cached_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function remove($name)
|
||||
{
|
||||
$item = $this->getCacheItem($name);
|
||||
$item->clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set($name, $value)
|
||||
{
|
||||
$item = $this->getCacheItem($name);
|
||||
// Cache templates for ten minutes
|
||||
$item->set($value, 10*60);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an item from the cache pool.
|
||||
*
|
||||
* @param string $name Name of the item.
|
||||
* @return \Stash\Interfaces\ItemInterface
|
||||
*/
|
||||
protected function getCacheItem($name)
|
||||
{
|
||||
return $this->getCache()->getItem('handlebars/template/' . $name);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user