Laravel OOP Config is a package that enables developers to generate type-safe, object-oriented wrappers for their Laravel configuration files. This approach provides code completion, type safety, and improved maintainability when working with configuration values.
- 🔒 Type-safe config access: Generate PHP classes and methods for config files.
- ⚡ Batch config class generation: Generate classes for all config files at once.
- 🛠 Customizable stubs: Publish and modify the code templates for generated classes and methods.
- 🧩 Automatic service provider integration: Seamlessly integrates with Laravel’s service container.
- 📝 IDE autocompletion: Enables modern IDEs to provide autocompletion for config keys.
- 🧪 Tested & reliable: Includes feature and unit tests for stable operation.
Install via Composer:
composer require veiliglanceren/laravel-oop-config --dev
Publish the configuration file (optional):
php artisan vendor:publish --tag=oop-config
After publishing, you can customize the config in config/oop-config.php
:
- namespace: Namespace for generated config classes (default:
App\Config
). - path: Directory path where generated config classes are stored (default:
app/Config
). - autoload: Whether to automatically register generated classes (default:
true
). - stubs: Paths to custom stubs for class and method generation.
To generate a class for a specific config file (e.g., config/mail.php
):
php artisan make:config mail
This creates App\Config\MailConfig
, with methods corresponding to the keys in mail.php
.
php artisan config:generate-all
Add --force
to overwrite existing classes:
php artisan config:generate-all --force
Use the generated class to access config values with autocompletion and type-safety:
use App\Config\MailConfig;
$mailConfig = app(MailConfig::class);
$host = $mailConfig->host();
$fromAddress = $mailConfig->fromAddress();
For nested config arrays, methods are generated using camelCase:
$fromAddress = $mailConfig->fromAddress();
You can customize the generated code by publishing the default stubs:
php artisan vendor:publish --tag=stubs
Edit the stub files in the published stubs
directory, then update the stubs
paths in config/oop-config.php
if needed.
This package uses Pest. To run the tests:
./vendor/bin/pest
Ensure your test environment is configured according to Laravel’s testing guidelines.
- Config is not an array: Ensure that your config files return arrays.
- Missing or unreadable stub file: Check the
stubs
path in your config and ensure files exist and are readable. - Classes not autoloaded: Make sure
autoload
is enabled inoop-config.php
, and clear the config cache if needed.
Contributions are welcome! Please:
- Fork the repository.
- Create a feature branch.
- Write tests for your changes.
- Submit a pull request.
For suggestions, bug reports, or feature requests, open an issue.
This package is open-sourced software licensed under the MIT license.
Made with ❤️ by VeiligLanceren.nl, credits for the idea to Oussama Mater on LinkedIn