Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Sep 28, 2015
2 parents b1ff2f1 + f61b110 commit c24bedd
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 84 deletions.
Binary file modified .gitignore
Binary file not shown.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
Expand All @@ -49,7 +49,6 @@
"wpstarter": "WCM\\WPStarter\\Setup::runAsRoot"
},
"config": {
"vendor-dir": "public/content/vendor",
"optimize-autoloader": true
},
"extra": {
Expand Down
2 changes: 1 addition & 1 deletion docs/complete-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
5 changes: 5 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
143 changes: 77 additions & 66 deletions wpstarter/src/Env.php → wpstarter/src/Env/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
* file that was distributed with this source code.
*/

namespace WCM\WPStarter;

use Dotenv;
namespace WCM\WPStarter\Env;

/**
* Extends Dotenv to load and store all environment variables.
Expand All @@ -19,17 +17,17 @@
* @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
Expand Down Expand Up @@ -166,92 +164,105 @@ 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);
}

/**
* Return all vars have been set
*
* @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) {
$value = getenv($var);
$values[$var] = $value;

if (empty($normName) || is_null($normValue)) {
return array(null, null);
}

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;
}

/**
Expand All @@ -260,7 +271,7 @@ private static function normalise($name, $value)
* @param string $mod
* @return int|null
*/
private static function checkMod($mod)
private function checkMod($mod)
{
if ($mod[0] === '0') {
$mod = substr($mod, 1);
Expand All @@ -270,4 +281,4 @@ private static function checkMod($mod)
? octdec($mod)
: null;
}
}
}
51 changes: 51 additions & 0 deletions wpstarter/src/Env/Loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/*
* This file is part of the wpstarter package.
*
* (c) Giuseppe Mazzapica <[email protected]>
*
* 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 <[email protected]>
* @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)
{
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;
}

/**
* @return array
*/
public function allVarNames()
{
return $this->allVars;
}
}
28 changes: 20 additions & 8 deletions wpstarter/src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,33 @@
*/
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')
{
Env::load($dir);
Env::required(array('DB_NAME', 'DB_USER', 'DB_PASSWORD'));
self::$env_loaded = true;
$env = Env\Env::load($dir, $file);

$settings = $env->allVars();

$required = array(
'DB_NAME',
'DB_USER',
'DB_PASSWORD',
);

foreach ($required as $key) {
if (! isset($settings[$key]) || empty($settings[$key])) {
$names = implode(', ', $required);
throw new \RuntimeException($names.' environment variables are required.');
}
}

return Env::all();
return $settings;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion wpstarter/src/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]), '\\/');
}
}
14 changes: 14 additions & 0 deletions wpstarter/src/Setup/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -52,13 +53,15 @@ 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)
{
$valid = array('is-root' => $configs['is-root'], 'wp-version' => $configs['wp-version']);
$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'),
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit c24bedd

Please sign in to comment.