-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathKintExtension.php
102 lines (85 loc) · 2.54 KB
/
KintExtension.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
<?php
/*
* This file is part of KintBundle.
*
* (c) 2012 Carlos Granados
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cg\KintBundle\Twig\Extension;
use Kint;
use Twig\TwigFunction;
use Twig\Extension\AbstractExtension;
use Twig\Template;
class KintExtension extends AbstractExtension
{
private $enabled;
private $nesting_depth;
private $string_length;
private $kernel;
public function __construct($enabled, $nesting_depth, $string_length, $kernel)
{
$this->enabled = $enabled;
$this->nesting_depth = $nesting_depth;
$this->string_length = $string_length;
$this->kernel = $kernel;
}
/**
* Returns a list of global functions to add to the existing list.
*
* @return array An array of global functions
*/
public function getFunctions()
{
return array(
'kint' => new TwigFunction('kint', array($this, 'twig_kint'), array('is_safe' => array('html'), 'needs_context' => true)),
);
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'kint';
}
public function twig_kint($context)
{
if (!$this->enabled) {
return;
}
Kint::$displayCalledFrom = false;
Kint::$devel = false;
Kint::$maxLevels = $this->nesting_depth;
Kint::$maxStrLength = $this->string_length;
Kint::$appRootDirs = array(
$this->kernel->getRootDir() => '<APP ROOT>'
);
$output = '';
$count = func_num_args();
if (1 === $count) {
//no extra parameters passed, so we dump the whole twig context
$kint_variable = array();
foreach ($context as $key => $value) {
if (!$value instanceof Template) {
$kint_variable[$key] = $value;
}
}
ob_start();
Kint::dump($kint_variable);
$output = ob_get_clean();
$output = str_replace('$kint_variable', 'TWIG CONTEXT', $output);
} else {
for ($i = 1; $i < $count; $i++) {
$kint_variable = func_get_arg($i);
ob_start();
Kint::dump($kint_variable);
$result = ob_get_clean();
$output .= str_replace('$kint_variable', $i, $result);
}
}
return $output;
}
}