unleashedtech/dotenv-drupal 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

unleashedtech/dotenv-drupal

Composer 安装命令:

composer require unleashedtech/dotenv-drupal

包简介

README 文档

README

This package extends Symfony's symfony/dotenv component to allow streamlined Drupal config via Environment Variables. Please refer to the Symfony component's documentation about how .env files should be used. It is important to note that .env files are ignored if the APP_ENV var has already been set by the server. For performance purposes, production environments should ideally rely on pre-configured environment variables, rather than environment variable values loaded from .env files.

Installation

composer require unleashedtech/dotenv-drupal

Configuring Drupal

First, you'll need to configure Drupal to use this package.

Drupal Settings & Sites Files

Drupal is typically configured via settings.php files in various directories. To use this package with Drupal, some code will need to be added to the top of the relevant settings.php file(s):

<?php
use UnleashedTech\Drupal\Dotenv\Dotenv;
$dotenv = $dotenv ?? new Dotenv();
$config = $dotenv->getConfig();
$databases = $dotenv->getDatabases();
$settings = $dotenv->getSettings();
Conditional Logic

If conditional logic is required for a given site, such logic is still supported. This package will auto-load various settings.{{environment}}.php, config.{{environment}}.php or databases.{{environment}}.php files, if they exist. For instance, if you need to set a database prefix for staging, you can create databases.staging.php:

<?php
$databases['default']['default']['prefix'] = 'foo_';

Each included file only has the related variable in scope (e.g. config.dev.php only has $config in scope).

Multi-Site Drupal

You can use the default settings file to provide base configuration for a multi-site install:

<?php
use UnleashedTech\Drupal\Dotenv\Dotenv;
$dotenv = new Dotenv();
$dotenv->setSiteName(basename(__DIR__));
require __DIR__ . '/../default/settings.php';

If you'd like, you can manually set the database name for each site via the setDatabaseName method:

<?php
use UnleashedTech\Drupal\Dotenv\Dotenv;
$dotenv = new Dotenv();
$dotenv->setSiteName(basename(__DIR__));
$dotenv->setDatabaseName('foo');
require __DIR__ . '/../default/settings.php';
Using the Multi-Site Default Site

If you need to use the default site as part of your multi-site install, you can allow it by calling the DotEnv::setMultiSiteDefaultSiteAllowed method in default/settings.php:

<?php
use UnleashedTech\Drupal\Dotenv\Dotenv;
$dotenv = $dotenv ?? new Dotenv();
$dotenv->setMultiSiteDefaultSiteAllowed();
$config = $dotenv->getConfig();
$databases = $dotenv->getDatabases();
$settings = $dotenv->getSettings();
Sites Files

This package also provides functionality to configure Drupal's $sites variable via sites.php. The $sites variable is built from data in the DOMAINS & SITES environment variables. You will need to add the following code to sites.php:

<?php
use UnleashedTech\Drupal\Dotenv\Dotenv;
$dotenv = new Dotenv();
$sites = $dotenv->getSites();

Installation Conclusion

That's it! Drupal will now attempt to load essential connection information from Environment Variables.

Configuring Drupal via ENV Variables

This package will provide many default setting & configuration values based on the detected environment. Some of these values can be populated by environment variables.

Environment variables can be set in .env files, or via modifying server configuration. For production environments, environment variables should ideally be defined via server configuration.

Multi-site installations often need config that differs from the default site. This package first checks for variables following the {{ app }}__{{ site }}__{{ var }} naming convention, before falling back to the {{ var }} naming convention.

You can provide site-specific information via namespaced environment variables.

DATABASE_URL

The default database connection can be configured via a DSN:

DATABASE_URL=driver://user:password@host:port/database

For example:

DATABASE_URL=mysql://foo:bar@localhost:3306/baz

For multi-site installations, do not specify a database name nor credentials in the DATABASE_URL variable:

DATABASE_URL=mysql://localhost:3306

For an "earth" Drupal App with a "default" site & an "antarctica" site:

DATABASE_URL=mysql://localhost:3306

EARTH__DEFAULT__DATABASE_USER=foo
EARTH__DEFAULT__DATABASE_PASSWORD=bar

EARTH__ANTARCTICA__DATABASE_USER=baz
EARTH__ANTARCTICA__DATABASE_PASSWORD=qux

