Completed
Push — master ( c8fd93...a9ab00 )
by Georges
01:49
created

CacheNode::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 5
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
18
namespace phpFastCache\Bundle\Twig\CacheExtension\Node;
19
20
/**
21
 * Cache twig node.
22
 *
23
 * @author Alexander <[email protected]>
24
 */
25
class CacheNode extends \Twig_Node
26
{
27
    private static $cacheCount = 1;
28
29
    /**
30
     * @param \Twig_Node_Expression $annotation
31
     * @param \Twig_Node_Expression $keyInfo
32
     * @param \Twig_NodeInterface   $body
33
     * @param integer               $lineno
34
     * @param string                $tag
35
     */
36
    public function __construct(\Twig_Node_Expression $annotation, \Twig_Node_Expression $keyInfo, \Twig_Node $body, $lineno, $tag = null)
37
    {
38
        parent::__construct(array('key_info' => $keyInfo, 'body' => $body, 'annotation' => $annotation), array(), $lineno, $tag);
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function compile(\Twig_Compiler $compiler)
45
    {
46
        $i = self::$cacheCount++;
47
48
        if (version_compare(\Twig_Environment::VERSION, '1.26.0', '>=')) {
49
            $extension = 'phpFastCache\Bundle\Twig\CacheExtension\Extension';
50
        } else {
51
            $extension = 'phpfastcache_cache';
52
        }
53
54
        $compiler
55
            ->addDebugInfo($this)
56
            ->write("\$phpfastcacheCacheStrategy".$i." = \$this->env->getExtension('{$extension}')->getCacheStrategy();\n")
57
            ->write("\$phpfastcacheKey".$i." = \$phpfastcacheCacheStrategy".$i."->generateKey(")
58
                ->subcompile($this->getNode('annotation'))
59
                ->raw(", ")
60
                ->subcompile($this->getNode('key_info'))
61
            ->write(");\n")
62
            ->write("\$phpfastcacheCacheBody".$i." = \$phpfastcacheCacheStrategy".$i."->fetchBlock(\$phpfastcacheKey".$i.");\n")
63
            ->write("if (\$phpfastcacheCacheBody".$i." === false) {\n")
64
            ->indent()
65
                ->write("ob_start();\n")
66
                ->write("\$compileMc = microtime(true);\n")
67
                    ->indent()
68
                        ->subcompile($this->getNode('body'))
69
                    ->outdent()
70
                ->write("\n")
71
                // ->write("sleep(2);\n") // For debug purpose
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
72
                ->write("\$phpfastcacheCacheBody".$i." = ob_get_clean();\n")
73
                ->write("\$phpfastcacheCacheStrategy".$i."->saveBlock(\$phpfastcacheKey".$i.", \$phpfastcacheCacheBody".$i.", microtime(true) - \$compileMc);\n")
74
            ->outdent()
75
            ->write("}\n")
76
            ->write("echo \$phpfastcacheCacheBody".$i.";\n")
77
        ;
78
    }
79
}
80