Skip to content

Commit

Permalink
Modify the bootup process to be able to read the environment from the…
Browse files Browse the repository at this point in the history
… env file. Fixes #519
  • Loading branch information
lonnieezell committed May 26, 2017
1 parent b0e2b7d commit cd1c312
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
2 changes: 1 addition & 1 deletion application/Controllers/Checks.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function image()
->getFile()
->getProperties(true);

dd($info);
dd(ENVIRONMENT);

$images = Services::image('imagick')
->getVersion();
Expand Down
4 changes: 2 additions & 2 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -307,7 +307,7 @@ protected function detectEnvironment()
}
else
{
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'production');
define('ENVIRONMENT', $_SERVER['CI_ENVIRONMENT'] ?? 'production');
}
}

Expand Down
33 changes: 17 additions & 16 deletions system/Config/DotEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class DotEnv
*/
public function __construct(string $path, $file = '.env')
{
if ( ! is_string($file))
if (! is_string($file))
{
$file = '.env';
}
Expand All @@ -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}");
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
Expand Down Expand Up @@ -173,7 +174,7 @@ public function normaliseVariable(string $name, string $value = ''): array
*/
protected function sanitizeValue(string $value): string
{
if ( ! $value)
if (! $value)
{
return $value;
}
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions user_guide_src/source/general/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ 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"

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
Expand Down
23 changes: 17 additions & 6 deletions user_guide_src/source/general/environments.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 </general/configuration>`::

CI_ENVIRONMENT = develop

Apache
------

This server variable can be set in your ``.htaccess`` file, or Apache
config using `SetEnv <https://httpd.apache.org/docs/2.2/mod/mod_env.html#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
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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 </general/configuration>` documentation.
:doc:`Working with Configuration Files </general/configuration>` documentation.

0 comments on commit cd1c312

Please sign in to comment.