-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScripting.php
More file actions
147 lines (125 loc) · 3.81 KB
/
Copy pathScripting.php
File metadata and controls
147 lines (125 loc) · 3.81 KB
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
<?php
namespace PhpWinTools\WmiScripting;
use PHPUnit\Framework\TestCase;
use PhpWinTools\WmiScripting\Testing\FakeFactory;
use PhpWinTools\WmiScripting\Configuration\Config;
use PhpWinTools\WmiScripting\Connections\ComConnection;
use PhpWinTools\WmiScripting\Testing\CallStacks\ComCallStack;
use PhpWinTools\WmiScripting\Exceptions\InvalidConnectionException;
use PhpWinTools\WmiScripting\Testing\CallStacks\ApiObjectCallStack;
use PhpWinTools\WmiScripting\Exceptions\InvalidConfigArgumentException;
class Scripting
{
/** @var Config */
protected $config;
/**
* @param Config|array|null $config
*/
public function __construct($config = null)
{
if (is_null($config)) {
$this->config = Config::instance();
}
if ($config instanceof Config) {
$this->config = $config;
}
if ($config instanceof ComConnection) {
$this->config = Config::instance();
$this->setDefaultConnection('default', $config);
}
if (is_array($config)) {
$this->config = Config::instance($config);
}
if (is_null($this->config)) {
throw new InvalidConfigArgumentException('Cannot instantiate Config with given argument(s).');
}
}
/**
* @param TestCase $testCase
* @param Config|null $config
*
* @return FakeFactory
*/
public static function fake(TestCase $testCase, Config $config = null): FakeFactory
{
ComCallStack::newInstance();
ApiObjectCallStack::newInstance();
return new FakeFactory($testCase, $config ?? Config::testInstance());
}
/**
* @return Config
*/
public function getConfig(): Config
{
return $this->config;
}
/**
* @param ComConnection|string|null $connection
*
* @return WmiQueryFactory
*/
public function query($connection = null): WmiQueryFactory
{
return new WmiQueryFactory($connection);
}
/**
* @param string $name
* @param ComConnection|array|null $connection
*
* @throws InvalidConnectionException
*
* @return self
*/
public function addConnection(string $name, $connection): self
{
if ($connection instanceof ComConnection) {
$this->getConfig()->addConnection($name, $connection);
return $this;
}
if (is_array($connection)) {
$this->getConfig()->addConnection($name, new ComConnection(
$connection['server'] ?? ComConnection::DEFAULT_SERVER,
$connection['namespace'] ?? ComConnection::DEFAULT_NAMESPACE,
$connection['user'] ?? null,
$connection['password'] ?? null,
$connection['locale'] ?? null,
$connection['authority'] ?? null,
$connection['security_flags'] ?? null
));
return $this;
}
throw new InvalidConnectionException("Could not create connection '{$name}'.");
}
/**
* @param string $name
* @param ComConnection|array|null $connection
*
* @throws InvalidConnectionException
*
* @return self
*/
public function setDefaultConnection(string $name, $connection = null): self
{
if ($connection) {
$this->addConnection($name, $connection);
}
$this->getConfig()->setDefaultConnection($name);
return $this;
}
/**
* @param string|null $name
*
* @return ComConnection|null
*/
public function getConnection(string $name = null)
{
return $this->getConfig()->getConnection($name);
}
/**
* @return ComConnection|null
*/
public function getDefaultConnection()
{
return $this->getConnection();
}
}