public function initTemplate($forceReplace = false)
 {
     if ($this->_templateInitialized && !$forceReplace) {
         return;
     }
     $tpl = Template::getTemplate();
     //no template
     if (!$tpl) {
         $this->log->debug('try initialize template, but no template configured', 'router');
         return false;
     }
     $this->_template = $tpl;
     // Set langs/urls vars into tpl
     $this->_template->setVar('urls', Router::getUrls($this->language->getLanguage(), Http::isHttps()), false, true);
     $this->_template->setVar('langs', $this->language->getVars(true), false, true);
     $this->_template->setVar('lang', $this->language->getLanguage(), false, true);
     //init assets
     if (!Http::isAjax()) {
         $this->_template->initAssets();
     }
     $this->_templateInitialized = true;
     $this->log->debug('Initialize template', 'router');
 }
 public function stop()
 {
     if ($this->_isInit && $this->_isRun) {
         // run caches gc
         $caches = Cache::getCaches();
         foreach ($caches as $cache) {
             $cache->runGc();
         }
         //profiling
         if (self::getProfiler()) {
             // Caches
             foreach ($caches as $cache) {
                 Logger::getInstance()->debug('Adaptater : "' . get_class($cache) . '"', 'cache' . $cache->getName());
             }
             // Databases
             $databases = Database::getDatabases();
             foreach ($databases as $database) {
                 Logger::getInstance()->debug('Type : ' . $database->getType(), 'database' . $database->getName());
                 Logger::getInstance()->debug('Adaptater : ' . get_class($database->getAdaptater()), 'database' . $database->getName());
                 $stats = $database->getStats();
                 Logger::getInstance()->debug('Queries : ' . (string) $database->getQueryCount() . ' (Aproximately memory used  : ' . $stats['ram'] . ' KB in aproximately ' . $stats['time'] . ' ms)', 'database' . $database->getName());
                 Logger::getInstance()->debug('Servers : ' . $database->countServers() . ' (Masters : ' . $database->countServers(Server::TYPE_MASTER) . '  Slaves : ' . $database->countServers(Server::TYPE_SLAVE) . ')', 'database' . $database->getName());
             }
             // Templates
             $templates = Template::getTemplates();
             foreach ($templates as $template) {
                 Logger::getInstance()->debug('Adaptater : ' . get_class($template), 'template' . $template->getName());
             }
             // Language
             Logger::getInstance()->debug('Language default is : "' . Language::getInstance()->getDefaultLanguage() . '"', 'language');
             Logger::getInstance()->debug(Language::getInstance()->countVars() . ' vars defined', 'language');
             // Router
             Logger::getInstance()->debug('Current url : ' . Http::getCurrentUrl(), 'router');
             Logger::getInstance()->debug('Current route : ' . Router::getInstance()->getCurrentRoute(), 'router');
             Logger::getInstance()->debug('Current route rule : ' . Router::getInstance()->getCurrentRule(), 'router');
             Logger::getInstance()->debug('Ajax request : ' . (int) Http::isAjax(), 'router');
             Logger::getInstance()->debug('Ssl request : ' . (int) Http::isHttps(), 'router');
             Logger::getInstance()->debug('Request dispatched in aproximately : ' . Benchmark::getInstance('router')->stopTime()->getStatsTime() . ' ms', 'router');
             Logger::getInstance()->debug('Aproximately memory used  : ' . Benchmark::getInstance('router')->stopRam()->getStatsRam() . ' KB', 'router');
             // Logger debug informations and benchmark
             Logger::getInstance()->addGroup('logger', 'Logger Benchmark and Informations', true);
             Logger::getInstance()->debug(Logger::getInstance()->countObservers() . ' observers registered', 'logger');
             Logger::getInstance()->debug(Logger::getInstance()->countGroups() . ' groups and ' . (Logger::getInstance()->countLogs() + 3) . ' logs', 'logger');
             Logger::getInstance()->debug('In aproximately ' . Benchmark::getInstance('logger')->stopTime()->getStatsTime() . ' ms', 'logger');
             Logger::getInstance()->debug('Aproximately memory used  : ' . Benchmark::getInstance('logger')->stopRam()->getStatsRam() . ' KB', 'logger');
             // Autoloader
             Logger::getInstance()->addGroup('autoloader', 'Autoloader report', true);
             $logs = Autoloader::getLogs();
             foreach ($logs as &$log) {
                 Logger::getInstance()->debug($log, 'autoloader');
             }
             Logger::getInstance()->debug(count(Autoloader::getAutoloaders()) . ' autoloader adaptaters, ' . count(Autoloader::getDirectories()) . ' directories and ' . count(Autoloader::getNamespaces()) . ' namespaces registered', 'autoloader');
             Logger::getInstance()->debug('Loading ' . count(Autoloader::getClasses()) . ' classes (' . Autoloader::countGlobalizedClasses() . ' globalized classes)  in aproximately ' . round(Autoloader::getBenchmark('time') * 1000, 4) . ' ms', 'autoloader');
             Logger::getInstance()->debug('Aproximately memory used  : ' . round(Autoloader::getBenchmark('memory') / 1024, 4) . ' KB', 'autoloader');
             Autoloader::purgeLogs();
             Autoloader::purgeBenchmark();
             // Global informations && Benchmark
             Logger::getInstance()->addGroup('global', 'Global Benchmark and Informations', true);
             Logger::getInstance()->debug('Page generated in aproximately : ' . Benchmark::getInstance('global')->stopTime()->getStatsTime() . ' ms', 'global');
             Logger::getInstance()->debug('Aproximately memory used  : ' . Benchmark::getInstance('global')->stopRam()->getStatsRam() . ' KB - Memory allocated : ' . memory_get_peak_usage(true) / 1024 . ' KB', 'global');
         }
         //notify logger
         Logger::getInstance()->notify();
         // Stop managers
         ExceptionManager::getInstance()->stop();
         ErrorManager::getInstance()->stop();
         // avoid multi call
         $this->_isInit = false;
         $this->_isRun = false;
     }
 }
