aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/VueApp.php
blob: 8e6136421fa89270d9b1047dbb3db489312d1154 (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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
namespace Studip;

use Flexi\Template;
use Stringable;

/**
 * PHP abstraction of vue app
 *
 * The VueApp is used to create a Vue app in a general way. Just create it
 * using the name of the case component and pass in any required props or
 * stores including initial data.
 *
 * The store data is passed as an associative array where the key is the name
 * of the mutation to call with the given value as data.
 *
 * All methods are written in fluid manner so that you can create the app like this:
 *
 * <code>
 *     <?= Studip\VueApp::create('ExampleComponent')
 *         ->withProps(['foo' => 'bar'])
 *         ->withStore('exampleStore', data: ['setBar' => 'baz']) ?>
 * </code>
 *
 * All with* methods will always create a new cloned instance so the original
 * instance is immutable.
 *
 * @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @since Stud.IP 6.0
 */
final class VueApp implements Stringable
{
    /**
     * Creates a vue app with the given base component.
     */
    public static function create(string $base_component): VueApp
    {
        return new self($base_component);
    }

    private array $plugins = [];
    private array $props = [];
    private array $slots = [];
    private array $stores = [];
    private array $storeData = [];
    private array $components = [];

    private array $vuexStores = [];
    private array $vuexStoreData = [];

    /**
     * Private constructor since we want to enforce the use of VueApp::create().
     */
    private function __construct(
        private readonly string $base_component
    ) {
        $this->components[] = $base_component;
    }

    /**
     * Returns the base component
     */
    public function getBaseComponent(): string
    {
        return $this->base_component;
    }

    /**
     * Add props
     *
     * You may choose to overwrite the defined props
     */
    public function withProps(array $props, bool $overwrite = false): VueApp
    {
        $clone = clone $this;
        $clone->props = [...$overwrite ? [] : $clone->props, ...$props];
        return $clone;
    }

    /**
     * Returns all props
     */
    public function getProps(): array
    {
        return $this->props;
    }

    /**
     * Set the content of a slot.
     */
    public function setSlot(string $name, string|Template $content): VueApp
    {
        $this->slots[$name] = $content instanceof Template ? $content->render() : $content;
        return $this;
    }

    /**
     * Add a slot with the given name
     *
     * If you pass a flexi template as the content, it will be rendered.
     */
    public function withSlot(string $name, string|Template $content): VueApp
    {
        $clone = clone $this;
        $clone->slots[$name] = $content instanceof Template ? $content->render() : $content;
        return $clone;
    }

    /**
     * Returns all slots
     */
    public function getSlots(): array
    {
        return $this->slots;
    }

    /**
     * Adds a store
     */
    public function withStore(string $store, ?string $command = null, ?array $data = null): VueApp
    {
        $clone = clone $this;

        if ($command === null) {
            $command = 'use' . strtopascalcase($store) . 'Store';
        }

        $clone->stores[$store] = $command;

        if ($data !== null) {
            $clone->storeData[$store] = $data;
        }

        return $clone;
    }

    /**
     * Returns all stores
     */
    public function getStores(): array
    {
        return $this->stores;
    }

    /**
     * Returns all store data
     */
    public function getStoreData(): array
    {
        return $this->storeData;
    }

    /**
     * Adds a vuex store
     */
    public function withVuexStore(string $store, ?string $index = null, ?array $data = null): VueApp
    {
        $clone = clone $this;

        $clone->vuexStores[$index ?? $store] = $store;

        if ($data !== null) {
            $clone->vuexStoreData[$index ?? $store] = $data;
        }

        return $clone;
    }

    /**
     * Returns all vuex stores
     */
    public function getVuexStores(): array
    {
        return $this->vuexStores;
    }

    /**
     * Returns all vuex store data
     */
    public function getVuexStoreData(): array
    {
        return $this->vuexStoreData;
    }

    /**
     * Adds a plugin
     *
     * You may specify a different filename for the plugin.
     */
    public function withPlugin(string $plugin, string $filename = null): VueApp
    {
        $clone = clone $this;
        $clone->plugins[$plugin] = $filename ?? $plugin;
        return $clone;
    }

    /**
     * Returns all plugins
     */
    public function getPlugins(): array
    {
        return $this->plugins;
    }

    /**
     * Registers a component for use e.g. in slots.
     */
    public function withComponent(string $component): VueApp
    {
        $clone = clone $this;
        $clone->components[] = $component;
        return $clone;
    }

    /**
     * Returns all components
     */
    public function getComponents(): array
    {
        return $this->components;
    }

    /**
     * Returns the template to render the vue app
     */
    public function getTemplate(): Template
    {
        $data = [
            'components' => $this->components,
        ];

        if (count($this->stores) > 0) {
            $data['stores'] = $this->stores;
        }

        if (count($this->vuexStores) > 0) {
            $data['vuexStores'] = $this->vuexStores;
        }

        if (count($this->plugins) > 0) {
            $data['plugins'] = $this->plugins;
        }

        $template = $GLOBALS['template_factory']->open('vue-app.php');
        $template->baseComponent = basename($this->base_component);
        $template->attributes = ['data-vue-app' => json_encode($data)];
        $template->props = $this->getPreparedProps();
        $template->storeData = $this->storeData;
        $template->vuexStoreData = $this->vuexStoreData;
        $template->slots = $this->getSlots();
        return $template;
    }

    /**
     * Returns the props as required to include them in the html
     */
    private function getPreparedProps(): array
    {
        $result = [];
        foreach ($this->props as $name => $value) {
            $name = ltrim($name, ':');
            $name = strtokebabcase($name);
            $result[":{$name}"] = json_encode($value);
        }
        return $result;
    }

    /**
     * Renders the vue app
     */
    public function render(): string
    {
        if (Debug\DebugBar::isActivated()) {
            $debugbar = app()->get(\DebugBar\DebugBar::class);
            $collector = new Debug\VueCollector($this);
            $debugbar->addCollector($collector);
        }

        \NotificationCenter::postNotification('VueAppWillRender', $this);

        $content = $this->getTemplate()->render();

        \NotificationCenter::postNotification('VueAppDidRender', $this);

        return $content;
    }

    /**
     * Returns a string representation of the vue app by rendering it.
     */
    public function __toString(): string
    {
        return $this->render();
    }
}