Passed
Pull Request — main (#148)
by Nils
03:04
created

OSHelper::getOS()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Startwind\Forrest\Util;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
7
abstract class OSHelper
8
{
9
    /**
10
     * Return true if the user is root.
11
     *
12
     * Does not work on Windows machines yet.
13
     */
14
    public static function isRoot(): bool
15
    {
16
        if (function_exists('posix_getuid')) {
17
            return posix_getuid() === 0;
18
        } else {
19
            return false;
20
        }
21
    }
22
23
    /**
24
     * @throws \Exception
25
     */
26
    public static function copyToClipboard(string $string): bool
27
    {
28
        if (PHP_OS_FAMILY === "Windows") {
29
            // works on windows 7 +
30
            $clip = popen("clip", "wb");
31
        } elseif (PHP_OS_FAMILY === "Linux") {
32
            return false;
33
            // tested, works on ArchLinux
34
            // $clip = popen('xclip -selection clipboard', 'wb');
35
        } elseif (PHP_OS_FAMILY === "Darwin") {
36
            // untested!
37
            $clip = popen('pbcopy', 'wb');
38
        } else {
39
            throw new \Exception("running on unsupported OS: " . PHP_OS_FAMILY . " - only Windows, Linux, and MacOS supported.");
40
        }
41
        $written = fwrite($clip, $string);
42
        return (pclose($clip) === 0 && strlen($string) === $written);
43
    }
44
45
    public static function getOS(): array
46
    {
47
        if (PHP_OS_FAMILY === "Darwin") {
48
            return ['name' => 'MacOS', 'version' => ''];
49
        }
50
51
        return ['name' => PHP_OS_FAMILY, 'version' => ''];
52
    }
53
}
54