You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**TL;DR:** Run the following command to generate Typescript (or Javascript) enums from your PHP enums:
25
41
@@ -59,7 +75,7 @@ This package also supports generating Javascript enums. To do so, simply pass th
59
75
php artisan paragon:enum:generate --javascript
60
76
```
61
77
62
-
### Public Methods
78
+
####Public Methods
63
79
64
80
A good majority of the time it is useful to use public methods to return a proper human-readable label or some other functionality on an enum. Paragon got this covered too. Assuming the following method exists on the above `Status` enum:
While this package ignores static methods on the PHP Enums, we allow you to create additional methods that Paragon will make available for every generated Enum.
96
112
@@ -127,12 +143,12 @@ use Kirschbaum\Paragon\Concerns\IgnoreParagon;
127
143
128
144
enum Status
129
145
{
130
-
...
146
+
// ...
131
147
132
148
#[IgnoreParagon]
133
149
public method ignoreMe()
134
150
{
135
-
...
151
+
// ...
136
152
}
137
153
}
138
154
```
@@ -145,7 +161,7 @@ You can publish the configuration file by running `php artisan vendor:publish` a
145
161
146
162
It is recommended that the generated path for the enums is added to the `.gitignore` file. Make sure to run this command during deployment if you do this.
147
163
148
-
## Automatically Re-generating When Modifying PHP Enums
164
+
###Automatically Re-generating When Modifying PHP Enums
149
165
150
166
Install the [`vite-plugin-watch`](https://www.npmjs.com/package/vite-plugin-watch) plugin in your project via `npm`:
151
167
@@ -170,7 +186,7 @@ export default defineConfig({
170
186
});
171
187
```
172
188
173
-
## Technical Details 🤓
189
+
###Technical Details 🤓
174
190
175
191
Enums are a fantastic addition to the PHP-verse but are really lame in the TypeScript-verse. However, it can be annoying trying to get those enum values on the
176
192
front-end of your project. Are you supposed to pass them as a method when returning a view or perhaps via an API? This
@@ -223,4 +239,60 @@ export default Status;
223
239
224
240
At first glance it appears as though a lot more stuff is happening, but the above generated code allows us to interact
225
241
with the enum in a nearly identical way as in PHP. And you may notice the generated TypeScript class extends the `Enum`
226
-
class. This gives us some underlying functionality that is available to every enum.
242
+
class. This gives us some underlying functionality that is available to every enum.
243
+
244
+
## Broadcast Events
245
+
246
+
**TL;DR:** Run the following command to generate a Typescript (or Javascript) broadcast event object based on your broadcastable events:
247
+
248
+
```bash
249
+
php artisan paragon:event:generate
250
+
```
251
+
252
+
Any events within the `App\Events` namespace will be automatically added to the `Events` object. Just make sure your event implements the `ShouldBroadcast` contract!
253
+
254
+
If javascript is needed, just make sure to add the `--javascript` or `-j` flag.
255
+
256
+
### Usage
257
+
258
+
The primary usage of this tool is for Laravel Echo. Listening to for an event class based on a hard-coded string is not exactly preferable. The can easily get out of sync or just get mistyped.
259
+
260
+
Here is a quick example event:
261
+
262
+
```php
263
+
namespace App\Events;
264
+
265
+
class ParagonGenerated implements ShouldBroadcast
266
+
{
267
+
// ...
268
+
}
269
+
```
270
+
271
+
Then listen for it with Echo:
272
+
273
+
```js
274
+
importEventsfrom'@/events/Events.ts';
275
+
276
+
Echo.channel(...)
277
+
.listen(Events.ParagonGenerated, /* ... */);
278
+
```
279
+
280
+
If you have defined a `broadcastOn` method that returns a custom name, this will be handled correctly for you as well.
281
+
282
+
One thing to note is that if an event namespace starts with `App\Events` or `App`, these will be removed from the dot path within the object. If an event is nested, the dot path will reflect this: For example:
0 commit comments