Download the PHP package kirschbaum-development/paragon without Composer
On this page you can find all versions of the php package kirschbaum-development/paragon. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kirschbaum-development/paragon
More information about kirschbaum-development/paragon
Files in kirschbaum-development/paragon
Package paragon
Short Description A Laravel package for generating enum-like objects in typescript based on PHP enum classes.
License MIT
Homepage https://github.com/kirschbaum-development/paragon
Informations about the package paragon
Paragon
A tool for automatically generating typescript/javascript objects and utilities based on their PHP counterparts.
Table of Contents
- Requirements
- Installation
- Enums
- Custom Enum Methods
- Ignoring Enums or Methods
- Configuration
- Recommendations
- Automatically Regenerate
- Technical Details
- Events
- Usage
Requirements
Laravel Version | Paragon Version |
---|---|
12.0 | ^1.0 |
11.0 | ^1.0 |
10.0 | ^1.0 |
Installation
Enums
TL;DR: Run the following command to generate Typescript (or Javascript) enums from your PHP enums:
That's it. Now, wherever you may have had enums in your project, "paragons" or near perfect duplicates of those have been recreated inside of resources/js/enums
. Here are some examples of the API:
As you can see the API is nearly the same, the only difference being how the two languages expect you to access objects!
Generating javascript enums
This package also supports generating Javascript enums. To do so, simply pass the --javascript
flag to the command:
Public Methods
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:
The following method would become accessible using TypeScript:
Custom Enum Methods
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.
You will be prompted to search for the enum this method should belong to. It will be placed inside a directory that matches the namespace of the enum. For example, an enum at App\Enums\Status
would place an enum method file at resources/js/vendors/paragon/enums/App/Enums/Status
. You are free to do whatever you need inside this file. You have direct access to this.items
which allows you to interact with the enum cases in whatever way you need. Just keep in mind that because the items are "frozen", you can't mutate them directly. An example would be to have a method that automatically generates a select list from your Enum.
If you need to create a method that is available to every enum, just create an enum method with the --global
or -g
flag.
This will create a new file at resources/js/vendors/paragon/enums
containing a method.
Ignoring Enums Or Public Methods
There may be enums or enum methods that you don't want inside your automatically generated code. If this is the case simply use the IgnoreParagon
attribute.
Configuration
You can publish the configuration file by running php artisan vendor:publish
and locating the Paragon config which will be published at config/paragon.php
.
Recommendations
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.
Automatically Re-generating When Modifying PHP Enums
Install the vite-plugin-watch
plugin in your project via npm
:
In your vite.config.js
file, import the plugin, and add the plugin paramaters to your plugins array:
Technical Details 🤓
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 front-end of your project. Are you supposed to pass them as a method when returning a view or perhaps via an API? This generator solves that problem by scraping your app directory for any and all enums and recreates them as TypeScript classes so you can import them directly into your Vue, React, or Svelte front-end!
Let's take a closer look at a simple PHP enum and its generated Typescript code.
At first glance it appears as though a lot more stuff is happening, but the above generated code allows us to interact
with the enum in a nearly identical way as in PHP. And you may notice the generated TypeScript class extends the Enum
class. This gives us some underlying functionality that is available to every enum.
Broadcast Events
TL;DR: Run the following command to generate a Typescript (or Javascript) broadcast event object based on your broadcastable events:
Any events within the App\Events
namespace will be automatically added to the Events
object. Just make sure your event implements the ShouldBroadcast
contract!
If javascript is needed, just make sure to add the --javascript
or -j
flag.
Usage
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.
Here is a quick example event:
Then listen for it with Echo:
If you have defined a broadcastOn
method that returns a custom name, this will be handled correctly for you as well.
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:
All versions of paragon with dependencies
illuminate/filesystem Version >=10.0
illuminate/support Version >=10.0
symfony/filesystem Version >=6.4