Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2023 tweaks #11

Merged
merged 26 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1bf6b3e
Set default Drupal version as 10.0.0 (for tests)
back-2-95 May 20, 2023
b1a4859
true/false case and use of null coalescing operator for default values
back-2-95 May 20, 2023
a029049
Update constraint for friendsofphp/php-cs-fixer
back-2-95 May 20, 2023
3498d3b
Run tests also with PHP 8.2
back-2-95 May 20, 2023
0cacc9b
Update README.md
back-2-95 May 20, 2023
9639e41
Use PHP 8.1 in local tests
back-2-95 May 20, 2023
071ced6
Remove old DrupalEnvDetector.php
back-2-95 May 20, 2023
1b65755
Fix typo
back-2-95 May 20, 2023
847ab17
Drop PHP 7.4 support
back-2-95 May 21, 2023
8f54d32
Combine defaults to one class and use match(), Add new Features class…
back-2-95 May 21, 2023
6f44b20
assertTrue
back-2-95 May 22, 2023
5167b5b
Add test setup for unknown system
back-2-95 May 22, 2023
61269ce
Refactor testing
back-2-95 May 22, 2023
f996043
Update README.md with latest changes
back-2-95 May 22, 2023
2771623
Rename env to system
back-2-95 May 22, 2023
2c94eb6
Small tweaks
back-2-95 Nov 24, 2023
5d44278
Add PHP 8.3 to tests and use GHA's new syntax
back-2-95 Nov 24, 2023
b2e5716
Use "verbose" as new default value for error level in dev
back-2-95 Nov 24, 2023
2082978
Update GHA action versions
back-2-95 Nov 24, 2023
52b900f
Update PHPUnit tests
back-2-95 Nov 24, 2023
e65b587
Fix some settings
back-2-95 Nov 24, 2023
0bb20a0
Remove support for Drupal 9
back-2-95 Nov 24, 2023
0ec5871
Fix typo in composer.json
back-2-95 Nov 24, 2023
003cbcf
Features class is not yet ready.
back-2-95 Nov 24, 2023
24074a3
Use friendsofphp/php-cs-fixer v3 only
back-2-95 Nov 24, 2023
80ca1a4
Don't allow phpunit 10
back-2-95 Nov 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Combine defaults to one class and use match(), Add new Features class…
… to detect additional services.
  • Loading branch information
back-2-95 committed May 21, 2023
commit 8f54d32713e718381152fe664bc70804797476be
58 changes: 58 additions & 0 deletions src/Defaults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Druidfi\Omen;

class Defaults
{
private array $config = [];

private array $settings = [
'config_exclude_modules' => [
'devel',
'stage_file_proxy',
'upgrade_status',
],
];

public function __construct(string $app_env)
{
$this->config['system.logging']['error_level'] = match ($app_env) {
'dev' => 'all',
default => 'hide',
};

$this->config['system.performance'] = match ($app_env) {
'dev' => [
'cache' => ['page' => ['max_age' => 0]],
'css' => ['preprocess' => 0],
'js' => ['preprocess' => 0],
],
default => [
'cache' => ['page' => ['max_age' => 900]],
'css' => ['preprocess' => 1],
'js' => ['preprocess' => 1],
],
};

$this->settings['skip_permissions_hardening'] = match ($app_env) {
'dev' => TRUE,
default => FALSE,
};

// Simple Environment Indicator.
$this->settings['simple_environment_indicator'] = match ($app_env) {
'dev' => 'Black Development',
'test' => 'Blue Testing',
'prod' => 'DarkRed Production',
default => FALSE,
};
}

public function getDefaults() : array
{
return [
'config' => $this->config,
'settings' => $this->settings,
];
}
}
31 changes: 0 additions & 31 deletions src/EnvDefaults/AbstractDefaults.php

This file was deleted.

33 changes: 0 additions & 33 deletions src/EnvDefaults/DevDefaults.php

This file was deleted.

33 changes: 0 additions & 33 deletions src/EnvDefaults/ProdDefaults.php

This file was deleted.

33 changes: 0 additions & 33 deletions src/EnvDefaults/TestDefaults.php

This file was deleted.

53 changes: 53 additions & 0 deletions src/Features.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Druidfi\Omen;