示例#3
0
$exc = ExceptionManager::getInstance()->start();
$err = ErrorManager::getInstance()->start(true, static::getDebug(), static::getDebug());
$log = Logger::getInstance();
// Set language
if (!defined('PATH_LANGUAGE')) {
    throw new \Exception('Miss language path datas');
}
Language::setDatasPath(PATH_LANGUAGE);
$language = Language::getInstance();
if (!defined('LANGUAGE_DEFAULT')) {
    throw new \Exception('Miss language default');
}
$language->setLanguage(LANGUAGE_DEFAULT, true, true);
// Set default template
if (defined('TEMPLATE_DEFAULT')) {
    Template::setTemplate(TEMPLATE_DEFAULT);
}
//Enable debug tools
if (static::getDebug()) {
    $log->setLevel(Logger::DEBUG);
    Autoloader::setDebug(true);
    //Debug error and exeception
    $exc->attach(new Display());
    $err->attach(new Display());
}
// Logger parameters
if (defined('LOGGER_CACHE') && LOGGER_CACHE && !static::getDebug()) {
    $log->setCache(LOGGER_CACHE);
}
if (defined('LOGGER_LEVEL') && !static::getDebug()) {
    $log->setLevel(LOGGER_LEVEL);
示例#4
0
 protected function _runController($controller, $methods = array(), $vars = array(), $requireSsl = false, $requireAjax = false, $autoSetAjax = true, $requireHttpMethod = null, $httpResponseStatusCode = null, $httpProtocol = null)
 {
     $controllerExplode = explode($this->getNamespaceSeparator(), (string) $controller);
     if (is_array($controllerExplode) && count($controllerExplode) > 1) {
         $controllerName = $this->getNamespaceSeparator() . ucfirst(array_pop($controllerExplode));
         $controller = implode($this->getNamespaceSeparator(), $controllerExplode) . $controllerName;
     } else {
         $controller = (string) ucfirst($controller);
     }
     Logger::getInstance()->debug('Run controller : "' . $controller . '"', 'router');
     $controllerClass = $this->getControllersNamespace(true) . $controller;
     // Check if controller exists (with controllers namespace)
     if (!class_exists($controllerClass)) {
         throw new \Exception('Controller "' . $controllerClass . '" not found');
     }
     $controller = $controllerClass;
     if (!is_array($vars)) {
         throw new \Exception('Controller : "' . $controller . '" vars must be an array');
     }
     if (!is_array($methods)) {
         throw new \Exception('Controller : "' . $controller . '" methodes must be an array');
     }
     $inst = new \ReflectionClass($controller);
     if ($inst->isInterface() || $inst->isAbstract()) {
         throw new \Exception('Controller "' . $controller . '" cannot be an interface of an abstract class');
     }
     $ctrl = $inst->newInstance();
     if ($ctrl->getAutoCallDisplay()) {
         if (!$inst->hasMethod('display')) {
             throw new \Exception('Controller "' . $controller . '" must be implement method "Diplay');
         }
         if (!$inst->hasMethod('initTemplate')) {
             throw new \Exception('Controller "' . $controller . '" must be implement method "initTemplate');
         }
     }
     if (!Cli::isCli()) {
         if (!Http::isHttps() && $requireSsl) {
             Logger::getInstance()->debug('Controller "' . $controller . '" need ssl http request', 'router');
             $this->show400(true);
         }
         if (!is_null($requireHttpMethod)) {
             if ($requireHttpMethod != Http::getMethod()) {
                 Logger::getInstance()->debug('Controller "' . $controller . '" invalid http method');
                 $this->show405(true);
             }
         }
         if (!Http::isAjax() && $requireAjax) {
             Logger::getInstance()->debug('Controller "' . $controller . '" need ajax http request');
             $this->show400(true);
         }
         if (Http::isAjax() && $autoSetAjax) {
             $ctrl->setAjaxController();
         }
         if (!is_null($httpResponseStatusCode) || !is_null($httpProtocol)) {
             Header::setResponseStatusCode(is_null($httpResponseStatusCode) ? 200 : $httpResponseStatusCode, true, true, $httpProtocol);
         }
     }
     if ($methods) {
         foreach ($methods as $methodName => $methodParams) {
             Logger::getInstance()->debug('Call method : "' . $methodName . '"', 'router');
             if (!method_exists($ctrl, $methodName) || !$inst->getMethod($methodName)->isPublic()) {
                 throw new \Exception('Method "' . $methodName . '" don\'t exists or isn\'t public on controller "' . $controller . '"');
             }
             $args = array();
             if (!is_array($methodParams)) {
                 $args[] = $methodParams;
             } else {
                 foreach ($methodParams as $parameter) {
                     //check if is [['key']] type, or direct value
                     if (stripos($parameter, '[[') === false) {
                         $args[] = $parameter;
                     } else {
                         if (count($vars) > 0) {
                             $key = (int) str_replace(array('[', ']'), '', $parameter);
                             if (array_key_exists($key, $vars)) {
                                 $args[] = $vars[$key];
                             }
                         } else {
                             $args[] = $parameter;
                         }
                     }
                 }
             }
             foreach ($args as $arg) {
                 Logger::getInstance()->debug('Add argument : "' . $arg . '"', 'router');
             }
             // Call method with $args
             \call_user_func_array(array($ctrl, $methodName), $args);
         }
     }
     $this->_controller = $ctrl;
     //call display only when have a template
     if ($ctrl->getAutoCallDisplay() && Template::getTemplate()) {
         Logger::getInstance()->debug('Call method "display"', 'router');
         $ctrl->display();
     }
 }