Skip to content

Commit b40d46b

Browse files
committed
Init optimization
1 parent e06eb49 commit b40d46b

7 files changed

Lines changed: 296 additions & 58 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"description": "Webman database",
66
"require": {
77
"workerman/webman-framework": "^2.0 || dev-master",
8+
"workerman/webman": "^2.0",
89
"webman/coroutine": "^1.0 || dev-main",
910
"illuminate/database": "^10.0",
1011
"laravel/serializable-closure": "^2.0"

src/DatabaseManager.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Webman\Context;
88
use Webman\Coroutine\Pool;
99

10+
/**
11+
* Class DatabaseManager
12+
*/
1013
class DatabaseManager extends BaseDatabaseManager
1114
{
1215

@@ -37,10 +40,10 @@ public function connection($name = null)
3740
return $this->configure($this->makeConnection($database), $type);
3841
});
3942
$pool->setConnectionCloser(function ($connection) {
40-
$connection->disconnect();
43+
$this->closeAndFreeConnection($connection);
4144
});
4245
$pool->setHeartbeatChecker(function ($connection) {
43-
return $connection->select('select 1');
46+
$connection->select('select 1');
4447
});
4548
static::$pools[$name] = $pool;
4649
}
@@ -60,4 +63,19 @@ public function connection($name = null)
6063
return $connection;
6164
}
6265

66+
/**
67+
* Close connection.
68+
*
69+
* @param $connection
70+
* @return void
71+
*/
72+
protected function closeAndFreeConnection($connection): void
73+
{
74+
$connection->disconnect();
75+
$clearProperties = function () {
76+
$this->queryGrammar = null;
77+
};
78+
$clearProperties->call($connection);
79+
}
80+
6381
}

src/Initializer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
use MongoDB\Laravel\Connection as LaravelMongodbConnection;
2525
use support\Container;
2626

27+
/**
28+
* Class Initializer
29+
*/
2730
class Initializer
2831
{
2932

@@ -96,3 +99,5 @@ public static function init($config): void
9699
}
97100

98101
}
102+
103+
Initializer::init(config('database', []));

src/Manager.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Webman\Database;
44

5-
use Illuminate\Database\Connection;
65
use Illuminate\Database\Connectors\ConnectionFactory;
76

7+
/**
8+
* Class Manager
9+
*/
810
class Manager extends \Illuminate\Database\Capsule\Manager
911
{
1012
/**
@@ -16,8 +18,5 @@ protected function setupManager()
1618
{
1719
$factory = new ConnectionFactory($this->container);
1820
$this->manager = new DatabaseManager($this->container, $factory);
19-
Connection::resolverFor('mysql', function ($connection, $database, $prefix, $config) {
20-
return new MySqlConnection($connection, $database, $prefix, $config);
21-
});
2221
}
2322
}

src/MysqlConnection.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/support/Db.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
namespace support;
1616

1717
use Closure;
18-
use Illuminate\Database\Capsule\Manager;
19-
use Illuminate\Database\Connection;
18+
use Webman\Database\Manager;
19+
20+
require_once __DIR__ . '/../Initializer.php';
2021

2122
/**
2223
* Class Db
@@ -33,20 +34,5 @@
3334
*/
3435
class Db extends Manager
3536
{
36-
/**
37-
* @return Manager
38-
*/
39-
public static function getInstance(): Manager
40-
{
41-
return static::$instance;
42-
}
43-
44-
/**
45-
* @return Connection[]
46-
*/
47-
public static function getConnections(): array
48-
{
49-
return static::$instance->getDatabaseManager()->getConnections();
50-
}
5137

5238
}

0 commit comments

Comments
 (0)