The default Drupal App Name is "default". For most use cases, you'll want to set the Drupal App Name in the default settings.php file, as shown below:

<?php
use UnleashedTech\Drupal\Dotenv\Dotenv;
$dotenv = $dotenv ?? new Dotenv();
$dotenv->setAppName('earth');
// ...

The database name for a site can be overridden via definition of a {{ app }}__{{ site }}__DATABASE_NAME environment variable:

EARTH__ANTARCTICA__DATABASE_NAME=ant

FILE_PUBLIC_PATH

Allows you to override the default $settings['file_public_path'] value:

FILE_PUBLIC_PATH=sites/all/files

Drupal expects this path to be relative to DRUPAL_ROOT.

FILE_PRIVATE_PATH

Allows you to override the default $settings['file_private_path'] value:

FILE_PRIVATE_PATH=/private

FILE_TEMP_PATH

Allows you to override the default $settings['file_temp_path'] value:

FILE_TEMP_PATH=/tmp

CONFIG_SYNC_PATH

Allows you to override the default $settings['config_sync_path'] value:

CONFIG_SYNC_PATH=/sync

DOMAINS

A CSV list of domains used by the given environment:

DOMAINS=foo.example,bar.example,baz.example

MAILGUN_URL

The information Drupal should use to authenticate with the Mailgun API.

The "user" in the DSN is the API key.

US URL Example
MAILGUN_URL=https://key-1234567890abcdefghijklmnopqrstu@api.mailgun.net
EU URL Example
MAILGUN_URL=https://key-1234567890abcdefghijklmnopqrstu@api.eu.mailgun.net

PUBLIC

A string allowing the enabling/disabling of Shield module auth functionality.

If true, Shield will not be enabled.

If false, the username will be the App Name & the password will be the Site Name.

Note: Make sure the "Enable Shield" checkbox is checked in Drupal & that config is committed.

SITES

A CSV list of Drupal "sites" (e.g. "subdomains") used by the given environment:

SITES=foo,bar,baz,qux

SALT

A string used for "one-time login links, cancel links, form tokens, etc."

Best Practices Issue

Use drush eval "var_dump(Drupal\Component\Utility\Crypt::randomBytesBase64(55))" to generate one.

SALT=generatedSaltValue

SOLR_URL

The default Solr connection can be configured via a DSN:

SOLR_URL=http://localhost

This package makes several assumptions, which can be overridden via the SOLR_URL DSN. The DSN in the example above is automatically expanded to:

SOLR_URL=http://default@localhost:8983#default

In the expanded example above, the user is the name of the Solr core & the fragment is the Drupal machine name for the connection. Consider revising Solr core & Drupal Solr server machine names to default, so the shorter DSN can be used.

Streamlined environment-dependent configuration of one Solr core is supported at this time.

TRUSTED_HOST_PATTERNS

Optional. A CSV list of patterns specifying trusted hosts.

Start (^) & end ($) characters are added to every pattern, by default.

Fallback Functionality

If the TRUSTED_HOST_PATTERNS variable is not set, this package will populate Drupal's trusted_host_patterns array based on the values of DOMAINS & SITES variables.

Examples

If you have DOMAINS set to foo.example,bar.example, the Drupal trusted_host_patterns settings array would have the following values:

  • ^foo\.example$
  • ^www\.foo\.example$
  • ^bar\.example$
  • ^www\.bar\.example$

If you have DOMAINS set to foo.example & SITES set to bar,baz,qux, the Drupal trusted_host_patterns setting array would have the following values:

  • ^bar\.foo\.example$
  • ^baz\.foo\.example$
  • ^qux\.foo\.example$

Terms

App Name

The machine name of the Drupal App (e.g. "default" or "earth").

Site Name

The site name for a Drupal App Site (e.g. "default" or "antarctica").

Configuration Conclusion

With these few Environment Variables, you will be able to configure Drupal in a streamlined fashion similar to the way Symfony is configured. Support for many more common Drupal features can be expected soon. Please consider creating a Pull Request with features you would like to this package to support.

unleashedtech/dotenv-drupal 适用场景与选型建议

unleashedtech/dotenv-drupal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.44k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 11 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 unleashedtech/dotenv-drupal 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 unleashedtech/dotenv-drupal 我们能提供哪些服务?
定制开发 / 二次开发

基于 unleashedtech/dotenv-drupal 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.44k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 8
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-11-29