Last active
January 12, 2018 19:28
-
-
Save a-r-m-i-n/bfc203c9146e42c59d6832693990cee3 to your computer and use it in GitHub Desktop.
Run local webserver in PHP and open default browser (on windows)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"symfony/process": "^3.2" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\Process\Process; | |
require_once('vendor/autoload.php'); | |
$host = 'localhost'; | |
$port = '8080'; | |
$htdocs = './app'; | |
$timeout = 5; // seconds | |
$url = $host . ':' . $port; | |
$cmd = PHP_BINARY . ' -S ' . $url . ' -t ' . $htdocs; | |
$webserverProcess = new Process($cmd, __DIR__, null, null, null); | |
$webserverProcess->start(); | |
if ('\\' === DIRECTORY_SEPARATOR && $webserverProcess->isRunning()) { | |
$errno = 0; | |
$errstr = ''; | |
$fp = @fsockopen($host, $port, $errno, $errstr, $timeout); | |
if ($fp) { | |
fclose($fp); | |
$openWebsiteCommand = DIRECTORY_SEPARATOR === '\\' ? 'explorer ' : 'open '; | |
$startBrowser = new Process($openWebsiteCommand . escapeshellarg('http://' . $url)); | |
$startBrowser->run(); | |
echo "To exit webserver press ctrl+c"; | |
} else { | |
echo "Error when starting webserver!" . PHP_EOL; | |
} | |
} | |
$webserverProcess->wait(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment