/
Jade.php
111 lines (95 loc) · 2.73 KB
/
Jade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace Bundle\Everzet\JadeBundle;
use Symfony\Component\Templating\Storage\Storage;
use Symfony\Component\Templating\Storage\FileStorage;
use Everzet\Jade\Jade as BaseJade;
use Everzet\Jade\Parser;
use Everzet\Jade\Dumper\DumperInterface;
/*
* This file is part of the EverzetJadeBundle.
* (c) 2010 Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Jade.
*/
class Jade extends BaseJade
{
/**
* Initialize parser.
*
* @param LexerInterface $lexer jade lexer
*/
public function __construct(Parser $parser, DumperInterface $dumper, $cache = null)
{
if (null !== $cache && !is_dir($cache)) {
mkdir($cache, 0777, true);
}
parent::__construct($parser, $dumper, $cache);
}
/**
* Return source from input (Jade template).
*
* @param string $input input string or file path
*
* @return string
*/
protected function getInputSource($input)
{
if ($input instanceof Storage) {
return $input->getContent();
}
throw new \InvalidArgumentException(sprintf('The template "%s" does not exist.', $input));
}
/**
* Return caching key for input.
*
* @param string $input input string or file path
*
* @return string
*/
protected function getInputCacheKey($input)
{
if ($input instanceof Storage) {
return (string) $input;
}
throw new \InvalidArgumentException(sprintf('The template "%s" does not exist.', $input));
}
/**
* 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 ($input instanceof Storage) {
if ($input instanceof FileStorage) {
return filemtime((string) $input) < $cacheTime;
}
}
return false;
}
/**
* Cache rendered input at provided key.
*
* @param string $cacheKey new cache key
* @param string $rendered rendered input
*
* @return string new cache path
*/
protected function cacheInput($cacheKey, $rendered)
{
$path = $this->getCachePath($cacheKey);
$dir = dirname($path);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
file_put_contents($path, $rendered);
return $path;
}
}