Skip to content

Commit 63cf38e

Browse files
committed
Configure Joomla with environment variables
1 parent 95be2c1 commit 63cf38e

File tree

10 files changed

+385
-12
lines changed

10 files changed

+385
-12
lines changed

administrator/includes/framework.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Joomla\CMS\Version;
1313
use Joomla\Utilities\IpHelper;
14+
use Symfony\Component\Dotenv\Dotenv;
1415

1516
// System includes
1617
require_once JPATH_LIBRARIES . '/bootstrap.php';
@@ -38,13 +39,30 @@
3839
exit;
3940
}
4041

42+
// Load .env files
43+
if (file_exists(JPATH_ROOT . '/.env.local.php') || file_exists(JPATH_ROOT . '/.env')) {
44+
(new Dotenv('JOOMLA_ENV', 'JOOMLA_DEBUG'))->bootEnv(JPATH_ROOT . '/.env', 'prod');
45+
}
46+
4147
// Pre-Load configuration. Don't remove the Output Buffering due to BOM issues, see JCode 26026
4248
ob_start();
4349
require_once JPATH_CONFIGURATION . '/configuration.php';
4450
ob_end_clean();
4551

52+
// getenv() is not thread-safe and it can cause segmentaion fault, so we should try $_SERVER first
53+
$envs = !empty($_SERVER) ? $_SERVER : getenv();
54+
4655
// System configuration.
47-
$config = new JConfig();
56+
$config = new JConfig();
57+
$config->error_reporting = $envs['JOOMLA_ERROR_REPORTING'] ?? $config->error_reporting;
58+
$config->debug = (bool) ($envs['JOOMLA_DEBUG'] ?? $config->debug);
59+
if (isset($envs['JOOMLA_LOG_DEPRECATED'])) {
60+
$config->log_deprecated = (int) $envs['JOOMLA_LOG_DEPRECATED'];
61+
}
62+
if (isset($envs['JOOMLA_BEHIND_LOADBALANCER'])) {
63+
$config->behind_loadbalancer = (bool) $envs['JOOMLA_BEHIND_LOADBALANCER'];
64+
}
65+
unset($envs);
4866

4967
// Set the error_reporting, and adjust a global Error Handler
5068
switch ($config->error_reporting) {

api/includes/framework.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Joomla\CMS\Version;
1313
use Joomla\Utilities\IpHelper;
14+
use Symfony\Component\Dotenv\Dotenv;
1415

1516
// System includes
1617
require_once JPATH_LIBRARIES . '/bootstrap.php';
@@ -38,13 +39,30 @@
3839
exit;
3940
}
4041

42+
// Load .env files
43+
if (file_exists(JPATH_ROOT . '/.env.local.php') || file_exists(JPATH_ROOT . '/.env')) {
44+
(new Dotenv('JOOMLA_ENV', 'JOOMLA_DEBUG'))->bootEnv(JPATH_ROOT . '/.env', 'prod');
45+
}
46+
4147
// Pre-Load configuration. Don't remove the Output Buffering due to BOM issues, see JCode 26026
4248
ob_start();
4349
require_once JPATH_CONFIGURATION . '/configuration.php';
4450
ob_end_clean();
4551

52+
// getenv() is not thread-safe and it can cause segmentaion fault, so we should try $_SERVER first
53+
$envs = !empty($_SERVER) ? $_SERVER : getenv();
54+
4655
// System configuration.
47-
$config = new JConfig();
56+
$config = new JConfig();
57+
$config->error_reporting = $envs['JOOMLA_ERROR_REPORTING'] ?? $config->error_reporting;
58+
$config->debug = (bool) ($envs['JOOMLA_DEBUG'] ?? $config->debug);
59+
if (isset($envs['JOOMLA_LOG_DEPRECATED'])) {
60+
$config->log_deprecated = (int) $envs['JOOMLA_LOG_DEPRECATED'];
61+
}
62+
if (isset($envs['JOOMLA_BEHIND_LOADBALANCER'])) {
63+
$config->behind_loadbalancer = (bool) $envs['JOOMLA_BEHIND_LOADBALANCER'];
64+
}
65+
unset($envs);
4866

4967
// Set the error_reporting
5068
switch ($config->error_reporting) {

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"php": "8.1.0"
1515
},
1616
"vendor-dir": "libraries/vendor",
17-
"github-protocols": ["https"]
17+
"github-protocols": [
18+
"https"
19+
]
1820
},
1921
"support": {
2022
"issues": "https://issues.joomla.org/",
@@ -26,7 +28,7 @@
2628
{
2729
"type": "vcs",
2830
"url": "https://github.com/joomla-backports/json-api-php.git",
29-
"no-api": true
31+
"no-api": true
3032
},
3133
{
3234
"type": "vcs",
@@ -109,7 +111,8 @@
109111
"jfcherng/php-diff": "^6.16.2",
110112
"voku/portable-utf8": "dev-joomla-5.3 as 6.0.13",
111113
"php-tuf/php-tuf": "^1.0.2",
112-
"php-debugbar/php-debugbar": "^2.1"
114+
"php-debugbar/php-debugbar": "^2.1",
115+
"symfony/dotenv": "^6.4"
113116
},
114117
"require-dev": {
115118
"phpunit/phpunit": "^9.6.22",
@@ -135,7 +138,7 @@
135138
},
136139
"scripts": {
137140
"post-install-cmd": [
138-
"php build/update_fido_cache.php"
141+
"php build/update_fido_cache.php"
139142
]
140143
}
141144
}

