/** * checks database connection status * if theres no connection creates a new one * @return PDO */ public function get_connection() { # lets check if we already got a connection to this host if (empty($this->connection)) { # set the username and password $config = App::get_instance()->get_config(); $user = $config->database['mysql']['user']; $password = $config->database['mysql']['password']; # make the connection $this->connect($user, $password); } return $this->connection; }
/** * constructor */ public function __construct() { // get the application config $config = App::get_instance()->get_config(); // set the default db engine $this->set_default_db_engine($config); // set the model adapter $this->set_model_adapter(); // set a database prefix if (isset($config->database[$this->db_engine]['prefix'])) { $this->set_prefix($config->database[$this->db_engine]['prefix']); } }
/** * Sets an application error * @param string $message the error message * @param boolean $is_critical whether is a critical error or not */ public static function set($message, $level = null) { // make sure the error level is valid $error_level = array_key_exists($level, self::$error_levels) ? $level : self::ERROR; $error_level_name = self::$error_levels[$error_level]; // get the error caller $error_caller = self::get_error_caller(); // sets the error message $error_message = sprintf(self::ERROR_FORMAT, $error_level_name, (string) $message, $error_caller['class'], $error_caller['function'], isset($error_caller['file']) ? $error_caller['file'] : '', print_r($error_caller['args'], true)); $config = App::get_instance()->get_config(); if (isset($config->database) && !empty($config->database['mongo']['host'])) { Logger::log($error_message, 'error', $error_level_name); } // send the error trigger_error($error_message, $error_level); }
/** * Get an instance of the required cache system * @param string $type the cache type * @param array $options the config options set for the cache system * @return ICache */ public static function get_instance($type = null, array $options = []) { if (empty($options)) { $options = App::get_instance()->get_config()->cache; } // return the null object if cache is not enabled if (empty($options[self::ENABLED])) { return new Void($options); } switch ($type) { case self::DEFAULT_KEY: case '': case null: if (isset($options[self::DEFAULT_KEY])) { return self::get_instance($options[self::DEFAULT_KEY], $options); } else { Error::set(self::ERROR_DEFAULT_NOT_SET, false); } break; case self::APC: return new Apc($options); break; case self::FILE: return new File($options); break; case self::MEMCACHED: return new Memcached($options); break; case self::REDIS: return new Redis($options); break; case self::VOID: return new Void($options); break; default: Error::set(sprintf(self::ERROR_INVALID_CACHE_SYSTEM, $type)); break; } }
/** * Gets the controller instance * @return Controller */ private function get_controller_instance() { $default_path = 'Controller\\' . str_replace(DIRECTORY_SEPARATOR, '\\', $this->controller); $module = App::get_instance()->get_module(); if (isset($module)) { $module_path = 'Module\\' . ucfirst(strtolower($module)) . '\\' . $default_path; if (class_exists($module_path)) { return new $module_path(); } } if (!class_exists($default_path)) { Error::set(sprintf(self::ERROR_NO_CONTROLLER_FILE, $this->controller, $default_path)); } return new $default_path(); }
/** * Gets the path to a configuration file * @param string $module * @return string */ private function get_config_path($module = null) { return $this->app->get_application_folder() . (isset($module) ? sprintf(self::DEFAULT_CONFIG_MODULE_FOLDER, $module) : self::DEFAULT_CONFIG_PATH); }
<?php /** * Namespaces */ use Kima\Prime\App; // Define path to app directory if (!defined('ROOT_FOLDER')) { define('ROOT_FOLDER', realpath(dirname(__FILE__) . '/..')); } // loading composer autoload require_once ROOT_FOLDER . '/vendor/autoload.php'; App::get_instance()->run(['/' => 'Index', '/([A-Za-z0-9]+)' => 'Index']);
/** * Gets the required http protocol * @return string */ public static function get_protocol() { return App::get_instance()->is_https() ? self::PROTOCOL_HTTPS : self::PROTOCOL_HTTP; }
/** * Test bootstrap method */ public function set_language() { // custom language app setup $app = App::get_instance()->set_language('en'); }
/** * Checks database connection status * if theres no connection creates a new one * * @return mixed */ public function get_connection() { // check if we already got a connection to this host if (empty($this->connection)) { // set the username and password $config = App::get_instance()->get_config(); $user = !empty($config->database['mongo']['user']) ? $config->database['mongo']['user'] : ''; $password = !empty($config->database['mongo']['password']) ? $config->database['mongo']['password'] : ''; // make the connection $this->connect($user, $password); } return $this->connection; }
/** * Alternative commit function while the bug 'waiteflush' is fixed * @See https://bugs.php.net/bug.php?id=62332 * @param bool $asynch asynchronous solr commit * @return */ private function commit($asynch = true) { $config = App::get_instance()->get_config(); $response = false; if (empty($config->search['solr'][$this->core])) { Error: set(self::ERROR_NO_CONFIG); } else { $solrConfig = $config->search['solr'][$this->core]; $solrAddress = $solrConfig['hostname'] . ':' . $solrConfig['port'] . '/' . $solrConfig['path']; $url = 'http://' . $solrAddress . '/update?commit=true'; // Open curl session $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); // timeout after 10 ms, tried with 100 ms but it seems some of the // calls were not correctly routed if ($asynch) { curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10); } // this allows curl to ignore the signals and correctly timeout on MS // "If libcurl is built to use the standard system name resolver, that // portion of the transfer will still use full-second resolution for timeouts // with a minimum timeout allowed of one second." // http://nl3.php.net/manual/en/function.curl-setopt.php#104597 curl_setopt($ch, CURLOPT_NOSIGNAL, 1); // Stop printing the result on the screen curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //execute post, this will return false as we are not waiting for the response $response = curl_exec($ch); //close connection curl_close($ch); } return $response; }
/** * Gets the config adapted for the current view * @param array $config The view config * @param string $module */ private function get_view_config(array $config, $module) { $app = App::get_instance(); $app_config = $app->get_config(); // disable layout if not wanted if (!$this->use_layout) { unset($config['layout']); } // set cache config $config['cache'] = $app_config->cache; // set module config if necessary if ($module) { $config['folder_failover'] = $app->get_view_folder(); $config['folder'] = $app->get_module_folder() . $module . '/view'; } return $config; }
/** * Gets the strings paths for the current language * @param string $language * @return array */ private static function get_strings_paths($language) { // set the strings paths using if exists the array of l10n paths else the default application path $strings_paths = empty(self::$l10n_paths) ? [App::get_instance()->get_l10n_folder()] : self::$l10n_paths; foreach ($strings_paths as &$strings_path) { // add file name to the string path $strings_path .= $language . '.ini'; // validate the string path if (!is_readable($strings_path)) { Error::set(sprintf(self::ERROR_INVALID_STRINGS_PATH, $strings_path)); } } return $strings_paths; }