/** * 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.'); } }
/** * 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; }