← All changes
|
includes/sdk/google/ramsey/uuid/src/Provider/Node/SystemNodeProvider.php
+54
-43
1.2.13
→
1.4.1
View file →
| @@ -9,13 +9,13 @@ | ||
| 9 | 9 | * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 | 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | 11 | */ |
| 12 | 12 | declare (strict_types=1); |
| 13 | -namespace Dudlewebs\WPMCS\Ramsey\Uuid\Provider\Node; | |
| 13 | +namespace Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Provider\Node; | |
| 14 | 14 | |
| 15 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\NodeException; | |
| 16 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Provider\NodeProviderInterface; | |
| 17 | -use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Hexadecimal; | |
| 15 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Exception\NodeException; | |
| 16 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Provider\NodeProviderInterface; | |
| 17 | +use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Hexadecimal; | |
| 18 | 18 | use function array_filter; |
| 19 | 19 | use function array_map; |
| 20 | 20 | use function array_walk; |
| 21 | 21 | use function count; |
| @@ -23,10 +23,10 @@ | ||
| 23 | 23 | use function ob_start; |
| 24 | 24 | use function preg_match; |
| 25 | 25 | use function preg_match_all; |
| 26 | 26 | use function reset; |
| 27 | +use function str_contains; | |
| 27 | 28 | use function str_replace; |
| 28 | -use function strpos; | |
| 29 | 29 | use function strtolower; |
| 30 | 30 | use function strtoupper; |
| 31 | 31 | use function substr; |
| 32 | 32 | use const GLOB_NOSORT; |
| @@ -33,22 +33,21 @@ | ||
| 33 | 33 | use const PREG_PATTERN_ORDER; |
| 34 | 34 | /** |
| 35 | 35 | * SystemNodeProvider retrieves the system node ID, if possible |
| 36 | 36 | * |
| 37 | - * The system node ID, or host ID, is often the same as the MAC address for a | |
| 38 | - * network interface on the host. | |
| 37 | + * The system node ID, or host ID, is often the same as the MAC address for a network interface on the host. | |
| 39 | 38 | */ |
| 40 | 39 | class SystemNodeProvider implements NodeProviderInterface |
| 41 | 40 | { |
| 42 | 41 | /** |
| 43 | - * Pattern to match nodes in ifconfig and ipconfig output. | |
| 42 | + * Pattern to match nodes in `ifconfig` and `ipconfig` output. | |
| 44 | 43 | */ |
| 45 | - private const IFCONFIG_PATTERN = '/[^:]([0-9a-f]{2}([:-])[0-9a-f]{2}(\2[0-9a-f]{2}){4})[^:]/i'; | |
| 44 | + private const IFCONFIG_PATTERN = '/[^:]([0-9a-f]{2}([:-])[0-9a-f]{2}(\\2[0-9a-f]{2}){4})[^:]/i'; | |
| 46 | 45 | /** |
| 47 | 46 | * Pattern to match nodes in sysfs stream output. |
| 48 | 47 | */ |
| 49 | 48 | private const SYSFS_PATTERN = '/^([0-9a-f]{2}:){5}[0-9a-f]{2}$/i'; |
| 50 | - public function getNode(): Hexadecimal | |
| 49 | + public function getNode() : Hexadecimal | |
| 51 | 50 | { |
| 52 | 51 | $node = $this->getNodeFromSystem(); |
| 53 | 52 | if ($node === '') { |
| 54 | 53 | throw new NodeException('Unable to fetch a node for this system'); |
| @@ -55,15 +54,16 @@ | ||
| 55 | 54 | } |
| 56 | 55 | return new Hexadecimal($node); |
| 57 | 56 | } |
| 58 | 57 | /** |
| 59 | - * Returns the system node, if it can find it | |
| 58 | + * Returns the system node if found | |
| 60 | 59 | */ |
| 61 | - protected function getNodeFromSystem(): string | |
| 60 | + protected function getNodeFromSystem() : string | |
| 62 | 61 | { |
| 62 | + /** @var string | null $node */ | |
| 63 | 63 | static $node = null; |
| 64 | 64 | if ($node !== null) { |
| 65 | - return (string) $node; | |
| 65 | + return $node; | |
| 66 | 66 | } |
| 67 | 67 | // First, try a Linux-specific approach. |
| 68 | 68 | $node = $this->getSysfs(); |
| 69 | 69 | if ($node === '') { |
| @@ -77,60 +77,71 @@ | ||
| 77 | 77 | * Returns the network interface configuration for the system |
| 78 | 78 | * |
| 79 | 79 | * @codeCoverageIgnore |
| 80 | 80 | */ |
| 81 | - protected function getIfconfig(): string | |
| 81 | + protected function getIfconfig() : string | |
| 82 | 82 | { |
| 83 | - $disabledFunctions = strtolower((string) ini_get('disable_functions')); | |
| 84 | - if (strpos($disabledFunctions, 'passthru') !== \false) { | |
| 83 | + if (str_contains(strtolower((string) \ini_get('disable_functions')), 'passthru')) { | |
| 85 | 84 | return ''; |
| 86 | 85 | } |
| 86 | + /** @var string $phpOs */ | |
| 87 | + $phpOs = \constant('PHP_OS'); | |
| 87 | 88 | ob_start(); |
| 88 | - switch (strtoupper(substr(constant('PHP_OS'), 0, 3))) { | |
| 89 | + switch (strtoupper(substr($phpOs, 0, 3))) { | |
| 89 | 90 | case 'WIN': |
| 90 | - passthru('ipconfig /all 2>&1'); | |
| 91 | + \passthru('ipconfig /all 2>&1'); | |
| 91 | 92 | break; |
| 92 | 93 | case 'DAR': |
| 93 | - passthru('ifconfig 2>&1'); | |
| 94 | + \passthru('ifconfig 2>&1'); | |
| 94 | 95 | break; |
| 95 | 96 | case 'FRE': |
| 96 | - passthru('netstat -i -f link 2>&1'); | |
| 97 | + \passthru('netstat -i -f link 2>&1'); | |
| 97 | 98 | break; |
| 98 | 99 | case 'LIN': |
| 99 | 100 | default: |
| 100 | - passthru('netstat -ie 2>&1'); | |
| 101 | + \passthru('netstat -ie 2>&1'); | |
| 101 | 102 | break; |
| 102 | 103 | } |
| 103 | 104 | $ifconfig = (string) ob_get_clean(); |
| 104 | - $node = ''; | |
| 105 | 105 | if (preg_match_all(self::IFCONFIG_PATTERN, $ifconfig, $matches, PREG_PATTERN_ORDER)) { |
| 106 | - $node = $matches[1][0] ?? ''; | |
| 106 | + foreach ($matches[1] as $iface) { | |
| 107 | + if ($iface !== '00:00:00:00:00:00' && $iface !== '00-00-00-00-00-00') { | |
| 108 | + return $iface; | |
| 109 | + } | |
| 110 | + } | |
| 107 | 111 | } |
| 108 | - return $node; | |
| 112 | + return ''; | |
| 109 | 113 | } |
| 110 | 114 | /** |
| 111 | 115 | * Returns MAC address from the first system interface via the sysfs interface |
| 112 | 116 | */ |
| 113 | - protected function getSysfs(): string | |
| 117 | + protected function getSysfs() : string | |
| 114 | 118 | { |
| 115 | - $mac = ''; | |
| 116 | - if (strtoupper(constant('PHP_OS')) === 'LINUX') { | |
| 117 | - $addressPaths = glob('/sys/class/net/*/address', GLOB_NOSORT); | |
| 118 | - if ($addressPaths === \false || count($addressPaths) === 0) { | |
| 119 | - return ''; | |
| 119 | + /** @var string $phpOs */ | |
| 120 | + $phpOs = \constant('PHP_OS'); | |
| 121 | + if (strtoupper($phpOs) !== 'LINUX') { | |
| 122 | + return ''; | |
| 123 | + } | |
| 124 | + $addressPaths = \glob('/sys/class/net/*/address', GLOB_NOSORT); | |
| 125 | + if ($addressPaths === \false || count($addressPaths) === 0) { | |
| 126 | + return ''; | |
| 127 | + } | |
| 128 | + /** @var array<array-key, string> $macs */ | |
| 129 | + $macs = []; | |
| 130 | + array_walk($addressPaths, function (string $addressPath) use(&$macs) : void { | |
| 131 | + if (\is_readable($addressPath)) { | |
| 132 | + $macs[] = \file_get_contents($addressPath); | |
| 120 | 133 | } |
| 121 | - $macs = []; | |
| 122 | - array_walk($addressPaths, function (string $addressPath) use (&$macs): void { | |
| 123 | - if (is_readable($addressPath)) { | |
| 124 | - $macs[] = file_get_contents($addressPath); | |
| 125 | - } | |
| 126 | - }); | |
| 127 | - $macs = array_map('trim', $macs); | |
| 128 | - // Remove invalid entries. | |
| 129 | - $macs = array_filter($macs, function (string $address) { | |
| 130 | - return $address !== '00:00:00:00:00:00' && preg_match(self::SYSFS_PATTERN, $address); | |
| 131 | - }); | |
| 132 | - $mac = reset($macs); | |
| 133 | - } | |
| 134 | + }); | |
| 135 | + /** @var callable $trim */ | |
| 136 | + $trim = 'trim'; | |
| 137 | + $macs = array_map($trim, $macs); | |
| 138 | + // Remove invalid entries. | |
| 139 | + $macs = array_filter($macs, function (mixed $address) : bool { | |
| 140 | + \assert(\is_string($address)); | |
| 141 | + return $address !== '00:00:00:00:00:00' && preg_match(self::SYSFS_PATTERN, $address); | |
| 142 | + }); | |
| 143 | + /** @var bool | string $mac */ | |
| 144 | + $mac = reset($macs); | |
| 134 | 145 | return (string) $mac; |
| 135 | 146 | } |
| 136 | 147 | } |