|
<?php |
|
/** |
|
* General environment setup for all contexts |
|
* Defines paths, auto-loading, and bootstrapped resources/dependencies |
|
* |
|
* @package ... |
|
* @copyright ... |
|
* @author ... |
|
*/ |
|
|
|
/******************************************************************************* |
|
initial error reporting (application should override this based on environment) |
|
*******************************************************************************/ |
|
|
|
// always set to (-1) to report all errors |
|
error_reporting(-1); |
|
|
|
// set to 'syslog' so all error logs go to /var/log/messages by default |
|
ini_set('error_log', 'syslog'); |
|
ini_set('html_errors', false); |
|
ini_set('display_errors', false); |
|
|
|
// set timezone to 'Etc/UTC' |
|
ini_set('date.timezone', 'Etc/UTC'); |
|
date_default_timezone_set('Etc/UTC'); |
|
|
|
/******************************************************************************* |
|
command-line option parsing |
|
*******************************************************************************/ |
|
|
|
//$argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array(); |
|
if (! empty($_SERVER['argv'])) { |
|
$argv=& $_SERVER['argv']; |
|
|
|
foreach ($argv as $argKey => $arg) { |
|
$paramKey = $argKey + 1; |
|
|
|
if ($arg == '--environment' && isset($argv[$paramKey])) { |
|
putenv(sprintf('APPLICATION_ENV=%s', $argv[$paramKey])); |
|
unset($argv[$argKey], $argv[$paramKey]); |
|
} |
|
} |
|
|
|
unset($argKey, $paramKey, $arg, $env); |
|
} |
|
|
|
/******************************************************************************* |
|
application paths |
|
*******************************************************************************/ |
|
|
|
// application name |
|
defined('APPLICATION_NAME') |
|
|| define('APPLICATION_NAME', 'your-application-name'); |
|
|
|
// root project path |
|
defined('BASE_PATH') |
|
|| define('BASE_PATH', __DIR__); |
|
|
|
// source directory path |
|
defined('SOURCE_PATH') |
|
|| define('SOURCE_PATH', realpath(BASE_PATH . DIRECTORY_SEPARATOR . 'src')); |
|
|
|
// configs directory path |
|
defined('CONFIGS_PATH') |
|
|| define('CONFIGS_PATH', realpath(SOURCE_PATH . DIRECTORY_SEPARATOR . 'configs')); |
|
|
|
// web application path |
|
defined('WEBAPP_PATH') |
|
|| define('WEBAPP_PATH', realpath(SOURCE_PATH . DIRECTORY_SEPARATOR . 'webapp')); |
|
|
|
/** |
|
* application environment |
|
* |
|
* Check for environment override, otherwise, get the configuration parameter |
|
* value. Failing that, default to common. This can be set via: |
|
* (1) Virtual Host Configuration (SetEnv APPLICATION_ENV ##APPLICATION_ENV##) |
|
* (2) Command-Line Scripts (APPLICATION_ENV=local mqueue.php -d) |
|
* (3) Command-Line Script Args (mqueue.php --environment local) |
|
*/ |
|
if (! defined('APPLICATION_ENV')) { |
|
if (false !== getenv('APPLICATION_ENV_OVERRIDE')) { |
|
define('APPLICATION_ENV', getenv('APPLICATION_ENV_OVERRIDE')); |
|
} else { |
|
define('APPLICATION_ENV', getenv('APPLICATION_ENV') ?: 'common'); |
|
} |
|
} |
|
|
|
/******************************************************************************* |
|
define constant 'APPLICATION_CONFIG' (application configuration file) |
|
*******************************************************************************/ |
|
|
|
$files[] = realpath(CONFIGS_PATH.'/application.ini'); |
|
$files[] = realpath(CONFIGS_PATH.'/application.ini.dist'); |
|
|
|
foreach ($files as $file) { |
|
is_readable($file) && !defined('APPLICATION_CONFIG') && define('APPLICATION_CONFIG', $file); |
|
} |
|
unset($files, $file); |
|
|
|
if (!defined('APPLICATION_CONFIG')) { |
|
throw new RuntimeException('Unable to locate an application configuration file.'); |
|
} |
|
|
|
/******************************************************************************* |
|
setup autoloader |
|
*******************************************************************************/ |
|
|
|
$files[] = realpath(__DIR__.'/autoload.php'); |
|
$files[] = realpath(__DIR__.'/autoload.php.dist'); |
|
|
|
foreach ($files as $file) { |
|
is_readable($file) && !defined('AUTOLOADER_PATH') && define('AUTOLOADER_PATH', $file); |
|
} |
|
unset($files, $file); |
|
|
|
if (!defined('AUTOLOADER_PATH')) { |
|
throw new RuntimeException('Unable to locate an autoloader file.'); |
|
} |
|
|
|
require_once AUTOLOADER_PATH; |
|
|
|
/******************************************************************************* |
|
setup root error/exception handler |
|
*******************************************************************************/ |
|
|
|
m3\common\error\Exception::handle(); |
This is should be considered deprecated in favor of Composer and its autoloader.