Example #1
0
 /**
  * Prepares component
  *
  * @param stdClass $database
  */
 public static function setup($database)
 {
     if (!isset($database->adapter)) {
         throw new \Phalcon\Exception('Unspecified database Adapter in your configuration!');
     }
     $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $database->adapter;
     if (!class_exists($adapter)) {
         throw new \Phalcon\Exception('Invalid database Adapter!');
     }
     $configArray = $database->toArray();
     unset($configArray['adapter']);
     self::$_connection = new $adapter($configArray);
     self::$_databaseConfig = $database;
     $profiler = new Profiler();
     $eventsManager = new EventsManager();
     $eventsManager->attach('db', function ($event, $connection) use($profiler) {
         if ($event->getType() == 'beforeQuery') {
             $profiler->startProfile($connection->getSQLStatement());
         }
         if ($event->getType() == 'afterQuery') {
             $profiler->stopProfile();
         }
     });
     self::$_connection->setEventsManager($eventsManager);
 }