Laravel package to streamline config values with dependency injection

A Laravel package can be used to develop faster, or provide a better developer experience. One such package is the package I just released, which allows users to autowire config values through dependency injection.

Purpose of the package

The package allows developers to access their config values easily and conveniently, and the two strategies offer some flexibility.
Config keys can be guessed from the camelcase property name it is injected in or can be specified by using PHP which receives the config key.

Usage of the package

When using the property name strategy, a config value can be injected as follows:

class Foo implements AutowiresConfigs{
    public function __construct(
        public StringConfig $appName,
    ){}
}

And when using the attribute strategy, the developer can supply an attribute which specifies the config value to inject, like so:

class Foo implements AutowiresConfigs{
    public function __construct(
        #[StringConfig('app.name')]
        public StringConfig $appName,
    ){}
}

The full documentation for the package can be found here: https://github.com/MelchiorKokernoot/laravel-autowire-config/

Benefits of the package

This approach makes class dependencies more visible, as they are now declared explicitly in the constructor signature. Developers can easily see which classes and methods depend on which config values, making their code easier to understand and maintain. Before this package, it was common for developers to use the global config helper to access config values throughout their codebase. While this approach may have been convenient, it often led to scattered and hard-to-trace dependencies (as a config value is often a hard dependency). As a result, it could be difficult for developers to understand how different classes and methods relied on different config values.

Of course, you could achieve the same by creating custom service container bindings for you classes, using the following structure:

$this->app->when(ReportAggregator::class)
    ->needs('$timezone')
    ->giveConfig('app.timezone');

The approach of my package has my preference though, as service container bindings can become quite bloaty in large applications, then again, different service providers can be written for specific domains of the application, but even then, this can quickly bloat the structure of the project and improve the cognitive complexity, as the problem is just being relocated.

Conclusion

In summary, the melchiorkokernoot/laravel-autowire-config package is a useful tool for Laravel developers who want to make their applications more transparent and maintainable. Its two modes provide some flexibility, and it is open source, allowing for community contributions. However, it is important to note that this package may not be suitable for every Laravel project, and developers should evaluate its usefulness for their specific needs before implementing it. The package can be found on Github at https://github.com/MelchiorKokernoot/laravel-autowire-config/.

Be sure to leave the repository a star, and let me know what you think through either this comment section, or the discussions page on GitHub.

Kind regards,

Melchior