bnomei/kirby3-dotenv

Kirby 3 Plugin for environment variables from .env

Installs: 1 089

Dependents: 0

Suggesters: 2

Stars: 7

Watchers: 0

Forks: 0

Open Issues: 0

Type:kirby-plugin

1.2.0 2019-08-18 18:28 UTC

README

Release Stars Downloads Issues Build Status Coverage Status Maintainability
Demo Gitter Twitter

Kirby 3 Plugin for environment variables from .env

Commercial Usage

This plugin is free but if you use it in a commercial project please consider to

Installation

  • unzip master.zip as folder site/plugins/kirby3-dotenv or
  • git submodule add https://github.com/bnomei/kirby3-dotenv.git site/plugins/kirby3-dotenv or
  • composer require bnomei/kirby3-dotenv

Setup

.env file

APP_MODE=production
APP_DEBUG=true
ALGOLIA_APIKEY=12d7331a21d8a28b3069c49830f463e833e30f6d
KIRBY_API_USER=bnomei
KIRBY_API_PW=52d3a0edcc78be6c5645fdb7568f94d3d83d1c2a

plugin helper methods

echo env('APP_MODE'); // production
// or
echo $page->getenv('ALGOLIA_APIKEY'); // 12d7331a21d8a28b3069c49830f463e833e30f6d

plain php

Bnomei\DotEnv::load();
echo getenv('APP_DEBUG'); // true
echo env('APP_DEBUG'); // true

Using getenv() in the Kirby config file

site/config/config.php

return [
    // ... other options
    'bnomei.cloudconvert.apikey' => function() { 
        return env('CLOUDCONVERT_APIKEY'); 
    },
    'bnomei.instagram.token' => function() { 
        return env('INSTAGRAM_TOKEN'); 
    },
    'bnomei.thumbimageoptim.apikey' => function() { 
        return env('IMAGEOPTIM_APIKEY'); 
    },
];

No callback - no luck? 3 line are enough!

Unless you preload the Bnomei\DotEnv class using an include_once statement yourself the class will not be available in the kirby config files. But some options take a callback not just a string value. If your desired option does not then consider reporting a github issue at their repository. Adding a callback for an option is 3 lines of work.

code/in/another/plugin.php

public function thisIsWereAllConfigValuesAreLoaded()
{
    $fancyOption = option('another.plugin.fancy');
    // add these 3 lines
    if (is_callable($fancyOptions)) {
        $fancyOption = $fancyOption();
    }
}

Settings

bnomei.dotenv. Default Description
dir callback returning kirby()->roots()->index(). When installing Kirby 3 with Composer use a function() { return realpath(kirby()->roots()->index() . '/../'); }`
filename .env
required [] You can define required variables in the Settings using an array. If any of these is missing a RuntimeException will be thrown.

Dependencies

Disclaimer

This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.

License

MIT

It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.

Credits

based on K2 version of