|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SwooleTW\Http\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Console\Application as ConsoleApp; |
|
6
|
|
|
use Swoole\Table; |
|
7
|
|
|
use SwooleTW\Http\Table\SwooleTable; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Trait InteractsWithSwooleTable |
|
11
|
|
|
* |
|
12
|
|
|
* @property \Illuminate\Contracts\Container\Container $container |
|
13
|
|
|
* @property \Illuminate\Contracts\Container\Container $app |
|
14
|
|
|
*/ |
|
15
|
|
|
trait InteractsWithSwooleTable |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \SwooleTW\Http\Table\SwooleTable |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $currentTable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Register customized swoole talbes. |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function createTables() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->currentTable = new SwooleTable; |
|
28
|
|
|
$this->registerTables(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Register user-defined swoole tables. |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function registerTables() |
|
35
|
|
|
{ |
|
36
|
|
|
$tables = $this->container->make('config')->get('swoole_http.tables', []); |
|
37
|
|
|
|
|
38
|
|
|
foreach ($tables as $key => $value) { |
|
39
|
|
|
$table = new Table($value['size']); |
|
40
|
|
|
$columns = $value['columns'] ?? []; |
|
41
|
|
|
foreach ($columns as $column) { |
|
42
|
|
|
if (isset($column['size'])) { |
|
43
|
|
|
$table->column($column['name'], $column['type'], $column['size']); |
|
44
|
|
|
} else { |
|
45
|
|
|
$table->column($column['name'], $column['type']); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
$table->create(); |
|
49
|
|
|
|
|
50
|
|
|
$this->currentTable->add($key, $table); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Bind swoole table to Laravel app container. |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function bindSwooleTable() |
|
58
|
|
|
{ |
|
59
|
|
|
if (! $this->app instanceof ConsoleApp) { |
|
60
|
|
|
$this->app->singleton(SwooleTable::class, function () { |
|
61
|
|
|
return $this->currentTable; |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
$this->app->alias(SwooleTable::class, 'swoole.table'); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|