LaravelPackages.net
Acme Inc.
Toggle sidebar
crazybooot/laravel-constants-to-js

Map php class constants to js

749
5
1.0.2
About crazybooot/laravel-constants-to-js

crazybooot/laravel-constants-to-js is a Laravel package for map php class constants to js. It currently has 5 GitHub stars and 749 downloads on Packagist (latest version 1.0.2). Install it with composer require crazybooot/laravel-constants-to-js. Discover more Laravel packages by crazybooot or browse all Laravel packages to compare alternatives.

Last updated

Exports Laravel config and class constants to JavaScript

Includes artisan command to generate JavaScript ES6 or UMD module

Installation

You can install the package via composer:

composer require crazybooot/laravel-constants-to-js

Publish config:

php artisan vendor:publish --provider="Crazybooot\ConstantsToJs\Providers\ServiceProvider" --tag=config

Usage

Add JavaScript resulted object hierarchy to config\constants-to-js.php config file to constants section as an assoc array.

To include constants from specified class use class as key and class name as value:

return [
    'constants' => [
        'user' => [
            'type' => [
                'class' => App\User::class,
            ],
        ],
    ],
];

To inculde values from config file use config as key and string with path to config as value:

return [
    'constants' => [
        'queue' => [
            'connections' => [
                'config' => 'queue.connections',
            ],
        ],
    ],
];

Including class constants supports to filter them by constant name using starts_with option:

return [
    'constants' => [
        'user' => [
            'type'   => [
                'class'       => App\User::class,
                'starts_with' => 'TYPE_',
            ],
            'status' => [
                'class'       => App\User::class,
                'starts_with' => 'STATUS_',  
            ],
        ],
    ],
];

To make some transformation with kyes and values use transform_key or transform_value options array where each key is a function will be applied to key or value and its value is array of additional parameters which will be passed to function: as value

return [
    'constants' => [
        'user' => [
            'type'   => [
                'class'       => App\User::class,
                'transform_key' => [
                    'strtoupper' => [],
                ]
            ],
        ],
        'queue' => [
            'connections' => [
                'config' => 'queue.connections',
                'transform_value' => [
                    'array_get'  => ['driver'],
                    'title_case' => []
                ],
            ],
        ],
    ],
];

To change destination and generated JavaScript file name use target_path configuration option:

return [
    'target_path' => resource_path('assets/js/constants.js'),
];

After cofiguration run next command to generate resulted JavaScript file:

php artisan constants:js

License

The MIT License (MIT). Please see License File for more information.

Star History Chart