Skip to content

Commit

Permalink
Merge pull request #9 from druidfi/get-site-dir-automatically
Browse files Browse the repository at this point in the history
Get site path automatically without __DIR__
  • Loading branch information
back-2-95 authored Jul 19, 2022
2 parents 5ecf332 + f0cb74e commit f60cc11
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ And then use this as your `sites/default/settings.php`:
<?php

// Use druidfi/omen
extract((new Druidfi\Omen\Reader())->get());
extract(Druidfi\Omen\Reader::get(get_defined_vars()));
```

Or print out all configuration (aka debug):
Expand All @@ -33,7 +33,7 @@ Or print out all configuration (aka debug):
<?php

// Print out detected configuration by druidfi/omen
(new Druidfi\Omen\Reader())->show();
Druidfi\Omen\Reader::show(get_defined_vars());
```

See the whole example [here](settings.php).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 9 additions & 1 deletion src/DrupalEnvDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<h1>🔥 BC BREAK IN OMEN! 🔥</h1>";
echo "<p>Update your call in settings.php in ". $settings_dir ."</p>";
echo "<p><strong>Old line:</strong>";
echo "<p><code>extract((new Druidfi\Omen\DrupalEnvDetector(__DIR__))->getConfiguration());</code>";
echo "<p><strong>New line:</strong>";
echo "<p><code>extract((new Druidfi\Omen\Reader(__DIR__))->get());</code>";
echo "<p><code>extract(Druidfi\Omen\Reader::get(get_defined_vars()));</code>";
echo '<p><img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBYWFRgWFhUZGBgaHBwaGhwYGhoYHh4cGiEeHBwcGhohIS4lHh4rHx4eJzgmKy8xNTU1HiQ7QDs0Py40NTEBDAwMEA8QHhISGjQhISE0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0ND80NTQxPzQ0Mf/AABEIAL4BCQMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAACBAEDBQAGB//EAEcQAAIBAgMEBwUFBAYKAwAAAAECAAMRBBIhBTFB8AYiUWFxgZEHE6GxwRQystHhNXJzdCNCUmKz8SQlMzRUY4KSoqMVFnX/xAAYAQADAQEAAAAAAAAAAAAAAAAAAQIDBP/EAB8RAQEBAQACAwEBAQAAAAAAAAABAhESIQMxQRNRIv/aAAwDAQACEQMRAD8A+kMsHLLWlYHPpK6YXp3ExcTQsd035n43Dkm47JpjRanYUoUgb3HCRUwo4RgC3DsgteaSp9E8gvKXWxl1VTKmE0jOoppeMvTFu+Dhzbzjy0xaRq8q5PTGdbQJufZV7JRU2eOBjnyC4ZMEzRq4MAb4hiXVFzN4eesrz9dR4dvFZPfIMxamPbOCT1b9m7w+HpNthHnXkfyY8fQLSJzOoIBIHmJMrvtnc2IkSTIglE4SbToGgyBJIkxwIM4QmnAaw6aJ0ID4TlHPPP1WtKTm+UkEzgunlCCzMcEqc+kZpCLqbXlgiq40sO+sZzDsmXQ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hmCi2YjJoJXgcEahIzWUb+3ynoMPhlpjqqB89O0zbGOztY71PqfirG7Y2nWRqa08Ph8wKtUFRqzKpBBKJlUZuwk2mbszA19nG+By1EZVFShVbLnZFy+8RwLK5sLgix9Juk7+6CTNPCM/IviOlO0KgK0sElBjp7yrWWoF71pot2NtRc20lextnihTFMMzm7M7H7zu5LO57yfgI5bnztKsQl0Zf7SlT4MLHTwJhMye4Os7o7i8fg8OuGXC0agQuQ7VmQsHdnuVyG33rb+EQ2VszF4b7LVSmlR6QxAdGdkX/AEhs/VfKb5bW3az0DUmN9V1JbcbXNQ1RfXhYDy9KKmHJFgw3AAkEnSiaY1zf8wt4+smZHSuLfF4nE4arWoU6S0Fq2yVGqFvehRuyC1su/vlewsXj8Lhhhlw1GoqZ1DmsylszM18uQ2Ou6/CaWL6t6jm63BygE/10OozAH7g9PSpaTFVbMFJQjS+/rga3FwDVJ8uEVz+DqnYmBejh6VN/vIiq1r2uO+PhDuym+7jv/OUVaTf2gBcHQEWXLTWw62+yD1M5aZuCCo6xqHQ3OZxUI+9/dC630lezWhTwF9baa69kgIddD6X9fT4SmnTKlSCAQFBIBuQjMwF82gLNc9tu/SadAqqKMtlA4HUhWW517XB/6YewsKG17G1ib8LcTK7ytqRINyLFcm469VEuetY6J8ZZl8PSHKH/2Q==" width="251" height="180" alt="YOU CAN DO IT! - Supportive Hedgehog | Meme Generator"></p>';
exit;
}
Expand Down
22 changes: 17 additions & 5 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Reader
];

private $app_env;
private ?string $app_root;
private ?array $config = [];
private ?array $databases = [];
private string $drupal_version;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions tests/BaseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit f60cc11

Please sign in to comment.