taskOpenBrowser('http://localhost') * ->run(); * * // open two browser windows * $this->taskOpenBrowser([ * 'http://localhost/mysite', * 'http://localhost/mysite2' * ]) * ->run(); * ``` */ class OpenBrowser extends BaseTask { /** * @var string[] */ protected $urls = []; /** * @param string|array $url */ public function __construct($url) { $this->urls = (array) $url; } /** * {@inheritdoc} */ public function run() { $openCommand = $this->getOpenCommand(); if (empty($openCommand)) { return Result::error($this, 'no suitable browser opening command found'); } foreach ($this->urls as $url) { passthru(sprintf($openCommand, ProcessUtils::escapeArgument($url))); $this->printTaskInfo('Opened {url}', ['url' => $url]); } return Result::success($this); } /** * @return null|string */ private function getOpenCommand() { if (defined('PHP_WINDOWS_VERSION_MAJOR')) { return 'start "web" explorer "%s"'; } passthru('which xdg-open', $linux); passthru('which open', $osx); if (0 === $linux) { return 'xdg-open %s'; } if (0 === $osx) { return 'open %s'; } } }