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
TypeScript Enums
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!
The simplest way to get started is to run the following command:
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!
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.
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. We've got this covered too. Assuming the following method exists on the above Status
enum:
The following method would become accessible using TypeScript:
Custom Static Methods
Traits can also be applied to enums that give the extra functionality via static methods. While this package ignores static methods as it would be pretty difficult to convert PHP code into TypeScript automatically, we allow you to create files that Paragon will auto-import for you so you can add the same type of functionality on the front-end! Simply run
You can either let it prompt you for a name or pass it in via the CLI name
argument. This will create a new file at
resources/js/vendors/paragon/enums
. 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.
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:
All versions of paragon with dependencies
illuminate/filesystem Version >=10.0
illuminate/support Version >=10.0
symfony/filesystem Version >=6.4