protected function getDatabase($dsid, $withDbName = true)
 {
     $datasources = $this->datasources->xpath("/DataSources/DataSource[@id='" . $dsid . "']");
     $dbid = (int) $datasources[0]['database'];
     $databases = $this->datasources->xpath("/DataSources/Databases/Database[@id='" . $dbid . "']");
     $dbtype = (string) $databases[0]['type'];
     $dbname = (string) $databases[0]['name'];
     $database = new Database(null, $dbid, $dbtype, $dbname);
     if ((string) $databases[0]['label'] != "") {
         $database->setLabel((string) $databases[0]['label']);
     } else {
         $database->setLabel($dbname);
     }
     if ((string) $databases[0]['host'] != "") {
         $database->setHost((string) $databases[0]['host']);
     }
     if ((string) $databases[0]['port'] != "") {
         $database->setPort((int) $databases[0]['port']);
     }
     if ((string) $databases[0]['user'] != "") {
         $database->setUser((string) $databases[0]['user']);
     }
     if ((string) $databases[0]['password'] != "") {
         $database->setPassword((string) $databases[0]['password']);
     } elseif ((string) $databases[0]['user'] != "") {
         try {
             $user = $this->get('kernel')->getContainer()->getParameter('database_user');
             if ((string) $databases[0]['user'] == $user) {
                 $database->setPassword($this->get('kernel')->getContainer()->getParameter('database_password'));
             }
         } catch (\Exception $e) {
         }
     }
     $database->connect($withDbName);
     return $database;
 }
示例#2
0
 public static function installUsers(Event $event)
 {
     $event->getIO()->write("Installing the users of the administration interface");
     $extras = $event->getComposer()->getPackage()->getExtra();
     $symfonyDir = dirname(dirname(dirname(dirname(__DIR__))));
     $appDir = $symfonyDir . DIRECTORY_SEPARATOR . $extras['symfony-app-dir'];
     $configDir = $appDir . DIRECTORY_SEPARATOR . 'config';
     $databasesDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'databases';
     if (($parameters = self::getParameters($event, $configDir)) === false) {
         return;
     }
     $parameters = (object) $parameters;
     $driver = $parameters->database_driver;
     switch ($driver) {
         case 'pdo_mysql':
             $database = new Database(null, 1, 'mysqli', $parameters->database_name);
             $database->setHost($parameters->database_host);
             if (isset($parameters->database_port)) {
                 $database->setPort($parameters->database_port);
             }
             $database->setUser($parameters->database_user);
             if (isset($parameters->database_password)) {
                 $database->setPassword($parameters->database_password);
             }
             break;
         case 'pdo_pgsql':
             $database = new Database(null, 1, 'pgsql', $parameters->database_name);
             $database->setHost($parameters->database_host);
             if (isset($parameters->database_port)) {
                 $database->setPort($parameters->database_port);
             }
             $database->setUser($parameters->database_user);
             if (isset($parameters->database_password)) {
                 $database->setPassword($parameters->database_password);
             }
             break;
         case 'pdo_sqlite':
             $name = basename($parameters->database_path);
             $database = new Database(null, 1, 'sqlite', $name);
             break;
         default:
             $event->getIO()->write(sprintf("Unsupported database driver: %s", $driver));
             return;
     }
     if (($script = file_get_contents($databasesDir . DIRECTORY_SEPARATOR . 'fos_user.sql')) === FALSE) {
         $event->getIO()->write(sprintf("Unable to read  %s", $databasesDir . DIRECTORY_SEPARATOR . 'fos_user.sql'));
         return;
     }
     try {
         $database->connect(false);
         switch ($driver) {
             case 'pdo_mysql':
                 $database->exec("create database if not exists " . $parameters->database_name . " character set utf8");
                 $database->setConnected(false);
                 $database->connect();
                 break;
             case 'pdo_pgsql':
                 $database->exec("create database " . $parameters->database_name . " encoding 'UTF8'");
                 $database->setConnected(false);
                 $database->connect();
                 break;
         }
     } catch (\Exception $e) {
         $event->getIO()->write("Can't connect to database : " . $e->getMessage());
         return;
     }
     $script = preg_split("/;\n/", $script, -1, PREG_SPLIT_NO_EMPTY);
     foreach ($script as $i => $sql) {
         if (preg_match("/^CREATE TABLE/i", $sql)) {
             switch ($driver) {
                 case 'pdo_mysql':
                     $sql = preg_replace("/id INTEGER NOT NULL,/i", "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,", $sql);
                     break;
                 case 'pdo_pgsql':
                     $sql = preg_replace("/id INTEGER NOT NULL,/i", "id SERIAL PRIMARY KEY,", $sql);
                     $sql = preg_replace("/\\s+DATETIME\\s+/i", " TIMESTAMP ", $sql);
                     break;
                 case 'pdo_sqlite':
                     $sql = preg_replace("/id INTEGER NOT NULL,/i", "id INTEGER PRIMARY KEY AUTOINCREMENT,", $sql);
                     break;
             }
         }
         try {
             $database->exec($sql);
         } catch (\Exception $e) {
             $event->getIO()->write("Can't execute install users script : " . $e->getMessage());
             break;
         }
     }
     switch ($driver) {
         case 'pdo_mysql':
             $sql = 'alter table fos_user auto_increment = 3';
             break;
         case 'pdo_pgsql':
             $sql = "alter sequence fos_user_id_seq restart with 3";
             break;
         case 'pdo_sqlite':
             $sql = "update sqlite_sequence set seq = 2 where name = 'fos_user'";
             break;
         default:
             return;
     }
     try {
         $database->exec($sql);
     } catch (\Exception $e) {
         $event->getIO()->write("Can't set sequence for table fos_user : " . $e->getMessage());
     }
 }