2017 © Pedro Peláez
 

library application-context

Define the context of the current enviroment, in order to adapt configuration options depending on the stage an app is running on

image

pixelbrackets/application-context

Define the context of the current enviroment, in order to adapt configuration options depending on the stage an app is running on

  • Wednesday, March 21, 2018
  • by pixelbrackets
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Application Context

Version Build Status Made With License, (*1)

The »Application Context« library allows to define the context of the current environment, in order to adapt configuration options depending on the stage an app is running on., (*2)

For example if an application is running in Production mode, it should send mails and not create any log files. In Development mode however it should send mails, but to a different recipient, and create excessive log files., (*3)

export APPLICATION_CONTEXT=Development/Local/JohnDoe
if($applicationContext->isDevelopment()) {
    // … do this in development mode only
}

An environment variable sets the context, whichs is retrieved using this class., (*4)

The main advantage of this approach is, that the code may stay the same on all stages, but only configuration values may change, depending on the context., (*5)

Requirements

  • PHP

Installation

Packagist Entry https://packagist.org/packages/pixelbrackets/application-context/, (*6)

Source

https://gitlab.com/pixelbrackets/application-context/, (*7)

Mirror https://github.com/pixelbrackets/application-context/, (*8)

Usage

  1. Set the application context using an environment variable, (*9)

    A context may contain arbitrary sub-contexts. They are delimited with a slash. For example Production/Integration or Development/LocalMachines/JohnDoe., (*10)

    The top-level contexts however, must be one of Development, Testing or Production. Testing should be used to run unit tests only. Use Production and Development and any sub-context for all stages., (*11)

    export APPLICATION_CONTEXT=Development/Local/JohnDoe
    

    or pass to the script like this, (*12)

    APPLICATION_CONTEXT=Development php index.php
    

    💡 Hint: The package helhum/dotenv-connector lets you store these variables in an .env file and automatically parse it., (*13)

  2. Integrate the ApplicationContext class, (*14)

    $applicationContext = new \Pixelbrackets\ApplicationContext\ApplicationContext(getenv('APPLICATION_CONTEXT'));
    

    If the context variable is empty, then Production is the default., (*15)

  3. Change code or configuration depending on the given context, (*16)

    $config['write-logs'] = true;
    $config['mail']['to'] = 'johndoe@example.com';
    if($applicationContext->isDevelopment()) {
       $config['mail']['to'] = 'test-test@localhost.tld';
    }
    

    Available methods to check the top-level context are isProduction(), isTesting() and isDevelopment()., (*17)

    If the context object is casted to a string, then the return value is the context string as set in the environment variable. This may be used to load different files as in this example., (*18)

    $configFile = __DIR__ . '/Configuration/' . (string)$applicationContext . '.php';
    if (file_exists($configFile)) {
     require($configFile);
    }
    

License

GNU General Public License version 2 or later, (*19)

The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html., (*20)

Attributions:, (*21)

  • This library is a standalone version of the Application Context in TYPO3 CMS which derived from the TYPO3 Flow framework.

Author

Dan Untenzu (mail@pixelbrackets.de / @pixelbrackets), (*22)

Changelog

./CHANGELOG.md, (*23)

Contribution

This script is Open Source, so please use, patch, extend or fork it., (*24)

The Versions

21/03 2018

dev-master

9999999-dev

Define the context of the current enviroment, in order to adapt configuration options depending on the stage an app is running on

  Sources   Download

GPL-2.0-or-later

The Requires

  • php >=7.0.0

 

21/03 2018

1.0.0

1.0.0.0

Define the context of the current enviroment, in order to adapt configuration options depending on the stage an app is running on

  Sources   Download

GPL-2.0-or-later

The Requires

  • php >=7.0.0