| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the ramsey/uuid library |
| 4 |
* |
| 5 |
* For the full copyright and license information, please view the LICENSE |
| 6 |
* file that was distributed with this source code. |
| 7 |
* |
| 8 |
* @copyright Copyright (c) Ben Ramsey <[email protected]> |
| 9 |
* @license http://opensource.org/licenses/MIT MIT |
| 10 |
* @link https://benramsey.com/projects/ramsey-uuid/ Documentation |
| 11 |
* @link https://packagist.org/packages/ramsey/uuid Packagist |
| 12 |
* @link https://github.com/ramsey/uuid GitHub |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace Ramsey\Uuid\Provider\Node; |
| 16 |
|
| 17 |
use Exception; |
| 18 |
use Ramsey\Uuid\Provider\NodeProviderInterface; |
| 19 |
|
| 20 |
/** |
| 21 |
* FallbackNodeProvider attempts to gain the system host ID from an array of |
| 22 |
* providers, falling back to the next in line in the event a host ID can not be |
| 23 |
* obtained |
| 24 |
*/ |
| 25 |
class FallbackNodeProvider implements NodeProviderInterface |
| 26 |
{ |
| 27 |
/** |
| 28 |
* @var NodeProviderInterface[] |
| 29 |
*/ |
| 30 |
private $nodeProviders; |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructs a `FallbackNodeProvider` using an array of node providers |
| 34 |
* |
| 35 |
* @param NodeProviderInterface[] $providers Array of node providers |
| 36 |
*/ |
| 37 |
public function __construct(array $providers) |
| 38 |
{ |
| 39 |
$this->nodeProviders = $providers; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns the system node ID by iterating over an array of node providers |
| 44 |
* and returning the first non-empty value found |
| 45 |
* |
| 46 |
* @return string System node ID as a hexadecimal string |
| 47 |
* @throws Exception |
| 48 |
*/ |
| 49 |
public function getNode() |
| 50 |
{ |
| 51 |
foreach ($this->nodeProviders as $provider) { |
| 52 |
if ($node = $provider->getNode()) { |
| 53 |
return $node; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return null; |
| 58 |
} |
| 59 |
} |