denpamusic/php-levin

View on GitHub
bin/dummy-node

Summary

Maintainability
Test Coverage
#!/usr/bin/env php
<?php

declare(strict_types=1);

if (!defined('STDIN')) {
    exit('This command must be run from command line.');
}

$autoloadFiles = [
    __DIR__.'/../vendor/autoload.php',
    __DIR__.'/../../../../vendor/autoload.php',
];

$autoload = null;

foreach ($autoloadFiles as $file) {
    if (file_exists($file)) {
        $autoload = $file;
        break;
    }
}

if (! $autoload) {
    echo "Autoload file not found; try 'composer dump-autoload' first." . PHP_EOL;
    exit(1);
}

require $autoload;

// filter-out options from args
$argv = array_filter($argv, function ($arg) {
    return substr(trim($arg), 0, 1) != '-';
});

// re-key args
$argv = array_merge($argv);

if (!isset($argv[1])) {
    exit('Usage: '.basename($argv[0]).'[-v] [--colors] [--network-id=] <ip> <port>');
}

$address = $argv[1];
$port = $argv[2] ?? 18080;
$options = getopt('v', ['network-id::', 'colors']);

(new \Denpa\Levin\Nodes\DummyNode())->connect($address, $port, $options);