ToolbarHelper.php
4.37 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since DebugKit 0.1
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace DebugKit\View\Helper;
use Cake\Cache\Cache;
use Cake\Datasource\ConnectionManager;
use Cake\Event\Event;
use Cake\View\Helper;
use DebugKit\DebugKitDebugger;
/**
* Provides Base methods for content specific debug toolbar helpers.
* Acts as a facade for other toolbars helpers as well.
*
* @since DebugKit 0.1
*/
class ToolbarHelper extends Helper
{
/**
* helpers property
*
* @var array
*/
public $helpers = ['Html', 'Form', 'Url'];
/**
* Whether or not the top level keys should be sorted.
*
* @var bool
*/
protected $sort = false;
/**
* set sorting of values
*
* @param bool $sort Whether or not sort values by key
*/
public function setSort($sort)
{
$this->sort = $sort;
}
/**
* Recursively goes through an array and makes neat HTML out of it.
*
* @param mixed $values Array to make pretty.
* @param int $openDepth Depth to add open class
* @param int $currentDepth current depth.
* @param bool $doubleEncode Whether or not to double encode.
* @param \SplObjectStorage $currentAncestors Object references found down
* the path.
* @return string
*/
public function makeNeatArray($values, $openDepth = 0, $currentDepth = 0, $doubleEncode = false, \SplObjectStorage $currentAncestors = null)
{
if ($currentAncestors === null) {
$ancestors = new \SplObjectStorage();
} elseif (is_object($values)) {
$ancestors = new \SplObjectStorage();
$ancestors->addAll($currentAncestors);
$ancestors->attach($values);
} else {
$ancestors = $currentAncestors;
}
$className = "neat-array depth-$currentDepth";
if ($openDepth > $currentDepth) {
$className .= ' expanded';
}
$nextDepth = $currentDepth + 1;
$out = "<ul class=\"$className\">";
if (!is_array($values)) {
if (is_bool($values)) {
$values = [$values];
}
if ($values === null) {
$values = [null];
}
if (is_object($values) && method_exists($values, 'toArray')) {
$values = $values->toArray();
}
}
if (empty($values)) {
$values[] = '(empty)';
}
if ($this->sort && is_array($values) && $currentDepth === 0) {
ksort($values);
}
foreach ($values as $key => $value) {
$out .= '<li><strong>' . h($key, $doubleEncode) . '</strong>';
if (is_array($value) && count($value) > 0) {
$out .= '(array)';
} elseif (is_object($value)) {
$out .= '(object)';
}
if ($value === null) {
$value = '(null)';
}
if ($value === false) {
$value = '(false)';
}
if ($value === true) {
$value = '(true)';
}
if (empty($value) && $value != 0) {
$value = '(empty)';
}
if ($value instanceof Closure) {
$value = 'function';
}
$isObject = is_object($value);
if ($isObject && $ancestors->contains($value)) {
$isObject = false;
$value = ' - recursion';
}
if ((
$value instanceof ArrayAccess ||
$value instanceof Iterator ||
is_array($value) ||
$isObject
) && !empty($value)
) {
$out .= $this->makeNeatArray($value, $openDepth, $nextDepth, $doubleEncode, $ancestors);
} else {
$out .= h($value, $doubleEncode);
}
$out .= '</li>';
}
$out .= '</ul>';
return $out;
}
}