veiliglanceren / laravel-oop-config
Type hinting and autocomplete for config values
Requires
- php: ^8.1
- laravel/framework: ^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^8.0 || ^9.0 || ^10.0
- pestphp/pest: ^2.0 || ^3.0
- pestphp/pest-plugin-laravel: ^2.0 || ^3.0
This package is not auto-updated.
Last update: 2025-05-20 19:49:38 UTC
README
This package is maintained by VeiligLanceren.nl, your partner in website development and everything else to power up your online company.
Laravel OOP Config
Laravel OOP Config is a package that enables developers to generate object-oriented wrappers for Laravel configuration files. This approach enhances code readability, provides IDE autocompletion, and ensures type safety when accessing configuration values.
Features
- Generate PHP classes for Laravel config files.
- Access configuration values through typed methods.
- Batch generation of config classes.
- Seamless integration with Laravel's service container.
- Customizable stubs for generated classes.
Installation
You can install the package via Composer:
composer require veiliglanceren/laravel-oop-config --dev
To publish the configuration file:
php artisan vendor:publish --tag=config
Configuration
After publishing, the configuration file config/oop-config.php
will be available. You can customize the following settings:
namespace
: The namespace for the generated config classes (default:App\Config
).path
: The directory path where the config classes will be stored (default:app/Config
).autoload
: Whether to automatically register generated config classes (default:true
).stubs
: Paths to custom stubs for class and method generation.
Usage
Generating a Config Class
To generate a class for a specific config file:
php artisan make:config config_key
Replace config_key
with the name of the config file (without the .php
extension). For example, to generate a class for config/mail.php
:
php artisan make:config mail
This will create a class App\Config\MailConfig
with methods corresponding to the configuration keys.
Generating Classes for All Config Files
To generate classes for all config files:
php artisan config:generate-all
Use the --force
option to overwrite existing classes:
php artisan config:generate-all --force
Accessing Configuration Values
Once generated, you can access configuration values using the generated classes:
use App\Config\MailConfig; $mailConfig = app(MailConfig::class); $host = $mailConfig->host(); $port = $mailConfig->port();
For nested configuration values, methods are generated by concatenating the keys in camelCase:
$fromAddress = $mailConfig->fromAddress();
Customizing Stubs
You can customize the generated classes by modifying the stub files. Publish the stubs using:
php artisan vendor:publish --tag=stubs
This will publish the stub files to the stubs
directory, where you can modify them as needed.
Testing
To run the tests:
./vendor/bin/pest
Ensure that your test environment is properly configured.
Contributing
Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas or report bugs.
License
This package is open-sourced software licensed under the MIT license.