Skip to content

Commit

Permalink
critical update, no usage modification
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrick Peter committed Oct 13, 2023
1 parent 629e0aa commit ed7dffd
Show file tree
Hide file tree
Showing 19 changed files with 216 additions and 174 deletions.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
],
"require": {
"php": ">=8.0",
"tamedevelopers/support": "^2.0.0",
"vlucas/phpdotenv": "^5.4.1",
"filp/whoops": "^2.15"
"tamedevelopers/support": "^2.0.0"
},
"autoload": {
"files": [
Expand Down
4 changes: 2 additions & 2 deletions src/AutoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ public static function configPagination(?array $options = [])
| Adding Pagination Configuration into Constant
|--------------------------------------------------------------------------
*/
if ( ! defined('PAGINATION_CONFIG') ) {
define('PAGINATION_CONFIG', $default);
if ( ! defined('TAME_PAGI_CONFIG') ) {
define('TAME_PAGI_CONFIG', $default);
}
}

Expand Down
10 changes: 1 addition & 9 deletions src/Capsule/AppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Tamedevelopers\Database\AutoLoader;
use Tamedevelopers\Support\Capsule\Manager;
use Tamedevelopers\Support\Capsule\FileCache;
use Tamedevelopers\Database\Capsule\DebugManager;
use Tamedevelopers\Support\Capsule\DebugManager;

class AppManager{

Expand Down Expand Up @@ -63,14 +63,6 @@ public static function bootLoader()
|--------------------------------------------------------------------------
*/
AutoLoader::start();

/*
|--------------------------------------------------------------------------
| Default connection driver is `mysql`
| use DB::connection() \to connection to other connection instance
|--------------------------------------------------------------------------
*/
DB::connection();
}

}
49 changes: 0 additions & 49 deletions src/Capsule/DebugManager.php

This file was deleted.

16 changes: 15 additions & 1 deletion src/Connectors/ConnectionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,21 @@ public function __construct($data = null, $name = null, $connection = null)
$this->pdo = $connection['pdo'] ?? $connection['message'];
$this->config = array_merge($connection['config'], ['name' => $name]);
$this->database = $this->config['database'];
$this->tablePrefix = $this->config['prefix'];
$this->tablePrefix = $this->getTablePrefixIfAllowed();
}
}

/**
* Get Table Prefix
* @return string|null
*/
private function getTablePrefixIfAllowed()
{
// if prefixes is set and is `true`
if(isset($this->config['prefix_indexes']) && $this->config['prefix_indexes']){
if(isset($this->config['prefix'])){
return $this->config['prefix'];
}
}
}

Expand Down
134 changes: 105 additions & 29 deletions src/Connectors/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Tamedevelopers\Database\Connectors;

