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-06-17 20:11:38 UTC
README
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.
Features
- đź”’ 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.
Installation
Install via Composer:
composer require veiliglanceren/laravel-oop-config --dev
Publish the configuration file (optional):
php artisan vendor:publish --tag=oop-config
Configuration
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.
Usage
Generate a Config Class
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
.
Generate Classes for All Config Files
php artisan config:generate-all
Add --force
to overwrite existing classes:
php artisan config:generate-all --force
Accessing Config Values
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();
Customizing Code Generation
Custom Stubs
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.
Testing
This package uses Pest. To run the tests:
./vendor/bin/pest
Ensure your test environment is configured according to Laravel’s testing guidelines.
Troubleshooting & FAQ
- 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.
Contributing
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.
License
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