### Full Enumshare Configuration Example
Source: https://github.com/olivermbs/enumshare/blob/main/README.md
This PHP array shows a comprehensive configuration for Enumshare. It specifies which enums to export, the target directory for TypeScript files, the output mode, and settings for automatic discovery of enums within specified paths.
```php
// config/enumshare.php
return [
'enums' => [
App\Enums\Status::class,
],
'path' => resource_path('js/Enums'),
'mode' => 'full', // 'full' or 'minimal'
'auto_discovery' => true,
'auto_paths' => ['app/Enums'],
];
```
--------------------------------
### Install Enumshare Package with Composer
Source: https://github.com/olivermbs/enumshare/blob/main/README.md
This command installs the Enumshare package using Composer, the dependency manager for PHP. It adds the package to your project's dependencies, making its features available for use.
```bash
composer require olivermbs/enumshare
```
--------------------------------
### Enumshare Configuration File Setup
Source: https://context7.com/olivermbs/enumshare/llms.txt
The `config/enumshare.php` file allows customization of the enum export process. Key settings include explicitly listing enums, setting the output path, choosing the export mode ('full' or 'minimal'), defining default and additional locales, enabling auto-discovery, specifying paths for discovery, and setting a language namespace.
```php
[
App\Enums\UserStatus::class,
App\Enums\OrderStatus::class,
App\Enums\PaymentMethod::class,
],
// Output path for generated TypeScript files
'path' => resource_path('js/Enums'),
// Output mode: 'full' (with utilities) or 'minimal' (values only)
'mode' => 'full',
// Default locale for labels (null uses app()->getLocale())
'locale' => null,
// Additional locales for multilingual exports
'locales' => ['en', 'es', 'fr'],
// Enable automatic enum discovery in specified paths
'auto_discovery' => true,
// Paths to scan for enum files when auto-discovery is enabled
'auto_paths' => [
'app/Enums',
'app/Models/Enums',
'src/Domain/*/Enums',
],
// Language namespace for automatic label resolution
'lang_namespace' => 'enums',
];
```
--------------------------------
### Vue 3 Component Integration with TypeScript Enums
Source: https://context7.com/olivermbs/enumshare/llms.txt
Illustrates how to integrate generated TypeScript enums within Vue 3 components using the Composition API. This example showcases type-safe usage for displaying status badges and managing a select dropdown, utilizing computed properties and event emissions for reactive updates.
```typescript
{{ statusEntry?.label }}
{{ entry.label }}{{ entry.meta.icon }}
```
--------------------------------
### Enum Export Artisan Command Examples
Source: https://context7.com/olivermbs/enumshare/llms.txt
The `enums:export` Artisan command is the primary interface for generating TypeScript definitions from PHP enums. It offers various flags to control the export process, such as forcing regeneration, listing enums, creating an index file, exporting types, overriding the output path, and specifying locales.
```bash
php artisan enums:export
php artisan enums:export --force
php artisan enums:export --list
php artisan enums:export --index
php artisan enums:export --types
php artisan enums:export --path=resources/ts/enums
php artisan enums:export --locale=es
```
--------------------------------
### Auto-Regenerate TypeScript Enums with Vite and Wayfinder
Source: https://context7.com/olivermbs/enumshare/llms.txt
Automatically regenerate TypeScript enums during development using Laravel Wayfinder and Vite. This setup ensures that changes in PHP enums, translations, or configuration files trigger a command to update the TypeScript definitions, keeping them in sync.
```bash
# Install Wayfinder dependencies
composer require laravel/wayfinder
npm install @laravel/vite-plugin-wayfinder
```
```javascript
// vite.config.js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import { wayfinder } from '@laravel/vite-plugin-wayfinder';
export default defineConfig({
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.ts'],
refresh: true,
}),
wayfinder({
// Regenerate enums whenever PHP enums, translations, or config changes
command: 'php artisan enums:export --force',
patterns: [
'app/Enums/**/*.php',
'app/Models/Enums/**/*.php',
'lang/**/*.php',
'config/enumshare.php',
],
}),
],
});
// Now when you modify any enum file during development, TypeScript
// definitions are automatically regenerated
```
--------------------------------
### Run Project Tests
Source: https://github.com/olivermbs/enumshare/blob/main/README.md
This command executes the test suite for the Enumshare project using Composer. It helps ensure the package's functionality and stability by running automated tests.
```bash
composer test
```
--------------------------------
### PHP Multilingual Label Support for Enums
Source: https://context7.com/olivermbs/enumshare/llms.txt
Demonstrates setting up and using enums with translated labels for multiple locales in a PHP application. This includes configuration, language files, enum definition with translated labels, and accessing translated labels in TypeScript.
```php
// config/enumshare.php
return [
'locale' => 'en',
'locales' => ['en', 'es', 'fr'],
// ...
];
// lang/en/status.php
return [
'active' => 'Active',
'pending' => 'Pending',
'inactive' => 'Inactive',
];
// lang/es/status.php
return [
'active' => 'Activo',
'pending' => 'Pendiente',
'inactive' => 'Inactivo',
];
// App/Enums/Status.php
namespace App\Enums;
use Olivermbs\Enumshare\Attributes\TranslatedLabel;
enum Status: string
{
#[TranslatedLabel('status.active')]
case Active = 'active';
#[TranslatedLabel('status.pending')]
case Pending = 'pending';
#[TranslatedLabel('status.inactive')]
case Inactive = 'inactive';
}
```
```typescript
// Generated TypeScript with multilingual support
import { Status } from '@/Enums/Status';
// Labels are objects with locale keys
console.log(Status.Active.label);
// { en: 'Active', es: 'Activo', fr: 'Actif' }
// Get labels for specific locale
const spanishLabels = Status.labels('es');
console.log(spanishLabels); // ['Activo', 'Pendiente', 'Inactivo']
// Options with resolved labels
const options = Status.options; // Uses default locale
const spanishOptions = Status.options.map(opt => ({
value: opt.value,
label: resolveLabel(opt.label, 'es')
}));
```
--------------------------------
### Publish Enumshare Configuration
Source: https://github.com/olivermbs/enumshare/blob/main/README.md
This command publishes the Enumshare configuration file to your Laravel project. This allows you to customize settings such as the enums to export, the output path, the output mode, and auto-discovery preferences.
```bash
php artisan vendor:publish --tag="enumshare-config"
```
--------------------------------
### Enumshare Artisan Command Options
Source: https://github.com/olivermbs/enumshare/blob/main/README.md
This list details various options available for the 'enums:export' Artisan command. These options allow for controlling the export process, such as forcing overwrites, listing exportable enums, generating index files, specifying output paths, and overriding locales.
```bash
php artisan enums:export # Export enums
php artisan enums:export --force # Rewrite all, even if unchanged
php artisan enums:export --list # List enums that would be exported
php artisan enums:export --index # Generate barrel index file
php artisan enums:export --types # Export TypeScript helper types
php artisan enums:export --path=... # Override export path
php artisan enums:export --locale=... # Override locale for labels
```
--------------------------------
### Test EnumRuntime with Test Data (JavaScript)
Source: https://github.com/olivermbs/enumshare/blob/main/tests/browser-test.html
This snippet defines test data for a FlightBriefingStatus enum and a simplified `createTestEnum` function to simulate the EnumRuntime. It then tests the enum's properties and methods like `from` and `fromValue`, logging results to the console and updating the DOM.
```javascript
// Test data matching FlightBriefingStatus
const testEnumData = { "name": "TestEnum", "fqcn": "App\\Enums\\TestEnum", "backingType": "int", "entries": [ { "key": "DRAFT", "value": 0, "label": "Draft", "meta": { "color": "orange" } }, { "key": "SAVED", "value": 1, "label": "Saved", "meta": { "color": "blue" } } ], "options": [ { "value": 0, "label": "Draft" }, { "value": 1, "label": "Saved" } ] };
// Import and test the actual EnumRuntime if available
try {
// This would normally import from your actual EnumRuntime.ts
// For now, we'll use a simplified version
function createTestEnum(enumData) {
const entriesMap = {};
for (const entry of enumData.entries) {
entriesMap[entry.key] = entry;
}
const baseObject = {
name: enumData.name,
entries: enumData.entries,
options: enumData.options,
keys: () => enumData.entries.map(entry => entry.key),
values: () => enumData.entries.map(entry => entry.value ?? entry.key),
labels: () => enumData.entries.map(entry => entry.label),
};
const findEntryByValue = (value) => {
const entry = enumData.entries.find(entry => (entry.value !== null ? entry.value : entry.key) === value );
return entry || null;
};
baseObject.from = findEntryByValue;
baseObject.fromValue = findEntryByValue;
return new Proxy(baseObject, {
get(target, prop) {
if (typeof prop === 'string' && entriesMap[prop]) {
return entriesMap[prop];
}
return target[prop];
}
});
}
const testEnum = createTestEnum(testEnumData);
console.log('=== Browser Test Results ===');
console.log('Enum:', testEnum);
console.log('Has from method:', typeof testEnum.from === 'function');
console.log('Has fromValue method:', typeof testEnum.fromValue === 'function');
console.log('from(1):', testEnum.from(1));
console.log('fromValue(1):', testEnum.fromValue(1));
console.log('Object.getOwnPropertyNames:', Object.getOwnPropertyNames(testEnum));
console.log('hasOwnProperty("fromValue"):', testEnum.hasOwnProperty('fromValue'));
// Test the specific use case from Vue
const testStatus = 1;
console.log('Vue use case test - fromValue(' + testStatus + ')?.label:', testEnum.fromValue(testStatus)?.label);
document.getElementById('results').innerHTML = `
Test Results
from method exists: ${typeof testEnum.from === 'function'}