Commit b1a1458d by dev-meghraj

fixes code style.

parent eabcf1c7
...@@ -5,9 +5,9 @@ Yii Framework 2 twig extension Change Log ...@@ -5,9 +5,9 @@ Yii Framework 2 twig extension Change Log
---------------------------- ----------------------------
- no changes in this release. - no changes in this release.
- Add File based Twig loader for better caching and usability of twig's file based function
2.0.0 alpha, December 1, 2013 2.0.0 alpha, December 1, 2013
----------------------------- -----------------------------
- Initial release. - Initial release.
- Add more features like in old and file based loader for twig files
<?php <?php
/**
* Simple file system wrapper for twig to process twig files
*
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\twig; namespace yii\twig;
/** /**
* Twig view file loader class * Twig view file loader class.
* *
* @author dev-mraj <dev.meghraj@gmail.com> * @author dev-mraj <dev.meghraj@gmail.com>
* @version 1.0.0 * @version 1.0.0
...@@ -11,26 +19,58 @@ namespace yii\twig; ...@@ -11,26 +19,58 @@ namespace yii\twig;
class TwigSimpleFileLoader implements \Twig_LoaderInterface { class TwigSimpleFileLoader implements \Twig_LoaderInterface {
/** /**
* Path to directory where all file exists * @var string Path to directory
* @var string
*/ */
private $dir; private $_dir;
public function __construct($dir){ /*
$this->dir=$dir; * @param @dir string path to directory
*/
public function __construct($dir)
{
$this->_dir=$dir;
} }
public function isFresh($name, $time){ /**
* Compare a file's freshness with previously stored timestamp
*
* @param $name string file name to check
* @param $time int timestamp to compare with
* @return bool true if file is still fresh and not changes, false otherwise
*/
public function isFresh($name, $time)
{
return filemtime($this->getFilePath($name))<=$time; return filemtime($this->getFilePath($name))<=$time;
} }
public function getSource($name){
/**
* get the source of given file name
*
* @param $name string file name
* @return string contents of given file name
*/
public function getSource($name)
{
return file_get_contents($this->getFilePath($name)); return file_get_contents($this->getFilePath($name));
} }
public function getCacheKey($name){
/**
* get a unique key that can represent this file uniquely among other files.
* @param $name
* @return string
*/
public function getCacheKey($name)
{
return $this->getFilePath($name); return $this->getFilePath($name);
} }
/**
* internally used to get absolute path of given file name
* @param $name string file name
* @return string absolute path of file
*/
protected function getFilePath($name){ protected function getFilePath($name){
return $this->dir.'/'.$name; return $this->_dir.'/'.$name;
} }
} }
\ No newline at end of file
...@@ -13,6 +13,7 @@ use Yii; ...@@ -13,6 +13,7 @@ use Yii;
use yii\base\View; use yii\base\View;
use yii\base\ViewRenderer as BaseViewRenderer; use yii\base\ViewRenderer as BaseViewRenderer;
use yii\helpers\Html; use yii\helpers\Html;
use yii\twig\TwigSimpleFileLoader;
/** /**
* TwigViewRenderer allows you to use Twig templates in views. * TwigViewRenderer allows you to use Twig templates in views.
...@@ -76,7 +77,7 @@ class ViewRenderer extends BaseViewRenderer ...@@ -76,7 +77,7 @@ class ViewRenderer extends BaseViewRenderer
public $lexerOptions = []; public $lexerOptions = [];
/** /**
* @var \Twig_Environment * @var \Twig_Environment twig environment object that do all rendering twig templates
*/ */
public $twig; public $twig;
...@@ -85,7 +86,6 @@ class ViewRenderer extends BaseViewRenderer ...@@ -85,7 +86,6 @@ class ViewRenderer extends BaseViewRenderer
$this->twig = new \Twig_Environment(null, array_merge([ $this->twig = new \Twig_Environment(null, array_merge([
'cache' => Yii::getAlias($this->cachePath), 'cache' => Yii::getAlias($this->cachePath),
'auto_reload' => true,
'charset' => Yii::$app->charset, 'charset' => Yii::$app->charset,
], $this->options)); ], $this->options));
...@@ -95,6 +95,7 @@ class ViewRenderer extends BaseViewRenderer ...@@ -95,6 +95,7 @@ class ViewRenderer extends BaseViewRenderer
$this->twig->addExtension(new $extension()); $this->twig->addExtension(new $extension());
} }
} }
// Adding custom globals (objects or static classes) // Adding custom globals (objects or static classes)
if (!empty($this->globals)) { if (!empty($this->globals)) {
$this->addGlobals($this->globals); $this->addGlobals($this->globals);
......
<?php <?php
/**
* Twig view renderer class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\twig; namespace yii\twig;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment