1 | <?php |
||
2 | |||
3 | namespace Imanghafoori\Widgets\Utils; |
||
4 | |||
5 | use Illuminate\Support\Str; |
||
6 | |||
7 | class DebugInfo |
||
8 | { |
||
9 | private $widget; |
||
10 | |||
11 | private $html; |
||
12 | |||
13 | private $policies; |
||
14 | |||
15 | 10 | public function __construct() |
|
16 | { |
||
17 | 10 | $this->policies = app(Policies::class); |
|
18 | 10 | } |
|
19 | |||
20 | /** |
||
21 | * @param object $widget |
||
22 | * @param string $html |
||
23 | * @return string |
||
24 | */ |
||
25 | 10 | public function addIdentifierToHtml($widget, string $html) |
|
26 | { |
||
27 | 10 | $this->widget = $widget; |
|
28 | 10 | $this->html = $html; |
|
29 | 10 | $this->addDebugInfo(); |
|
30 | 10 | $this->addHtmlComments(); |
|
31 | |||
32 | 10 | return $this->html; |
|
33 | } |
||
34 | |||
35 | /** |
||
36 | * Adds debug info to html as HTML title tags. |
||
37 | */ |
||
38 | 10 | private function addDebugInfo(): void |
|
39 | { |
||
40 | 10 | $tpl = $this->getTplPath($this->widget->template); |
|
41 | |||
42 | 10 | $this->html = "<span title='WidgetObj : ".get_class($this->widget).".php
Template : {$tpl}{$this->cacheState()}'>{$this->html}</span>"; |
|
43 | 10 | } |
|
44 | |||
45 | /** |
||
46 | * Generates a string of current cache configurations. |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | 10 | private function cacheState() |
|
51 | { |
||
52 | 10 | if (! $this->policies->widgetShouldUseCache()) { |
|
53 | 10 | return ' 
 Cache: is globally turned off (You should put "enable_cache" => true in config\widgetize.php) '; |
|
54 | } |
||
55 | $l = $this->widget->cacheLifeTime->i ?? 0; |
||
56 | |||
57 | return " 
Cache : {$l} (min) "; |
||
58 | } |
||
59 | |||
60 | 10 | private function addHtmlComments(): void |
|
61 | { |
||
62 | 10 | $this->html = "<!-- '{".get_class($this->widget)."' Widget Start -->".$this->html."<!-- '".get_class($this->widget)."' Widget End -->"; |
|
63 | 10 | } |
|
64 | |||
65 | /** |
||
66 | * @param string $tpl |
||
67 | * @return string |
||
68 | */ |
||
69 | 10 | private function getTplPath(string $tpl) |
|
70 | { |
||
71 | 10 | if (Str::contains($tpl, 'Widgets::')) { |
|
72 | 1 | $tpl = str_replace('Widgets::', app()->getNamespace().'Widgets\\', $tpl); |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
73 | } |
||
74 | |||
75 | 10 | return str_replace('.', '\\', $tpl).'.blade.php'; |
|
76 | } |
||
77 | } |
||
78 |