Completed
Push — Assets/Image ( 628edb...186e01 )
by Arnaud
05:50 queued 03:04
created

Plateform   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 95
rs 10
c 3
b 0
f 0
wmc 20

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isPhar() 0 9 2
A isWindows() 0 3 1
A getPharPath() 0 7 2
B getOS() 0 18 11
A openBrowser() 0 13 4
1
<?php
2
/*
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Util;
12
13
class Plateform
14
{
15
    const OS_UNKNOWN = 1;
16
    const OS_WIN = 2;
17
    const OS_LINUX = 3;
18
    const OS_OSX = 4;
19
20
    protected static $pharPath;
21
22
    /**
23
     * Running from Phar or not?
24
     *
25
     * @return bool
26
     */
27
    public static function isPhar(): bool
28
    {
29
        if (!empty(\Phar::running())) {
30
            self::$pharPath = \Phar::running();
31
32
            return true;
33
        }
34
35
        return false;
36
    }
37
38
    /**
39
     * Returns the full path on disk to the currently executing Phar archive.
40
     *
41
     * @return string
42
     */
43
    public static function getPharPath(): string
44
    {
45
        if (!isset(self::$pharPath)) {
46
            self::isPhar();
47
        }
48
49
        return self::$pharPath;
50
    }
51
52
    /**
53
     * Whether the host machine is running a Windows OS.
54
     *
55
     * @return bool
56
     */
57
    public static function isWindows(): bool
58
    {
59
        return defined('PHP_WINDOWS_VERSION_BUILD');
60
    }
61
62
    /**
63
     * Opens a URL in the system default browser.
64
     *
65
     * @param string $url
66
     *
67
     * @return void
68
     */
69
    public static function openBrowser($url): void
70
    {
71
        if (self::isWindows()) {
72
            passthru('start "web" explorer "'.$url.'"');
73
74
            return;
75
        }
76
        passthru('which xdg-open', $linux);
77
        passthru('which open', $osx);
78
        if (0 === $linux) {
79
            passthru('xdg-open '.$url);
80
        } elseif (0 === $osx) {
81
            passthru('open '.$url);
82
        }
83
    }
84
85
    /**
86
     * Search for system OS in PHP_OS constant.
87
     *
88
     * @return int
89
     */
90
    public static function getOS(): int
91
    {
92
        switch (PHP_OS) {
93
            case 'Unix':
94
            case 'FreeBSD':
95
            case 'NetBSD':
96
            case 'OpenBSD':
97
            case 'Linux':
98
                return self::OS_LINUX;
99
            case 'WINNT':
100
            case 'WIN32':
101
            case 'Windows':
102
            case 'CYGWIN_NT':
103
                return self::OS_WIN;
104
            case 'Darwin':
105
                return self::OS_OSX;
106
            default:
107
                return self::OS_UNKNOWN;
108
        }
109
    }
110
}
111