veiliglanceren/laravel-oop-config

There is no license information available for the latest version (0.0.1) of this package.

Type hinting and autocomplete for config values

0.0.1 2025-05-19 21:28 UTC

This package is not auto-updated.

Last update: 2025-06-17 20:11:38 UTC


README

MIT License

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 in oop-config.php, and clear the config cache if needed.

Contributing

Contributions are welcome! Please:

  1. Fork the repository.
  2. Create a feature branch.
  3. Write tests for your changes.
  4. 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