[eluser]Kamarg[/eluser]
[quote author="Devon Lambert" date="1274391841"]A php version of the following would be nice!
http://travisonrails.com/2009/08/10/ruby...host-entry[/quote]
Untested but a quick attempt at translation without much error checking.
Code:
<?php
/*
########################################
##### VARIABLES YOU NEED TO CHANGE #####
########################################
host_dir = '/etc/hosts' # path to your hosts file
sites_dir = '/Library/Webserver' # path to the directory where you keep your sites (NO TRAILING SLASH!!!)
conf_dir = '/etc/apache2/sites' # path to directory where named conf files live
name = '' # the folder that contains the site
hostname = '' # an optional local domain
########################################
*/
$host_dir = null;
$sites_dir = null;
$conf_dir = null;
$name = null;
$hostname = null;
$conf_contents <<< EOF
<VirtualHost *:80>
ServerName $hostname
DocumentRoot "$sites_dir/$name"
DirectoryIndex index.php
<Directory "$sites_dir/$name">
Options FollowSymLinks MultiViews Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
EOF;
// first things first: make sure named conf file doesn't exist already
if(file_exists("$conf_dir/$name.conf")) {
return FALSE;
}
// check to make sure host file exists
if(($host_dir)) {
if(file_put_contents($host_dir, "127.0.0.1\$hostname") === FALSE) {
return FALSE;
}
if(file_put_contents($conf_dir . '/' . $name . '.conf', $conf_contents) === FALSE) {
return FALSE;
}
}
Edit: Noticed I still had Ruby comments instead of PHP comments.