Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored Jade API & added caching mechanism

  • Loading branch information...
commit 26435eb5f3db0ecc794e2698a065de61853c9e7c 1 parent 4b4c8a8
@everzet authored
View
12 README.md
@@ -29,14 +29,12 @@ and implemented for PHP 5.3.
$dumper->registerFilter('php', new PHPFilter());
$dumper->registerFilter('style', new CSSFilter());
- // Initialize parser & register PHP dumper
- $lexer = new Lexer();
- $jade = new Jade($lexer);
- $jade->registerDumper('php', $dumper);
+ // Initialize parser & Jade
+ $parser = new Parser(new Lexer());
+ $jade = new Jade($parser, $dumper);
- // Parse a string
- $jade->load($value);
- echo $jade->dump('php');
+ // Parse a template (both string & file containers)
+ echo $jade->render($template);
## Syntax
View
140 src/Everzet/Jade/Jade.php
@@ -20,103 +20,155 @@
class Jade
{
protected $parser;
- protected $document;
- protected $dumperName;
- protected $dumpers = array();
+ protected $dumper;
+ protected $cache;
/**
* Initialize parser.
*
* @param LexerInterface $lexer jade lexer
*/
- public function __construct(LexerInterface $lexer)
+ public function __construct(Parser $parser, DumperInterface $dumper, $cache = null)
{
- $this->parser = new Parser($lexer);
+ $this->parser = $parser;
+ $this->dumper = $dumper;
+ $this->cache = $cache;
}
/**
- * Register specific dumper with custom name.
+ * Render provided input to dumped string.
*
- * @param string $name dumper name
- * @param DumperInterface $dumper dumper
+ * @param string $input input string or file path
*
- * @throws \InvalidArgumentException if dumper with such name already registered
+ * @return string dumped string
*/
- public function registerDumper($name, DumperInterface $dumper)
+ public function render($input)
{
- if (isset($this->dumpers[$name])) {
- throw new \InvalidArgumentException(sprintf('Dumper with name %s already registered.', $name));
- }
+ $source = $this->getInputSource($input);
+ $parsed = $this->parser->parse($source);
- $this->dumpers[$name] = $dumper;
+ return $this->dumper->dump($parsed);
}
/**
- * Set default dumper name.
+ * Get current fresh cache path.
+ * Or render & dump input to new cache & return it's path.
*
- * @param string $name dumper name
+ * @param string $input input string or file path
*
- * @throws \InvalidArgumentException if dumper with such name not registered
+ * @return string cache path
*/
- public function setDefaultDumper($name)
+ public function cache($input)
{
- if (!isset($this->dumpers[$name])) {
- throw new \InvalidArgumentException(sprintf('No dumper with %s name registered.', $name));
+ if (null === $this->cache || !is_dir($this->cache)) {
+ throw new Exception('You must provide correct cache path to Jade for caching.');
+ }
+
+ $cacheKey = $this->getInputCacheKey($input);
+ $cacheTime = $this->getCacheTime($cacheKey);
+
+ if (false !== $cacheTime && $this->isCacheFresh($input, $cacheTime)) {
+ return $this->getCachePath($cacheKey);
}
- $this->dumperName = $name;
+ if (!is_writable($this->cache)) {
+ throw new Exception(sprintf('Cache directory must be writable. "%s" is not.', $this->cache));
+ }
+
+ $rendered = $this->render($input);
+
+ return $this->cacheInput($cacheKey, $rendered);
}
/**
- * Load & parse jade document.
+ * Return source from input (Jade template).
*
- * @param string $string jade document
+ * @param string $input input string or file path
*
- * @throws Exception on parsing errors
+ * @return string
*/
- public function load($string)
+ protected function getInputSource($input)
{
- $this->document = $this->parser->parse($string);
+ if (is_file($input)) {
+ return file_get_contents($input);
+ }
+
+ return (string) $input;
}
/**
- * Load & parse jade document from file.
+ * Return caching key for input.
*
- * @param string $filename jade document file path
+ * @param string $input input string or file path
+ *
+ * @return string
*/
- public function loadFile($filename)
+ protected function getInputCacheKey($input)
{
- $this->load(file_get_contents($filename));
+ if (is_file($input)) {
+ return basename($input, '.jade');
+ } else {
+ throw new \InvalidArgumentException('Only file templates can be cached.');
+ }
}
/**
- * Dump jade document with current dumper & return dumped string.
+ * Return full cache path for specified cache key.
*
- * @param string $dumperName dumper name (will use default if no provided)
+ * @param string $cacheKey cache key
*
- * @return string
+ * @return string absolute path
+ */
+ protected function getCachePath($cacheKey)
+ {
+ return $this->cache . '/' . $cacheKey . '.php';
+ }
+
+ /**
+ * Return cache time creation.
+ *
+ * @param string $cacheKey cache key
*
- * @throws Exception if dumper is not specified
+ * @return integer UNIX timestamp (filemtime used)
*/
- public function dump($dumperName = null)
+ protected function getCacheTime($cacheKey)
{
- $dumperName = null !== $dumperName ? $dumperName : $this->dumperName;
+ $path = $this->getCachePath($cacheKey);
+
+ return filemtime($path);
+ }
- if (null === $dumperName) {
- throw new Exception('You must specify dumper name');
+ /**
+ * Return true if cache, created at specified time is fresh enough for provided input.
+ *
+ * @param string $input input string or file
+ * @param srting $cacheTime cache key
+ *
+ * @return boolean true if fresh, false otherways
+ */
+ protected function isCacheFresh($input, $cacheTime)
+ {
+ if (is_file($input)) {
+ return filemtime($input) < $cacheTime;
}
- return $this->dumpers[$dumperName]->dump($this->document);
+ return false;
}
/**
- * Dump jade document with current dumper to specified file.
+ * Cache rendered input at provided key.
*
- * @param string $path dumping file path
- * @param string $dumperName dumper name (will use default if no provided)
+ * @param string $cacheKey new cache key
+ * @param string $rendered rendered input
+ *
+ * @return string new cache path
*/
- public function dumpToFile($path, $dumperName = null)
+ protected function cacheInput($cacheKey, $rendered)
{
- file_put_contents($path, $this->dump(), $dumperName);
+ $path = $this->getCachePath($cacheKey);
+
+ file_put_contents($path, $rendered);
+
+ return $path;
}
}
View
19 tests/Everzet/Jade/FiltersTest.php
@@ -1,6 +1,7 @@
<?php
use Everzet\Jade\Jade;
+use Everzet\Jade\Parser;
use Everzet\Jade\Lexer\Lexer;
use Everzet\Jade\Dumper\PHPDumper;
use Everzet\Jade\Visitor\AutotagsVisitor;
@@ -23,22 +24,24 @@
*/
class FiltersTest extends \PHPUnit_Framework_TestCase
{
- protected function parse($value)
- {
- $lexer = new Lexer();
- $jade = new Jade($lexer);
+ protected $jade;
+ public function __construct()
+ {
+ $parser = new Parser(new Lexer());
$dumper = new PHPDumper();
$dumper->registerVisitor('tag', new AutotagsVisitor());
-
$dumper->registerFilter('javascript', new JavaScriptFilter());
$dumper->registerFilter('cdata', new CDATAFilter());
$dumper->registerFilter('php', new PHPFilter());
$dumper->registerFilter('style', new CSSFilter());
- $jade->registerDumper('php', $dumper);
- $jade->load($value);
- return $jade->dump('php');
+ $this->jade = new Jade($parser, $dumper);
+ }
+
+ protected function parse($value)
+ {
+ return $this->jade->render($value);
}
public function testFilterCodeInsertion()
View
17 tests/Everzet/Jade/JadeTest.php
@@ -1,6 +1,7 @@
<?php
use Everzet\Jade\Jade;
+use Everzet\Jade\Parser;
use Everzet\Jade\Lexer\Lexer;
use Everzet\Jade\Dumper\PHPDumper;
use Everzet\Jade\Visitor\AutotagsVisitor;
@@ -18,18 +19,20 @@
*/
class JadeTest extends \PHPUnit_Framework_TestCase
{
- protected function parse($value)
- {
- $lexer = new Lexer();
- $jade = new Jade($lexer);
+ protected $jade;
+ public function __construct()
+ {
+ $parser = new Parser(new Lexer());
$dumper = new PHPDumper();
$dumper->registerVisitor('tag', new AutotagsVisitor());
- $jade->registerDumper('php', $dumper);
- $jade->load($value);
+ $this->jade = new Jade($parser, $dumper);
+ }
- return $jade->dump('php');
+ protected function parse($value)
+ {
+ return $this->jade->render($value);
}
public function testDoctypes()
Please sign in to comment.
Something went wrong with that request. Please try again.