예제 #1
0
 public function __construct($options = array())
 {
     parent::__construct($options);
     // Schedule: Update the users activity in the active users table
     Event::add("framework.router.beforehooks.after", function ($name, $parameters) {
         $session = Registry::get('session');
         $user = $session->get('user');
         $time = date("Y-m-d H:i:s");
         if ($user) {
             // @todo REPLACE INTO very Mysql specific build merge method for alternate db
             Database::replace("REPLACE INTO user_active VALUES (:user, :time)", array(':user' => $user, ':time' => $time));
         }
     });
 }
예제 #2
0
 public function load(Reader $reader)
 {
     $databases = $reader->read();
     foreach ($databases as $name => $datas) {
         // Check name
         if (!Validate::isVariableName($name)) {
             throw new \Exception('Name of database must be a valid variable name');
         }
         // Check essential parameters
         if (!isset($datas['adaptater'])) {
             throw new \Exception('Miss adaptater config param for database : "' . $name . '"');
         }
         if (!isset($datas['type'])) {
             throw new \Exception('Miss type config param for database : "' . $name . '"');
         }
         if (!isset($datas['server'])) {
             throw new \Exception('Miss server config param for database : "' . $name . '"');
         }
         // Create database instance
         $database = new DatabaseManager($name, $datas['type'], DatabaseManager::factory($datas['adaptater'], $name, 'framework\\database\\adaptaters', 'framework\\database\\IAdaptater'));
         // Fetch servers
         foreach ($datas['server'] as $server) {
             // extract server informations
             extract($server);
             // extract dsn
             if (isset($dsn)) {
                 extract(Tools::parseDsn($dsn));
             }
             // check required infos
             if (!isset($type)) {
                 throw new \Exception('Miss server type');
             }
             if (!isset($dbuser)) {
                 throw new \Exception('Miss server dbuser type');
             }
             if (!isset($dbpassword)) {
                 throw new \Exception('Miss server dbpassword type');
             }
             if (!isset($driver)) {
                 throw new \Exception('Miss driver type');
             }
             if (!isset($host)) {
                 throw new \Exception('Miss server host type');
             }
             if (!isset($port)) {
                 throw new \Exception('Miss server port type');
             }
             if (!isset($dbname)) {
                 throw new \Exception('Miss server dbname type');
             }
             if (!isset($charset)) {
                 throw new \Exception('Miss server charset type');
             }
             // Check driver is supported by database adaptater
             if (!$database->isValidDriver($driver)) {
                 throw new \Exception('Invalid driver : "' . $driver . '", not supported database adaptater : "' . $datas['adaptater'] . '"');
             }
             // Create server instance
             $serverInstance = new Server($type, $dbuser, $dbpassword, $driver, $host, $port, $dbname, $charset);
             if (isset($dsn)) {
                 $serverInstance->setDsn($dsn, false);
             }
             // Add into servers list
             $database->addServer($serverInstance);
             //flush vars
             unset($type, $dbuser, $dbpassword, $driver, $host, $port, $dbname, $charset, $dsn, $serverInstance);
         }
         // Add database
         DatabaseManager::addDatabase($name, $database, true);
     }
 }
예제 #3
0
 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;
     }
 }
예제 #4
0
 public function getDb($returnEngine = false)
 {
     return Database::getDatabase($this->_modelDBName, $returnEngine);
 }
예제 #5
0
 /**
  * Count the number of rows matched
  * @param  array  $where Any where clauses to be used in selection
  * @return int           Number of rows selected
  */
 protected function _count($where = array())
 {
     $query = Database::query()->from($this->table);
     foreach ($where as $clause => $value) {
         $query->where($clause, $value);
     }
     return $query->count();
 }
예제 #6
0
 public function execute($checkBindNumber = true)
 {
     if (Application::getDebug()) {
         Benchmark::getInstance($this->_configName)->startTime()->startRam();
     }
     if (is_null($this->_query) || !$this->_statement) {
         throw new \Exception('Prepare query before execute');
     }
     if ($checkBindNumber) {
         if (count($this->_params) < $this->_paramsNumberNecesary) {
             throw new \Exception('Miss bind parameters');
         }
     }
     // Bind parameters
     $i = 0;
     foreach ($this->_params as $param) {
         $bindName = $this->_bindParamType === Database::PARAM_BIND_POSITIONAL ? $i + 1 : ':' . $this->_namedParamOrder[$i];
         if ($param['isParam']) {
             $this->_statement->bindParam($bindName, $param['value'], $param['type']);
         } else {
             $this->_statement->bindValue($bindName, $param['value'], $param['type']);
         }
         $i++;
     }
     // Execute
     $this->_execute = $this->_statement->execute();
     //check error
     $lastError = $this->getLastError(true);
     if (!is_null($lastError)) {
         Logger::getInstance()->error('Sql query : ' . $this->_query . ' has error : ' . $lastError);
     }
     // Debug
     if (Application::getDebug()) {
         Database::getDatabase($this->_configName)->logQuery($this->_query, $this->_params, $this->_bindParamType, $lastError);
     }
     return $this->_execute;
 }
예제 #7
0
 /**
  * Validate that a given field value exists in a given database table
  * @param  string $field     Field being validated
  * @param  mixed $value      Value being validated
  * @param  array $parameter  Optional column name
  * @return bool
  */
 protected function _validateExists($field, $value, $parameters)
 {
     // If column name does not match field name, it can be specified
     // in the second parameter position, after the table name
     if (isset($parameters[0])) {
         $attribute = $parameters[0];
     }
     // Count the number of elements to be looked for.
     // If array count all values in array else count equals 1
     if (is_array($value)) {
         $count = count($value);
     } else {
         $count = 1;
     }
     $query = Database::query()->from($attribute);
     // If given value was an array check for all values in array
     // otherwise check for single given value in the table
     if (is_array($value)) {
         $query = $query->whereIn($field, $value);
     } else {
         $query = $query->where($field, '=', $value);
     }
     return $query->count()->num >= $count;
 }