From ddd6e29712807baa23918afd734117038655782e Mon Sep 17 00:00:00 2001 From: Austin Pray Date: Sat, 4 Aug 2018 18:47:37 -0500 Subject: [PATCH 01/11] Use static configuration class on top of define() --- composer.json | 5 ++ config/application.php | 72 ++++++++++------- config/environments/development.php | 13 +++- config/environments/production.php | 9 +-- config/environments/staging.php | 9 +-- lib/Bedrock/Config.php | 77 +++++++++++++++++++ .../ConstantAlreadyDefinedException.php | 13 ++++ 7 files changed, 154 insertions(+), 44 deletions(-) create mode 100644 lib/Bedrock/Config.php create mode 100644 lib/Bedrock/ConstantAlreadyDefinedException.php diff --git a/composer.json b/composer.json index 791c9c93b5..9d247636ab 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,11 @@ "url": "https://wpackagist.org" } ], + "autoload": { + "psr-4": { + "Roots\\Bedrock\\": "lib/Bedrock" + } + }, "require": { "php": ">=5.6", "composer/installers": "^1.4", diff --git a/config/application.php b/config/application.php index aecdc8b91f..511b6dcaa6 100644 --- a/config/application.php +++ b/config/application.php @@ -1,6 +1,9 @@ $value) { + if (defined($key)) { + $previous = constant($key); + $message = "Bedrock is trying to define '$key' as '$value' but it is already set to '$previous'."; + throw new ConstantAlreadyDefinedException($message); + } + define($key, $value); + } + } +} diff --git a/lib/Bedrock/ConstantAlreadyDefinedException.php b/lib/Bedrock/ConstantAlreadyDefinedException.php new file mode 100644 index 0000000000..9037776cd6 --- /dev/null +++ b/lib/Bedrock/ConstantAlreadyDefinedException.php @@ -0,0 +1,13 @@ + Date: Sun, 5 Aug 2018 08:59:12 -0500 Subject: [PATCH 02/11] Import clobbered docblock --- config/application.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/application.php b/config/application.php index 511b6dcaa6..21cffd95b6 100644 --- a/config/application.php +++ b/config/application.php @@ -1,9 +1,9 @@ Date: Sun, 5 Aug 2018 09:00:33 -0500 Subject: [PATCH 03/11] Add examples to staging --- config/environments/staging.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/environments/staging.php b/config/environments/staging.php index 26c6a9106d..82d65c9c9f 100644 --- a/config/environments/staging.php +++ b/config/environments/staging.php @@ -2,3 +2,6 @@ /** Configuration Overrides for Staging */ use Roots\Bedrock\Config; + +// Config::define('WP_DEBUG', true); +// ini_set('display_errors', 1); From 7cc6b3ecb47c66d57ccb5daf86cbd8091b2dd9bd Mon Sep 17 00:00:00 2001 From: Austin Pray Date: Sun, 5 Aug 2018 10:37:07 -0500 Subject: [PATCH 04/11] Additional safety and idempotence --- config/application.php | 1 - lib/Bedrock/Config.php | 37 +++++++++++++++++---- lib/Bedrock/UndefinedConfigKeyException.php | 8 +++++ 3 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 lib/Bedrock/UndefinedConfigKeyException.php diff --git a/config/application.php b/config/application.php index 21cffd95b6..4ee77c2c8c 100644 --- a/config/application.php +++ b/config/application.php @@ -2,7 +2,6 @@ use Roots\Bedrock\Config; - /** @var string Directory containing all of the site's files */ $root_dir = dirname(__DIR__); diff --git a/lib/Bedrock/Config.php b/lib/Bedrock/Config.php index 2b180ee0f3..a4769d7468 100644 --- a/lib/Bedrock/Config.php +++ b/lib/Bedrock/Config.php @@ -16,21 +16,28 @@ class Config /** * @param string $key * @param $value + * @throws ConstantAlreadyDefinedException */ public static function define($key, $value) { - self::$configMap[$key] = $value; + self::defined($key) or self::$configMap[$key] = $value; } /** * @param string $key * @return mixed + * @throws UndefinedConfigKeyException */ public static function get($key) { + if (!array_key_exists($key, self::$configMap)) { + $class = self::class; + throw new UndefinedConfigKeyException("'$key' has not been set by $class::define('$key', ...)"); + } + return self::$configMap[$key]; } - + /** * @param string $key */ @@ -66,12 +73,28 @@ public static function remove($key) public static function apply() { foreach (self::$configMap as $key => $value) { - if (defined($key)) { - $previous = constant($key); - $message = "Bedrock is trying to define '$key' as '$value' but it is already set to '$previous'."; - throw new ConstantAlreadyDefinedException($message); + try { + self::defined($key) or define($key, $value); + } catch (ConstantAlreadyDefinedException $e) { + if (constant($key) !== $value) { + throw $e; + } } - define($key, $value); } } + + /** + * @param $key + * @return bool + * @throws ConstantAlreadyDefinedException + */ + private static function defined($key) + { + if (defined($key)) { + $message = "Bedrock aborted trying to redefine constant '$key'"; + throw new ConstantAlreadyDefinedException($message); + } + + return false; + } } diff --git a/lib/Bedrock/UndefinedConfigKeyException.php b/lib/Bedrock/UndefinedConfigKeyException.php new file mode 100644 index 0000000000..602af7b1d2 --- /dev/null +++ b/lib/Bedrock/UndefinedConfigKeyException.php @@ -0,0 +1,8 @@ + Date: Sun, 5 Aug 2018 10:39:31 -0500 Subject: [PATCH 05/11] Use imported class name --- config/application.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/application.php b/config/application.php index 4ee77c2c8c..542fb28ed7 100644 --- a/config/application.php +++ b/config/application.php @@ -88,7 +88,7 @@ require_once $env_config; } -Roots\Bedrock\Config::apply(); +Config::apply(); /** * Bootstrap WordPress From caa7029c07973d515c09645a83cc381ee05393d0 Mon Sep 17 00:00:00 2001 From: Austin Pray Date: Sun, 5 Aug 2018 15:28:28 -0500 Subject: [PATCH 06/11] Don't half-apply config --- lib/Bedrock/Config.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/Bedrock/Config.php b/lib/Bedrock/Config.php index a4769d7468..41009a7a58 100644 --- a/lib/Bedrock/Config.php +++ b/lib/Bedrock/Config.php @@ -72,19 +72,27 @@ public static function remove($key) */ public static function apply() { + // Scan configMap to see if user is trying to redefine any constants. + // We do this because we don't want to 'half apply' the configMap. The user should be able to catch the + // exception, repair their config, and run apply() again foreach (self::$configMap as $key => $value) { try { - self::defined($key) or define($key, $value); + self::defined($key); } catch (ConstantAlreadyDefinedException $e) { if (constant($key) !== $value) { throw $e; } } } + + // If all is well, apply the configMap ignoring entries that have already been applied + foreach (self::$configMap as $key => $value) { + defined($key) or define($key, $value); + } } /** - * @param $key + * @param string $key * @return bool * @throws ConstantAlreadyDefinedException */ From ca5728ca2127626472e413e7cfe91a325703a2ac Mon Sep 17 00:00:00 2001 From: Austin Pray Date: Tue, 7 Aug 2018 07:45:48 -0500 Subject: [PATCH 07/11] Moves Config and friends to roots/wp-config https://github.com/roots/wp-config --- composer.json | 3 +- composer.lock | 48 +++++++- config/application.php | 2 +- config/environments/development.php | 2 +- config/environments/production.php | 2 +- config/environments/staging.php | 2 +- lib/Bedrock/Config.php | 108 ------------------ .../ConstantAlreadyDefinedException.php | 13 --- lib/Bedrock/UndefinedConfigKeyException.php | 8 -- 9 files changed, 52 insertions(+), 136 deletions(-) delete mode 100644 lib/Bedrock/Config.php delete mode 100644 lib/Bedrock/ConstantAlreadyDefinedException.php delete mode 100644 lib/Bedrock/UndefinedConfigKeyException.php diff --git a/composer.json b/composer.json index 9d247636ab..a9e1009889 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,8 @@ "vlucas/phpdotenv": "^2.0.1", "johnpbloch/wordpress": "4.9.8", "oscarotero/env": "^1.1.0", - "roots/wp-password-bcrypt": "1.0.0" + "roots/wp-password-bcrypt": "1.0.0", + "roots/wp-config": "dev-master" }, "require-dev": { "squizlabs/php_codesniffer": "^3.0.2" diff --git a/composer.lock b/composer.lock index ea6f569d25..3fb3414d2b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "718ce52178ac33df2abbfdfb174d2b74", + "content-hash": "71f7c19c5bb3dbca29d1dfc2d60ca642", "packages": [ { "name": "composer/installers", @@ -296,6 +296,48 @@ ], "time": "2017-07-17T20:41:59+00:00" }, + { + "name": "roots/wp-config", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/roots/wp-config.git", + "reference": "6e863699ec80d749a9d9deb766abb65ef621a6b9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/roots/wp-config/zipball/6e863699ec80d749a9d9deb766abb65ef621a6b9", + "reference": "6e863699ec80d749a9d9deb766abb65ef621a6b9", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "require-dev": { + "php-coveralls/php-coveralls": "^2.1", + "phpunit/phpunit": "^5.7", + "roave/security-advisories": "dev-master", + "squizlabs/php_codesniffer": "^3.3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Roots\\WPConfig\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Austin Pray", + "email": "austin@austinpray.com" + } + ], + "description": "Collect configuration values and safely define() them", + "time": "2018-08-07T06:11:42+00:00" + }, { "name": "roots/wp-password-bcrypt", "version": "1.0.0", @@ -459,7 +501,9 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "roots/wp-config": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/config/application.php b/config/application.php index 542fb28ed7..7fac505108 100644 --- a/config/application.php +++ b/config/application.php @@ -1,6 +1,6 @@ $value) { - try { - self::defined($key); - } catch (ConstantAlreadyDefinedException $e) { - if (constant($key) !== $value) { - throw $e; - } - } - } - - // If all is well, apply the configMap ignoring entries that have already been applied - foreach (self::$configMap as $key => $value) { - defined($key) or define($key, $value); - } - } - - /** - * @param string $key - * @return bool - * @throws ConstantAlreadyDefinedException - */ - private static function defined($key) - { - if (defined($key)) { - $message = "Bedrock aborted trying to redefine constant '$key'"; - throw new ConstantAlreadyDefinedException($message); - } - - return false; - } -} diff --git a/lib/Bedrock/ConstantAlreadyDefinedException.php b/lib/Bedrock/ConstantAlreadyDefinedException.php deleted file mode 100644 index 9037776cd6..0000000000 --- a/lib/Bedrock/ConstantAlreadyDefinedException.php +++ /dev/null @@ -1,13 +0,0 @@ - Date: Wed, 8 Aug 2018 12:11:30 -0500 Subject: [PATCH 08/11] Stray autoload --- composer.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/composer.json b/composer.json index a9e1009889..5c2a14e69a 100644 --- a/composer.json +++ b/composer.json @@ -32,11 +32,6 @@ "url": "https://wpackagist.org" } ], - "autoload": { - "psr-4": { - "Roots\\Bedrock\\": "lib/Bedrock" - } - }, "require": { "php": ">=5.6", "composer/installers": "^1.4", From 1e6ff6c44976fbafaf7e29010e08c834b2d08ac6 Mon Sep 17 00:00:00 2001 From: Austin Pray Date: Fri, 10 Aug 2018 00:00:58 -0500 Subject: [PATCH 09/11] Updates comments --- config/application.php | 11 +++++++++-- config/environments/development.php | 5 ++++- config/environments/production.php | 4 ---- config/environments/staging.php | 14 +++++++++++--- 4 files changed, 24 insertions(+), 10 deletions(-) delete mode 100644 config/environments/production.php diff --git a/config/application.php b/config/application.php index 7fac505108..269b977947 100644 --- a/config/application.php +++ b/config/application.php @@ -1,4 +1,12 @@ Date: Fri, 10 Aug 2018 00:25:09 -0500 Subject: [PATCH 10/11] Comment style --- config/environments/development.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config/environments/development.php b/config/environments/development.php index a191b570b5..d29ef6a43e 100644 --- a/config/environments/development.php +++ b/config/environments/development.php @@ -1,5 +1,7 @@ Date: Fri, 10 Aug 2018 22:45:21 -0500 Subject: [PATCH 11/11] Use wp-config 1.0.0 --- composer.json | 2 +- composer.lock | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 077bfdb3af..bef8d27e62 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "johnpbloch/wordpress": "4.9.8", "oscarotero/env": "^1.1.0", "roots/wp-password-bcrypt": "1.0.0", - "roots/wp-config": "dev-master" + "roots/wp-config": "1.0.0" }, "require-dev": { "squizlabs/php_codesniffer": "^3.0.2", diff --git a/composer.lock b/composer.lock index e3ddebaa0a..5a2ba15788 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "855f209e9a5ebd34b2bfcd9f71fcf022", + "content-hash": "01a9da7555b27b63a86fafda2eba0f15", "packages": [ { "name": "composer/installers", @@ -298,16 +298,16 @@ }, { "name": "roots/wp-config", - "version": "dev-master", + "version": "1.0.0", "source": { "type": "git", "url": "https://github.com/roots/wp-config.git", - "reference": "ebdfab7a7e595306e5416ca4ddf3ecb51e4ef094" + "reference": "37c38230796119fb487fa03346ab0706ce6d4962" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/roots/wp-config/zipball/ebdfab7a7e595306e5416ca4ddf3ecb51e4ef094", - "reference": "ebdfab7a7e595306e5416ca4ddf3ecb51e4ef094", + "url": "https://api.github.com/repos/roots/wp-config/zipball/37c38230796119fb487fa03346ab0706ce6d4962", + "reference": "37c38230796119fb487fa03346ab0706ce6d4962", "shasum": "" }, "require": { @@ -336,7 +336,7 @@ } ], "description": "Collect configuration values and safely define() them", - "time": "2018-08-08T14:49:26+00:00" + "time": "2018-08-10T14:18:38+00:00" }, { "name": "roots/wp-password-bcrypt", @@ -662,7 +662,6 @@ "aliases": [], "minimum-stability": "stable", "stability-flags": { - "roots/wp-config": 20, "roave/security-advisories": 20 }, "prefer-stable": false,