Skip to content

Commit a5ac74d

Browse files
committed
Added phpdotenv support
1 parent 028e5ac commit a5ac74d

File tree

1 file changed

+80
-6
lines changed

1 file changed

+80
-6
lines changed

core/ToolKit.php

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ protected function init( $base_dir = null, $args = null ) {
3636

3737
self::$config = $config;
3838

39+
// Load Environmental Variables
40+
$this->load_env_vars( [ $base_dir, self::$config->get( 'wordpress/root_dir' ) ] );
41+
3942
}
4043

4144
/**
@@ -104,7 +107,7 @@ protected function get_current_plugin_meta( $type = ConfigRegistry ) {
104107
* Returns the directory location of wp-config.php
105108
*
106109
* @return bool
107-
* @since 0.2.1
110+
* @since 0.3.0
108111
*/
109112
private function get_wordpress_config_dir() {
110113
$dir = dirname( __FILE__ );
@@ -117,7 +120,43 @@ private function get_wordpress_config_dir() {
117120
}
118121

119122
/**
120-
* Returns true if request is via Ajax.
123+
* Load environment variables from various .env, if present.
124+
*
125+
* @param string|array $paths The directories to look within for .env file
126+
* @return bool Returns true if .env found and loaded
127+
* @since 0.3.0
128+
*/
129+
private function load_env_vars( $paths = __DIR__ ) {
130+
131+
$paths = (array) $paths;
132+
133+
$result = null;
134+
foreach( $paths as $path ) {
135+
$result = $this->load_env_from_apth( $path );
136+
}
137+
138+
return $result;
139+
}
140+
141+
/**
142+
* Load environment variables from specified .env file.
143+
*
144+
* @param string $path The directory load .env file from
145+
* @return bool
146+
* @since 0.3.0
147+
*/
148+
private function load_env_from_apth( $path ) {
149+
try {
150+
$env = new \Dotenv\Dotenv( $path );
151+
$env->load();
152+
} catch ( \Dotenv\Exception\InvalidPathException $e ) {
153+
} catch ( Exception $e ) { }
154+
155+
$this->set_environment();
156+
}
157+
158+
/**
159+
* Returns true if request is via AJAX.
121160
*
122161
* @return bool
123162
* @since 0.2.1
@@ -127,15 +166,50 @@ public function is_ajax() {
127166
}
128167

129168
/**
130-
* Returns true if WP_ENV is anything other than 'development' or 'staging'.
131-
* Useful for determining whether or not to enqueue a minified or non-
132-
* minified script (which can be useful for debugging via browser).
169+
* Returns true if environment is anything other than 'development' or
170+
* 'staging'. For example, for determining whether or not to enqueue a
171+
* minified or non-minified script (which can be useful for debugging via
172+
* browser).
133173
*
134174
* @return bool
175+
* @see ToolKit::get_environment()
135176
* @since 0.1.0
136177
*/
137178
public static function is_production() {
138-
return ( !defined( 'WP_ENV' ) || ( defined('WP_ENV' ) && !in_array( WP_ENV, array( 'development', 'staging' ) ) ) );
179+
return strtolower( self::get_environment() ) == 'production';
180+
}
181+
182+
/**
183+
* Sets the environmental variable `ENVIRONMENT` if $env is passed or if
184+
* constant WP_ENV is define.
185+
*
186+
* @param string $env If provided, set's the enviroment to string provided
187+
* @since 0.3.0
188+
*/
189+
public static function set_environment( $env = null ) {
190+
191+
switch( true ) {
192+
case ( is_string( $env ) ):
193+
putenv( 'ENVIRONMENT=' . $env );
194+
break;
195+
case ( defined( 'WP_ENV' ) ):
196+
putenv( 'ENVIRONMENT=' . WP_ENV );
197+
break;
198+
}
199+
200+
}
201+
202+
/**
203+
* Gets the current development environmental variable if set via WP_ENV
204+
* constant or .env values.
205+
*
206+
* @param string $env If provided, set's the enviroment to string provided
207+
* @see ToolKit::load_env_vars()
208+
* @see ToolKit::set_environment()
209+
* @since 0.3.0
210+
*/
211+
public static function get_environment() {
212+
return getenv( 'ENVIRONMENT' ) ?: 'production';
139213
}
140214

141215
}

0 commit comments

Comments
 (0)