-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPv4.php
206 lines (194 loc) · 4.96 KB
/
IPv4.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
* IPv4.php
*
* Author: Larry Li <[email protected]>
*/
namespace larryli\ipv4\yii2;
use larryli\ipv4\Query;
use Yii;
use yii\base\Component;
use yii\base\ExitException;
use yii\db\Connection;
/**
* Class IPv4
* @package larryli\ipv4\yii2
*/
class IPv4 extends Component
{
/**
* @var string table prefix
*/
public $prefix = 'ipv4_';
/**
* @var \yii\db\Connection
*/
public $db;
/**
* @var string larryli\ipv4\query\Database class
*/
public $database;
/**
* @var array
*/
public $providers = [
'monipdb' => [
'filename' => '@runtime/17monipdb.dat',
],
'qqwry' => [
'filename' => '@runtime/qqwry.dat',
],
'full' => [
'providers' => ['monipdb', 'qqwry'],
],
'mini' => [
'providers' => 'full',
],
'china' => [
'providers' => 'full',
],
'world' => [
'providers' => 'full',
],
];
/**
* @var \larryli\ipv4\Query[]
*/
protected $objects = [];
/**
* @inheritdoc
*/
public function init()
{
$this->initDatabase();
$this->initProviders();
}
/**
* @throws ExitException
* @throws \yii\base\InvalidConfigException
*/
protected function initDatabase()
{
if (empty($this->db)) {
$this->db = Yii::$app->db;
} elseif (is_string($this->db)) {
$this->db = Yii::$app->get($this->db);
}
if (!is_a($this->db, Connection::className())) {
throw new ExitException(500, "{$this->db} is not a db connection object", 500);
}
if (empty($this->database)) {
$this->database = Database::className();
}
if (is_string($this->database)) {
$this->database = new $this->database([
'db' => $this->db,
'prefix' => $this->prefix,
]);
}
if (!Database::is_a($this->database)) {
throw new ExitException(500, "{$this->database} is not a ipv4 database object", 500);
}
}
/**
*
*/
protected function initProviders()
{
$result = [];
foreach ($this->providers as $name => $options) {
if (is_integer($name)) {
$name = $options;
$options = [];
}
$result[$name] = $options;
}
$this->providers = $result;
foreach ($this->providers as $name => $options) {
$providers = [];
if (is_array($options)) {
$opt = null;
if (isset($options['providers'])) {
if (is_array($options['providers'])) {
$providers = $options['providers'];
} else {
$providers[] = $options['providers'];
}
unset($options['providers']);
$opt = $this->database;
}
if (isset($options['filename'])) {
$opt = Yii::getAlias($options['filename']);
unset($options['filename']);
}
if (isset($options['class']) && !empty($opt)) {
$options['options'] = $opt;
} else {
$options = $opt;
}
}
$this->createQuery($name, $options, $providers);
}
}
/**
* @param string $name
* @param mixed $options
* @param array $providers
* @return Query|null
* @throws \Exception
*/
public function createQuery($name, $options, array $providers = [])
{
$query = $this->getQuery($name);
if ($query == null) {
$query = Query::create($name, $options);
$query->setProviders(array_map(function ($provider) {
return $this->getQuery($provider);
}, $providers));
$this->objects[$name] = $query;
}
return $query;
}
/**
* @param $name
* @return Query|null
*/
public function getQuery($name)
{
if (isset($this->objects[$name])) {
return $this->objects[$name];
}
return null;
}
/**
* @return \larryli\ipv4\Query[]
*/
public function getQueries()
{
return $this->objects;
}
/**
* @param string $name
* @return mixed
* @throws \yii\base\UnknownPropertyException
*/
public function __get($name)
{
if (isset($this->objects[$name])) {
return $this->objects[$name];
}
return parent::__get($name);
}
/**
* @param string $name
* @return bool|mixed
* @throws \yii\base\UnknownPropertyException
*/
public function __isset($name)
{
if (isset($this->objects[$name])) {
return true;
}
return parent::__isset($name);
}
}