Skip to content

Commit c256df1

Browse files
committed
refactor(index.php): Environment setup moved to Platform
1 parent 64015b9 commit c256df1

1 file changed

Lines changed: 6 additions & 94 deletions

File tree

index.php

Lines changed: 6 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717

1818
ob_start(); // Start output buffering to ensure headers can be sent later.
1919

20-
/**
21-
* Define the directory separator constant.
22-
*
23-
* This ensures platform-independent path building.
24-
*/
25-
define('DS', DIRECTORY_SEPARATOR);
26-
2720
/**
2821
* ------------------------------------------------------------------------
2922
* Application Bootstrapping Closure
@@ -36,7 +29,7 @@
3629
* - Configures PHP error reporting
3730
* - Ensures compatibility with CLI and web server
3831
*/
39-
(static function (): void {
32+
(function () {
4033

4134
/**
4235
* Normalize working directory for CLI.
@@ -64,14 +57,14 @@
6457
*
6558
* @throws RuntimeException if the path is invalid.
6659
*/
67-
$resolve = static function (string $path, string $name = ''): string {
60+
$resolve = function ($path, $name = '') {
6861
// Try resolving to absolute path
6962
if (($real = realpath($path)) !== false) {
7063
return $real;
7164
}
7265

7366
// Normalize slashes and check directory existence manually
74-
$real = rtrim(str_replace(['/', '\\'], DS, $path), DS);
67+
$real = rtrim(str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path), DIRECTORY_SEPARATOR);
7568
if (is_dir($real)) {
7669
return $real;
7770
}
@@ -97,97 +90,16 @@
9790
*/
9891

9992
// Path to this front controller (index.php)
100-
define('FCPATH', __DIR__.DS);
93+
define('FCPATH', __DIR__.DIRECTORY_SEPARATOR);
10194

10295
// Path to CiSkeleton system folder
103-
define('BASEPATH', $resolve('skeleton', 'system').DS);
96+
define('BASEPATH', $resolve('skeleton', 'system').DIRECTORY_SEPARATOR);
10497

10598
// Path to the application folder
106-
define('APPPATH', $resolve('application', 'application').DS);
99+
define('APPPATH', $resolve('application', 'application').DIRECTORY_SEPARATOR);
107100

108101
// The name of the "system" folder (used internally)
109102
define('SYSDIR', basename(BASEPATH));
110-
111-
// --------------------------------------------------------------------
112-
// ENVIRONMENT SETUP
113-
// --------------------------------------------------------------------
114-
115-
/**
116-
* Environment file path (APPPATH/config/environment)
117-
*
118-
* This file should contain a single line: "development", "testing", or "production".
119-
*/
120-
$envfile = APPPATH.'config'.DS.'environment';
121-
122-
/**
123-
* Default to "development" if file does not exist.
124-
* Also create the file with the default value.
125-
*/
126-
if (!is_file($envfile)) {
127-
$environment = 'development';
128-
file_put_contents($envfile, $environment, LOCK_EX);
129-
} else {
130-
$environment = trim(file_get_contents($envfile));
131-
}
132-
133-
/**
134-
* Fallback if file contents are invalid.
135-
* Only the three official environments are allowed.
136-
*/
137-
if (!in_array($environment, ['development', 'testing', 'production'], true)) {
138-
$environment = 'development';
139-
file_put_contents($envfile, $environment, LOCK_EX);
140-
}
141-
142-
/**
143-
* Define the ENVIRONMENT constant.
144-
*/
145-
define('ENVIRONMENT', $environment);
146-
147-
/**
148-
* Set PHP error reporting levels based on ENVIRONMENT.
149-
*
150-
* You may customize this if you add more environments.
151-
*/
152-
switch (ENVIRONMENT) {
153-
case 'development':
154-
case 'testing':
155-
// Show all errors, including strict and deprecated
156-
error_reporting(E_ALL);
157-
ini_set('display_errors', '1');
158-
ini_set('log_errors', '1');
159-
ini_set('error_log', APPPATH.'storage/logs/php_errors.log');
160-
161-
define('SHOW_DEBUG_BACKTRACE', true);
162-
define('CI_DEBUG', true);
163-
break;
164-
165-
case 'production':
166-
if (PHP_SAPI === 'cli' || defined('STDIN')) {
167-
// In CLI mode, we want full error visibility even in production
168-
error_reporting(E_ALL);
169-
ini_set('display_errors', '1');
170-
} else {
171-
// Suppress errors on the web frontend
172-
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
173-
ini_set('display_errors', '0');
174-
}
175-
176-
// Always log errors in production (CLI or Web)
177-
ini_set('log_errors', '1');
178-
ini_set('error_log', APPPATH.'storage/logs/php_errors.log');
179-
180-
define('SHOW_DEBUG_BACKTRACE', false);
181-
define('CI_DEBUG', false);
182-
break;
183-
184-
default:
185-
// This should never happen because we validated earlier
186-
header('HTTP/1.1 503 Service Unavailable.', true, 503);
187-
echo 'The application environment is not set correctly.';
188-
exit(1); // EXIT_ERROR
189-
}
190-
191103
})(); // <-- END of bootstrapping closure
192104

193105
/**

0 commit comments

Comments
 (0)