fewagency / boilerplate-pattern-library

A boilerplate for a PHP project with a macropiche pattern-library
1 stars 1 forks source link

Integrate dotenv #11

Closed bjuppa closed 7 years ago

bjuppa commented 8 years ago

For Craft: https://mattstauffer.co/blog/environment-specific-configuration-for-craftcms-using-phpdotenv (uses getenv())

bjuppa commented 8 years ago

composer require vlucas/phpdotenv

Create .env.example in project root directory

Add .env in .gitignore

Add this post-root-package-install script in composer.json:

{
  "scripts": {
    "post-root-package-install": [
      "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ]
  }
}

Then you can require_once PATH/TO/vendor/autoload.php from any php-file (for example in wp-config.php) and then load the .env file, e.g.:

(new Dotenv\Dotenv(__DIR__.'/..'))->load();

...and then use getenv() to retrieve values!

bjuppa commented 8 years ago

This gist contains a function definition for an env() method that takes default values: https://gist.github.com/bjuppa/50d26bb9ad6b4d6f66916be4158a9189

bjuppa commented 8 years ago

I'm planning to create a package fewagency/env that would require vlucas/phpdotenv and expose an init-method that loads the .env using Dotenv::load() and load the env() helper method from the gist above.

bjuppa commented 8 years ago

Here's the package: https://github.com/fewagency/env

bjuppa commented 7 years ago

All done!