mirror of
https://github.com/Mibew/mibew.git
synced 2025-04-18 12:57:24 +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\AuthenticationManagerAwareInterface;
|
||||||
use Mibew\Authentication\AuthenticationManagerInterface;
|
use Mibew\Authentication\AuthenticationManagerInterface;
|
||||||
use Mibew\Cache\CacheAwareInterface;
|
use Mibew\Cache\CacheAwareInterface;
|
||||||
|
use Mibew\Handlebars\CacheAdapter as HandlebarsCacheAdapter;
|
||||||
use Mibew\Handlebars\HandlebarsAwareInterface;
|
use Mibew\Handlebars\HandlebarsAwareInterface;
|
||||||
use Mibew\Handlebars\Helper\AssetHelper;
|
use Mibew\Handlebars\Helper\AssetHelper;
|
||||||
use Mibew\Handlebars\Helper\CsrfProtectedRouteHelper;
|
use Mibew\Handlebars\Helper\CsrfProtectedRouteHelper;
|
||||||
@ -160,6 +161,14 @@ abstract class AbstractController implements
|
|||||||
public function setCache(PoolInterface $cache)
|
public function setCache(PoolInterface $cache)
|
||||||
{
|
{
|
||||||
$this->cache = $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)
|
protected function prepareStyle(StyleInterface $style)
|
||||||
{
|
{
|
||||||
if ($style instanceof HandlebarsAwareInterface) {
|
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
|
// Add more helpers to template engine
|
||||||
$style->getHandlebars()->addHelper(
|
$hbs->addHelper(
|
||||||
'route',
|
'route',
|
||||||
new RouteHelper($this->getRouter())
|
new RouteHelper($this->getRouter())
|
||||||
);
|
);
|
||||||
$style->getHandlebars()->addHelper(
|
$hbs->addHelper(
|
||||||
'csrfProtectedRoute',
|
'csrfProtectedRoute',
|
||||||
new CsrfProtectedRouteHelper($this->getRouter())
|
new CsrfProtectedRouteHelper($this->getRouter())
|
||||||
);
|
);
|
||||||
$style->getHandlebars()->addHelper(
|
$hbs->addHelper(
|
||||||
'asset',
|
'asset',
|
||||||
new AssetHelper(
|
new AssetHelper(
|
||||||
$this->getAssetManager()->getUrlGenerator(),
|
$this->getAssetManager()->getUrlGenerator(),
|
||||||
array('CurrentStyle' => $style->getFilesPath())
|
array('CurrentStyle' => $style->getFilesPath())
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$style->getHandlebars()->addHelper(
|
$hbs->addHelper(
|
||||||
'jsAssets',
|
'jsAssets',
|
||||||
new JsAssetsHelper($this->getAssetManager())
|
new JsAssetsHelper($this->getAssetManager())
|
||||||
);
|
);
|
||||||
$style->getHandlebars()->addHelper(
|
$hbs->addHelper(
|
||||||
'cssAssets',
|
'cssAssets',
|
||||||
new CssAssetsHelper($this->getAssetManager())
|
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