Skip to content

Commit c24bedd

Browse files
committed
Merge branch 'dev'
2 parents b1ff2f1 + f61b110 commit c24bedd

File tree

12 files changed

+185
-84
lines changed

12 files changed

+185
-84
lines changed

.gitignore

26 Bytes
Binary file not shown.

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
],
3434
"require": {
3535
"php": ">=5.3.2",
36-
"vlucas/phpdotenv": "~1.0",
37-
"johnpbloch/wordpress": ">=3.9"
36+
"vlucas/phpdotenv": "~2.0",
37+
"johnpbloch/wordpress": ">=4.2"
3838
},
3939
"replace": {
4040
"wecodemore/wp-composer-config": "*"
@@ -49,7 +49,6 @@
4949
"wpstarter": "WCM\\WPStarter\\Setup::runAsRoot"
5050
},
5151
"config": {
52-
"vendor-dir": "public/content/vendor",
5352
"optimize-autoloader": true
5453
},
5554
"extra": {

docs/complete-example.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ all WP Starter configuration. Explanation is provided below in this page.
3232
],
3333
"require": {
3434
"wecodemore/wpstarter": "~2.0",
35-
"johnpbloch/wordpress": "4.1.*",
35+
"johnpbloch/wordpress": ">=4.3",
3636
"gmazzap/wpstarter-example-files": "*"
3737
},
3838
"require-dev": {

docs/options.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ Default value is `true`.
152152
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.
153153

154154

155+
## "`env-file`"
156+
157+
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.
158+
159+
155160
## "`gitignore`"
156161

157162
This option controls if and how WP Starter has to create a `.gitignore` file for the project.

wpstarter/src/Env.php renamed to wpstarter/src/Env/Env.php

Lines changed: 77 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
* file that was distributed with this source code.
99
*/
1010

11-
namespace WCM\WPStarter;
12-
13-
use Dotenv;
11+
namespace WCM\WPStarter\Env;
1412

1513
/**
1614
* Extends Dotenv to load and store all environment variables.
@@ -19,17 +17,17 @@
1917
* @license http://opensource.org/licenses/MIT MIT
2018
* @package WP Starter
2119
*/
22-
class Env extends Dotenv
20+
final class Env
2321
{
2422
/**
2523
* @var array
2624
*/
2725
private static $set = array();
2826

2927
/**
30-
* @var bool
28+
* @var static
3129
*/
32-
private static $loaded = false;
30+
private static $loaded;
3331

3432
/**
3533
* @var array
@@ -166,92 +164,105 @@ class Env extends Dotenv
166164
private static $all;
167165

168166
/**
169-
* @inheritdoc
167+
* @var array
168+
*/
169+
private $vars;
170+
171+
/**
172+
* @param string $path
173+
* @param string $file
174+
* @return \WCM\WPStarter\Env\Env|static
170175
*/
171-
public static function load($path, $path = null)
176+
public static function load($path, $file = '.env')
172177
{
173-
if (! self::$loaded) {
174-
if (is_null(self::$all)) {
175-
self::$all = array_merge(
176-
self::$isBool,
177-
self::$isBoolOrInt,
178-
self::$isInt,
179-
self::$isMod,
180-
self::$isString
181-
);
178+
if (is_null(self::$loaded)) {
179+
self::wpConstants();
180+
181+
if (! is_string($file)) {
182+
$file = '.env';
182183
}
183-
parent::load($path, $path);
184-
self::$loaded = true;
184+
185+
$filePath = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file;
186+
$loader = new Loader($filePath, true);
187+
$loader->load();
188+
self::$loaded = new static($loader->allVarNames());
185189
}
190+
191+
return self::$loaded;
186192
}
187193

188194
/**
189-
* Set a variable using putenv() and $_ENV.
190-
*
191-
* The environment variable value is stripped of single and double quotes.
192-
*
193-
* @param string $name
194-
* @param string|null $value
195+
* @return array
195196
*/
196-
public static function setEnvironmentVariable($name, $value = null)
197+
public static function wpConstants()
197198
{
198-
list($normName, $normValue) = self::normalise($name, $value);
199-
200-
if (! is_null($normName) && ! is_null($normValue) && ! isset(self::$set[$normName])) {
201-
putenv("{$normName}={$normValue}");
202-
$_ENV[$normName] = $normValue;
203-
in_array($normName, self::$all, true) and self::$set[$normName] = $normValue;
199+
if (is_null(self::$all)) {
200+
self::$all = array_merge(
201+
self::$isBool,
202+
self::$isBoolOrInt,
203+
self::$isInt,
204+
self::$isMod,
205+
self::$isString
206+
);
204207
}
208+
209+
return self::$all;
210+
}
211+
212+
/**
213+
* @param array $vars
214+
*/
215+
public function __construct(array $vars)
216+
{
217+
$this->vars = $this->process($vars);
205218
}
206219

207220
/**
208221
* Return all vars have been set
209222
*
210223
* @return array
211224
*/
212-
public static function all()
225+
public function allVars()
213226
{
214-
return self::$set;
227+
return $this->vars;
215228
}
216229

217230
/**
218-
* Check constants values and return a 2 items array name/value.
219-
* Invalid values are returned as null.
220-
*
221-
* @param string $name
222-
* @param string $value
231+
* @param array array
223232
* @return array
224233
*/
225-
private static function normalise($name, $value)
234+
private function process(array $vars)
226235
{
227-
list($normName, $normValue) = parent::normaliseEnvironmentVariable($name, $value);
236+
$values = array();
237+
$constants = self::wpConstants();
238+
foreach ($vars as $var) {
239+
$value = getenv($var);
240+
$values[$var] = $value;
228241

229-
if (empty($normName) || is_null($normValue)) {
230-
return array(null, null);
231-
}
232-
233-
switch (true) {
234-
case in_array($normName, self::$isInt, true):
235-
$filtered = filter_var($normValue, FILTER_VALIDATE_INT);
236-
$normValue = $filtered === false ? null : (int) $filtered;
237-
break;
238-
case in_array($normName, self::$isBool, true):
239-
$normValue = (bool) filter_var($normValue, FILTER_VALIDATE_BOOLEAN);
240-
break;
241-
case in_array($normName, self::$isBoolOrInt, true) :
242-
if (is_numeric($normValue)) {
243-
$filtered = filter_var($normValue, FILTER_VALIDATE_INT);
244-
$normValue = $filtered === false ? null : (int) $filtered;
245-
break;
242+
if (in_array($var, $constants, true)) {
243+
switch (true) {
244+
case in_array($var, self::$isInt, true):
245+
$values[$var] = (int) $value;
246+
break;
247+
case in_array($var, self::$isBool, true):
248+
$values[$var] = (bool) filter_var($value, FILTER_VALIDATE_BOOLEAN);
249+
break;
250+
case in_array($var, self::$isBoolOrInt, true) :
251+
if (is_numeric($value)) {
252+
$values[$var] = (int) $value;
253+
break;
254+
}
255+
$values[$var] = (bool) filter_var($value, FILTER_VALIDATE_BOOLEAN);
256+
break;
257+
case in_array($var, self::$isMod, true) :
258+
$check = $this->checkMod($value);
259+
is_null($check) or $values[$var] = $check;
260+
break;
246261
}
247-
$normValue = (bool) filter_var($normValue, FILTER_VALIDATE_BOOLEAN);
248-
break;
249-
case in_array($normName, self::$isMod, true) :
250-
$normValue = self::checkMod($normValue);
251-
break;
262+
}
252263
}
253264

254-
return array($normName, $normValue);
265+
return $values;
255266
}
256267

257268
/**
@@ -260,7 +271,7 @@ private static function normalise($name, $value)
260271
* @param string $mod
261272
* @return int|null
262273
*/
263-
private static function checkMod($mod)
274+
private function checkMod($mod)
264275
{
265276
if ($mod[0] === '0') {
266277
$mod = substr($mod, 1);
@@ -270,4 +281,4 @@ private static function checkMod($mod)
270281
? octdec($mod)
271282
: null;
272283
}
273-
}
284+
}

wpstarter/src/Env/Loader.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/*
3+
* This file is part of the wpstarter package.
4+
*
5+
* (c) Giuseppe Mazzapica <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace WCM\WPStarter\Env;
12+
13+
use Dotenv\Loader as DotenvLoader;
14+
15+
/**
16+
* @author Giuseppe Mazzapica <[email protected]>
17+
* @license http://opensource.org/licenses/MIT MIT
18+
* @package wpstarter
19+
*/
20+
final class Loader extends DotenvLoader
21+
{
22+
private $allVars = array();
23+
24+
/**
25+
* Set variable using Dotenv loader and store the name in class var
26+
*
27+
* @param string $name
28+
* @param mixed $value
29+
*/
30+
public function setEnvironmentVariable($name, $value = null)
31+
{
32+
list($name, $value) = $this->normaliseEnvironmentVariable($name, $value);
33+
34+
if ($this->immutable === true && ! is_null($this->getEnvironmentVariable($name))) {
35+
return;
36+
}
37+
38+
putenv("$name=$value");
39+
$_ENV[$name] = $value;
40+
$_SERVER[$name] = $value;
41+
$this->allVars[] = $name;
42+
}
43+
44+
/**
45+
* @return array
46+
*/
47+
public function allVarNames()
48+
{
49+
return $this->allVars;
50+
}
51+
}

wpstarter/src/Helpers.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,33 @@
1919
*/
2020
class Helpers
2121
{
22-
private static $env_loaded = false;
23-
2422
/**
2523
* Load all the environment variables using Dotenv class and return them.
2624
*
27-
* @param $dir
25+
* @param string $dir
26+
* @param string $file
2827
* @return array
2928
*/
30-
public static function settings($dir)
29+
public static function settings($dir, $file = '.env')
3130
{
32-
Env::load($dir);
33-
Env::required(array('DB_NAME', 'DB_USER', 'DB_PASSWORD'));
34-
self::$env_loaded = true;
31+
$env = Env\Env::load($dir, $file);
32+
33+
$settings = $env->allVars();
34+
35+
$required = array(
36+
'DB_NAME',
37+
'DB_USER',
38+
'DB_PASSWORD',
39+
);
40+
41+
foreach ($required as $key) {
42+
if (! isset($settings[$key]) || empty($settings[$key])) {
43+
$names = implode(', ', $required);
44+
throw new \RuntimeException($names.' environment variables are required.');
45+
}
46+
}
3547

36-
return Env::all();
48+
return $settings;
3749
}
3850

3951
/**

wpstarter/src/Setup.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ private function normalisePaths(array $paths)
212212
*/
213213
private function subdir($root, $path)
214214
{
215-
return trim(preg_replace('|^'.preg_quote(realpath($root)).'|', '', realpath($path)), '\\/');
215+
$paths = $this->normalisePaths(array($root, $path));
216+
217+
return trim(preg_replace('|^'.preg_quote($paths[0]).'|', '', $paths[1]), '\\/');
216218
}
217219
}

wpstarter/src/Setup/Config.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Config implements ArrayAccess
2323
private static $defaults = array(
2424
'gitignore' => true,
2525
'env-example' => true,
26+
'env-file' => '.env',
2627
'move-content' => false,
2728
'register-theme-folder' => true,
2829
'prevent-overwrite' => array('.gitignore'),
@@ -52,13 +53,15 @@ public function __construct($configs)
5253
* @see \WCM\WPStarter\Setup\Config::validatePathArray()
5354
* @see \WCM\WPStarter\Setup\Config::validateOverwrite()
5455
* @see \WCM\WPStarter\Setup\Config::validateVerbosity()
56+
* @see \WCM\WPStarter\Setup\Config::validateFilename()
5557
*/
5658
private function validate($configs)
5759
{
5860
$valid = array('is-root' => $configs['is-root'], 'wp-version' => $configs['wp-version']);
5961
$map = array(
6062
'gitignore' => array($this, 'validateGitignore'),
6163
'env-example' => array($this, 'validateBoolOrAskOrUrl'),
64+
'env-file' => array($this, 'validateFilename'),
6265
'register-theme-folder' => array($this, 'validateBoolOrAsk'),
6366
'move-content' => array($this, 'validateBoolOrAsk'),
6467
'dropins' => array($this, 'validatePathArray'),
@@ -191,6 +194,17 @@ private function validateUrl($value)
191194
return filter_var($value, FILTER_SANITIZE_URL) ?: null;
192195
}
193196

197+
/**
198+
* @param $value
199+
* @return string|null
200+
*/
201+
private function validateFilename($value)
202+
{
203+
$filtered = filter_var($value, FILTER_SANITIZE_URL) ?: null;
204+
205+
return $filtered ? basename($filtered) : null;
206+
}
207+
194208
/**
195209
* @param $value
196210
* @return bool

0 commit comments

Comments
 (0)