1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Cake\Orm; |
4
|
|
|
|
5
|
|
|
use Cake\Core\App; |
6
|
|
|
use Cake\Core\Configure; |
7
|
|
|
use Cake\ORM\Table; |
8
|
|
|
use React\EventLoop\LoopInterface; |
9
|
|
|
use WyriHaximus\React\ChildProcess\Pool\PoolUtilizerInterface; |
10
|
|
|
|
11
|
|
|
class AsyncTableRegistry implements PoolUtilizerInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var AsyncTable[] |
15
|
|
|
*/ |
16
|
|
|
protected static $tables = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var AsyncTableRegistry |
20
|
|
|
*/ |
21
|
|
|
protected static $instance = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
protected static $reset = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param LoopInterface $loop |
30
|
|
|
* @param array $config |
31
|
|
|
*/ |
32
|
|
|
public static function init(LoopInterface $loop, array $config = []) |
33
|
|
|
{ |
34
|
|
|
Pool::getInstance($loop, $config); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Table $table |
39
|
|
|
* |
40
|
|
|
* @return AsyncTable |
|
|
|
|
41
|
|
|
*/ |
42
|
|
|
public static function get(Table $table) |
43
|
|
|
{ |
44
|
|
|
$tableName = get_class($table); |
45
|
|
|
|
46
|
|
|
if (isset(static::$tables[$tableName])) { |
47
|
|
|
return static::$tables[$tableName]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$asyncTableName = (new AsyncTableGenerator( |
51
|
|
|
Configure::read('WyriHaximus.React.Cake.Orm.Cache.AsyncTables') |
52
|
|
|
))->generate($tableName)->getFQCN(); |
53
|
|
|
|
54
|
|
|
$asyncTable = new $asyncTableName(); |
55
|
|
|
|
56
|
|
|
if ($asyncTable instanceof AsyncTableInterface) { |
57
|
|
|
$asyncTable->setUpAsyncTable( |
58
|
|
|
Pool::getInstance(), |
59
|
|
|
$table->getRegistryAlias(), |
60
|
|
|
App::className($tableName, 'Model/Table', 'Table') |
|
|
|
|
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
static::$tables[$tableName] = $asyncTable; |
65
|
|
|
|
66
|
|
|
return static::$tables[$tableName]; |
67
|
|
|
} |
68
|
3 |
|
|
69
|
|
|
public static function paginate($tableName, $params, $settings) |
70
|
3 |
|
{ |
71
|
3 |
|
return Pool::getInstance()->paginate($tableName, $params, $settings); |
72
|
3 |
|
} |
73
|
|
|
|
74
|
|
|
public static function getInstance() |
75
|
3 |
|
{ |
76
|
|
|
if (null === self::$instance || self::$reset) { |
77
|
|
|
self::$instance = new static(); |
78
|
25 |
|
self::$reset = false; |
79
|
|
|
} |
80
|
25 |
|
|
81
|
25 |
|
return self::$instance; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public static function reset() |
85
|
|
|
{ |
86
|
|
|
self::$reset = true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritDoc |
91
|
|
|
*/ |
92
|
|
|
public function info() |
93
|
|
|
{ |
94
|
|
|
return Pool::getInstance()->info(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.