From cd1c31218ee75bcb33ec6bccd9ebe31a8288da1d Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Thu, 25 May 2017 23:21:54 -0500 Subject: [PATCH] Modify the bootup process to be able to read the environment from the env file. Fixes #519 --- application/Controllers/Checks.php | 2 +- system/CodeIgniter.php | 4 +-- system/Config/DotEnv.php | 33 ++++++++++--------- .../source/general/configuration.rst | 6 ++-- .../source/general/environments.rst | 23 +++++++++---- 5 files changed, 40 insertions(+), 28 deletions(-) diff --git a/application/Controllers/Checks.php b/application/Controllers/Checks.php index 3b59087181a2..56ccfb426233 100644 --- a/application/Controllers/Checks.php +++ b/application/Controllers/Checks.php @@ -158,7 +158,7 @@ public function image() ->getFile() ->getProperties(true); - dd($info); + dd(ENVIRONMENT); $images = Services::image('imagick') ->getVersion(); diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 61d9883669c1..674ab56b51db 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -158,9 +158,9 @@ public function initialize() Config\Services::exceptions($this->config, true) ->initialize(); + $this->loadEnvironment(); $this->detectEnvironment(); $this->bootstrapEnvironment(); - $this->loadEnvironment(); if (CI_DEBUG) { @@ -307,7 +307,7 @@ protected function detectEnvironment() } else { - define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'production'); + define('ENVIRONMENT', $_SERVER['CI_ENVIRONMENT'] ?? 'production'); } } diff --git a/system/Config/DotEnv.php b/system/Config/DotEnv.php index faacad8c9c86..fff1f0e8be8b 100644 --- a/system/Config/DotEnv.php +++ b/system/Config/DotEnv.php @@ -58,7 +58,7 @@ class DotEnv */ public function __construct(string $path, $file = '.env') { - if ( ! is_string($file)) + if (! is_string($file)) { $file = '.env'; } @@ -77,13 +77,13 @@ public function load() { // We don't want to enforce the presence of a .env file, // they should be optional. - if ( ! is_file($this->path)) + if (! is_file($this->path)) { return false; } // Ensure file is readable - if ( ! is_readable($this->path)) + if (! is_readable($this->path)) { throw new \InvalidArgumentException("The .env file is not readable: {$this->path}"); } @@ -122,7 +122,7 @@ protected function setVariable(string $name, string $value = '') list($name, $value) = $this->normaliseVariable($name, $value); putenv("$name=$value"); - $_ENV[$name] = $value; + $_ENV[$name] = $value; $_SERVER[$name] = $value; } @@ -134,6 +134,7 @@ protected function setVariable(string $name, string $value = '') * * @param string $name * @param string $value + * * @return array */ public function normaliseVariable(string $name, string $value = ''): array @@ -173,7 +174,7 @@ public function normaliseVariable(string $name, string $value = ''): array */ protected function sanitizeValue(string $value): string { - if ( ! $value) + if (! $value) { return $value; } @@ -184,7 +185,7 @@ protected function sanitizeValue(string $value): string // value starts with a quote $quote = $value[0]; $regexPattern = sprintf( - '/^ + '/^ %1$s # match a quote at the start of the value ( # capturing sub-pattern used (?: # we do not need to capture this @@ -196,8 +197,8 @@ protected function sanitizeValue(string $value): string %1$s # and the closing quote .*$ # and discard any string after the closing quote /mx', - $quote - ); + $quote + ); $value = preg_replace($regexPattern, '$1', $value); $value = str_replace("\\$quote", $quote, $value); $value = str_replace('\\\\', '\\', $value); @@ -240,22 +241,22 @@ protected function resolveNestedVariables(string $value): string $loader = $this; $value = preg_replace_callback( - '/\${([a-zA-Z0-9_]+)}/', - function ($matchedPatterns) use ($loader) - { + '/\${([a-zA-Z0-9_]+)}/', + function ($matchedPatterns) use ($loader) + { $nestedVariable = $loader->getVariable($matchedPatterns[1]); if (is_null($nestedVariable)) { - return $matchedPatterns[0]; + return $matchedPatterns[0]; } else { - return $nestedVariable; + return $nestedVariable; } - }, - $value - ); + }, + $value + ); } return $value; diff --git a/user_guide_src/source/general/configuration.rst b/user_guide_src/source/general/configuration.rst index 8f1a3e5fb583..ff51649d90e4 100644 --- a/user_guide_src/source/general/configuration.rst +++ b/user_guide_src/source/general/configuration.rst @@ -57,8 +57,8 @@ the server used for the production site, you can modify your values based on the you will have settings that might change depending on the server it's running on.This can include database settings, API credentials, and other settings that will vary between deploys. -You can store values in a **.env** file in the **/application** directory. It is simply a collection of name/value pairs separated by an equal -sign, much like a .ini file:: +You can store values in a **.env** file in the root directory, alongside the system and application directories. +It is simply a collection of name/value pairs separated by an equal sign, much like a .ini file:: S3_BUCKET="dotenv" SECRET_KEY="super_secret_key" @@ -66,7 +66,7 @@ sign, much like a .ini file:: If the variable exists in the environment already, it will NOT be overwritten. .. important:: Make sure the **.env** file is added to **.gitignore** (or your version control system's equivalent) - so it is not checked-in the code. Failure to do so could result in sensitive credentials being stored in the + so it is not checked in the code. Failure to do so could result in sensitive credentials being stored in the repository for anyone to find. You are encouraged to create a template file, like **env.example**, that has all of the variables your project diff --git a/user_guide_src/source/general/environments.rst b/user_guide_src/source/general/environments.rst index 9a4bf9762917..e8c8e806f2b2 100644 --- a/user_guide_src/source/general/environments.rst +++ b/user_guide_src/source/general/environments.rst @@ -13,16 +13,27 @@ The ENVIRONMENT Constant ======================== By default, CodeIgniter comes with the environment constant set to use -the value provided in ``$_SERVER['CI_ENV']``, otherwise defaulting to -'production'. At the top of index.php, you will see:: +the value provided in ``$_SERVER['CI_ENVIRONMENT']``, otherwise defaulting to +'production'. This can be set in several ways depending on your server setup. - define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development'); +.env +---- + +The simplest method to set the variable is in your :doc:`.env file `:: + + CI_ENVIRONMENT = develop + +Apache +------ This server variable can be set in your ``.htaccess`` file, or Apache config using `SetEnv `_. :: - SetEnv CI_ENV development + SetEnv CI_ENVIRONMENT development + +nginx +----- Under nginx, you must pass the environment variable through the ``fastcgi_params`` in order for it to show up under the `$_SERVER` variable. This allows it to work on the @@ -36,7 +47,7 @@ like:: root /var/www; location ~* "\.php$" { - fastcgi_param CI_ENV "production"; + fastcgi_param CI_ENVIRONMENT "production"; include conf/fastcgi-php.conf; } } @@ -86,4 +97,4 @@ Optionally, you can have CodeIgniter load environment-specific configuration files. This may be useful for managing things like differing API keys across multiple environments. This is described in more detail in the Handling Different Environments section of the -:doc:`Working with Configuration Files ` documentation. \ No newline at end of file +:doc:`Working with Configuration Files ` documentation.