Skip to content

Commit f49e6f6

Browse files
committed
Allow a default connection to be passed to the constructor and update documentation
1 parent c593016 commit f49e6f6

File tree

10 files changed

+133
-40
lines changed

10 files changed

+133
-40
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The `ModelCollection` extends https://github.com/tightenco/collect / [Laravel Co
3333
You can also instantiate `Scripting`:
3434

3535
$scripting = new PhpWinTools\WmiScripting\Scripting;
36-
$scripting->addConnection('remote', PhpWinTools\WmiScripting\Connection::defaultNamespace('server', 'user', 'password'));
36+
$scripting->addConnection('remote', PhpWinTools\WmiScripting\Connection::simple('server', 'user', 'password'));
3737
$scripting->query('remote')->loggedOnUser()->get();
3838

3939
Whether you use `$scripting->query($connection = null)->modelName()` or `::query($connection = null)` you are dropped into a basic query

docs/.vuepress/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = {
2525
collapsable: false,
2626
children: [
2727
['documentation/', 'How It Works'],
28-
['documentation/getting-started/', 'Getting Started']
28+
['documentation/getting-started', 'Getting Started']
2929
]
3030
}
3131
]

docs/.vuepress/theme/styles/code.styl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
color #EC5975
1212
&.inserted
1313
color $accentColor
14-
1514
{$contentClass}
1615
pre, pre[class*="language-"]
1716
line-height 1.4
@@ -21,7 +20,7 @@
2120
border-radius 6px
2221
overflow auto
2322
code
24-
color #fff
23+
color lighten(#A9B7C6, 40%)
2524
padding 0
2625
background-color transparent
2726
border-radius 0
@@ -131,7 +130,20 @@ div[class~="language-python"]:before
131130
div[class~="language-bash"]:before
132131
content "sh"
133132

134-
div[class~="language-php"]:before
135-
content "php"
133+
div[class~="language-php"]
134+
.token.keyword
135+
color saturate(#CC7832, 40%)
136+
.token.variable
137+
color saturate(#9876AA, 40%)
138+
.token.comment
139+
font-style italic
140+
color saturate(#629755, 40%)
141+
.token.string
142+
color saturate(#6A8759, 40%)
143+
.token.class-name
144+
.token.operator
145+
color lighten(#A9B7C6, 40%)
146+
&:before
147+
content "php"
136148

137149
@import '~prismjs/themes/prism-tomorrow.css'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
$contentClass = '.theme-default-content'
2+
$codeBgColor = #232525
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Getting Started
2+
3+
:::warning
4+
#### REQUIREMENTS
5+
\>= PHP 7.1
6+
7+
Windows and the `com_dotnet` extension enabled (available in every [`Windows PHP`](https://windows.php.net/download/) release).
8+
:::
9+
10+
## Install
11+
``` sh
12+
composer require phpwintools/wmi-scripting
13+
```
14+
15+
## Basic Configuration
16+
17+
### Static call on Win32Model class
18+
``` php
19+
$connection = Connection::simple('computer-one', 'admin', 'password');
20+
21+
// Returns an instance of Query\Builder
22+
$builder = LoggedOnUser::query($connection);
23+
24+
// Returns a ModelCollection of Models\LoggedOnUsers
25+
$loggedOnUsers = $builder->get();
26+
```
27+
28+
A connection defined in this way will not be stored in the `Config` container. This is useful when you need to reference
29+
many connections in an array to poll many computers across your network.
30+
31+
``` php
32+
$username = 'admin';
33+
$password = 'password';
34+
$computers = ['computer_one', 'computer_two'];
35+
36+
$results = [];
37+
38+
foreach ($computers as $computer) {
39+
// $results becomes an array of ModelCollections
40+
$results[] = LoggedOnUser::query(Connection::simple($computer, $user $password));
41+
}
42+
```
43+
44+
### Instance of Scripting
45+
``` php
46+
$scripting = new Scripting(Connection::simple('computer-one', 'admin', 'password);
47+
48+
// Returns and instance of Query\Builder (same as above)
49+
$builder = $scripting->query()->loggedOnUser();
50+
```
51+
52+
This will set the given connection as the default connection. All models queried without a given connection will use
53+
this connection.
54+
55+
### No Configuration
56+
``` php
57+
// Returns a ModelCollection of LoggedOnUser models on the machine that PHP is installed on
58+
$local = LoggedOnUser::query()->get();
59+
60+
$scripting = new Scripting;
61+
// Same as above
62+
$local = $scripting->query()->loggedOnUser()->get();
63+
```
64+
65+
If no configuration is provided then the library's default connection is localhost.
66+
67+
::: danger
68+
`Scripting` should only be instantiated once. If it is instantiated more than once then the last configuration options
69+
will be used.
70+
:::

docs/documentation/getting-started/readme.md

Lines changed: 0 additions & 27 deletions
This file was deleted.

docs/documentation/readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
The basic goal of this library is to make querying information from Windows systems on your network as easy as possible.
44
This is accomplished by adopting an active record approach similar to
5-
[Laravel's Eloquent](https://laravel.com/docs/6.0/eloquent).
5+
[Laravel's Eloquent](https://laravel.com/docs/5.8/eloquent).
66

77
The WMI Scripting library consists of models that represent the
88
[WMI Win32 Provider classes](https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-provider)
99
and a basic query builder using [WQL](https://docs.microsoft.com/en-us/windows/win32/wmisdk/querying-with-wql)
1010
as it's target language.
1111

12+
I also take advantage of [Laravel's Collections](https://laravel.com/docs/5.8/collections) via the extraction done by
13+
[TightenCo](https://github.com/tightenco/collect), so you are not bound directly to the Laravel framework. Anytime you
14+
query a model you will get back an instance of `ModelCollection` which extends Laravel's `Collection`. This allows for
15+
fluent access to this data: `$modelCollection->map->getAttribute('name);` returns a collection of only the model names.
16+
1217
## Todo
1318

1419
I'm still working through the API and some of the core code to make this library as clean and resilient as possible.

docs/readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ composer require phpwintools/wmi-scripting
5050
$scripting = new Scripting;
5151

5252
// These connections can be called by name.
53-
$scripting->addConnection('server1', Connection::defaultNamespace('server1', 'user', 'password'));
54-
$scripting->addConnection('server2', Connection::defaultNamespace('server2', 'user', 'password'));
53+
$scripting->addConnection('server1', Connection::simple('server1', 'user', 'password'));
54+
$scripting->addConnection('server2', Connection::simple('server2', 'user', 'password'));
5555

5656
$scripting->query('server2')->loggedOnUser()->get();
5757

@@ -65,5 +65,5 @@ $scripting->query()->loggedOnUser()->get();
6565
### Connections with static calls
6666

6767
```php
68-
LoggedOnUser::query(Connection::defaultNamespace('server1', 'user', 'password'))->get();
68+
LoggedOnUser::query(Connection::simple('server1', 'user', 'password'))->get();
6969
```

src/WmiScripting/Scripting.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public function __construct($config = null)
2828
$this->config = $config;
2929
}
3030

31+
if ($config instanceof Connection) {
32+
$this->config = Config::instance();
33+
$this->setDefaultConnection('default', $config);
34+
}
35+
3136
if (is_array($config)) {
3237
$this->config = Config::instance($config);
3338
}
@@ -80,13 +85,13 @@ public function query($connection = null): WmiQueryFactory
8085
public function addConnection(string $name, $connection): self
8186
{
8287
if ($connection instanceof Connection) {
83-
$this->config->addConnection($name, $connection);
88+
$this->getConfig()->addConnection($name, $connection);
8489

8590
return $this;
8691
}
8792

8893
if (is_array($connection)) {
89-
$this->config->addConnection($name, new Connection(
94+
$this->getConfig()->addConnection($name, new Connection(
9095
$connection['server'] ?? Connection::DEFAULT_SERVER,
9196
$connection['namespace'] ?? Connection::DEFAULT_NAMESPACE,
9297
$connection['user'] ?? null,
@@ -116,8 +121,26 @@ public function setDefaultConnection(string $name, $connection = null): self
116121
$this->addConnection($name, $connection);
117122
}
118123

119-
$this->config->setDefaultConnection($name);
124+
$this->getConfig()->setDefaultConnection($name);
120125

121126
return $this;
122127
}
128+
129+
/**
130+
* @param string|null $name
131+
*
132+
* @return Connection|null
133+
*/
134+
public function getConnection(string $name = null)
135+
{
136+
return $this->getConfig()->getConnection($name);
137+
}
138+
139+
/**
140+
* @return Connection|null
141+
*/
142+
public function getDefaultConnection()
143+
{
144+
return $this->getConnection();
145+
}
123146
}

tests/WmiScripting/ScriptingTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public function it_can_instantiate_and_use_the_given_configuration()
3838
$this->assertSame($config, $scripting->getConfig());
3939
}
4040

41+
/** @test */
42+
public function it_can_instantiate_with_a_connection_and_set_it_as_the_default_connection()
43+
{
44+
$connection = Connection::simple('server', 'user', 'password');
45+
$scripting = new Scripting($connection);
46+
47+
$this->assertSame($connection, $scripting->getDefaultConnection());
48+
}
49+
4150
/** @test */
4251
public function it_can_instantiate_and_merge_in_a_configuration_array()
4352
{

0 commit comments

Comments
 (0)