From 47468a99e03a9b37bc09089c7955211439b84d54 Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Tue, 19 Jul 2022 11:19:47 +0300 Subject: [PATCH 1/2] Refactor: use static function get & show --- README.md | 4 ++-- settings.php | 2 +- src/DrupalEnvDetector.php | 10 +++++++++- src/Reader.php | 22 +++++++++++++++++----- tests/BaseCase.php | 5 ++--- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 123b8a4..ab55780 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ And then use this as your `sites/default/settings.php`: get()); +extract(Druidfi\Omen\Reader::get(get_defined_vars())); ``` Or print out all configuration (aka debug): @@ -33,7 +33,7 @@ Or print out all configuration (aka debug): show(); +Druidfi\Omen\Reader::show(get_defined_vars()); ``` See the whole example [here](settings.php). diff --git a/settings.php b/settings.php index ef0c822..cddf4f1 100644 --- a/settings.php +++ b/settings.php @@ -10,7 +10,7 @@ // // These files are loaded automatically if found. // -extract((new Druidfi\Omen\Reader(__DIR__))->getConfiguration()); +extract(Druidfi\Omen\Reader::get(get_defined_vars())); // Here you can still override things diff --git a/src/DrupalEnvDetector.php b/src/DrupalEnvDetector.php index 07efe49..12a3f8e 100644 --- a/src/DrupalEnvDetector.php +++ b/src/DrupalEnvDetector.php @@ -6,12 +6,20 @@ class DrupalEnvDetector { public function __construct(string $settings_dir) { + if (PHP_SAPI === 'cli') { + echo "🔥 BC BREAK IN OMEN! 🔥\n"; + echo "Update your call in settings.php in ". $settings_dir ."\n"; + echo "Old line: extract((new Druidfi\Omen\DrupalEnvDetector(__DIR__))->getConfiguration());\n"; + echo "New line: extract(Druidfi\Omen\Reader::get(get_defined_vars()));\n\n"; + exit; + } + echo "

🔥 BC BREAK IN OMEN! 🔥

"; echo "

Update your call in settings.php in ". $settings_dir ."

"; echo "

Old line:"; echo "

extract((new Druidfi\Omen\DrupalEnvDetector(__DIR__))->getConfiguration());"; echo "

New line:"; - echo "

extract((new Druidfi\Omen\Reader(__DIR__))->get());"; + echo "

extract(Druidfi\Omen\Reader::get(get_defined_vars()));"; echo '

YOU CAN DO IT! - Supportive Hedgehog | Meme Generator

'; exit; } diff --git a/src/Reader.php b/src/Reader.php index 73063e7..4f297aa 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -30,6 +30,7 @@ class Reader ]; private $app_env; + private ?string $app_root; private ?array $config = []; private ?array $databases = []; private string $drupal_version; @@ -40,10 +41,15 @@ class Reader private $omen; private ?array $settings = []; - public function __construct(string $settings_dir) + public function __construct(array $vars) { - global $config, $databases, $settings; + unset($vars['class_loader']); + extract($vars); + unset($vars); + $settings_dir = $app_root . self::DS . $site_path; + + $this->app_root = $app_root; $this->config = &$config; $this->databases = &$databases; $this->settings = &$settings; @@ -114,12 +120,17 @@ public function __construct(string $settings_dir) $this->setDatabaseConnection(); } + public static function get(array $vars) : array + { + return (new Reader($vars))->getConf(); + } + /** * Get read configuration. * * @return array */ - public function get() : array + public function getConf() : array { $conf = [ 'config' => $this->config, @@ -143,9 +154,10 @@ public function get() : array /** * Print out configuration. */ - public function show() + public static function show(array $vars) { - $this->printConfiguration($this->get()); + $reader = new Reader($vars); + $reader->printConfiguration($reader->getConf()); } protected function printConfiguration($conf) diff --git a/tests/BaseCase.php b/tests/BaseCase.php index 400553a..661cf32 100644 --- a/tests/BaseCase.php +++ b/tests/BaseCase.php @@ -30,11 +30,10 @@ abstract class BaseCase extends TestCase protected function setUp(): void { if (!class_exists('Drupal')) { - eval("class Drupal { const VERSION = '9.3.0'; }"); + eval("class Drupal { const VERSION = '9.4.0'; }"); } - $detector = new Reader(__DIR__); - $conf = $detector->get(); + $conf = Reader::get(['app_root' => '/app/public', 'site_path' => 'site/default']); /** @var array $config */ /** @var array $settings */ From f0cb74ef7dc32ab83a45ee4a38ea78abdebb3dfc Mon Sep 17 00:00:00 2001 From: Marko Korhonen Date: Tue, 19 Jul 2022 11:25:14 +0300 Subject: [PATCH 2/2] Bump up the min Drupal core version to 9.3 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9cf935c..198cff0 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ }, "require": { "php": "^7.4 || ^8.0", - "drupal/core-recommended": "^9.2 || ^10.0", + "drupal/core-recommended": "^9.3 || ^10.0", "ext-json": "*" }, "require-dev": {