Created
August 1, 2019 09:26
-
-
Save Mordo95/f463658493a76643c148b729a56e8666 to your computer and use it in GitHub Desktop.
Laravel add hosts entry and vhosts to an xampp environment
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 | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
class testenv extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'testenv:register {path}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Register the testenv'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
if (PHP_OS != "WINNT") { | |
$this->error("This command can only be run on windows for now"); | |
return; | |
} | |
$basepath = base_path(); | |
$pubpath = public_path(); | |
$domain = $this->input->getArgument('path'); | |
exec('powershell.exe Start-Process powershell.exe -ArgumentList \'Add-Content -Path C:\Windows\System32\drivers\etc\hosts -Value \"127.0.0.1`t' . $domain . '\" -Force\' -Verb runAs'); | |
$vhc = []; | |
$vhc[] = "### ADDED BY ADDHOST: " . date("Y-m-d H:i:s") . "###"; | |
$vhc[] = "<VirtualHost *:80>"; | |
$vhc[] = "\tServerAdmin webmaster@localhost"; | |
$vhc[] = "\tDocumentRoot $pubpath"; | |
$vhc[] = "\tServerName $domain"; | |
$vhc[] = "\tServerAlias www.{$domain} *.{$domain}"; | |
$vhc[] = "\tErrorLog \"logs/{$domain}-error.log\""; | |
$vhc[] = "\tCustomLog \"logs/{$domain}-access.log\" common"; | |
$vhc[] = ""; | |
$vhc[] = "\t<Directory \"$pubpath\">"; | |
$vhc[] = "\t\tOptions Indexes FollowSymLinks Includes ExecCGI"; | |
$vhc[] = "\t\tAllowOverride All"; | |
$vhc[] = "\t\tOrder allow,deny"; | |
$vhc[] = "\t\tAllow from all"; | |
$vhc[] = "\t</Directory>"; | |
$vhc[] = "</VirtualHost>"; | |
$apache_extra_dir = str_replace('php.exe', '', PHP_BINARY); | |
$apache_extra_dir .= '..\\apache\\conf\\extra\\'; | |
file_put_contents($apache_extra_dir . "httpd-vhosts.conf", PHP_EOL . PHP_EOL . implode(PHP_EOL, $vhc), FILE_APPEND); | |
$this->info('Please restart your apache webserver for changes to take effect.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment