From 218191ace2e7df36c76a3239cf739b294ca96aed Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Sun, 6 Sep 2015 21:09:24 +0200 Subject: [PATCH 01/11] Implement Dotenv 2 - See #14 --- .gitignore | Bin 1346 -> 1372 bytes composer.json | 5 +- wpstarter/src/{ => Env}/Env.php | 144 ++++++++++++++------------ wpstarter/src/Env/Loader.php | 46 ++++++++ wpstarter/src/Helpers.php | 33 ++++-- wpstarter/templates/wp-config.example | 2 +- 6 files changed, 151 insertions(+), 79 deletions(-) rename wpstarter/src/{ => Env}/Env.php (61%) create mode 100644 wpstarter/src/Env/Loader.php diff --git a/.gitignore b/.gitignore index 43306dfde7fb501baaae5a372b5219394b8ddbc2..0a563d2767df5ef73b9a13f74810a62688ad061f 100644 GIT binary patch delta 29 kcmX@ab%$$$u26V>QA$8jYH=|ymwrKMQch;_Mpao>0G>Sw=l}o! delta 10 Rcmcb^b%<+%?nZY}Rsa`U1A+hm diff --git a/composer.json b/composer.json index 7dd7fca4..e8f4f492 100644 --- a/composer.json +++ b/composer.json @@ -33,8 +33,8 @@ ], "require": { "php": ">=5.3.2", - "vlucas/phpdotenv": "~1.0", - "johnpbloch/wordpress": ">=3.9" + "vlucas/phpdotenv": "~2.0", + "johnpbloch/wordpress": ">=4.2" }, "replace": { "wecodemore/wp-composer-config": "*" @@ -49,7 +49,6 @@ "wpstarter": "WCM\\WPStarter\\Setup::runAsRoot" }, "config": { - "vendor-dir": "public/content/vendor", "optimize-autoloader": true }, "extra": { diff --git a/wpstarter/src/Env.php b/wpstarter/src/Env/Env.php similarity index 61% rename from wpstarter/src/Env.php rename to wpstarter/src/Env/Env.php index 8f8b7a96..a2442c15 100644 --- a/wpstarter/src/Env.php +++ b/wpstarter/src/Env/Env.php @@ -8,9 +8,8 @@ * file that was distributed with this source code. */ -namespace WCM\WPStarter; +namespace WCM\WPStarter\Env; -use Dotenv; /** * Extends Dotenv to load and store all environment variables. @@ -19,17 +18,18 @@ * @license http://opensource.org/licenses/MIT MIT * @package WP Starter */ -class Env extends Dotenv +final class Env { + /** * @var array */ private static $set = array(); /** - * @var bool + * @var static */ - private static $loaded = false; + private static $loaded; /** * @var array @@ -166,42 +166,58 @@ class Env extends Dotenv private static $all; /** - * @inheritdoc + * @var array + */ + private $vars; + + /** + * @param string $path + * @param string $file + * @return \WCM\WPStarter\Env\Env|static */ - public static function load($path, $path = null) + public static function load($path, $file = '.env') { - if (! self::$loaded) { - if (is_null(self::$all)) { - self::$all = array_merge( - self::$isBool, - self::$isBoolOrInt, - self::$isInt, - self::$isMod, - self::$isString - ); + if (is_null(self::$loaded)) { + + self::wpConstants(); + + if ( ! is_string($file)) { + $file = '.env'; } - parent::load($path, $path); - self::$loaded = true; + + $filePath = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file; + $loader = new Loader($filePath, true); + $loader->load(); + self::$loaded = new static($loader->allVarNames()); } + + return self::$loaded; } /** - * Set a variable using putenv() and $_ENV. - * - * The environment variable value is stripped of single and double quotes. - * - * @param string $name - * @param string|null $value + * @return array */ - public static function setEnvironmentVariable($name, $value = null) + public static function wpConstants() { - list($normName, $normValue) = self::normalise($name, $value); - - if (! is_null($normName) && ! is_null($normValue) && ! isset(self::$set[$normName])) { - putenv("{$normName}={$normValue}"); - $_ENV[$normName] = $normValue; - in_array($normName, self::$all, true) and self::$set[$normName] = $normValue; + if (is_null(self::$all)) { + self::$all = array_merge( + self::$isBool, + self::$isBoolOrInt, + self::$isInt, + self::$isMod, + self::$isString + ); } + + return self::$all; + } + + /** + * @param array $vars + */ + public function __construct(array $vars) + { + $this->vars = $this->process($vars); } /** @@ -209,58 +225,57 @@ public static function setEnvironmentVariable($name, $value = null) * * @return array */ - public static function all() + public function allVars() { - return self::$set; + return $this->vars; } /** - * Check constants values and return a 2 items array name/value. - * Invalid values are returned as null. - * - * @param string $name - * @param string $value + * @param array array * @return array */ - private static function normalise($name, $value) + private function process(array $vars) { - list($normName, $normValue) = parent::normaliseEnvironmentVariable($name, $value); + $values = array(); + $constants = self::wpConstants(); + foreach ($vars as $var) { - if (empty($normName) || is_null($normValue)) { - return array(null, null); - } + $value = getenv($var); + $values[$var] = $value; - switch (true) { - case in_array($normName, self::$isInt, true): - $filtered = filter_var($normValue, FILTER_VALIDATE_INT); - $normValue = $filtered === false ? null : (int) $filtered; - break; - case in_array($normName, self::$isBool, true): - $normValue = (bool) filter_var($normValue, FILTER_VALIDATE_BOOLEAN); - break; - case in_array($normName, self::$isBoolOrInt, true) : - if (is_numeric($normValue)) { - $filtered = filter_var($normValue, FILTER_VALIDATE_INT); - $normValue = $filtered === false ? null : (int) $filtered; - break; + if (in_array($var, $constants, true)) { + switch (true) { + case in_array($var, self::$isInt, true): + $values[$var] = (int)$value; + break; + case in_array($var, self::$isBool, true): + $values[$var] = (bool)filter_var($value, FILTER_VALIDATE_BOOLEAN); + break; + case in_array($var, self::$isBoolOrInt, true) : + if (is_numeric($value)) { + $values[$var] = (int)$value; + break; + } + $values[$var] = (bool)filter_var($value, FILTER_VALIDATE_BOOLEAN); + break; + case in_array($var, self::$isMod, true) : + $check = $this->checkMod($value); + is_null($check) or $values[$var] = $check; + break; } - $normValue = (bool) filter_var($normValue, FILTER_VALIDATE_BOOLEAN); - break; - case in_array($normName, self::$isMod, true) : - $normValue = self::checkMod($normValue); - break; + } } - return array($normName, $normValue); + return $values; } /** * Checks that a value is a valid string representation of octal int file permission code. * - * @param string $mod + * @param string $mod * @return int|null */ - private static function checkMod($mod) + private function checkMod($mod) { if ($mod[0] === '0') { $mod = substr($mod, 1); @@ -270,4 +285,5 @@ private static function checkMod($mod) ? octdec($mod) : null; } + } \ No newline at end of file diff --git a/wpstarter/src/Env/Loader.php b/wpstarter/src/Env/Loader.php new file mode 100644 index 00000000..b4562080 --- /dev/null +++ b/wpstarter/src/Env/Loader.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace WCM\WPStarter\Env; + +use Dotenv\Loader as DotenvLoader; + +/** + * @author Giuseppe Mazzapica + * @license http://opensource.org/licenses/MIT MIT + * @package wpstarter + */ +final class Loader extends DotenvLoader +{ + + private $allVars = array(); + + /** + * Set variable using Dotenv loader and store the name in class var + * + * @param string $name + * @param mixed $value + */ + public function setEnvironmentVariable($name, $value = null) + { + parent::setEnvironmentVariable($name, $value); + + $this->allVars[] = $name; + } + + /** + * @return array + */ + public function allVarNames() + { + return $this->allVars; + } + +} \ No newline at end of file diff --git a/wpstarter/src/Helpers.php b/wpstarter/src/Helpers.php index 65432036..a739884e 100644 --- a/wpstarter/src/Helpers.php +++ b/wpstarter/src/Helpers.php @@ -19,21 +19,32 @@ */ class Helpers { - private static $env_loaded = false; /** * Load all the environment variables using Dotenv class and return them. * - * @param $dir + * @param string $dir + * @param string $file * @return array */ - public static function settings($dir) + public static function settings($dir, $file = '') { - Env::load($dir); - Env::required(array('DB_NAME', 'DB_USER', 'DB_PASSWORD')); - self::$env_loaded = true; + $env = Env\Env::load($dir, $file); - return Env::all(); + $settings = $env->allVars(); + + $required = array( + 'DB_NAME', + 'DB_USER', + 'DB_PASSWORD' + ); + $set = array_map('getenv', $required); + if (count($set) !== count($required)) { + $names = implode(', ', $required); + throw new \RuntimeException($names.' environment variables are required.'); + } + + return $settings; } /** @@ -46,17 +57,17 @@ public static function settings($dir) */ public static function addHook($hook, $callable, $priority = 10, $argsNum = 1) { - if (! is_callable($callable) || function_exists('add_action')) { + if ( ! is_callable($callable) || function_exists('add_action')) { return; } global $wp_filter; - if (! is_array($wp_filter)) { + if ( ! is_array($wp_filter)) { $wp_filter = array(); } - if (! isset($wp_filter[$hook])) { + if ( ! isset($wp_filter[$hook])) { $wp_filter[$hook] = array(); } - if (! isset($wp_filter[$hook][$priority])) { + if ( ! isset($wp_filter[$hook][$priority])) { $wp_filter[$hook][$priority] = array(); } /** @var \Closure|object $function */ diff --git a/wpstarter/templates/wp-config.example b/wpstarter/templates/wp-config.example index 1d8cd520..ee275a52 100644 --- a/wpstarter/templates/wp-config.example +++ b/wpstarter/templates/wp-config.example @@ -25,7 +25,7 @@ require_once realpath({{{VENDOR_PATH}}}.'/autoload.php'); * * @var array $env All the variables stored in .env file */ -$env = WCM\WPStarter\Helpers::settings({{{ENV_REL_PATH}}}); +$env = WCM\WPStarter\Helpers::settings({{{ENV_REL_PATH}}}, '.env'); array_walk($env, function ($value, $name) { switch ($name) { case 'DB_TABLE_PREFIX': From b270f3bea6f339bf3cb0c69ac5ce0340aef67e37 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Sun, 6 Sep 2015 21:19:39 +0200 Subject: [PATCH 02/11] Update WP version in docs --- docs/complete-example.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/complete-example.md b/docs/complete-example.md index d781cbd4..2ad4e08c 100644 --- a/docs/complete-example.md +++ b/docs/complete-example.md @@ -32,7 +32,7 @@ all WP Starter configuration. Explanation is provided below in this page. ], "require": { "wecodemore/wpstarter": "~2.0", - "johnpbloch/wordpress": "4.1.*", + "johnpbloch/wordpress": ">=4.3", "gmazzap/wpstarter-example-files": "*" }, "require-dev": { From 6a4b7d4261717998beca46b1a7a22cf68ce481cb Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Sun, 6 Sep 2015 21:58:59 +0200 Subject: [PATCH 03/11] Fix a bug in loaded that saved not-normalized var We need to copy & paste setEnvironmentVariable() because there's no other way to access normalized variable name. - See #14 --- wpstarter/src/Env/Loader.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/wpstarter/src/Env/Loader.php b/wpstarter/src/Env/Loader.php index b4562080..7fb0db94 100644 --- a/wpstarter/src/Env/Loader.php +++ b/wpstarter/src/Env/Loader.php @@ -30,8 +30,15 @@ final class Loader extends DotenvLoader */ public function setEnvironmentVariable($name, $value = null) { - parent::setEnvironmentVariable($name, $value); + list($name, $value) = $this->normaliseEnvironmentVariable($name, $value); + if ($this->immutable === true && ! is_null($this->getEnvironmentVariable($name))) { + return; + } + + putenv("$name=$value"); + $_ENV[$name] = $value; + $_SERVER[$name] = $value; $this->allVars[] = $name; } From 31d70289ab9a962507962068b532479816f66bd2 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Sun, 6 Sep 2015 21:59:45 +0200 Subject: [PATCH 04/11] Fix bug prevened content folder to be recognized when there was nothing in it --- wpstarter/src/Setup.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wpstarter/src/Setup.php b/wpstarter/src/Setup.php index e7a0ba53..566d98c1 100644 --- a/wpstarter/src/Setup.php +++ b/wpstarter/src/Setup.php @@ -212,6 +212,8 @@ private function normalisePaths(array $paths) */ private function subdir($root, $path) { - return trim(preg_replace('|^'.preg_quote(realpath($root)).'|', '', realpath($path)), '\\/'); + $paths = $this->normalisePaths(array($root, $path)); + + return trim(preg_replace('|^'.preg_quote($paths[0]).'|', '', $paths[1]), '\\/'); } } From 11339b5ed73a8552d219de6824f86deebefe0b74 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Sun, 6 Sep 2015 22:14:31 +0200 Subject: [PATCH 05/11] CS fix --- wpstarter/src/Env/Env.php | 23 ++++++++------------- wpstarter/src/Env/Loader.php | 4 +--- wpstarter/src/Helpers.php | 15 +++++++------- wpstarter/src/Setup/Steps/GitignoreStep.php | 2 +- 4 files changed, 18 insertions(+), 26 deletions(-) diff --git a/wpstarter/src/Env/Env.php b/wpstarter/src/Env/Env.php index a2442c15..9eadf4c1 100644 --- a/wpstarter/src/Env/Env.php +++ b/wpstarter/src/Env/Env.php @@ -10,7 +10,6 @@ namespace WCM\WPStarter\Env; - /** * Extends Dotenv to load and store all environment variables. * @@ -20,7 +19,6 @@ */ final class Env { - /** * @var array */ @@ -171,17 +169,16 @@ final class Env private $vars; /** - * @param string $path - * @param string $file + * @param string $path + * @param string $file * @return \WCM\WPStarter\Env\Env|static */ public static function load($path, $file = '.env') { if (is_null(self::$loaded)) { - self::wpConstants(); - if ( ! is_string($file)) { + if (! is_string($file)) { $file = '.env'; } @@ -239,24 +236,23 @@ private function process(array $vars) $values = array(); $constants = self::wpConstants(); foreach ($vars as $var) { - $value = getenv($var); $values[$var] = $value; if (in_array($var, $constants, true)) { switch (true) { case in_array($var, self::$isInt, true): - $values[$var] = (int)$value; + $values[$var] = (int) $value; break; case in_array($var, self::$isBool, true): - $values[$var] = (bool)filter_var($value, FILTER_VALIDATE_BOOLEAN); + $values[$var] = (bool) filter_var($value, FILTER_VALIDATE_BOOLEAN); break; case in_array($var, self::$isBoolOrInt, true) : if (is_numeric($value)) { - $values[$var] = (int)$value; + $values[$var] = (int) $value; break; } - $values[$var] = (bool)filter_var($value, FILTER_VALIDATE_BOOLEAN); + $values[$var] = (bool) filter_var($value, FILTER_VALIDATE_BOOLEAN); break; case in_array($var, self::$isMod, true) : $check = $this->checkMod($value); @@ -272,7 +268,7 @@ private function process(array $vars) /** * Checks that a value is a valid string representation of octal int file permission code. * - * @param string $mod + * @param string $mod * @return int|null */ private function checkMod($mod) @@ -285,5 +281,4 @@ private function checkMod($mod) ? octdec($mod) : null; } - -} \ No newline at end of file +} diff --git a/wpstarter/src/Env/Loader.php b/wpstarter/src/Env/Loader.php index 7fb0db94..6d90e340 100644 --- a/wpstarter/src/Env/Loader.php +++ b/wpstarter/src/Env/Loader.php @@ -19,7 +19,6 @@ */ final class Loader extends DotenvLoader { - private $allVars = array(); /** @@ -49,5 +48,4 @@ public function allVarNames() { return $this->allVars; } - -} \ No newline at end of file +} diff --git a/wpstarter/src/Helpers.php b/wpstarter/src/Helpers.php index a739884e..36e82e1d 100644 --- a/wpstarter/src/Helpers.php +++ b/wpstarter/src/Helpers.php @@ -19,12 +19,11 @@ */ class Helpers { - /** * Load all the environment variables using Dotenv class and return them. * - * @param string $dir - * @param string $file + * @param string $dir + * @param string $file * @return array */ public static function settings($dir, $file = '') @@ -36,7 +35,7 @@ public static function settings($dir, $file = '') $required = array( 'DB_NAME', 'DB_USER', - 'DB_PASSWORD' + 'DB_PASSWORD', ); $set = array_map('getenv', $required); if (count($set) !== count($required)) { @@ -57,17 +56,17 @@ public static function settings($dir, $file = '') */ public static function addHook($hook, $callable, $priority = 10, $argsNum = 1) { - if ( ! is_callable($callable) || function_exists('add_action')) { + if (! is_callable($callable) || function_exists('add_action')) { return; } global $wp_filter; - if ( ! is_array($wp_filter)) { + if (! is_array($wp_filter)) { $wp_filter = array(); } - if ( ! isset($wp_filter[$hook])) { + if (! isset($wp_filter[$hook])) { $wp_filter[$hook] = array(); } - if ( ! isset($wp_filter[$hook][$priority])) { + if (! isset($wp_filter[$hook][$priority])) { $wp_filter[$hook][$priority] = array(); } /** @var \Closure|object $function */ diff --git a/wpstarter/src/Setup/Steps/GitignoreStep.php b/wpstarter/src/Setup/Steps/GitignoreStep.php index 95396424..ba62253b 100644 --- a/wpstarter/src/Setup/Steps/GitignoreStep.php +++ b/wpstarter/src/Setup/Steps/GitignoreStep.php @@ -306,4 +306,4 @@ private function maybeAdd($path, array $parsedPaths) return array_values(array_unique($parsedPaths)); } -} \ No newline at end of file +} From d4774ebd6ab9c89bbbd15de022f40b997e278f67 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Sun, 6 Sep 2015 22:24:07 +0200 Subject: [PATCH 06/11] Introduce new option for .env filename - See #14 --- docs/options.md | 5 +++++ wpstarter/src/Setup/Config.php | 14 ++++++++++++++ wpstarter/src/Setup/Steps/WPConfigStep.php | 5 +++-- wpstarter/templates/wp-config.example | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/docs/options.md b/docs/options.md index 1b550c50..b1e82be5 100644 --- a/docs/options.md +++ b/docs/options.md @@ -152,6 +152,11 @@ Default value is `true`. It worth noting that if a `.env` file exists in project folder WP Starter just skip the creation of `.env.example`, no matter which value the option has. +## "`env-file`" + +The default environment file name is `.env`, using this option can be changed to something else. Note that you can specify here only the file name, and not the path. + + ## "`gitignore`" This option controls if and how WP Starter has to create a `.gitignore` file for the project. diff --git a/wpstarter/src/Setup/Config.php b/wpstarter/src/Setup/Config.php index 13cf03a5..213b2c8b 100644 --- a/wpstarter/src/Setup/Config.php +++ b/wpstarter/src/Setup/Config.php @@ -23,6 +23,7 @@ class Config implements ArrayAccess private static $defaults = array( 'gitignore' => true, 'env-example' => true, + 'env-file' => '.env', 'move-content' => false, 'register-theme-folder' => true, 'prevent-overwrite' => array('.gitignore'), @@ -52,6 +53,7 @@ public function __construct($configs) * @see \WCM\WPStarter\Setup\Config::validatePathArray() * @see \WCM\WPStarter\Setup\Config::validateOverwrite() * @see \WCM\WPStarter\Setup\Config::validateVerbosity() + * @see \WCM\WPStarter\Setup\Config::validateFilename() */ private function validate($configs) { @@ -59,6 +61,7 @@ private function validate($configs) $map = array( 'gitignore' => array($this, 'validateGitignore'), 'env-example' => array($this, 'validateBoolOrAskOrUrl'), + 'env-file' => array($this, 'validateFilename'), 'register-theme-folder' => array($this, 'validateBoolOrAsk'), 'move-content' => array($this, 'validateBoolOrAsk'), 'dropins' => array($this, 'validatePathArray'), @@ -191,6 +194,17 @@ private function validateUrl($value) return filter_var($value, FILTER_SANITIZE_URL) ?: null; } + /** + * @param $value + * @return string|null + */ + private function validateFilename($value) + { + $filtered = filter_var($value, FILTER_SANITIZE_URL) ?: null; + + return $filtered ? basename($filtered) : null; + } + /** * @param $value * @return bool diff --git a/wpstarter/src/Setup/Steps/WPConfigStep.php b/wpstarter/src/Setup/Steps/WPConfigStep.php index e7f43ebd..70bbf398 100644 --- a/wpstarter/src/Setup/Steps/WPConfigStep.php +++ b/wpstarter/src/Setup/Steps/WPConfigStep.php @@ -87,7 +87,7 @@ public function run(ArrayAccess $paths) { $register = $this->config['register-theme-folder']; if ($register === 'ask') { - $register = $this->ask(); + $register = $this->askForRegister(); } $n = count(explode('/', str_replace('\\', '/', $paths['wp']))) - 1; $rootPathRel = str_repeat('dirname(', $n).'__DIR__'.str_repeat(')', $n); @@ -105,6 +105,7 @@ public function run(ArrayAccess $paths) 'WP_SITEURL' => $relUrl($paths['wp']), 'WP_CONTENT_URL' => $relUrl($paths['wp-content']), 'REGISTER_THEME_DIR' => $register ? 'true' : 'false', + 'ENV_FILE_NAME' => $this->config['env-file'], ), $this->salter->keys() ); @@ -137,7 +138,7 @@ public function success() /** * @return bool */ - private function ask() + private function askForRegister() { $lines = array( 'Do you want to register WordPress package wp-content folder', diff --git a/wpstarter/templates/wp-config.example b/wpstarter/templates/wp-config.example index ee275a52..aed0fbee 100644 --- a/wpstarter/templates/wp-config.example +++ b/wpstarter/templates/wp-config.example @@ -25,7 +25,7 @@ require_once realpath({{{VENDOR_PATH}}}.'/autoload.php'); * * @var array $env All the variables stored in .env file */ -$env = WCM\WPStarter\Helpers::settings({{{ENV_REL_PATH}}}, '.env'); +$env = WCM\WPStarter\Helpers::settings({{{ENV_REL_PATH}}}, '{{{ENV_FILE_NAME}}}'); array_walk($env, function ($value, $name) { switch ($name) { case 'DB_TABLE_PREFIX': From 78a49acca8685c71a71f273d8bf826fc8c829c66 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Sun, 6 Sep 2015 23:04:44 +0200 Subject: [PATCH 07/11] Fix bug on required env vars check --- wpstarter/src/Helpers.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/wpstarter/src/Helpers.php b/wpstarter/src/Helpers.php index 36e82e1d..907db342 100644 --- a/wpstarter/src/Helpers.php +++ b/wpstarter/src/Helpers.php @@ -33,11 +33,12 @@ public static function settings($dir, $file = '') $settings = $env->allVars(); $required = array( - 'DB_NAME', - 'DB_USER', - 'DB_PASSWORD', + 'DB_NAME' => '', + 'DB_USER' => '', + 'DB_PASSWORD' => '', ); - $set = array_map('getenv', $required); + + $set = array_filter(array_intersect_key($required, $settings)); if (count($set) !== count($required)) { $names = implode(', ', $required); throw new \RuntimeException($names.' environment variables are required.'); From 34e0f2168584560aeabb33cef4d2bea33e85d2fe Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Mon, 7 Sep 2015 21:18:01 +0200 Subject: [PATCH 08/11] Use '.env' as default file for Helpers::settings() --- wpstarter/src/Helpers.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wpstarter/src/Helpers.php b/wpstarter/src/Helpers.php index 907db342..7b3b55c2 100644 --- a/wpstarter/src/Helpers.php +++ b/wpstarter/src/Helpers.php @@ -26,7 +26,7 @@ class Helpers * @param string $file * @return array */ - public static function settings($dir, $file = '') + public static function settings($dir, $file = '.env') { $env = Env\Env::load($dir, $file); @@ -37,7 +37,7 @@ public static function settings($dir, $file = '') 'DB_USER' => '', 'DB_PASSWORD' => '', ); - + $set = array_filter(array_intersect_key($required, $settings)); if (count($set) !== count($required)) { $names = implode(', ', $required); From 8a8e0988e1222b34fdfbcacc7481166d92cc9547 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Mon, 7 Sep 2015 22:44:16 +0200 Subject: [PATCH 09/11] Simplify required env vars check - See #14 Address https://github.com/wecodemore/wpstarter/issues/14#issuecomment-138299324 --- wpstarter/src/Helpers.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/wpstarter/src/Helpers.php b/wpstarter/src/Helpers.php index 7b3b55c2..3d1f1a3f 100644 --- a/wpstarter/src/Helpers.php +++ b/wpstarter/src/Helpers.php @@ -38,10 +38,11 @@ public static function settings($dir, $file = '.env') 'DB_PASSWORD' => '', ); - $set = array_filter(array_intersect_key($required, $settings)); - if (count($set) !== count($required)) { - $names = implode(', ', $required); - throw new \RuntimeException($names.' environment variables are required.'); + foreach ($required as $key) { + if (! isset($settings[$key]) || empty($settings[$key])) { + $names = implode(', ', $required); + throw new \RuntimeException($names.' environment variables are required.'); + } } return $settings; From 6e80851065e705a1cdc5abef822ef83c254f04fe Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Wed, 9 Sep 2015 14:52:22 +0200 Subject: [PATCH 10/11] Fix bug in Helpers::settings() - See #14 Looping the wrong array --- wpstarter/src/Helpers.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wpstarter/src/Helpers.php b/wpstarter/src/Helpers.php index 3d1f1a3f..68fe452e 100644 --- a/wpstarter/src/Helpers.php +++ b/wpstarter/src/Helpers.php @@ -33,9 +33,9 @@ public static function settings($dir, $file = '.env') $settings = $env->allVars(); $required = array( - 'DB_NAME' => '', - 'DB_USER' => '', - 'DB_PASSWORD' => '', + 'DB_NAME', + 'DB_USER', + 'DB_PASSWORD', ); foreach ($required as $key) { From f61b11041ea14e1fb45f085706f9c59ae55b188d Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Sat, 26 Sep 2015 16:40:39 +0200 Subject: [PATCH 11/11] Use custom env file in gitignore when set See #13 and #14 --- wpstarter/src/Setup/Steps/GitignoreStep.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/wpstarter/src/Setup/Steps/GitignoreStep.php b/wpstarter/src/Setup/Steps/GitignoreStep.php index ba62253b..fe195300 100644 --- a/wpstarter/src/Setup/Steps/GitignoreStep.php +++ b/wpstarter/src/Setup/Steps/GitignoreStep.php @@ -42,6 +42,11 @@ class GitignoreStep implements FileStepInterface, OptionalStepInterface, PostPro */ private $config; + /** + * @var string + */ + private $env; + /** * @var \WCM\WPStarter\Setup\IO */ @@ -78,6 +83,7 @@ public function __construct(IO $io, FileBuilder $builder) public function allowed(Config $config, ArrayAccess $paths) { $this->config = $config['gitignore']; + $this->env = $config['env-file']; $this->found = is_file($this->targetPath($paths)); return $this->config !== false; @@ -235,7 +241,7 @@ private function create(ArrayAccess $paths) $filePaths = array_unique( array_filter( array_merge( - array('.env', $paths['wp-parent'].'/wp-config.php'), + array($this->env, $paths['wp-parent'].'/wp-config.php'), $toDo['custom'], $this->paths($toDo, $paths) )