1
|
|
|
<?php |
2
|
|
|
namespace Boekkooi\Bundle\TwigJackBundle\Twig\Extension; |
3
|
|
|
|
4
|
|
|
use Boekkooi\Bundle\TwigJackBundle\Twig\TokenParser; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @author Warnar Boekkooi <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
class DeferExtension extends \Twig_Extension |
10
|
|
|
{ |
11
|
|
|
protected $references = array(); |
12
|
|
|
|
13
|
|
|
protected $subReferences = array(); |
14
|
|
|
|
15
|
|
|
protected $deferBlockPrefix; |
16
|
|
|
|
17
|
6 |
|
public function __construct($deferBlockPrefix = '_defer_ref_') |
18
|
|
|
{ |
19
|
6 |
|
$this->deferBlockPrefix = $deferBlockPrefix; |
20
|
6 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
1 |
|
public function getTokenParsers() |
26
|
|
|
{ |
27
|
|
|
return array( |
28
|
1 |
|
new TokenParser\Defer($this->deferBlockPrefix) |
29
|
1 |
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
public function getFunctions() |
33
|
|
|
{ |
34
|
|
|
return array( |
35
|
1 |
|
new \Twig_SimpleFunction('defer', null, array( |
|
|
|
|
36
|
1 |
|
'node_class' => 'Boekkooi\Bundle\TwigJackBundle\Twig\Node\Expression\DeferReference', |
37
|
1 |
|
)) |
38
|
1 |
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
5 |
|
public function cache($type, $content, $name = null, $offset = null) |
42
|
|
|
{ |
43
|
5 |
|
if (!isset($this->references[$type])) { |
44
|
5 |
|
$this->references[$type] = array(); |
45
|
5 |
|
$this->subReferences[$type] = array(); |
46
|
5 |
|
} |
47
|
|
|
|
48
|
5 |
|
if ($offset === null || $offset >= count($this->references[$type])) { |
49
|
5 |
|
$this->references[$type][] = $content; |
50
|
5 |
|
} elseif ($offset <= 0) { |
51
|
1 |
|
array_unshift($this->references[$type], $content); |
52
|
1 |
|
} else { |
53
|
1 |
|
array_splice($this->references[$type], $offset, 0, $content); |
54
|
|
|
} |
55
|
|
|
|
56
|
5 |
|
if ($name !== null) { |
57
|
5 |
|
$this->subReferences[$type][] = $name; |
58
|
5 |
|
} |
59
|
5 |
|
} |
60
|
|
|
|
61
|
1 |
|
public function contains($type, $name) |
62
|
|
|
{ |
63
|
1 |
|
return isset($this->subReferences[$type]) && in_array($name, $this->subReferences[$type], true); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
public function retrieve($type, $clear = true) |
67
|
|
|
{ |
68
|
4 |
|
if (!isset($this->references[$type])) { |
69
|
1 |
|
return array(); |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
$rtn = $this->references[$type]; |
73
|
4 |
|
if ($clear) { |
74
|
4 |
|
unset($this->references[$type]); |
75
|
4 |
|
} |
76
|
|
|
|
77
|
4 |
|
return $rtn; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the name of the extension. |
82
|
|
|
* |
83
|
|
|
* @return string The extension name |
84
|
|
|
*/ |
85
|
1 |
|
public function getName() |
86
|
|
|
{ |
87
|
1 |
|
return 'defer'; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.