All Projects → sebastiaanluca → laravel-conditional-providers

sebastiaanluca / laravel-conditional-providers

Licence: MIT license
THIS PACKAGE HAS BEEN DEPRECATED — Load Laravel service providers and facades based on the current environment.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-conditional-providers

Php Ide Serenata
Atom IDE package that integrates the Serenata server to provide PHP code assistance
Stars: ✭ 277 (+965.38%)
Mutual labels:  package, service
bandmaster
Simple and easily extendable Go library for managing runtime services & dependencies (datastores, APIs, MQs, custom...).
Stars: ✭ 43 (+65.38%)
Mutual labels:  environment, service
Nupdate
A comfortable update solution for .NET-applications.
Stars: ✭ 394 (+1415.38%)
Mutual labels:  package, service
Environ Config
Python Application Configuration With Environment Variables
Stars: ✭ 210 (+707.69%)
Mutual labels:  environment, configuration
CoSky
High-performance, low-cost microservice governance platform. Service Discovery and Configuration Service | 高性能、低成本微服务治理平台
Stars: ✭ 57 (+119.23%)
Mutual labels:  service, configuration
Config
12 factor configuration as a typesafe struct in as little as two function calls
Stars: ✭ 251 (+865.38%)
Mutual labels:  environment, configuration
Setup Miniconda
Set up your GitHub Actions workflow with conda via miniconda
Stars: ✭ 222 (+753.85%)
Mutual labels:  package, environment
Conf
Go package for loading program configuration from multiple sources.
Stars: ✭ 70 (+169.23%)
Mutual labels:  environment, configuration
cfg-rs
A Configuration Library for Rust Applications
Stars: ✭ 18 (-30.77%)
Mutual labels:  environment, configuration
dart environment config
Environment specific config generator for Dart and Flutter applications during CI/CD builds
Stars: ✭ 87 (+234.62%)
Mutual labels:  environment, configuration
PushMeBaby
iOS Push Notification Debug App. You can use this app during iOS Push Notification (development or production) to push notifications on your device from your Mac.
Stars: ✭ 47 (+80.77%)
Mutual labels:  service, debug
parse it
A python library for parsing multiple types of config files, envvars & command line arguments that takes the headache out of setting app configurations.
Stars: ✭ 86 (+230.77%)
Mutual labels:  package, configuration
Fig
A minimalist Go configuration library
Stars: ✭ 142 (+446.15%)
Mutual labels:  environment, configuration
Confex
Useful helper to read and use application configuration from environment variables.
Stars: ✭ 272 (+946.15%)
Mutual labels:  package, configuration
Next Runtime Dotenv
Expose environment variables to the runtime config of Next.js
Stars: ✭ 136 (+423.08%)
Mutual labels:  environment, configuration
Env Providers
👷 Load Laravel service providers based on your application's environment.
Stars: ✭ 73 (+180.77%)
Mutual labels:  package, environment
Erl Env
Make retrieving configuration parameters super fast(7x faster than application:get_env)and stable.
Stars: ✭ 32 (+23.08%)
Mutual labels:  environment, configuration
Common Env
🔑 The only configuration library you will ever need
Stars: ✭ 67 (+157.69%)
Mutual labels:  environment, configuration
live-platform
Add breakpoints, logs, metrics, and spans to live production applications
Stars: ✭ 37 (+42.31%)
Mutual labels:  production, debug
salak.rs
A multi layered configuration loader and zero-boilerplate configuration parser.
Stars: ✭ 27 (+3.85%)
Mutual labels:  environment, configuration

Laravel Conditional Providers

Latest stable release Software license Build status Total downloads Total stars

Read my blog View my other packages and projects Follow @sebastiaanluca on Twitter Share this package on Twitter

Load Laravel service providers and facades based on the current environment.

Specify the service providers and facades to load per environment directly in your configuration file. No more need to add lengthy blocks of conditionals to your AppServiceProvider, do it all in the app configuration file like you would with any service provider and facade!

Inspired by Matt Staufer, Sven Luijten, and others.

Table of contents

What does it solve?

Say you're using a package like barryvdh/laravel-ide-helper in your project. If you've followed its installation instructions and require it only in development environments (like you should), you'd do the following:

composer require barryvdh/laravel-ide-helper --dev

And then add the service provider to your app's config providers array:

'providers' => [

    Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,

]

Now when you run composer install --no-dev in your production environment to install all but development packages, this will throw an exception. Laravel will try to load the registered service provider class which it can't find, because the package is not installed.

[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider' not found

Of course you can —per the instructions— conditionally load the provider manually in the register method of the app/Providers/AppServiceProvider.php file:

public function register()
{
    if ($this->app->environment() !== 'production') {
        $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
    }
}

But you'd have to do this for each development package and each environment you don't want it loaded in, which is hardly maintainable and pollutes your application-specific code.

Enter Laravel conditional providers to easily do all of this in your main application config file!

Requirements

  • PHP 7.3 or higher
  • Laravel 6.0 or higher

How to install

composer require sebastiaanluca/laravel-conditional-providers

How to use

Conditional providers

Disable auto-discovery of the package's service provider by adding it to your composer.json's relevant section:

"extra": {
    "laravel": {
        "dont-discover": [
            "barryvdh/laravel-debugbar"
        ]
    }
},

Once you're set up, simply add a providers array per environment to your config/app.php file:

'providers' => [

    // Contains your global providers which will load in any environment

],

'local_providers' => [

    // Contains your 'local' environment providers

    // Mostly used to load debug helpers, optimization tools, et cetera

    Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
    Barryvdh\Debugbar\ServiceProvider::class,

],

'production_providers' => [

    // Contains your 'production' environment providers

    // Great for when you only want to get analytics or
    // bug reports in production and disable the provider
    // entirely when developing.

],

Each providers key is optional and can be empty —so you could just use the local_providers array or none at all.

The example above will do the following in a local environment:

  • Load every provider from providers
  • Load every provider from local_providers
  • Ignore everything in production_providers

All done! Now your app service provider is clean and you get a better view on what's loaded and when, with the added benefit of enabling or disabling packages based on environment.

Conditional aliases

In addition to conditionally loading providers, this workflow is also available for aliases/facades. Add a facades/aliases array per environment to your config/app.php file like so:

'aliases' => [

    // Contains your global aliases which will load in any environment

],

'local_aliases' => [

    // Contains your 'local' environment aliases/facades

    'Debugbar' => Barryvdh\Debugbar\Facade::class,

],

'production_aliases' => [

    // Contains your 'production' environment aliases/facades

],

That's it! This will load the Debugbar facade only in the local environment.

License

This package operates under the MIT License (MIT). Please see LICENSE for more information.

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

composer install
composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

About

My name is Sebastiaan and I'm a freelance Laravel developer specializing in building custom Laravel applications. Check out my portfolio for more information, my blog for the latest tips and tricks, and my other packages to kick-start your next project.

Have a project that could use some guidance? Send me an e-mail at [email protected]!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].