aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/Debug/VueCollector.php
blob: a2d90f3ab540ca5de88dc5e1938ca7fccc15b310 (plain)
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
<?php
namespace Studip\Debug;

use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable;
use Studip\VueApp;

final class VueCollector extends DataCollector implements Renderable
{
    public function __construct(
        private readonly VueApp $app
    ) {
        $this->useHtmlVarDumper(false);
    }

    public function collect()
    {
        $data = [];

        $props = $this->app->getProps();
        if (count($props) > 0) {
            ksort($props);

            $data['== DATA =='] = count($props) . ' items';
            foreach ($props as $key => $value) {
                $data[$key] = $this->dumpVar($value);
            }
        }

        $stores = $this->app->getStores();
        if (count($stores) > 0) {
            ksort($stores);
            $storeData = $this->app->getStoreData();

            $data['== STORES =='] = '';

            foreach ($stores as $index => $store) {
                $data[$index] = $store === $index ? '' : "({$store})";

                $tmp = $storeData[$index] ?? [];
                ksort($tmp);
                foreach ($tmp as $key => $value) {
                    $data["- {$key}"] = $this->dumpVar($value);
                }
            }
        }

        return $data;
    }

    public function getName()
    {
        return '[Vue]' . basename($this->app->getBaseComponent());
    }

    /**
     * @return array
     */
    public function getAssets()
    {
        return $this->isHtmlVarDumperUsed() ? $this->getVarDumper()->getAssets() : [];
    }

    /**
     * @return array[]
     */
    public function getWidgets()
    {
        $name = $this->getName();
        $widget = $this->isHtmlVarDumperUsed()
            ? 'PhpDebugBar.Widgets.HtmlVariableListWidget'
            : 'PhpDebugBar.Widgets.VariableListWidget';


        return [
            $name => [
                'icon'   => 'code',
                'widget' => $widget,
                'map' => $name,
                'default' => '{}'
            ],
        ];
    }

    private function dumpVar(mixed $variable): string
    {
        if ($this->isHtmlVarDumperUsed()) {
            return $this->getVarDumper()->renderVar($variable);
        }

        if (!is_string($variable)) {
            return $this->getDataFormatter()->formatVar($variable);
        }

        return $variable;
    }
}