-
Notifications
You must be signed in to change notification settings - Fork 0
/
Processes.php
204 lines (185 loc) · 5.51 KB
/
Processes.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
<?php declare(strict_types=1);
namespace Winkill\Kernel;
use Winkill\Kernel\Exception\{
ProcessTerminationFailure,
ProcessParsingFailure,
SystemScanningFailure,
UnsupportedComparisonOperator
};
use Winkill\Kernel\Interface\{
Configuration,
Process,
ProcessTermination,
};
use Winkill\Kernel\OS\Common\Comparison;
/**
* Builder Pattern + Facade Pattern*
*
* @see https://refactoring.guru/design-patterns/builder
*/
final class Processes
{
/**
* Object Collection
*
* @var Process[]|array<int, Process>
*/
private array $scanned;
/**
* Object Collection
*
* @var array<string, bool|array<int, Process>>
*/
private array $selected;
/**
* @param Configuration $factory
*
* @throws SystemScanningFailure
* @throws ProcessParsingFailure
*/
public function __construct(private readonly Configuration $factory)
{
$scanning_strategy = $this->factory->createScanningStrategy();
$parsing_strategy = $this->factory->createParsingStrategy();
foreach ($scanning_strategy->scan() as $process) {
$this->scanned[] = $parsing_strategy->parse($process);
}
$this->selected['is'] = false;
$this->selected['processes'] = [];
}
/**
* Return an array of selected or scanned processes
*
* (Note: this method is non-idempotent - in one
* case, it returns one thing, in the other case,
* something else.)
*
* Usage:
*
* ```
* $winkill = new Winkill();
* $processes = $winkill->scan();
*
* $scanned = $processes->get();
*
* // Or by specifying selection
*
* $selected = $processes->where(
* attribute: 'consumed_memory',
* compareAs: '=',
* value: 500
* )->get();
* ```
*
* @return Process[]|array<int, Process>
*/
public function get(): array
{
$this->selected['is'] = ! $this->selected['is'];
return (! $this->selected['is'])
? $this->selected['processes']
: $this->scanned;
}
/**
* Select from scanned processes
* appropriate ones to conditions
*
* Usage:
*
* ```
* $winkill = new Winkill();
* $processes = $winkill->scan();
*
* $processes = $processes->where(
* attribute: 'process_name',
* compareAs: '=',
* value: 'chrome' # You can pass the process name in the following formats:
* # [string]: simple name (chrome)
* # [string]: name with .ext (chrome.exe)
* # [string]: uppercase name (Chrome.exe)
* );
* ```
*
* - List of all available attributes — {@see \Winkill\Kernel\OS\Windows\WindowsProcess}
* - List of all available compare operators — {@see \Winkill\Kernel\OS\Common\Comparison}
*
* @param string $attribute
* @param string $compareAs
* @param int|string $value
*
* @return $this
*
* @throws UnsupportedComparisonOperator
*/
public function where(
string $attribute,
string $compareAs,
int|string $value
): static
{
if (! in_array(trim($compareAs), Comparison::values())) {
throw new UnsupportedComparisonOperator($compareAs);
}
$this->selected['processes'] = []; // Remove all previous selected processes
$this->selected['is'] = false; // Begin processes selection
foreach ($this->scanned as $process) {
$is_handled = $process->handleAttribute($attribute, $compareAs, $value);
if ($is_handled) $this->selected['processes'][] = $process;
}
$this->selected['is'] = true; // Finish processes selection
return $this;
}
/**
* Kill (terminate) the selected processes
* (optionally, by using custom strategy)
*
* Usage:
*
* ```
* $winkill = new Winkill();
* $processes = $winkill->scan();
*
* $killed = $processes->where(
* attribute: 'consumed_memory',
* compareAs: '=',
* value: 500
* )->kill();
* ```
*
* You can create your own process termination strategy by implementing
* kernel interface {@see \Winkill\Kernel\Interface\ProcessTermination}
*
* The custom strategy will execute for each of selected processes
* (from processes selected by using $processes->where(...)).
*
* ```
* $winkill = new Winkill();
* $processes = $winkill->scan();
*
* $killed = $processes->where(
* attribute: 'consumed_memory',
* compareAs: '=',
* value: 500
* )->kill(strategy: new CustomTermination());
* ```
*
* @param ProcessTermination|null $strategy
*
* @return Process[]|array<int, Process> Those processes from
* the selected which were killed.
*
* @throws ProcessTerminationFailure
*/
public function kill(?ProcessTermination $strategy = null): array
{
/** @var Process[]|array<int, Process> $killed */
$killed = [];
/** @var ProcessTermination $termination_strategy */
$termination_strategy = $strategy ?: $this->factory->createTerminationStrategy();
foreach ($this->selected['processes'] as $process) {
$termination_strategy->terminate($process);
$killed[] = $process;
}
return $killed;
}
}