1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\LaravelTracy; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Recca0120\LaravelTracy\Contracts\IAjaxPanel; |
10
|
|
|
use Tracy\Bar; |
11
|
|
|
use Tracy\Debugger; |
12
|
|
|
use Tracy\IBarPanel; |
13
|
|
|
|
14
|
|
|
class BarManager |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* $panels. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $panels = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* $bar. |
25
|
|
|
* |
26
|
|
|
* @var \Tracy\Bar |
27
|
|
|
*/ |
28
|
|
|
protected $bar; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* $request. |
32
|
|
|
* |
33
|
|
|
* @var \Illuminate\Http\Request |
34
|
|
|
*/ |
35
|
|
|
protected $request; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* $app. |
39
|
|
|
* |
40
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
41
|
|
|
*/ |
42
|
|
|
protected $app; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* __construct. |
46
|
|
|
* |
47
|
|
|
* @param \Tracy\Bar $bar |
48
|
|
|
* @param \Illuminate\Http\Request $request |
49
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
50
|
|
|
*/ |
51
|
3 |
|
public function __construct(Bar $bar = null, Request $request = null, Application $app = null) |
52
|
|
|
{ |
53
|
3 |
|
$this->bar = $bar ?: Debugger::getBar(); |
54
|
3 |
|
$this->request = $request ?: Request::capture(); |
55
|
3 |
|
$this->app = $app; |
56
|
3 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* getBar. |
60
|
|
|
* |
61
|
|
|
* @return \Tracy\Bar |
62
|
|
|
*/ |
63
|
3 |
|
public function getBar() |
64
|
|
|
{ |
65
|
3 |
|
return $this->bar; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* loadPanels. |
70
|
|
|
* |
71
|
|
|
* @param array $panels |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
3 |
|
public function loadPanels($panels = []) |
75
|
|
|
{ |
76
|
3 |
|
if (isset($panels['user']) === true) { |
77
|
1 |
|
$panels['auth'] = $panels['user']; |
78
|
1 |
|
unset($panels['user']); |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
$ajax = $this->request->ajax(); |
82
|
|
|
|
83
|
3 |
|
foreach ($panels as $id => $enabled) { |
84
|
3 |
|
if ($enabled === false) { |
85
|
1 |
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
if ($ajax === true && $this->isAjaxPanel($id) === false) { |
89
|
1 |
|
continue; |
90
|
|
|
} |
91
|
3 |
|
$panel = static::make($id); |
92
|
3 |
|
$this->set($panel, $id); |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* make. |
100
|
|
|
* |
101
|
|
|
* @param string $id |
102
|
|
|
* @return \Tracy\IBarPanel |
103
|
|
|
*/ |
104
|
3 |
|
public static function make($id) |
105
|
|
|
{ |
106
|
3 |
|
$className = static::name($id); |
107
|
3 |
|
$panel = new $className(new Template()); |
108
|
|
|
|
109
|
3 |
|
return $panel; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* set. |
114
|
|
|
* |
115
|
|
|
* @param \Tracy\IBarPanel $panel |
116
|
|
|
* @param string $id |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
3 |
|
public function set(IBarPanel $panel, $id) |
120
|
|
|
{ |
121
|
3 |
|
$panel->setLaravel($this->app); |
122
|
3 |
|
$this->panels[$id] = $panel; |
123
|
3 |
|
$this->bar->addPanel($panel, $id); |
124
|
|
|
|
125
|
3 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* get. |
130
|
|
|
* |
131
|
|
|
* @param string $id |
132
|
|
|
* @return \Tracy\IBarPanel |
133
|
|
|
*/ |
134
|
1 |
|
public function get($id) |
135
|
|
|
{ |
136
|
1 |
|
return Arr::get($this->panels, $id); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* isAjaxPanel. |
141
|
|
|
* |
142
|
|
|
* @param string $id |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
1 |
|
protected function isAjaxPanel($id) |
146
|
|
|
{ |
147
|
1 |
|
return is_subclass_of(static::name($id), IAjaxPanel::class) === true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* name. |
152
|
|
|
* |
153
|
|
|
* @param string $id |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
3 |
|
protected static function name($id) |
157
|
|
|
{ |
158
|
3 |
|
return '\\'.__NAMESPACE__.'\Panels\\'.Str::studly($id).'Panel'; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|