1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Cake\Orm; |
4
|
|
|
|
5
|
|
|
use Cake\Core\App; |
6
|
|
|
use Cake\ORM\Table; |
7
|
|
|
use React\EventLoop\LoopInterface; |
8
|
|
|
use WyriHaximus\React\ChildProcess\Pool\PoolUtilizerInterface; |
9
|
|
|
|
10
|
|
|
class AsyncTableRegistry implements PoolUtilizerInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var AsyncTable[] |
14
|
|
|
*/ |
15
|
|
|
protected static $tables = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var AsyncTableRegistry |
19
|
|
|
*/ |
20
|
|
|
protected static $instance = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var boolean |
24
|
|
|
*/ |
25
|
|
|
protected static $reset = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param LoopInterface $loop |
29
|
|
|
* @param array $config |
30
|
|
|
*/ |
31
|
|
|
public static function init(LoopInterface $loop, array $config = []) |
32
|
|
|
{ |
33
|
|
|
Pool::getInstance($loop, $config); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Table $table |
38
|
|
|
* |
39
|
|
|
* @return AsyncTable |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
public static function get(Table $table) |
42
|
|
|
{ |
43
|
|
|
$tableName = get_class($table); |
44
|
|
|
|
45
|
|
|
if (isset(static::$tables[$tableName])) { |
46
|
|
|
return static::$tables[$tableName]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$asyncTableName = (new AsyncTableGenerator(CACHE . 'asyncTables' . DS))->generate($tableName)->getFQCN(); |
50
|
|
|
|
51
|
|
|
$asyncTable = new $asyncTableName(); |
52
|
|
|
|
53
|
|
|
if ($asyncTable instanceof AsyncTableInterface) { |
54
|
|
|
$asyncTable->setUpAsyncTable( |
55
|
|
|
Pool::getInstance(), |
56
|
|
|
$table->table(), |
|
|
|
|
57
|
|
|
App::className($tableName, 'Model/Table', 'Table') |
|
|
|
|
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
static::$tables[$tableName] = $asyncTable; |
62
|
|
|
return static::$tables[$tableName]; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
public static function getInstance() |
66
|
|
|
{ |
67
|
3 |
|
if (null === self::$instance || self::$reset) { |
68
|
3 |
|
self::$instance = new static(); |
69
|
3 |
|
self::$reset = false; |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
return self::$instance; |
73
|
|
|
} |
74
|
|
|
|
75
|
25 |
|
public static function reset() |
76
|
|
|
{ |
77
|
25 |
|
self::$reset = true; |
78
|
25 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritDoc |
82
|
|
|
*/ |
83
|
|
|
public function info() |
84
|
|
|
{ |
85
|
|
|
return Pool::getInstance()->info(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
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.