Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DAOPooling.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\orm\traits;
4
7
19
20 abstract public static function startDatabase(&$config, $offset = null);
21
22 abstract public static function getDbOffset(&$config, $offset = null);
23 protected static $pool;
24
32 public static function initPooling(&$config, $offset = null, int $size = 16) {
33 $dbConfig = self::getDbOffset ( $config, $offset );
34 $wrapperClass = $dbConfig ['wrapper'] ?? \Ubiquity\db\providers\pdo\PDOWrapper::class;
35 if (\method_exists ( $wrapperClass, 'getPoolClass' )) {
36 $poolClass = \call_user_func ( $wrapperClass . '::getPoolClass' );
37 if (\class_exists ( $poolClass, true )) {
38 $reflection_class = new \ReflectionClass ( $poolClass );
39 self::$pool = $reflection_class->newInstanceArgs ( [ &$config,$offset,$size ] );
40 } else {
41 throw new DAOException ( $poolClass . ' class does not exists!' );
42 }
43 } else {
44 throw new DAOException ( $wrapperClass . ' does not support connection pooling!' );
45 }
46 self::startDatabase ( $config, $offset );
47 }
48
55 public static function pool($offset = 'default') {
56 if (! isset ( self::$db [$offset] )) {
57 self::startDatabase ( Startup::$config, $offset );
58 }
59 return self::$db [$offset]->pool ();
60 }
61
62 public static function freePool($db) {
63 self::$pool->put ( $db );
64 }
65
66 public static function go($asyncCallable, $offset = 'default') {
67 $vars = \get_defined_vars ();
68 \Swoole\Coroutine::create ( function () use ($vars, $asyncCallable, $offset) {
69 $db = self::pool ( $offset );
70 \call_user_func_array ( $asyncCallable, $vars );
71 self::freePool ( $db );
72 } );
73 }
74}
75
Starts the framework.
Definition Startup.php:19
static go($asyncCallable, $offset='default')
static getDbOffset(&$config, $offset=null)
static initPooling(&$config, $offset=null, int $size=16)
Initialize pooling (To invoke during Swoole startup)
static startDatabase(&$config, $offset=null)
static pool($offset='default')
gets a new DbConnection from pool
Ubiquity\orm\traits This class is part of Ubiquity.