da vereid/drupal-environment

提供用于处理环境变量和Drupal托管提供商的辅助工具。

1.0.0-beta2 2023-06-27 15:54 UTC

This package is auto-updated.

Last update: 2024-09-04 17:27:22 UTC


README

CI Maintainability Test Coverage Packagist

提供用于处理Drupal环境和环境变量的类。

这还标准化了不同托管提供商之间的某些环境术语,以便您可以在不同的主机上使用相同的代码。

基本用法

获取环境变量

use DrupalEnvironment\Environment;
$value = Environment::get('VARIABLE_NAME');

使用此方法的优势是结果被静态缓存。

测试Drupal托管或CI环境

use DrupalEnvironment\Environment;

// These all return a boolean true/false
Environment::isPantheon();
Environment::isAcquia();
Environment::isTugboat();
Environment::isGitHubWorkflow();
Environment::isGitLabCi();
Environment::isCircleCi();

测试特定环境

use DrupalEnvironment\Environment;

// This gets the specific environment string.
$environment = Environment::getEnvironment();

// These all return a boolean true/false
Environment::isProduction();
Environment::isStaging();
Environment::isDevelopment();
Environment::isCi();
Environment::isLocal(); // Covers both DDEV and Lando
Environment::isDdev();
Environment::isLando();

测试可执行命令

use DrupalEnvironment\Environment;

// This returns a boolean true/false:
Environment::commandExists('composer');

示例用法

settings.php

use DrupalEnvironment\Environment;

if (Environment::isProduction()) {
  // Set some production environment settings overrides.
}
elseif (Environment::isStaging()) {
  // Set some staging environment settings overrides.
}
elseif (Environment::isDevelopment()) {
  // Set some development environment settings overrides.
}
elseif (Environment::isLocal()) {
  // Set some development environment settings overrides.
}

// Include a environment-specific settings file.
if ($environment = Environment::getEnvironment()) {
  $settings_file = 'settings.' . $environment . '.php';
  if (is_file($settings_file)) {
    require_once $settings_file;
  }
}