Exemplo n.º 1
0
 /**
  * On charge les différente connexions à la BDD
  */
 public function initDatabase()
 {
     $configDatabase = Config::get('database');
     if ($configDatabase) {
         $databases = $configDatabase->config;
         foreach ($databases as $name => $database) {
             $type = $this->getConfigField($database, 'type', 'mysql');
             $host = $this->getConfigField($database, 'host', 'localhost');
             $port = $this->getConfigField($database, 'port', 3306);
             $encoding = $this->getConfigField($database, 'encoding', 'utf8');
             $user = $this->getConfigField($database, 'user', 'root');
             $pass = $this->getConfigField($database, 'pass', 'secret');
             $name = $this->getConfigField($database, 'name', 'dev');
             $prefixDbname = 'dbname=';
             $resultPrefixShow = in_array($type, ['sqlite', 'sqlite2']);
             if ($resultPrefixShow) {
                 $prefixDbname = '';
             }
             $dsn = "{$type}:{$prefixDbname}{$name}";
             if (!$resultPrefixShow) {
                 $dsn .= ";host={$host};charset={$encoding}";
                 if ($port != 3306) {
                     $dsn .= ";port={$port}";
                 }
             }
             try {
                 $connection = new Connection($dsn, $user, $pass);
                 Connections::add($name, $connection);
             } catch (PDOException $exception) {
             }
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Chargement une classe de type Table
  *
  * @param string $tableName
  * @return Table
  */
 protected function loadTable($tableName)
 {
     if (!isset($this->tables[$tableName])) {
         $this->tables[$tableName] = DIC::get($tableName);
         $this->tables[$tableName]->db = Connections::get();
     }
     return $this->tables[$tableName];
 }
Exemplo n.º 3
0
 /**
  * Test du chargement des tables
  */
 public function testLoadTable()
 {
     require_once __DIR__ . '/../Test/Table/NewsTable.php';
     Config::add(__DIR__ . '/../Test/test.php', 'app');
     Connections::add('dev', new Connection('sqlite::memory:'));
     $controller = new Controller();
     $this->assertCount(0, $controller->tables);
     $this->assertInstanceOf(NewsTable::class, $controller->news);
     $this->assertCount(1, $controller->tables);
 }