composer.lock

Lines changed: 75 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

env.dev.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
JOOMLA_DEBUG=1
2+
JOOMLA_DEBUG_LANG=0
3+
JOOMLA_DEBUG_LANG_CONST=0
4+
JOOMLA_ERROR_REPORTING='maximum'
5+
JOOMLA_LOG_DEPRECATED=1
6+
JOOMLA_LOG_EVERYTHING=1

env.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# In all environments, the following files are loaded if they exist,
2+
# the latter taking precedence over the former:
3+
#
4+
# * .env contains default values for the environment variables needed by the app
5+
# * .env.local uncommitted file with local overrides
6+
# * .env.$JOOMLA_ENV committed environment-specific defaults
7+
# * .env.$JOOMLA_ENV.local uncommitted environment-specific overrides
8+
#
9+
# Real environment variables win over .env files.
10+
#
11+
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
12+
#
13+
# To set a configuration value use format JOOMLA_$KEY:
14+
#
15+
# JOOMLA_OFFLINE=1
16+
# JOOMLA_SITENAME='Joomla!'
17+
#
18+
# All available configuration values are listed in installation/configuration.php-dist.
19+
20+
JOOMLA_ENV=dev
21+
22+
# Database configuration
23+
#
24+
# JOOMLA_DBTYPE='mysqli'
25+
# JOOMLA_DBHOST='localhost'
26+
# JOOMLA_DBNAME='db'
27+
# JOOMLA_DBUSER='db'
28+
# JOOMLA_DBPASSWORD='db'
29+
# JOOMLA_DBPREFIX='jos_'

includes/framework.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Joomla\CMS\Uri\Uri;
1313
use Joomla\CMS\Version;
1414
use Joomla\Utilities\IpHelper;
15+
use Symfony\Component\Dotenv\Dotenv;
1516

1617
// System includes
1718
require_once JPATH_LIBRARIES . '/bootstrap.php';
@@ -39,13 +40,30 @@
3940
exit;
4041
}
4142

43+
// Load .env files
44+
if (file_exists(JPATH_ROOT . '/.env.local.php') || file_exists(JPATH_ROOT . '/.env')) {
45+
(new Dotenv('JOOMLA_ENV', 'JOOMLA_DEBUG'))->bootEnv(JPATH_ROOT . '/.env', 'prod');
46+
}
47+
4248
// Pre-Load configuration. Don't remove the Output Buffering due to BOM issues, see JCode 26026
4349
ob_start();
4450
require_once JPATH_CONFIGURATION . '/configuration.php';
4551
ob_end_clean();
4652

53+
// getenv() is not thread-safe and it can cause segmentaion fault, so we should try $_SERVER first
54+
$envs = !empty($_SERVER) ? $_SERVER : getenv();
55+
4756
// System configuration.
48-
$config = new JConfig();
57+
$config = new JConfig();
58+
$config->error_reporting = $envs['JOOMLA_ERROR_REPORTING'] ?? $config->error_reporting;
59+
$config->debug = (bool) ($envs['JOOMLA_DEBUG'] ?? $config->debug);
60+
if (isset($envs['JOOMLA_LOG_DEPRECATED'])) {
61+
$config->log_deprecated = (int) $envs['JOOMLA_LOG_DEPRECATED'];
62+
}
63+
if (isset($envs['JOOMLA_BEHIND_LOADBALANCER'])) {
64+
$config->behind_loadbalancer = (bool) $envs['JOOMLA_BEHIND_LOADBALANCER'];
65+
}
66+
unset($envs);
4967

5068
// Set the error_reporting, and adjust a global Error Handler
5169
switch ($config->error_reporting) {

installation/includes/framework.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
\defined('_JEXEC') or die;
1212

13-
// Ensure sensible default for JDEBUG is set.
14-
const JDEBUG = false;
13+
use Symfony\Component\Dotenv\Dotenv;
1514

1615
// Check if a configuration file already exists.
1716
if (
@@ -26,6 +25,14 @@
2625
// Import the Joomla Platform.
2726
require_once JPATH_LIBRARIES . '/bootstrap.php';
2827

28+
// Load .env files
29+
if (file_exists(JPATH_ROOT . '/.env.local.php') || file_exists(JPATH_ROOT . '/.env')) {
30+
(new Dotenv('JOOMLA_ENV', 'JOOMLA_DEBUG'))->bootEnv(JPATH_ROOT . '/.env', 'prod');
31+
}
32+
33+
// Ensure sensible default for JDEBUG is set.
34+
\define('JDEBUG', (bool) ($_SERVER['JOOMLA_DEBUG'] ?? getenv('JOOMLA_DEBUG')));
35+
2936
// If debug mode enabled, set new Exception handler with debug enabled.
3037
if (JDEBUG) {
3138
$errorHandler->setExceptionHandler(

0 commit comments

Comments
 (0)