Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

php - Is .env fallback evaluated even though the value is set?

Given the following line:

env('AN_ENV_KEY', file_exists('mypath') ? 'value' : 'other-value');

Is the fallback evaluated even though the AN_ENV_KEY is specified in the .env file?

I'm asking this because in my server, after the project update, one of the dependencies added a similar line in its changes and now the deployment fails even though I specified the AN_ENV_KEY=value due to some permission issues with the file_exists() part

I'm just curious of this given that my assumption is that if the value is set then the fallback should be omitted.

Thanks in advance

question from:https://stackoverflow.com/questions/65923250/is-env-fallback-evaluated-even-though-the-value-is-set

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since you are defining this env variable you should alter this config file. There are 2 arguments passed to env here and all of them must be resolved as they are being passed to a function. This is before the function even runs, so it doesn't matter if that env variable exists that file_exists call will happen no matter what.

You can remove the expression which uses file_exists as its unneeded in your case since you are setting the env variable.

env('USER_MODEL', 'some sane default?');

This is fine since you are supposed to publish this config file so its in your application config, so you control it at this point.

Part 2

Now that your version of the config file is fixed we don't want the package's config file to be merged since it will still try to do this file_exists call every time. To do this we will extend their provider and override the register method. Then we will add your version of their provider to the providers list and also tell the auto discovery system to ignore this package (so it doesn't register its provider or alias):

app/Providers/SomeCoolNamedProvider:

namespace AppProviders;

use SilvaniteBrandenburgProvidersBrandenburgServiceProvider;

class SomeCoolNamedProvider extends BrandenburgServiceProvider
{
    public function register()
    {
        // in their version they merge the config here
        // but we don't want that, so leave this blank
    }
}

config/app.php:

$providers = [
    ...
    AppProvidersSomeCoolNamedProvider::class,
];

$aliases = [
    ...
    'BrandenburgPolicy' => SilvaniteBrandenburgFacadesPolicyFacade::class,
];

composer.json:

"extra": {
    "laravel": {
        "dont-discover": [
            "silvanite/brandenburg"
        ]
    }
},

You can run the auto discovery command after this, php artisan package:discover


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...