public static function init() { Event::register('content', function ($content) { // Block: template with cache return preg_replace_callback('/{{block ([^}]*)}}/', function ($M) { $params = array(); $parameters = preg_replace_callback('/([a-zA-Z_0-9]*)=([^"]{1}[^ ]*)/', function ($S) { return "{$S[1]}=\"{$S[2]}\""; }, $M[1]); // template vars if (preg_match_all('/([a-zA-Z_0-9]*)="([^"]*)"/', $parameters, $L, PREG_SET_ORDER)) { foreach ($L as $param) { $varname = trim($param[1]); $value = trim($param[2]); if (substr($value, 0, 3) == '~~~') { $value = I18n::getSingleton()->translate(substr($value, 3)); } $params[$varname] = $value; } } // config vars $block_key = "block.{$params['template']}."; $block_key_length = strlen($block_key); foreach (Mvc::getConfig()->getData() as $key => $value) { if (substr($key, 0, $block_key_length) == $block_key) { $varname = trim(substr($key, $block_key_length)); $value = trim($value); if (substr($value, 0, 3) === '~~~') { $value = I18n::getSingleton()->translate(substr($value, 3)); } $params[$varname] = $value; } } return Block::factory($params)->getHtml(); }, $content); }); }
public function __construct($root_dir) { global $MVC; $MVC = $this; $root_dir = realpath($root_dir); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 1. Config $this->config = new Config($root_dir . '/etc/config.ini'); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 2. Setup $this->session = new Session(); require $root_dir . '/functions.php'; define('LP_DEBUG', $this->config->getData('app.debug', false) ? true : false); timer('start', 'init'); define('LP_ROOT_DIRECTORY', $root_dir); define('LP_APP_DIRECTORY', "{$root_dir}/app/{$this->config->getData('app.name', 'default')}"); define('LP_DEFAULT_APP_DIRECTORY', "{$root_dir}/app/default"); // /landingpages/ $document_root = realpath($_SERVER['DOCUMENT_ROOT']); $base_uri = trim(substr($root_dir, strlen($document_root)), '/'); define('LP_BASE_URI', str_replace('//', '/', "/{$base_uri}/")); define('LP_APP_URI', LP_BASE_URI . "app/{$this->config->getData('app.name', 'default')}/"); define('LP_DEFAULT_APP_URI', LP_BASE_URI . "app/default/"); // Ex: http://localhost/landingpages/example-simple-v1.html $uri = trim(array_shift(explode('?', $_SERVER['REQUEST_URI'])), '/'); if (preg_match('/^(.*)\\/index\\.php$/', $uri, $M)) { $uri = $M[1]; } define('LP_URL', "http://{$_SERVER['SERVER_NAME']}/{$uri}"); define('LP_BASE_URL', "http://{$_SERVER['SERVER_NAME']}" . LP_BASE_URI); // Ex: example-simple-v1.html $uri = trim(substr($uri, strlen($base_uri)), '/'); define('LP_URI', $uri); // languages if (!$this->config->issetData('locale.detect_methods')) { $this->config->setData('locale.detect_methods', 'url,domain,geoip,browser'); } if (!$this->config->issetData('locale.enabled')) { $this->config->setData('locale.enabled', 'en_US,en_GB'); } if (!$this->config->issetData('locale.default')) { $this->config->setData('locale.default', array_shift(explode(',', $this->config->getData('locale.enabled')))); } // Database if (!$this->config->issetData('database')) { $this->config->setData('database', 'sqlite:' . LP_ROOT_DIRECTORY . '/var/general.db'); } Response::init(); Response\Cache::init(); Block::init(); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 3. Request $this->request = new Request(); $this->request->setRootDirectory(LP_ROOT_DIRECTORY); $this->request->setUrl(LP_URL); $this->request->setBaseUri(LP_BASE_URI); $this->request->setUri(LP_URI); $this->request->setSession($this->session); $this->request->setConfig($this->config); if (!($this->response = $this->request->getCacheResponse())) { //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 4. Router $this->router = new Router($this->request); $token = $this->router->getToken(); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 5. Dispatcher $this->dispatcher = new Dispatcher($this->request); if (!$token || !$this->dispatcher->isDispatchable($token)) { $token = array('error', '404', array('uri' => LP_URI)); } // We have a token to dispatch $this->dispatcher->addToken($token); // Dispatch tokens and get the final response /** @var Response $response */ $this->response = $this->dispatcher->doLoop(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 6. Response $this->response->exec(); timer('end', 'init'); timer('print'); }