aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/VueApp.php
blob: db65abc539c8d94249899c92fc2733666384547d (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
<?php
namespace Studip;

use Flexi\Factory;
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 relative path of the app 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('ExampleApp')
 *         ->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 relative path to the app component.
     */
    public static function create(string $appPath): VueApp
    {
        return new self($appPath);
    }

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

    /**
     * Private constructor since we want to enforce the use of VueApp::create().
     */
    private function __construct(
        private readonly string $appPath
    ) {
    }

    /**
     * Returns the relative path to the app component.
     */
    public function getAppPath(): string
    {
        return $this->appPath;
    }

    /**
     * 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;
    }

    /**
     * Add slots
     *
     * You may choose to overwrite the defined slots
     */
    public function withSlots(array $slots, bool $overwrite = false): VueApp
    {
        $clone = clone $this;
        $clone->slots = [...$overwrite ? [] : $clone->slots, ...$slots];
        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;
    }

    /**
     * Returns the template to render the vue app
     */
    public function getTemplate(): Template
    {
        $template = app(Factory::class)->open('vue-app.php');
        $template->set_attributes(['app' => $this]);
        return $template;
    }

    /**
     * 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();
    }
}