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.
Requirements
Laravel Version | Paragon Version |
---|---|
11.0 | 1.0.x |
10.0 | 1.0.x |
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:
Additional 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.
This will create a new file at resources/js/vendors/paragon/enums
containing a method. 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.
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.
All versions of paragon with dependencies
illuminate/filesystem Version >=10.0
illuminate/support Version >=10.0
symfony/filesystem Version >=6.4