68publishers/environment

Composer plugin for environment loading.

Installs: 1 141

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:composer-plugin

v0.4 2021-10-07 22:15 UTC

This package is auto-updated.

Last update: 2021-11-07 22:34:15 UTC


README

The component based on the symfony/dotenv for simple loading of the ENV variables and detecting a debug mode.

Installation

$ composer require 68publishers/environment

Usage

Default ENV variables

The default ENV variables are:

  • APP_ENV
  • APP_DEBUG

Both variables will be always accessible in the global arrays $_ENV and $_SERVER. The default value for APP_ENV is dev and 0 for APP_DEBUG.

ENV variables loading

For ENV variables loading call this static method after requiring Composer's autoload and before your application is stared. The first argument is a relative path to the application root directory and the second argument is an array of debug mode detectors.

<?php

use SixtyEightPublishers\Environment\Debug;
use SixtyEightPublishers\Environment\Bootstrap\EnvBootstrap;

require __DIR__ . '/../vendor/autoload.php';

EnvBootstrap::boot([
    new Debug\CliOptionDetector('debug_please'), // the debug mode is enabled if an option "--debug_please" is defined (CLI only)
    new Debug\IpAddressDetector([
        '185.141.252.240', // the debug mode is enabled always for this IP address
        'john_dee@135.151.252.240', // the debug mode is enabled for this IP address and a cookie called "debug_please" must exist with value "john_dee"
    ], 'debug_please'),
    new Debug\SimpleCookieDetector('ineeddebug123', 'debug_please'), // the debug mode is enabled if a cookie called "debug_please" exists and has the value "ineeddebug123"
    new Debug\EnvDetector(), // the detection is performed from loaded ENV variables, the debug mode is enabled if a variable "DEBUG=1" is defined or if a variable "APP_ENV" has a different value than, "prod"
]);

// All your ENV variables are now accessible in the global arrays `$_ENV` and `$_SERVER`

If you're using Nette Framework then you can in the bootstrap use this method instead:

<?php

use SixtyEightPublishers\Environment\Bootstrap\EnvBootstrap;

require __DIR__ . '/../vendor/autoload.php';

$configurator = new Nette\Configurator;

EnvBootstrap::bootNetteConfigurator($configurator, [
	// define detectors here
]);

// All your ENV variables are now accessible in the global arrays `$_ENV` and `$_SERVER`
// The debug mode on the configurator is set by the ENV variable `APP_DEBUG`
// The ENV variables are accessible in DI container and Neon configuration as dynamic parameters with prefix `env.` e.g. `%env.APP_ENV%`

ENV variables caching/dumping

All ENV variables are always (in each request) parsed from .env files by default. This is a good solution for developers because all changes are immediately applied after the change. But sometimes (mainly on the production) you don't want to parse .env files in each request. If you want to cache the ENV variables then run following Composer command:

$ composer environment:dump <APP_ENV>

or shorter

$ composer env:dump <APP_ENV>

The file .env.local.php will be created in the application's root directory and it will be used instead of all your .env files.

Contributing

Before committing any changes, don't forget to run

$ vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run

and

$ vendor/bin/tester ./tests