|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpWinTools\WmiScripting\Connections; |
|
4
|
|
|
|
|
5
|
|
|
use function PhpWinTools\WmiScripting\Support\resolve; |
|
6
|
|
|
use PhpWinTools\WmiScripting\Support\ApiObjects\Contracts\Services; |
|
7
|
|
|
|
|
8
|
|
|
class ComConnection implements Connection |
|
9
|
|
|
{ |
|
10
|
|
|
const DEFAULT_SERVER = '.'; |
|
11
|
|
|
|
|
12
|
|
|
const DEFAULT_NAMESPACE = 'Root\CIMv2'; |
|
13
|
|
|
|
|
14
|
|
|
/** @var float */ |
|
15
|
|
|
private $time_to_connect = 0.0; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
private $server; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private $namespace; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string|null */ |
|
24
|
|
|
private $user; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string|null */ |
|
27
|
|
|
private $password; |
|
28
|
|
|
|
|
29
|
|
|
private $locale; |
|
30
|
|
|
|
|
31
|
|
|
/** @var null */ |
|
32
|
|
|
private $authority; |
|
33
|
|
|
|
|
34
|
|
|
/** @var null */ |
|
35
|
|
|
private $security_flags; |
|
36
|
|
|
|
|
37
|
|
|
private $services; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct( |
|
40
|
|
|
string $server = self::DEFAULT_SERVER, |
|
41
|
|
|
string $namespace = self::DEFAULT_NAMESPACE, |
|
42
|
|
|
string $user = null, |
|
43
|
|
|
string $password = null, |
|
44
|
|
|
$locale = null, |
|
45
|
|
|
$authority = null, |
|
46
|
|
|
$security_flags = null |
|
47
|
|
|
) { |
|
48
|
|
|
$this->server = $server; |
|
49
|
|
|
$this->namespace = $namespace; |
|
50
|
|
|
$this->user = $user; |
|
51
|
|
|
$this->password = $password; |
|
52
|
|
|
$this->locale = $locale; |
|
53
|
|
|
$this->authority = $authority; |
|
54
|
|
|
$this->security_flags = $security_flags; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $server |
|
59
|
|
|
* @param string|null $user |
|
60
|
|
|
* @param string|null $password |
|
61
|
|
|
* @param mixed|null $locale |
|
62
|
|
|
* @param mixed|null $authority |
|
63
|
|
|
* @param mixed|null $security_flags |
|
64
|
|
|
* |
|
65
|
|
|
* @return ComConnection |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function defaultNamespace( |
|
68
|
|
|
string $server, |
|
69
|
|
|
string $user = null, |
|
70
|
|
|
string $password = null, |
|
71
|
|
|
$locale = null, |
|
72
|
|
|
$authority = null, |
|
73
|
|
|
$security_flags = null |
|
74
|
|
|
): self { |
|
75
|
|
|
return new self($server, self::DEFAULT_NAMESPACE, $user, $password, $locale, $authority, $security_flags); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $server |
|
80
|
|
|
* @param string $user |
|
81
|
|
|
* @param string $password |
|
82
|
|
|
* |
|
83
|
|
|
* @return ComConnection |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function simple(string $server, string $user, string $password): self |
|
86
|
|
|
{ |
|
87
|
|
|
return static::defaultNamespace($server, $user, $password); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return Services |
|
92
|
|
|
*/ |
|
93
|
|
|
public function connect(): Services |
|
94
|
|
|
{ |
|
95
|
|
|
if (is_null($this->services)) { |
|
96
|
|
|
$start = microtime(true); |
|
97
|
|
|
$this->services = resolve()->locator()->connectServer($this); |
|
98
|
|
|
$this->time_to_connect = microtime(true) - $start; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->services; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function query($query, $model, $relationships) |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->connect() |
|
107
|
|
|
->resolvePropertySets($relationships) |
|
108
|
|
|
->execQuery($query) |
|
109
|
|
|
->get(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return float |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getTimeToConnect() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->time_to_connect; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getServer(): string |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->server; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getNamespace(): string |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->namespace; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return string|null |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getUser(): ?string |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->user; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return string|null |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getPassword(): ?string |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->password; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function getLocale() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->locale; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return null |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getAuthority() |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->authority; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param $authority |
|
167
|
|
|
* |
|
168
|
|
|
* @return self |
|
169
|
|
|
*/ |
|
170
|
|
|
public function setAuthority($authority) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->authority = $authority; |
|
173
|
|
|
|
|
174
|
|
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return null |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getSecurityFlags() |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->security_flags; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param int $flags |
|
187
|
|
|
* |
|
188
|
|
|
* @return self |
|
189
|
|
|
*/ |
|
190
|
|
|
public function setSecurityFlags(int $flags) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->security_flags = $flags; |
|
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..