forked from Mikulas/nette-plugins-navigationpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NavigationPanel.php
184 lines (157 loc) · 5.41 KB
/
NavigationPanel.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* NavigationPanel for Nette 2.0. Fast navigation for developers.
*
* @author Mikuláš Dítě
* @license MIT
*/
namespace Panel;
use Nette\Object;
use Nette\Diagnostics\IBarPanel;
use Nette\Diagnostics\Debugger;
use Nette\Templating\FileTemplate;
use Nette\Utils\Finder;
use Nette\Utils\Strings as String;
use Nette\Utils\SafeStream;
use Nette\Latte\Engine;
use Nette\Application\UI\Presenter;
class Navigation extends Object implements IBarPanel
{
/** @var Presenter */
private $presenter;
public function __construct(Presenter $presenter)
{
$this->presenter = $presenter;
//Debugger::addPanel($this);
}
/**
* Renders HTML code for custom tab
* IDebugPanel
* @return void
*/
public function getTab()
{
return '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAaBJREFUeNqkUz1rAkEQnT0XK8FYCpLCNFYWISgi/oRA2vSSgAhWJgQsDEkvlv6EXFqrNGIhSEAQKwsbueLAwsIPRE+97Jtwx5moETIwDDM7++bN7Kwol8vXRBSmE2S73e74tm2bVCqV7u0Tpd1u7/jFYvFO22w2QKKr9wuu0Gq1qNPpuAofceRYlsW2VquxnU6nJFVQIOHt8oMTk8nkXupCCFqtVnwxm82ynUwmJNfrNQMMBgOKRCIH+9c0jWazGdXrdTdmGAZpoOVQbDQaNBwO6fPpgdAa/G63S/1+n3OWyyWFQiFXx+MxSUWLGUAzmQwjh59fGSCdTv96AcS9wjNA0NFDghnsA9C8DJrNJvf1qJ9xIvxer8fqZeAtJlVfLoBD+eVmRDhPpVJ/t+AwwBCPteAAqFfbBQADXAwEArw00WiUdF2nfD5PavN42pB4PM4WLJHv8/m+AebzuUD1WCzmouZyOa6YSCS8e88xMCgUClStVgEkpGmaEkvi9/uPfiTkLBYLtpVKha3aRCmCweCtQj4/8TdaP0ANpv8f+RJgAMs5a/v6pdj7AAAAAElFTkSuQmCC">' .
'Navigation';
}
/**
* Renders HTML code for custom panel
* IDebugPanel
* @return void
*/
function getPanel()
{
ob_start();
$template = new FileTemplate(dirname(__FILE__) . '/bar.navigation.panel.latte');
$template->registerFilter(new Engine());
$template->tree = $this->getPresenters();
$template->render();
return $cache['output'] = ob_get_clean();
}
/**
* Returns panel ID
* IDebugPanel
* @return string
*/
function getId()
{
return __CLASS__;
}
/**
* Registers panel to Debug bar
*/
static function register(Presenter $presenter)
{
Debugger::addPanel(new self($presenter));
}
/**
* @return array
*/
private function getPresenters()
{
@SafeStream::register(); //intentionally @ (prevents multiple registration warning)
$tree = array();
foreach (Finder::findFiles('*Presenter.php')->from($this->presenter->context->parameters['appDir']) as $path => $file) {
$data = $this->processPresenter($file);
if ($data === FALSE) {
continue;
}
list($module, $presenter, $actions) = $data;
$tree[$module][$presenter] = $actions;
}
foreach (Finder::findFiles('*.latte', '*.phtml')->from($this->presenter->context->parameters['appDir']) as $path => $file) {
$data = $this->processTemplate($file);
if ($data === FALSE) {
continue;
}
list($module, $presenter, $action) = $data;
if (!isset($tree[$module][$presenter])) {
$tree[$module][$presenter] = array();
}
if (array_search($action, $tree[$module][$presenter]) === FALSE) {
$tree[$module][$presenter][] = $action;
}
}
$tree = $this->removeSystemPresenters($tree);
return $tree;
}
/**
* @param array $array
* @return int
*/
public static function getRowspan(array $array)
{
$size = 0;
foreach ($array as $content) {
$size += count($content);
}
return $size;
}
/**
* Removes presenters such as Error etc.
* @param array $tree
*/
public function removeSystemPresenters($tree)
{
unset($tree[NULL]['Error']);
return $tree;
}
/**
* @param \SplFileInfo $file
*/
private function processPresenter($file)
{
$stream = fopen("safe://" . $file->getRealPath(), 'r');
$content = fread($stream, filesize("safe://" . $file->getRealPath()));
fclose($stream);
$module = String::match($content, '~(^|;)\s*namespace (?P<name>[A-z0-9_-]+)Module;~m');
$module = $module['name'];
$presenter = String::match($content, '~(^|;)\s*class (?P<name>[A-z0-9_-]+)Presenter(\s|$)~m');
if ($presenter === NULL || $presenter['name'] === 'Error') {
return FALSE;
}
$presenter = $presenter['name'];
$actions = array();
foreach (String::matchAll($content, '~function (action|render)(?P<name>[A-z0-9_-]+)(\s|\\()~') as $action) {
$action = lcfirst($action['name']);
if (array_search($action, $actions) === FALSE) {
$actions[] = $action;
}
}
return array($module, $presenter, $actions);
}
/**
* @param \SplFileInfo $file
*/
public function processTemplate($file)
{
$match = String::match($file->getRealPath(), '~(?:(?P<module>[A-z0-9_-]+)Module)?/templates/(?P<presenter>[A-z0-9_-]+)/(?P<action>[A-z0-9_-]+)\.(latte|phtml)$~m');
if (!$match) {
return FALSE;
}
$module = $match['module'];
$presenter = $match['presenter'];
$action = $match['action'];
return array($module, $presenter, $action);
}
}