Code Monkey home page Code Monkey logo

env-providers's Introduction

env-providers

Laravel EnvProviders

Latest Version on Packagist Total Downloads Software License Build Status StyleCI

A more finetuned way of managing your service providers in Laravel. This package allows you to configure the environment certain service providers and aliases are loaded in.

Installation

Via composer:

$ composer require sven/env-providers

Or add the package to your dependencies in composer.json and run composer update to download the package:

{
    "require": {
        "sven/env-providers": "^4.0"
    }
}

Next, add the ServiceProvider to your providers array in config/app.php:

// config/app.php
'providers' => [
    ...
    Sven\EnvProviders\ServiceProvider::class,
];

Usage

You must publish this package's configuration file for it to work properly. To do so, run the following command:

$ php artisan vendor:publish --provider="Sven\EnvProviders\ServiceProvider"

After that, you should see the file config/providers.php. In the created configuration file you can see 2 pre-defined provider groups that will help you set up what providers and aliases should be loaded when the application is in any of the configured environments.

Environments

In the environments array you can define what are known as "environment aliases". For example, if you use more than one name for local development (eg. dev, development, and local), you can alias all of these to one name to use in this package's configuration.

Note: You can set your application's environment in either config/app.php under env or via your .env file. If you want to manage your .env file via php artisan, you can check out sven/flex-env.

Groups

The groups key in the configration is used to load in service providers and aliases (also know as facades) in one of the previously defined environments. You can use * as a wildcard here to always load that group, regardless of the application's environment.

Providers

The providers array is where you can put the providers you want to have loaded in the defined environment. This should be pretty straight forward as it is similar to how you would register service providers in config/app.php.

Aliases

In the aliases array you may define all your aliases (facades). As with the providers, this is the same as how you would register aliases in the default config/app.php configuration file.

Example

return [
    'environments' => [
        'dev' => ['local', 'development', 'dev'],
        'prod' => ['production'],
    ],
    
    'groups' => [
        'dev' => [
            'providers' => [
                Sven\ArtisanView\ArtisanViewServiceProvider::class,
                Barryvdh\Debugbar\ServiceProvider::class,
            ],
            'aliases' => [
                'Debugbar' => Barryvdh\Debugbar\Facade::class,
            ],
        ],
        'prod' => [
            'providers' => [ /* ... */ ],
            'aliases' => [ /* ... */ ],
        ],
        '*' => [
            'providers' => [ /* ... */ ],
            'aliases' => [ /* ... */ ],
        ],
    ],
    
],

Notice how we're only loading the Debugbar ServiceProvider and facade when our application's environment is either local, development, or dev. This means we can't use the Debugbar facade in our project when the environment doesn't match any of those.

Contributing

All contributions (pull requests, issues and feature requests) are welcome. Make sure to read through the CONTRIBUTING.md first, though. See the contributors page for all contributors.

License

sven/env-providers is licensed under the MIT License (MIT). Please see the license file for more information.

env-providers's People

Contributors

dependabot[bot] avatar gogl92 avatar svenluijten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

env-providers's Issues

Composer will load all service providers in background.

Hey, composer will load all service provider classes in background!, that means only instantiation is remaining. Can we do kind of trick where only those classes will be loaded by composer which are specified in config? This will help us in many ways. ๐Ÿ˜‡

You did not follow semantic versioning

Your commit cf7ea2b broke backwards compatibility (you renamed \Sven\EnvProviders\EnvServiceProvider to \Sven\EnvProviders\ServiceProvider). But this commit was included in a minor update of your package. Version 3.1.0 should have been version 4.0.0.
Please follow semantic versioning in the future to prevent our applications to break.

3.1.2 seems to have problems with package discovery

Hi @svenluijten I'm seeing a problem with package discovery in v3.1.2.

env_providers_error

composer.json seems to be looking for "Sven\EnvProviders\EnvServiceProvider" while the actual service provider seems to have been renamed to ServiceProvider again.

I can put in a pull request to fix it, but I'm not sure which direction you want to take it so I figured I'd throw it in an issue and if you don't have time to work on it or just want to point me in a direction I should be able to put in a pull request for it.

Thanks,
-Ethan

Keyed environment groups

Right now, the configuration file seems a bit "messy", with top-level so-called "environment groups" and no real structure. This might be solved by introducing keyed environment groups. The current configuration file looks (something like) this:

return [
    ['environments' => ['local', 'dev', 'staging'], 'providers' => [/* ... */]],
    ['environments' => ['production'], 'providers' => [/* ... */], 'aliases' => [/* ... */]],
];

I propose a new syntax that would break BC, but could alleviate some confusion, and hopefully increase readability for newcomers coming into projects or this library:

return [
    'local|dev|staging' => ['providers' => [/* ... */]],
    'production' => ['providers' => [/* ... */], 'aliases' => [/* ... */]],
];

Using the pipe operator (|) here for symmetry with Laravel and its validation, which is done in largely the same way. I also considered commas (,), but that felt a bit too "foreign".


There might be a way to keep backwards compatibility by checking if the array (returned from config('providers')) is associative. If it isn't, we just fall back to the current behaviour.

Upgrade Illuminate Support

To make the package compatible with Laravel 9 Illuminate Support 9 needs to be added as well.

^8.0|^9.0

Write tests

This package needs integration tests, but I'm not too familiar with writing those yet. I'd appreciate any help(ful resources)!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.