use PDO;
use Exception;
use Tamedevelopers\Support\Server;
use Tamedevelopers\Database\Schema\Builder;
use Tamedevelopers\Support\Capsule\Manager;
use Tamedevelopers\Database\DatabaseManager;
Expand All @@ -25,23 +27,77 @@ class Connector {
*/
private $connection;

/**
* @var array|null
*/
private $default;

/**
* @var string|null
*/
private $name;

/**
* Save instances of all connection
* @var mixed
*/
private static $connections = [];

/**
* Constructors
*
* @param string|null $name\Database connection name
* @param mixed $connection \Connection instance
*
* @param string|null $name - Driver name
* @param mixed $connection - Connection instance
* @param array $data - Default data
*/
public function __construct($name = null, mixed $connection = null)
public function __construct($name = null, $connection = null, $data = [])
{
$this->setConnectionName($name);
$this->setConnection($connection);
$this->setDefaultData($data);
}

/**
* Add to connection instance
*
* @param string|null $name - Driver name
* @param mixed $connection - Connection instance
* @param array $data - Default data
*
* @return \Tamedevelopers\Database\Connectors\Connector
*/
static public function addConnection($name = null, $connection = null, $data = [])
{
$driver = DatabaseManager::driverValidator($name);

// driver name
$driverName = $driver['name'];

// connector object
self::$connections[$driverName] = new self(
connection: $connection,
name: $driverName,
data: $data,
);

return self::$connections[$driverName];
}

/**
* Remove from connection instance
*
* @param string|null $name - Driver name
*
* @return void
*/
static public function removeFromConnection($name = null)
{
$driver = DatabaseManager::driverValidator($name);

// driver name
$driverName = $driver['name'];

unset(self::$connections[$driverName]);
}

/**
Expand Down Expand Up @@ -96,8 +152,8 @@ public function configPagination(array $options = [])

// Only if the Global Constant is not yet defined
// If set to allow global use of ENV Autoloader Settings
if(defined('PAGINATION_CONFIG') && Manager::isEnvBool(PAGINATION_CONFIG['allow']) === true){
$paginator->configPagination(PAGINATION_CONFIG);
if(defined('TAME_PAGI_CONFIG') && Manager::isEnvBool(TAME_PAGI_CONFIG['allow']) === true){
$paginator->configPagination(TAME_PAGI_CONFIG);
} else{
$paginator->configPagination($options);
}
Expand All @@ -111,18 +167,24 @@ public function configPagination(array $options = [])
*/
private function buidTable($table = null)
{
// begin build
$builder = new Builder;
$builder->manager = new Manager;
$builder->dbManager = new DatabaseManager;

// create instance of self
$instance = new self(
$this->name,
$this->dbConnection(),
);
// get saved connection from $connections array
$instance = self::$connections[$this->name] ?? null;

// There's no connecton instance set
if(empty($instance)){
throw new Exception("There's no active connection! Unknown connection [{$this->name}].");
}

// set connection
$instance->connection = $this->dbConnection();

// setup table name
$builder->from = $this->compileTableWithPrefix($table, $this->getConfig());
$builder->from = $table;

// building of table name is only called once
// so we will build the instance of Connection data into the
Expand Down Expand Up @@ -161,8 +223,9 @@ public function getTablePrefix()
public function dbConnection($mode = null)
{
// get connection data
$conn = DatabaseManager::getConnection($this->name);

// merge data to default if provided, before we try to connect
$conn = self::getConnectionFromDatabaseFile($this->name, $this->default);

// connection data
$connData = self::createConnector($conn['driver'])->connect($conn);

Expand Down Expand Up @@ -202,25 +265,25 @@ public function getDatabaseName()
*/
public function getConfig()
{
return DatabaseManager::getConnection($this->name);
return self::getConnectionFromDatabaseFile($this->name, $this->default);
}

/**
* Get Table Name
* @param string $table
* @param array $data
* @return string
* Get Connection data
*
* @param string|null $name
* @param array|null $default
*
* @return array
*/
private static function compileTableWithPrefix($table = null, ?array $data = null)
private static function getConnectionFromDatabaseFile($name = null, $default = [])
{
// check prefixes
if(isset($data['prefix_indexes']) && $data['prefix_indexes']){
if(isset($data['prefix'])){
$table = "{$data['prefix']}{$table}";
}
}
$data = Server::config(
DatabaseManager::getConnectionKey($name),
[]
);

return $table;
return array_merge($data, $default ?? []);
}

/**
Expand All @@ -247,6 +310,19 @@ private function setConnection($connection = null)
}
}

/**
* Set default connection data
*
* @param array $default
* @return void
*/
private function setDefaultData($default = [])
{
if(!empty($default)){
$this->default = $default;
}
}

/**
* Set connection connection name
*
Expand All @@ -256,7 +332,7 @@ private function setConnection($connection = null)
private function setConnectionName($name = null)
{
$this->name = empty($name)
? config("database.default")
? Server::config("database.default")
: $name;
}

Expand All @@ -270,7 +346,7 @@ private function isModelDriverCreated()
{
if(self::isModelExtended()){
$this->setConnectionName();
$key = DatabaseManager::getCacheKey($this->name);
$key = DatabaseManager::getConnectionKey($this->name);
if (!FileCache::exists($key)) {
DatabaseManager::connection($this->name);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Connectors/PostgresConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
namespace Tamedevelopers\Database\Connectors;

use PDO;
use Exception;
use PDOException;
use Tamedevelopers\Database\Constant;
use Tamedevelopers\Support\Str;
use Tamedevelopers\Database\Constant;
use Tamedevelopers\Database\Schema\Builder;
use Tamedevelopers\Database\Connectors\ConnectorInterface;
use Tamedevelopers\Database\Connectors\Traits\ConnectorTrait;
Expand Down Expand Up @@ -38,6 +39,7 @@ class PostgresConnector
public function connect(array $config)
{
//
throw new Exception("Driver cannot be used at the moment! Unsupported driver [{$config['driver']}].");
}


Expand Down
4 changes: 3 additions & 1 deletion src/Connectors/SQLiteConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
namespace Tamedevelopers\Database\Connectors;

use PDO;
use Exception;
use PDOException;
use Tamedevelopers\Database\Constant;
use Tamedevelopers\Support\Str;
use Tamedevelopers\Database\Constant;
use Tamedevelopers\Database\Schema\Builder;
use Tamedevelopers\Database\Connectors\ConnectorInterface;
use Tamedevelopers\Database\Connectors\Traits\ConnectorTrait;
Expand Down Expand Up @@ -38,6 +39,7 @@ class SQLiteConnector
public function connect(array $config)
{
//
throw new Exception("Driver cannot be used at the moment! Unsupported driver [{$config['driver']}].");
}


Expand Down
1 change: 0 additions & 1 deletion src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* @method static \Tamedevelopers\Database\Connectors\Connector reconnect(string|null $name = null)
* @method static \Tamedevelopers\Database\DatabaseManager disconnect(string|null $name = null)
* @method static string getDefaultConnection()
* @method static array getConnection(string|null $name)
* @method static \Tamedevelopers\Database\Schema\Builder table(string $table)
* @method static \Tamedevelopers\Database\Schema\Builder from(string $table)
* @method static \Tamedevelopers\Database\Schema\Builder query()
Expand Down
Loading

0 comments on commit ed7dffd

Please sign in to comment.