private function _connectToMemcacheServer() { $lion_runtime_directives = __Lion::getInstance()->getRuntimeDirectives(); //Checks if the Memcache module is loaded: if (!class_exists('Memcache')) { throw new Exception("PECL Memcache extension is not installed. Can not use the __MemCache cache handler."); } //Perform the connection to the memcache server: if (self::$_connection == null) { self::$_connection = new Memcache(); if ($lion_runtime_directives->hasDirective('MEMCACHE_SERVER')) { $server = $lion_runtime_directives->getDirective('MEMCACHE_SERVER'); } else { $server = self::MEMCACHE_DEFAULT_SERVER; } if ($lion_runtime_directives->hasDirective('MEMCACHE_PORT')) { $port = $lion_runtime_directives->getDirective('MEMCACHE_PORT'); } else { $port = self::MEMCACHE_DEFAULT_PORT; } if (!self::$_connection->connect($server, $port)) { throw new Exception("Can not connect to memcache server (server: {$server}, port: {$port})"); } } }
/** * Get the singleton instance of __Lion * * @return __Lion */ public static final function &getInstance() { if (self::$_instance == null) { self::$_instance = new __Lion(); } return self::$_instance; }
/** * Creates a new cache handler (a class implementing the {@link __ICacheHandler}) based on context configuration. * * @return __CacheHandler */ public static final function &createCacheHandler() { $return_value = null; $cache_handler_class = __Lion::getInstance()->getRuntimeDirectives()->getDirective('CACHE_HANDLER_CLASS'); if (!empty($cache_handler_class)) { $cache_impl_dir = LION_CACHE_DIR . DIRECTORY_SEPARATOR . 'impl'; if (!class_exists($cache_handler_class)) { switch ($cache_handler_class) { case '__Apc': $cache_impl_file = $cache_impl_dir . DIRECTORY_SEPARATOR . 'Apc.class.php'; break; case '__CacheLite': $cache_impl_file = $cache_impl_dir . DIRECTORY_SEPARATOR . 'CacheLite.class.php'; break; case '__MemCache': $cache_impl_file = $cache_impl_dir . DIRECTORY_SEPARATOR . 'MemCache.class.php'; break; case '__MemCached': $cache_impl_file = $cache_impl_dir . DIRECTORY_SEPARATOR . 'MemCached.class.php'; break; default: $cache_impl_file = $cache_impl_dir . DIRECTORY_SEPARATOR . __Lion::getInstance()->getRuntimeDirectives()->getDirective('CACHE_HANDLER_FILE'); break; } include_once $cache_impl_file; } $return_value = new $cache_handler_class(); if (!$return_value instanceof __ICacheHandler) { throw new Exception('Wrong cache handler class: ' . $cache_handler_class . '. A class implementing the __ICacheHandler was expected.'); } } return $return_value; }
private function _printLionInfo() { echo 'Lion framework ' . LION_VERSION_NUMBER . ' (built: ' . LION_VERSION_BUILD_DATE . ")\n"; echo "\n"; echo "Runtime Directives\n"; echo "------------------\n"; $lion_directives = __Lion::getInstance()->getRuntimeDirectives()->getDirectives(); $runtime_directives_values = array(); foreach ($lion_directives as $key => $value) { if (is_bool($value)) { if ($value) { $value = 'true'; } else { $value = 'false'; } } echo "{$key}: {$value}\n"; } echo "\nApplication Settings\n"; echo "--------------------\n"; $configuration = __ApplicationContext::getInstance()->getConfiguration(); $settings = $configuration->getSettings(); $setting_values = array(); foreach ($settings as $key => $setting) { $value = $configuration->getPropertyContent($key); if (is_bool($value)) { if ($value) { $value = 'true'; } else { $value = 'false'; } } echo "{$key}: {$value}\n"; } }
protected function _saveAnnotations() { if (__Lion::getInstance()->getRuntimeDirectives()->getDirective('DEBUG_MODE') == false) { $cache = __ApplicationContext::getInstance()->getCache(); $cache->setData('annotations', $this->_annotations); } }
private function _connectToMemcachedServer() { $lion_runtime_directives = __Lion::getInstance()->getRuntimeDirectives(); //Checks if the Memcached module is loaded: if (!class_exists('Memcached')) { throw new Exception("PECL Memcached extension is not installed. Can not use the __MemCached cache handler."); } //Perform the connection to the memcached server: if (self::$_memcached == null) { self::$_memcached = new Memcached(); if ($lion_runtime_directives->hasDirective('MEMCACHED_SERVER')) { $server = $lion_runtime_directives->getDirective('MEMCACHED_SERVER'); } else { $server = self::MEMCACHED_DEFAULT_SERVER; } if ($lion_runtime_directives->hasDirective('MEMCACHED_PORT')) { $port = $lion_runtime_directives->getDirective('MEMCACHED_PORT'); } else { $port = self::MEMCACHED_DEFAULT_PORT; } if (self::$_memcached->addServer($server, $port)) { self::$_memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true); } else { throw new Exception("Can not connect to memcached server (server: {$server}, port: {$port})"); } } }
public final function __construct() { $cache_enabled = __Lion::getInstance()->getRuntimeDirectives()->getDirective('CACHE_ENABLED'); if (class_exists('__Client') && __Client::getInstance()->getRequestType() == REQUEST_TYPE_COMMAND_LINE) { $cache_enabled = false; } $this->setEnabled($cache_enabled); }
public function isCacheable() { //anonymous users in non-debug mode are candidates to cache the response if (__AuthenticationManager::getInstance()->isAnonymous() && !__Lion::getInstance()->getRuntimeDirectives()->getDirective('DEBUG_MODE')) { $return_value = $this->_cacheable; } else { $return_value = false; } return $return_value; }
protected function _getCacheDirectory() { if ($this->_cache_directory == null) { $cache_dir = __Lion::getInstance()->getRuntimeDirectives()->getDirective('CACHE_FILE_DIR') . DIRECTORY_SEPARATOR; if (preg_match('/^\\//', $cache_dir) || preg_match('/^\\w+:/', $cache_dir)) { $this->_cache_directory = $cache_dir; } else { $this->_cache_directory = APP_DIR . DIRECTORY_SEPARATOR . $cache_dir; } } return $this->_cache_directory; }
public function startRender() { if (__FrontController::getInstance()->getRequestType() != REQUEST_TYPE_XMLHTTP) { if (__ApplicationContext::getInstance()->hasProperty('INCLUDE_LION_JS')) { $include_lion_js = __ApplicationContext::getInstance()->getPropertyContent('INCLUDE_LION_JS'); } else { $include_lion_js = true; } if ($include_lion_js) { $local_js_lib = __ApplicationContext::getInstance()->getPropertyContent('JS_LIB_DIR'); $lion_js_file = __UrlHelper::resolveUrl(__UrlHelper::glueUrlParts($local_js_lib, 'lion.js')); __FrontController::getInstance()->getResponse()->prependContent('<script language="javascript" type="text/javascript" src="' . $lion_js_file . '"></script>' . "\n", 'lion-js'); } } $response_writer_manager = __ResponseWriterManager::getInstance(); if ($response_writer_manager->hasResponseWriter('javascript')) { $javascript_response_writer = $response_writer_manager->getResponseWriter('javascript'); } else { $javascript_response_writer = new __JavascriptOnDemandResponseWriter('javascript'); $response_writer_manager->addResponseWriter($javascript_response_writer); } if (!$javascript_response_writer->hasResponseWriter('setup-client-event-handler')) { $setup_client_event_handler_rw = new __JavascriptOnDemandResponseWriter('setup-client-event-handler'); $js_code = "\n" . '(__ClientEventHandler.getInstance()).setCode("' . __CurrentContext::getInstance()->getId() . '");' . "\n"; if (__Lion::getInstance()->getRuntimeDirectives()->getDirective('DEBUG_MODE')) { $js_code .= "(__ClientEventHandler.getInstance()).setDebug(true);\n"; if (__ApplicationContext::getInstance()->getPropertyContent('DEBUG_AJAX_CALLS') == true) { if (strtoupper(__ApplicationContext::getInstance()->getPropertyContent('DEBUGGER')) == 'ZEND') { $client_ip = $_SERVER['REMOTE_ADDR']; $debug_port = __ApplicationContext::getInstance()->getPropertyContent('ZEND_DEBUG_PORT'); $debug_url = 'index.ajax?' . 'start_debug=1&debug_port=' . $debug_port . '&debug_fastfile=1&debug_host=' . $client_ip . '&send_sess_end=1&debug_stop=1&debug_url=1&debug_new_session=1&no_remote=1'; $js_code .= "(__ClientEventHandler.getInstance()).setUrl('" . $debug_url . "');\n"; } } } if (!__FrontController::getInstance() instanceof __ComponentLazyLoaderFrontController && __FrontController::getInstance()->getRequestType() == REQUEST_TYPE_HTTP) { $url = __FrontController::getInstance()->getRequest()->getUrl(); $encoded_url = base64_encode(serialize($url)); $js_code .= "(__ClientEventHandler.getInstance()).setViewCode('" . $encoded_url . "');\n"; $flow_scope = __ApplicationContext::getInstance()->getFlowScope(); if ($flow_scope != null) { $js_code .= "(__ClientEventHandler.getInstance()).setFlowExecutionKey('" . $flow_scope->getId() . "');\n"; } } $setup_client_event_handler_rw->addJsCode($js_code); $javascript_response_writer->addResponseWriter($setup_client_event_handler_rw); } parent::startRender(); }
public function parse($content, __Configuration &$configuration) { $temp_dir = __PathResolver::resolvePath(__Lion::getInstance()->getRuntimeDirectives()->getDirective('TEMP_DIR')); $prefix = uniqid(); $filename = tempnam($temp_dir, $prefix); $fp = fopen($filename, 'w'); fwrite($fp, $content); fclose($fp); try { $this->load($filename, $configuration); } catch (Exception $e) { unlink($file); throw $e; } unlink($filename); }
private function &_createLoggerInstance($log_id) { $logger_class = __LogManager::DEFAULT_LOGGER_CLASS; //by default $lion_runtime_directives = __Lion::getInstance()->getRuntimeDirectives(); if ($lion_runtime_directives->hasDirective('LOG_ENABLED') && $lion_runtime_directives->getDirective('LOG_ENABLED')) { if ($lion_runtime_directives->hasDirective('LOGGER_CLASS')) { $logger_class = $lion_runtime_directives->getDirective('LOGGER_CLASS'); } } if (class_exists($logger_class)) { $logger = new $logger_class($log_id); } else { throw new __ConfigurationException('Class not found: ' . $logger_class); } return $logger; }
/** * Starts the session. * * This method raises 2 events:<br> * - EVENT_ON_SESSION_START: If the session has been started successful. * * @throws __SessionException if the session has already been started * */ public function startSession() { if ($this->getSessionStatus() != __SessionManager::STATUS_STOPPED) { throw new __SessionException('An atempt to start the session has been detected while the session was already started.'); } try { session_start(); $session = $this->getSession(); $app_name = __Lion::getInstance()->getRuntimeDirectives()->getDirective('APP_NAME'); if (!$session->hasData($app_name)) { $dummy_value = true; $session->setData($app_name, $dummy_value); $this->_new_session = true; } __EventDispatcher::getInstance()->broadcastEvent(new __Event($this, EVENT_ON_SESSION_START)); } catch (Exception $e) { throw new __SessionException($e->getMessage(), $e->getCode()); } }
private function _loadLog4PhpConf($log_id) { $lion_runtime_directives = __Lion::getInstance()->getRuntimeDirectives(); if (!defined('LOG4PHP_CONFIGURATION') && $lion_runtime_directives->hasDirective('LOG4PHP_CONFIG_FILE')) { $log4php_config_file = __PathResolver::resolvePath($lion_runtime_directives->getDirective('LOG4PHP_CONFIG_FILE')); define('LOG4PHP_CONFIGURATION', $log4php_config_file); } if (!defined('LOG4PHP_CONFIGURATOR_CLASS')) { if ($lion_runtime_directives->hasDirective('LOG4PHP_CONFIGURATION_TYPE')) { $configuration_type = $lion_runtime_directives->getDirective('LOG4PHP_CONFIGURATION_TYPE'); } else { $configuration_type = __Log4PhpLogger::DEFAULT_CONFIGURATION_TYPE; } switch (strtoupper($configuration_type)) { case 'XML': define('LOG4PHP_CONFIGURATOR_CLASS', LOG4PHP_DIR . '/xml/LoggerDOMConfigurator'); break; } } $this->_log4php_instance = LoggerManager::getLogger($log_id); }
public function defaultAction() { $mav = new __ModelAndView('settings'); try { $mav->lion_version = LION_VERSION_NUMBER; $mav->lion_build_date = LION_VERSION_BUILD_DATE; $mav->lion_build_changelist = LION_VERSION_CHANGE_LIST; $configuration = __ApplicationContext::getInstance()->getConfiguration(); $settings = $configuration->getSettings(); $setting_values = array(); foreach ($settings as $key => $setting) { $value = $configuration->getPropertyContent($key); if (is_bool($value)) { if ($value) { $value = 'true'; } else { $value = 'false'; } } $setting_values[] = array('name' => $key, 'value' => $value); } $mav->settings = $setting_values; $lion_directives = __Lion::getInstance()->getRuntimeDirectives()->getDirectives(); $runtime_directives_values = array(); foreach ($lion_directives as $key => $value) { if (is_bool($value)) { if ($value) { $value = 'true'; } else { $value = 'false'; } } $runtime_directives_values[] = array('name' => $key, 'value' => $value); } $mav->runtime_directives = $runtime_directives_values; } catch (Exception $e) { $mav->status = 'ERROR'; } return $mav; }
public function defaultAction() { if (__Lion::getInstance()->getRuntimeDirectives()->getDirective('DEBUG_MODE')) { //perform the validation: __ServerConfigurationValidator::validate(); $url = __UriFactory::getInstance()->createUri()->setController('index')->getAbsoluteUrl(); $file = basename($url); $baseurl = str_replace($file, '', $url); $message = <<<CODESET <h1>Do not execute index.php from your browser</h1><br> Lion intercepts all requests in the form of <b>{$baseurl}...</b> and redirects them to the MVC in order to show the page corresponding to each one.<br> i.e. you may use {$url}, which will be intercepted by lion and redirected to the index controller<br> <br> Go to <a href="{$url}">{$file}</a><br> <br> <br> See the <a href="http://www.lionframework.org/documentation">documentation</a> for more information about urls and the MVC. CODESET; echo $message; } }
public function displayError(Exception $exception) { if ($exception instanceof __LionException) { $error_title = $exception->getErrorTitle(); } else { $error_title = 'Core Error'; } $error_message = $exception->getMessage(); $error_code = $exception->getCode(); $message = new __AsyncMessage(); $message->getHeader()->setStatus(__AsyncMessageHeader::ASYNC_MESSAGE_STATUS_ERROR); if (__Lion::getInstance()->getRuntimeDirectives()->getDirective('DEBUG_MODE')) { $message->getHeader()->setMessage("{$error_title} ({$error_code}):\n{$error_message}"); } __FrontController::getInstance()->getResponse()->addContent($message->toJson()); __FrontController::getInstance()->getResponse()->flush(); exit; }
private function &_parseIncludes(__Configuration &$configuration, $basedir) { $configuration_section = $configuration->getSection('configuration'); if ($configuration_section != null) { //read configuration directives before read other sections: $configuration_directives = $configuration_section->getSection('configuration-directives'); if ($configuration_directives != null) { $this->_readConfigurationDirectives($configuration_directives); } //read the other sections: $sections = $configuration_section->getSections(); foreach ($sections as $section) { switch (strtoupper($section->getName())) { case 'INCLUDE': $expression = $section->getProperty('#text')->getContent(); //finally, resolve the path: $expression = __PathResolver::resolvePath($expression, $basedir); if (__Lion::getInstance()->getRuntimeDirectives()->getDirective('DEBUG_MODE') && (strpos($expression, '...') !== false || strpos($expression, '*') !== false)) { $this->_configuration_locators[$expression] = $expression; } $files_to_include = __FileResolver::resolveFiles($expression); foreach ($files_to_include as $file_to_include) { $included_configuration = $this->loadConfigurationFile($file_to_include); $configuration->merge($included_configuration); unset($included_configuration); } break; } } } return $configuration; }
public function &createContext($context_id, $context_base_dir, $configuration_file = null) { if ($this->hasContext($context_id)) { throw __ExceptionFactory::getInstance()->createException('Context already exists for context identifier ' . $context_id); } //create a class file locator for the context to create to $class_file_locator = new __ClassFileLocator($context_base_dir); __ClassLoader::getInstance()->addClassFileLocator($class_file_locator); //scann in depth starting from configuration location before scanning the context basedir if ($configuration_file != null) { $configuration_base_dir = dirname($configuration_file); if (strpos($configuration_base_dir, $context_base_dir) === false) { $configuration_class_file_locator = new __ClassFileLocator($configuration_base_dir); __ClassLoader::getInstance()->addClassFileLocator($configuration_class_file_locator); } } //do not read nor store into the cache the initial context in case of DEBUG_MODE active: if (__Lion::getInstance()->getRuntimeDirectives()->getDirective('DEBUG_MODE')) { $cache = null; //by default $context = null; } else { $cache = __CacheManager::getInstance()->getCache(); $context = $cache->getData('__Context__' . $context_id); if ($context != null) { $this->_addContext($context); } } //if no context has been read from cache: if ($context == null) { $context = new __Context($context_id, $context_base_dir); $this->_addContext($context); $context->loadConfiguration($configuration_file); if ($cache != null) { $cache->setData('__Context__' . $context_id, $context); } } //Startup the context $context->startup(); //return a reference to the already created context: return $context; }
protected function _loadClassLocations() { //normalize the base dir: $this->_normalized_basedir = rtrim($this->_base_dir, DIRECTORY_SEPARATOR); //load the classes: $debug_mode = __Lion::getInstance()->getRuntimeDirectives()->getDirective('DEBUG_MODE'); if (!$debug_mode) { $this->_metadata = __CacheManager::getInstance()->getCache()->getData('classloader_' . $this->_normalized_basedir); } if ($this->_metadata == null) { $this->_metadata = $this->_loadMetadata(); if (!$debug_mode) { __CacheManager::getInstance()->getCache()->setData('classloader_' . $this->_normalized_basedir, $this->_metadata); } } $this->_mapping = $this->_metadata['mapping']; $this->_autoloaders = $this->_metadata['autoloaders']; $this->_classpaths = $this->_metadata['classpaths']; }