You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and dots ('.'), can be up to 35 characters long. Letters must be lowercase.
44 lines
1.1 KiB
44 lines
1.1 KiB
<?php |
|
|
|
namespace App\Docker; |
|
|
|
use Exception; |
|
use Psr\Http\Message\ResponseInterface; |
|
use React\Http\Browser; |
|
use React\Socket\FixedUriConnector; |
|
use React\Socket\Connector; |
|
use React\Socket\UnixConnector; |
|
use function React\Async\await; |
|
|
|
|
|
class Docker |
|
{ |
|
static protected $connection = null; |
|
|
|
public static function connect($fromSocket = true, $socket = 'unix:///var/run/docker.sock') |
|
{ |
|
$connector = $fromSocket ? new FixedUriConnector( |
|
$socket, |
|
new UnixConnector() |
|
) : null; |
|
$browser = new Browser($connector); |
|
Docker::$connection = (object)[ "connector" => $connector, "browser" => $browser ]; |
|
return Docker::$connection; |
|
} |
|
|
|
public static function endpoint($url) |
|
{ |
|
return config("docker.endpoint", "http://localhost") . $url; |
|
} |
|
|
|
public static function info() |
|
{ |
|
try { |
|
$connection = Docker::connect(); |
|
$response = await($connection->browser->get(Docker::endpoint('/info'))); |
|
return json_decode($response->getBody()); |
|
} catch(Exception $e) { |
|
throw $e; |
|
} |
|
} |
|
} |