public static function init($configFile) { $opts = null; $apcEnabled = (bool) ini_get('apc.enabled'); if ($apcEnabled && apc_exists('hmcsoftmvc-config')) { if (file_exists($configFile)) { $lastModTime = filemtime($configFile); $lastConfTime = 0; if (apc_exists('hmcsoftmvc-config-updated')) { $lastConfTime = apc_fetch('hmcsoftmvc-config-updated'); } if ($lastConfTime < $lastModTime) { $opts = json_decode(file_get_contents($configFile), true); apc_store('hmcsoftmvc-config', $opts); apc_store('hmcsoftmvc-config-updated', $lastModTime); } else { $opts = apc_fetch('hmcsoftmvc-config'); } } } else { if (is_readable($configFile)) { $opts = json_decode(file_get_contents($configFile), true); } } $defaults = self::get_defaults(); if ($opts !== null) { self::$options = (array) self::merge_defaults($defaults, (array) $opts); } self::$options = Hooks::run('config', self::$options); if (self::$options['SITE']['LANGUAGE'] == null) { //try to figure if we support the user's language. $userlangs = explode(';', $_SERVER['HTTP_ACCEPT_LANGUAGE']); $foundLang = false; foreach (self::$options['SITE']['LANGUAGES'] as $ourLang) { if (!$foundLang) { foreach ($userlangs as $ulang) { if (!$foundLang && $ulang === $ourLang) { self::$options['SITE']['LANGUAGE'] = $ourLang; $foundLang = true; } } } } if (!$foundLang) { self::$options['SITE']['LANGUAGE'] = self::$options['SITE']['DEFAULTLANG']; } } if (isset(self::$options['SITE'])) { if (isset(self::$options['ENVIRONMENT'])) { if (self::$options['SITE']['ENVIRONMENT'] == 'development') { error_reporting(E_NONE); } else { error_reporting(E_NONE); } } //set timezone if (isset(self::$options['SITE']['TIMEZONE'])) { date_default_timezone_set(self::$options['SITE']['TIMEZONE']); } } //turn on output buffering ob_start(); //turn on custom error handling if (isset(self::$options['LOG'])) { \HMC\Logger::init(self::$options['LOG']); if (isset(self::$options['LOG']['EXCEPTIONS'])) { set_exception_handler(self::$options['LOG']['EXCEPTIONS']); } else { set_exception_handler('HMC\\Logger::exceptionHandler'); } if (isset(self::$options['LOG']['ERRORS'])) { set_error_handler(self::$options['LOG']['ERRORS']); } else { set_error_handler('HMC\\Logger::errorHandler'); } } //start sessions if (isset(self::$options['SESSION'])) { Session::init(self::$options['SESSION']); } else { Session::init(); } return self::$options; }
<?php use HMC\Config; use HMC\Hooks; ?> <!DOCTYPE html> <html> <head> <title><?php echo $data['title']; ?> | <?php echo Config::SITE_TITLE(); ?> </title> <?php Hooks::run('meta'); Hooks::run('css'); //use Assets::css or combine_css to inject stylesheets ?> </head> <body>
<?php use HMC\Hooks; Hooks::run('footer'); Hooks::run('afterBody'); Hooks::run('js'); //do js here ?> </body> </html>
private static function init() { if (!headers_sent()) { \HMC\Hooks::run('headers'); } }
} else { echo "<h1>Please install via composer.json</h1>"; echo "<p>Install Composer instructions: <a href='https://getcomposer.org/doc/00-intro.md#globally'>https://getcomposer.org/doc/00-intro.md#globally</a></p>"; echo "<p>Once composer is installed navigate to the working directory in your terminal/command promt and enter 'composer install'</p>"; exit; } use HMC\Config; use HMC\Hooks; use HMC\Router; use HMC\View; //Routes are defined in the config file. $configFile = '.app_config.json'; //initiate config Hooks::run('init'); $configFile = Hooks::run('pre-config', $configFile); $config = Config::init($configFile); Hooks::run('config-ready'); Hooks::addHook('headers', 'addNotice'); //Initialize Router Router::init($config); //if no route found //Router::error('Core\Error@index'); //To route with the url/Controller/Method/args schema uncomment this. Router::$fallback = true; Hooks::run('pre-dispatch'); //execute matched routes Router::dispatch(); function addNotice() { View::addHeader('X-Uses: HMC-soft MVC'); }