Ejemplo n.º 1
0
 /**
  * @inheritDoc
  */
 public function __construct(Config $config, Instance $instance)
 {
     $config = new Config(array_merge($this->defaultConfig, $config->toArray()));
     $this->instance = $instance;
     if (isset($config->socket)) {
         $dsn = 'mysql:' . 'unix_socket=' . $config->socket . ';' . 'dbname=' . $config->database;
     }
     if (!isset($config->socket)) {
         $dsn = 'mysql:' . 'host=' . $config->hostname . ';' . 'port=' . $config->port . ';' . 'dbname=' . $config->database;
     }
     $this->client = new PDO($dsn, $config->username, $config->password);
 }
Ejemplo n.º 2
0
 /**
  * Create a new instance of the database and driver.
  *
  * @param string|null       $name   The connection name
  * @param Config|array|null $config The config for the instance
  */
 public function __construct($name = null, $config = null)
 {
     // If a name isn't provided, then we'll use the default
     $this->name = $name ?: 'default';
     // Get the config for this instance
     switch (true) {
         case $config instanceof Config:
             break;
         case is_array($config):
             $config = new Config($config);
             break;
         case $config === null:
             $config = Config::get($this->name);
             break;
     }
     // Store the database name
     $this->databaseName = $config->database;
     // Initialise the driver
     $driverClass = $this->drivers[$config->driver];
     $this->driver = new $driverClass($config, $this);
     // If the driver isn't created, throw an exception
     if (!$this->driver instanceof Driver) {
         throw new InvalidDriverException($driver . ' is not a valid driver.');
     }
 }
Ejemplo n.º 3
0
 /**
  * Work the model class based on the table name, and cache it for later.
  *
  * @param Table $table The table to get a model for
  *
  * @return string The name of a model class
  */
 protected function getModelClass($table)
 {
     if (isset(static::$modelClassCache[$table->name])) {
         return static::$modelClassCache[$table->name];
     }
     $namespace = null;
     $config = Config::get($table->instance->name);
     if (isset($config->model_namespace)) {
         $namespace = $config->model_namespace;
     }
     if ($namespace === null) {
         $config = Config::get('default');
         if (isset($config->model_namespace)) {
             $namespace = $config->model_namespace;
         }
         if ($namespace === null) {
             $namespace = 'Models';
         }
     }
     $modelClass = Str::singularize($table->name);
     $modelClass = Str::camelCaps($namespace . '\\' . $modelClass);
     if (!class_exists($modelClass)) {
         $modelClass = Model::class;
     }
     return static::$modelClassCache[$table->name] = $modelClass;
 }
Ejemplo n.º 4
0
 /**
  * @inheritDoc
  */
 public function __construct(Config $config, Instance $instance)
 {
     $config = new Config(array_merge($this->defaultConfig, $config->toArray()));
     $this->client = new Client($config->hostname, $config->username, $config->password, $config->database, $config->port, $config->socket);
     $this->instance = $instance;
 }