aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/Debug/VueCollector.php
blob: 0dd479b447146313be192ee11cfc0d5a1c5ea73a (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?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);
                }
            }
        }

        $slots = $this->app->getSlots();
        if (count($slots) > 0) {
            ksort($slots);

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

        $components = $this->app->getComponents();
        ksort($components);
        $data['== COMPONENTS =='] = count($components) . ' items';
        foreach ($components as $value) {
            $data[$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;
    }
}