1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-06 16:50:09 +01:00

added ngrok class

This commit is contained in:
Taylor Otwell
2016-05-09 13:46:07 -05:00
parent f6abc45223
commit 9d2245b015
3 changed files with 50 additions and 15 deletions

View File

@@ -33,5 +33,6 @@ class CommandLine extends Facade {}
class Configuration extends Facade {}
class DnsMasq extends Facade {}
class Filesystem extends Facade {}
class Ngrok extends Facade {}
class PhpFpm extends Facade {}
class Site extends Facade {}

47
src/Ngrok.php Normal file
View File

@@ -0,0 +1,47 @@
<?php
namespace Valet;
use Httpful\Request;
use DomainException;
class Ngrok
{
public $tunnelsEndpoint = 'http://127.0.0.1:4040/api/tunnels';
/**
* Get the current tunnel URL from the Ngrok API.
*
* @return string
*/
public function currentTunnelUrl()
{
return retry(20, function () {
$body = Request::get($this->tunnelsEndpoint)->send()->body;
// If there are active tunnels on the Ngrok instance we will spin through them and
// find the one responding on HTTP. Each tunnel has an HTTP and a HTTPS address
// but for local testing purposes we just desire the plain HTTP URL endpoint.
if (isset($body->tunnels) && count($body->tunnels) > 0) {
return $this->findHttpTunnelUrl($body->tunnels);
} else {
throw new DomainException("Tunnel not established.");
}
}, 250);
}
/**
* Find the HTTP tunnel URL from the list of tunnels.
*
* @param array $tunnels
* @return string|null
*/
public function findHttpTunnelUrl(array $tunnels)
{
foreach ($tunnels as $tunnel) {
if ($tunnel->proto == 'http') {
return $tunnel->public_url;
}
}
}
}

View File

@@ -14,6 +14,7 @@
use Valet\Facades\Brew;
use Valet\Facades\Site;
use Valet\Facades\Caddy;
use Valet\Facades\Ngrok;
use Valet\Facades\PhpFpm;
use Valet\Facades\DnsMasq;
use Valet\Facades\Filesystem;
@@ -164,21 +165,7 @@
* Echo the currently tunneled URL.
*/
$app->command('fetch-share-url', function () {
retry(20, function () {
$response = Httpful\Request::get('http://127.0.0.1:4040/api/tunnels')->send();
$body = $response->body;
if (isset($body->tunnels) && count($body->tunnels) > 0) {
foreach ($body->tunnels as $tunnel) {
if ($tunnel->proto == 'http') {
return output($tunnel->public_url);
}
}
}
throw new Exception("Tunnel not established.");
}, 250);
output(Ngrok::currentTunnelUrl());
});
/**