-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.php
More file actions
121 lines (106 loc) · 2.88 KB
/
environment.php
File metadata and controls
121 lines (106 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Plugin Environment Configuration
*
* This file loads environment variables from the .env file
* and defines constants for the plugin.
*
* @package Plugin_Core
* @since 1.0.0
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Load Composer autoloader
if (file_exists(plugin_dir_path(__FILE__) . '/vendor/autoload.php')) {
require_once plugin_dir_path(__FILE__) . '/vendor/autoload.php';
}
// Load environment variables from .env file
if (file_exists(plugin_dir_path(__FILE__) . '/.env')) {
$dotenv = Dotenv\Dotenv::createImmutable(plugin_dir_path(__FILE__));
$dotenv->load();
}
// Define environment variables with defaults
$env_vars = [
'PLUGIN_DEBUG_MODE' => false,
// set env variables here
];
// Load environment variables and define constants
foreach ($env_vars as $var => $default_value) {
if (!defined($var)) {
$value = $_ENV[$var] ?? $default_value;
// Convert string values to appropriate types
if (is_string($value)) {
if ($value === 'true') {
$value = true;
} elseif ($value === 'false') {
$value = false;
} elseif (is_numeric($value)) {
$value = (float) $value;
if ($value == (int) $value) {
$value = (int) $value;
}
}
}
define($var, $value);
}
}
// Set error reporting based on debug mode
if (defined('PLUGIN_DEBUG_MODE') && PLUGIN_DEBUG_MODE) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
} else {
error_reporting(0);
ini_set('display_errors', 0);
}
// Set log level
if (defined('PLUGIN_LOG_LEVEL')) {
switch (PLUGIN_LOG_LEVEL) {
case 'debug':
$log_level = E_ALL;
break;
case 'info':
$log_level = E_ALL & ~E_NOTICE & ~E_WARNING;
break;
case 'warning':
$log_level = E_ERROR | E_WARNING | E_PARSE;
break;
case 'error':
default:
$log_level = E_ERROR | E_PARSE;
break;
}
error_reporting($log_level);
}
// Initialize logging if enabled
if (defined('LOGGING_ENABLED') && LOGGING_ENABLED) {
// Create logs directory if it doesn't exist
$log_path = plugin_dir_path(__FILE__) . LOGGING_FILE_PATH;
if (!file_exists($log_path)) {
wp_mkdir_p($log_path);
}
// Set custom error handler for logging
if (defined('PLUGIN_DEBUG_MODE') && PLUGIN_DEBUG_MODE) {
set_error_handler(function($errno, $errstr, $errfile, $errline) {
$log_file = plugin_dir_path(__FILE__) . LOGGING_FILE_PATH . 'debug.log';
$timestamp = date('Y-m-d H:i:s');
$message = "[{$timestamp}] [{$errno}] {$errstr} in {$errfile} on line {$errline}\n";
error_log($message, 3, $log_file);
});
}
}
// Load additional configuration files
$config_files = [
'config.php',
'config-local.php', // Local development overrides
];
foreach ($config_files as $config_file) {
$config_path = plugin_dir_path(__FILE__) . '/' . $config_file;
if (file_exists($config_path)) {
require_once $config_path;
}
}
// Initialize plugin environment
do_action('your_plugin_environment_loaded');
?>