class Features
{
public bool $redis = false;
public bool $solr = false;

private array $redisEnvVariables = [
'REDIS_HOST', // Lagoon
];

private array $solrEnvVariables = [
'SOLR_HOST', // Lagoon
];

public function __construct()
{
$this->detectRedis();
$this->detectSolr();
}

public function hasRedis(): bool
{
return $this->redis;
}

public function hasSolr(): bool
{
return $this->solr;
}

private function detectRedis(): void
{
foreach ($this->redisEnvVariables as $env) {
if (getenv($env)) {
$this->redis = true;
break;
}
}
}

private function detectSolr(): void
{
foreach ($this->solrEnvVariables as $env) {
if (getenv($env)) {
$this->solr = true;
break;
}
}
}
}
36 changes: 20 additions & 16 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Druidfi\Omen\EnvMapping\Pantheon;
use Druidfi\Omen\EnvMapping\Tugboat;
use Druidfi\Omen\EnvMapping\Wodby;
use JetBrains\PhpStorm\NoReturn;
use ReflectionClass;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -29,10 +30,10 @@ class Reader
'WODBY_INSTANCE_TYPE' => Wodby::class,
];

private $app_env;
private string $app_env;
private ?string $app_root;
private ?array $config = [];
private ?array $databases = [];
private array $config = [];
private array $databases = [];
private string $drupal_version;

/**
Expand Down Expand Up @@ -72,13 +73,15 @@ public function __construct(array $vars)
}
}

$features = new Features();

// Set mapped env variables IF we have detected something
if (!is_null($this->omen)) {
if ($this->omen) {
foreach ($this->omen->getEnvs() as $env_var => $env_val) {
putenv($env_var . '=' . $env_val);
}

// Set Env specific configuration
// Set system specific configuration
$this->omen->setConfiguration($config, $settings);
}
else {
Expand Down Expand Up @@ -125,7 +128,7 @@ public function __construct(array $vars)
$this->setDatabaseConnection();
}

public static function get(array $vars) : array
public static function get(array $vars): array
{
return (new Reader($vars))->getConf();
}
Expand All @@ -135,7 +138,7 @@ public static function get(array $vars) : array
*
* @return array
*/
public function getConf() : array
public function getConf(): array
{
$conf = [
'config' => $this->config,
Expand All @@ -159,13 +162,15 @@ public function getConf() : array
/**
* Print out configuration.
*/
public static function show(array $vars)
#[NoReturn]
public static function show(array $vars): void
{
$reader = new Reader($vars);
$reader->printConfiguration($reader->getConf());
}

protected function printConfiguration($conf)
#[NoReturn]
protected function printConfiguration($conf): void
{
$omen = is_null($this->omen) ? '[NOT_ANY_DETECTED_SYSTEM]' : get_class($this->omen);
echo '<h1>Drupal: '. $this->drupal_version .', APP_ENV: '. $this->app_env .' on '. $omen .'</h1>';
Expand All @@ -183,10 +188,9 @@ protected function printConfiguration($conf)
/**
* Set ENV specific default values.
*/
private function setEnvDefaults()
private function setEnvDefaults(): void
{
$class = "Druidfi\Omen\EnvDefaults\\". ucfirst($this->app_env) ."Defaults";
$env_defaults = (new $class())->getDefaults();
$env_defaults = (new Defaults($this->app_env))->getDefaults();

foreach ($env_defaults as $set => $values) {
if (!is_array($this->{$set})) {
Expand All @@ -200,7 +204,7 @@ private function setEnvDefaults()
/**
* Set global values. Same for all environments.
*/
private function setGlobalDefaults()
private function setGlobalDefaults(): void
{
// Set directory for loading CMI configuration.
$this->settings['config_sync_directory'] = getenv('DRUPAL_SYNC_DIR')
Expand All @@ -226,7 +230,7 @@ private function setGlobalDefaults()
*
* @see https://www.drupal.org/node/2410395
*/
private function setTrustedHostPatterns()
private function setTrustedHostPatterns(): void
{
if (!isset($this->settings['trusted_host_patterns'])) {
$this->settings['trusted_host_patterns'] = [];
Expand Down Expand Up @@ -263,7 +267,7 @@ private function setTrustedHostPatterns()
putenv('DRUSH_OPTIONS_URI=https://' . $hosts[0]);
}

if (!is_null($this->omen) && method_exists($this->omen, 'getTrustedHostPatterns')) {
if ($this->omen && method_exists($this->omen, 'getTrustedHostPatterns')) {
$patterns = $this->omen->getTrustedHostPatterns();
$this->settings['trusted_host_patterns'] = array_merge($this->settings['trusted_host_patterns'], $patterns);
}
Expand All @@ -272,7 +276,7 @@ private function setTrustedHostPatterns()
/**
* Set database connection.
*/
private function setDatabaseConnection()
private function setDatabaseConnection(): void
{
// DRUPAL_DB_* should be defined at this point.
$this->databases['default']['default'] = [
Expand Down