1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of phpFastCache. |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
10
|
|
|
* |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* @author PastisD https://github.com/PastisD |
13
|
|
|
* @author Alexander (asm89) <[email protected]> |
14
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
declare(strict_types=1); |
18
|
|
|
|
19
|
|
|
namespace Phpfastcache\Bundle\Twig\CacheExtension\Node; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Cache twig node. |
23
|
|
|
* |
24
|
|
|
* @author Alexander <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class CacheNode extends \Twig_Node |
27
|
|
|
{ |
28
|
|
|
private static $cacheCount = 1; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \Twig_Node_Expression $annotation |
32
|
|
|
* @param \Twig_Node_Expression $keyInfo |
33
|
|
|
* @param \Twig_NodeInterface $body |
34
|
|
|
* @param integer $lineno |
35
|
|
|
* @param string $tag |
36
|
|
|
*/ |
37
|
|
|
public function __construct(\Twig_Node_Expression $annotation, \Twig_Node_Expression $keyInfo, \Twig_Node $body, $lineno, $tag = null) |
38
|
|
|
{ |
39
|
|
|
parent::__construct([ |
40
|
|
|
'key_info' => $keyInfo, |
41
|
|
|
'body' => $body, |
42
|
|
|
'annotation' => $annotation |
43
|
|
|
], [], $lineno, $tag); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
public function compile(\Twig_Compiler $compiler) |
50
|
|
|
{ |
51
|
|
|
$i = self::$cacheCount++; |
52
|
|
|
|
53
|
|
|
if (\version_compare(\Twig_Environment::VERSION, '1.26.0', '>=')) { |
54
|
|
|
$extension = \Phpfastcache\Bundle\Twig\CacheExtension\Extension::class; |
55
|
|
|
} else { |
56
|
|
|
$extension = 'phpfastcache_cache'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$compiler |
60
|
|
|
->addDebugInfo($this) |
61
|
|
|
->write("\$phpfastcacheCacheStrategy".$i." = \$this->env->getExtension('{$extension}')->getCacheStrategy();\n") |
62
|
|
|
->write("\$phpfastcacheKey".$i." = \$phpfastcacheCacheStrategy".$i."->generateKey(") |
63
|
|
|
->subcompile($this->getNode('annotation')) |
64
|
|
|
->raw(", ") |
65
|
|
|
->subcompile($this->getNode('key_info')) |
66
|
|
|
->write(");\n") |
67
|
|
|
->write("\$phpfastcacheCacheBody".$i." = \$phpfastcacheCacheStrategy".$i."->fetchBlock(\$phpfastcacheKey".$i.", \$this->getSourceContext());\n") |
68
|
|
|
->write("if (\$phpfastcacheCacheBody".$i." === false) {\n") |
69
|
|
|
->indent() |
70
|
|
|
->write("\\ob_start();\n") |
71
|
|
|
->write("\$compileMc = \\microtime(true);\n") |
72
|
|
|
->indent() |
73
|
|
|
->subcompile($this->getNode('body')) |
74
|
|
|
->outdent() |
75
|
|
|
->write("\n") |
76
|
|
|
// ->write("sleep(2);\n") // For debug purpose |
|
|
|
|
77
|
|
|
->write("\$phpfastcacheCacheBody".$i." = \\ob_get_clean();\n") |
78
|
|
|
->write("\$phpfastcacheCacheStrategy".$i."->saveBlock(\$phpfastcacheKey".$i.", \$phpfastcacheCacheBody".$i.", \\microtime(true) - \$compileMc, \$this->getSourceContext());\n") |
79
|
|
|
->outdent() |
80
|
|
|
->write("}\n") |
81
|
|
|
->write("echo \$phpfastcacheCacheBody".$i.";\n") |
82
|
|
|
; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.