1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpWinTools\WmiScripting\Models; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use PhpWinTools\WmiScripting\Connection; |
7
|
|
|
use PhpWinTools\WmiScripting\Query\Builder; |
8
|
|
|
use PhpWinTools\WmiScripting\Contracts\Jsonable; |
9
|
|
|
use PhpWinTools\WmiScripting\Contracts\Arrayable; |
10
|
|
|
use PhpWinTools\WmiScripting\Contracts\HasAttributes; |
11
|
|
|
use PhpWinTools\WmiScripting\Concerns\ComHasAttributes; |
12
|
|
|
use function PhpWinTools\WmiScripting\Support\connection; |
13
|
|
|
use PhpWinTools\WmiScripting\Collections\ModelCollection; |
14
|
|
|
use PhpWinTools\WmiScripting\Exceptions\WmiClassNotFoundException; |
15
|
|
|
use PhpWinTools\WmiScripting\Exceptions\InvalidConnectionException; |
16
|
|
|
use PhpWinTools\WmiScripting\Support\ApiObjects\Contracts\ObjectPath; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @link https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-provider |
20
|
|
|
*/ |
21
|
|
|
class Win32Model implements Arrayable, Jsonable, HasAttributes |
22
|
|
|
{ |
23
|
|
|
use ComHasAttributes; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $uuid; |
27
|
|
|
|
28
|
|
|
/** @var ObjectPath|null */ |
29
|
|
|
protected $objectPath; |
30
|
|
|
|
31
|
|
|
/** @var Builder|null */ |
32
|
|
|
protected $queryBuilder; |
33
|
|
|
|
34
|
|
|
/** @var array */ |
35
|
|
|
protected $hidden_attributes = ['queryBuilder', 'connection', 'wmi_class_name']; |
36
|
|
|
|
37
|
|
|
/** @var bool */ |
38
|
|
|
protected $merge_parent_casting = true; |
39
|
|
|
|
40
|
|
|
/** @var bool */ |
41
|
|
|
protected $merge_parent_hidden_attributes = true; |
42
|
|
|
|
43
|
|
|
/** @var array */ |
44
|
|
|
protected $attribute_casting = []; |
45
|
|
|
|
46
|
|
|
protected $connection = 'default'; |
47
|
|
|
|
48
|
|
|
protected $wmi_class_name; |
49
|
|
|
|
50
|
|
|
/* TODO: Remove ObjectPath dependency. Should be passed into attributes */ |
51
|
|
|
public function __construct(array $attributes = [], ObjectPath $objectPath = null) |
52
|
|
|
{ |
53
|
|
|
$this->mergeHiddenAttributes($this->hidden_attributes, $this->merge_parent_hidden_attributes); |
54
|
|
|
$this->fill($attributes); |
55
|
|
|
|
56
|
|
|
$this->objectPath = $objectPath; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Connection|string|null $connection |
61
|
|
|
* |
62
|
|
|
* @return ModelCollection|Win32Model[] |
63
|
|
|
*/ |
64
|
|
|
public static function all($connection = null) |
65
|
|
|
{ |
66
|
|
|
return static::query(static::newInstance()->getConnection($connection))->all(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $attributes |
71
|
|
|
* @param ObjectPath|null $objectPath |
72
|
|
|
* |
73
|
|
|
* @return Win32Model |
74
|
|
|
*/ |
75
|
|
|
public static function newInstance(array $attributes = [], ObjectPath $objectPath = null) |
76
|
|
|
{ |
77
|
|
|
return new static($attributes, $objectPath); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Connection|string|null $connection |
82
|
|
|
* |
83
|
|
|
* @return Builder |
84
|
|
|
*/ |
85
|
|
|
public static function query($connection = null) |
86
|
|
|
{ |
87
|
|
|
return new Builder($self = static::newInstance(), $self->getConnection($connection)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getConnectionName() |
94
|
|
|
{ |
95
|
|
|
return $this->connection; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Connection|string|null $connection |
100
|
|
|
* |
101
|
|
|
* @throws InvalidConnectionException |
102
|
|
|
* |
103
|
|
|
* @return Connection |
104
|
|
|
*/ |
105
|
|
|
public function getConnection($connection = null) |
106
|
|
|
{ |
107
|
|
|
return connection($connection, $this->connection); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getModelNameAttribute() |
114
|
|
|
{ |
115
|
|
|
return $this->getClassName(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getWmiClassNameAttribute() |
122
|
|
|
{ |
123
|
|
|
if (!is_null($this->wmi_class_name)) { |
124
|
|
|
return $this->wmi_class_name; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($this->wmi_class_name = $this->wmiClassNameSearch()) { |
128
|
|
|
return $this->wmi_class_name; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
throw new WmiClassNotFoundException( |
132
|
|
|
'Cannot find a suitable WMI Class to query. Please set $wmi_class_name manually.' |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Uses the Classes constant class to map models to WMI models for querying. If the current instance |
138
|
|
|
* does not yield results then we search the class' parents until a suitable match is found. |
139
|
|
|
* |
140
|
|
|
* @return string|null |
141
|
|
|
*/ |
142
|
|
|
protected function wmiClassNameSearch() |
143
|
|
|
{ |
144
|
|
|
if ($result = array_search(static::class, Classes::CLASS_MAP, true)) { |
145
|
|
|
return $result; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
foreach (class_parents($this) as $parent) { |
149
|
|
|
if ($result = array_search($parent, Classes::CLASS_MAP, true)) { |
150
|
|
|
return $result; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return null; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function getClassName() |
161
|
|
|
{ |
162
|
|
|
$classname = get_called_class(); |
163
|
|
|
|
164
|
|
|
if (preg_match('@\\\\([\w]+)$@', $classname, $matches)) { |
165
|
|
|
$classname = $matches[1]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $classname; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function toJson(): string |
175
|
|
|
{ |
176
|
|
|
return json_encode($this->toArray()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function toString(): string |
183
|
|
|
{ |
184
|
|
|
return $this->toJson(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function __toString() |
191
|
|
|
{ |
192
|
|
|
return $this->toString(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param array $attributes |
197
|
|
|
*/ |
198
|
|
|
protected function fill(array $attributes) |
199
|
|
|
{ |
200
|
|
|
foreach ($attributes as $key => $value) { |
201
|
|
|
$key = lcfirst($key); |
202
|
|
|
$value = $this->reduceValueArray($value); |
203
|
|
|
if ($this->hasProperty($key)) { |
204
|
|
|
$this->{$key} = $this->cast($key, $value); |
205
|
|
|
continue; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$this->unmapped_attributes[$key] = |
209
|
|
|
$this->cast($key, $value); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param $value |
215
|
|
|
* |
216
|
|
|
* @return mixed |
217
|
|
|
*/ |
218
|
|
|
protected function reduceValueArray($value) |
219
|
|
|
{ |
220
|
|
|
if (is_array($value) && array_key_exists('value', $value)) { |
221
|
|
|
$value = $value['value']; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $value; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @TODO: This should just be a helper method instead of returning a callback. |
229
|
|
|
* |
230
|
|
|
* @param $constant_class |
231
|
|
|
* |
232
|
|
|
* @return Closure |
233
|
|
|
*/ |
234
|
|
|
protected function constantToStringCallback($constant_class) |
235
|
|
|
{ |
236
|
|
|
return function ($constant) use ($constant_class) { |
237
|
|
|
if (trim($type = call_user_func_array($constant_class . '::string', [$constant])) === '') { |
238
|
|
|
return "[{$constant}] - UNKNOWN"; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return "[{$constant}] - {$type}"; |
242
|
|
|
}; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|