2023-02-08 02:27:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace EesyPHP;
|
|
|
|
|
|
|
|
class App {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $options = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Root directory path
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected static $root_directory_path = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialization
|
|
|
|
* @param string|null $config_file Application configuration file path
|
|
|
|
* @param array|null $options Application options (default: null)
|
|
|
|
* @param string|null $root_directory_path Application root directory path (default: null)
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function init($config_file, $options=null, $root_directory_path=null) {
|
|
|
|
if (is_array($options)) self :: $options = $options;
|
|
|
|
|
|
|
|
if (is_null($root_directory_path)) {
|
|
|
|
$traces = debug_backtrace();
|
|
|
|
$root_directory_path = realpath(dirname($traces[0]['file']).'/../');
|
|
|
|
}
|
|
|
|
self :: $root_directory_path = $root_directory_path;
|
|
|
|
Config::register_extra_variable('root_directory_path', $root_directory_path);
|
|
|
|
|
|
|
|
$config_file = Config::replace_variables($config_file);
|
|
|
|
if (!Config::load($config_file)) {
|
|
|
|
Log::fatal('Fail to load configuration file (%s)', $config_file);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load overwrite configuration file
|
2023-02-12 00:30:36 +01:00
|
|
|
foreach (self :: get('overwrite_config_files', array(), 'array') as $file) {
|
2023-02-08 02:27:15 +01:00
|
|
|
$file = Config::replace_variables($file);
|
|
|
|
if (is_file($file)) Config::load($file, true);
|
|
|
|
}
|
|
|
|
|
2023-02-12 00:30:36 +01:00
|
|
|
if (self :: get('sentry.enabled', true, 'bool'))
|
2023-02-08 02:27:15 +01:00
|
|
|
SentryIntegration :: init();
|
|
|
|
$sentry_transaction = new SentryTransaction();
|
|
|
|
$sentry_span = new SentrySpan('app.init', 'Application initialization');
|
|
|
|
|
|
|
|
// Define upload_tmp_dir
|
2023-02-12 00:30:36 +01:00
|
|
|
if (is_string(self::get('upload_tmp_directory')))
|
|
|
|
ini_set('upload_tmp_dir', self::get('upload_tmp_directory'));
|
2023-02-08 02:27:15 +01:00
|
|
|
|
2023-02-12 00:30:36 +01:00
|
|
|
if (self :: get('log.enabled', true, 'bool'))
|
2023-02-08 02:27:15 +01:00
|
|
|
Log::init();
|
2023-02-12 00:30:36 +01:00
|
|
|
if (self :: get('session.enabled', true, 'bool'))
|
2023-02-08 02:27:15 +01:00
|
|
|
Session::init();
|
2023-02-13 00:42:20 +01:00
|
|
|
if (self :: get('templates.enabled', true, 'bool'))
|
2023-02-08 02:27:15 +01:00
|
|
|
Tpl :: init();
|
2023-02-13 00:42:20 +01:00
|
|
|
if (self :: get('url.enabled', true, 'bool')) {
|
2023-02-08 02:27:15 +01:00
|
|
|
Url::init();
|
2023-02-13 00:42:20 +01:00
|
|
|
Url :: add_url_handler('#^$#', array('EesyPHP\\App', 'handle_homepage'));
|
|
|
|
}
|
2023-02-12 00:30:36 +01:00
|
|
|
if (self :: get('mail.enabled', true, 'bool'))
|
2023-02-08 02:27:15 +01:00
|
|
|
Email :: init();
|
2023-02-12 00:30:36 +01:00
|
|
|
if (self :: get('i18n.enabled', true, 'bool'))
|
2023-02-08 02:27:15 +01:00
|
|
|
I18n::init();
|
|
|
|
$sentry_span->finish();
|
|
|
|
}
|
|
|
|
|
2023-02-12 00:30:36 +01:00
|
|
|
/**
|
|
|
|
* Check if the application is initialized
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function initialized() {
|
|
|
|
return !is_null(self :: $root_directory_path);
|
|
|
|
}
|
|
|
|
|
2023-02-08 02:27:15 +01:00
|
|
|
/**
|
|
|
|
* Get a specific option value
|
|
|
|
*
|
|
|
|
* @param string $key The configuration variable key
|
|
|
|
* @param mixed $default The default value to return if configuration variable
|
|
|
|
* is not set (Default : null)
|
|
|
|
* @param string $cast The type of expected value. The configuration variable
|
|
|
|
* value will be cast as this type. Could be : bool, int,
|
|
|
|
* float or string. (Optional, default : raw value)
|
|
|
|
* @param bool $split If true, $cast is 'array' and value retreived from configuration
|
|
|
|
* is a string, split the value by comma (optional, default: true)
|
|
|
|
* @return mixed The configuration variable value
|
|
|
|
**/
|
2023-02-12 00:30:36 +01:00
|
|
|
public static function get($key, $default=null, $cast=null, $split=true) {
|
2023-02-08 02:27:15 +01:00
|
|
|
return Config::get(
|
|
|
|
$key,
|
|
|
|
Config::loaded()?Config::get($key, $default, $cast, $split):$default,
|
|
|
|
$cast,
|
|
|
|
$split,
|
|
|
|
self :: $options
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retreive application root directory path
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public static function root_directory_path() {
|
|
|
|
return self :: $root_directory_path?self :: $root_directory_path:'.';
|
|
|
|
}
|
|
|
|
|
2023-02-13 00:42:20 +01:00
|
|
|
/**
|
|
|
|
* Default homepage handler
|
|
|
|
* @param UrlRequest $request
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function handle_homepage($request) {
|
|
|
|
if (Tpl::initialized())
|
|
|
|
Tpl :: display("homepage.tpl", _("Hello world !"));
|
|
|
|
else
|
|
|
|
echo "<h1>"._("Hello world!")."</h1>";
|
|
|
|
}
|
|
|
|
|
2023-02-08 02:27:15 +01:00
|
|
|
}
|