<commit_before><?php
// Force errors to show
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// global $argv;

// Array of all folders at file directory, filter for only dev sites
$sites = glob('*', GLOB_ONLYDIR);
$dev_sites = array_filter($sites, function($value) {
  $pathinfo = pathinfo($value);
  if(isset($pathinfo['extension']) && $pathinfo['extension'] === 'dev')
    return $pathinfo;
});
// Default file locations and settings
$conf_file = '/etc/apache2/sites-available/000-default.conf';
$hosts_file = '/etc/hosts';
$host = "127.0.0.1";
// $conf_file = '000-default.conf';
// $hosts_file = 'hosts';

function setFile($file) {
  if(!file_exists($file)) touch($file);
}
setFile($conf_file);
setFile($hosts_file);

$php_devhost = "#--- Generated from php devhost.php ---#";

// 000-default file reset
$conf_array = file($conf_file, FILE_IGNORE_NEW_LINES);
$php_devhost_key = array_search($php_devhost, $conf_array);
if(!$php_devhost_key) {
  $virtual_host_key = array_search('</VirtualHost>', $conf_array);
  $directory = "\n
<Directory \"/var/www/html/\">
  AllowOverride All
</Directory>\n
$php_devhost";
  array_splice($conf_array, $virtual_host_key+1);
  file_put_contents($conf_file, implode(PHP_EOL, $conf_array));
  file_put_contents($conf_file, $directory, FILE_APPEND);
} else {
  array_splice($conf_array, $php_devhost_key+1);
  file_put_contents($conf_file, implode(PHP_EOL, $conf_array));
}

// Hosts file reset
$hosts_array = file($hosts_file, FILE_IGNORE_NEW_LINES);
$php_host_devhost_key = array_search($php_devhost, $hosts_array);
$hosts_key = array_search('fc00:e968:6179::de52:7100 ip6-allrouters', $hosts_array);
if(!$php_host_devhost_key) {
  array_splice($hosts_array, $hosts_key+1);
  file_put_contents($hosts_file, implode(PHP_EOL, $hosts_array));
  file_put_contents($hosts_file, PHP_EOL.PHP_EOL.$php_devhost.PHP_EOL, FILE_APPEND);
} else {
  array_splice($hosts_array, $php_host_devhost_key+2);
  file_put_contents($hosts_file, implode(PHP_EOL, $hosts_array));
}

// Setup strings for files
$listen = 1010;
$conf_str = "\n";
$hosts_str = "\n";
foreach($dev_sites as $site) {
  $listen++;
  $conf_str .= "
listen *:$listen
<VirtualHost *:80 *:$listen>
ServerName $site
DocumentRoot /var/www/html/$site
</VirtualHost>" . PHP_EOL;

  $hosts_str .= $host . ' ' . $site . PHP_EOL;
}
// For visual purposes to see what is being appended
echo $conf_str;
echo $hosts_str;

// Append strings to files
file_put_contents($conf_file, $conf_str, FILE_APPEND);
file_put_contents($hosts_file, $hosts_str, FILE_APPEND);
?><commit_msg>Fixing devhost script<commit_after><?php
// Force errors to show
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

/**
 * Global arguments passed into the script
 *
 * $argv[0] = Name of script
 * $argv[#] = # Argument
 *
 * @var Array
 **/
global $argv;

/**
 * Array of all dev folders
 *
 * @return Array
 *
 **/
function getSites(String $location = '*')
{
  $sites = glob($location, GLOB_ONLYDIR);
  $dev_sites = array_filter($sites, function($value) {
    $pathinfo = pathinfo($value);
    if(isset($pathinfo['extension']) && $pathinfo['extension'] === 'dev')
      return $pathinfo;
  });

  return $dev_sites;
}

/**
 * Creates the file if it doesn't exist
 *
 * @return void
 *
 **/
function createFile(String $file)
{
  if(!file_exists($file)) touch($file);
}

/**
 * Array of the file
 *
 * @return Array
 *
 **/
function buildFile(String $file)
{
  $generated_tag = "#--- Generated from php devhost.php ---#";
  $directory = "<Directory \"/var/www/html/\">
  AllowOverride All
</Directory>";

  $file_array = file($file, FILE_IGNORE_NEW_LINES);
  $php_devhost_key = array_search($generated_tag, $file_array);
  if(!$php_devhost_key) {
    $file_array[] = $generated_tag;
  } else {
    array_splice($file_array, $php_devhost_key+1);
  }

  if(basename($file) === "000-default.conf") {
    $file_array = array_merge($file_array, preg_split('/$\R?^/m', $directory));
  }

  return $file_array;
}

/**
 * Array of created directories
 *
 * @return Array
 *
 **/
function createDirectories(String $file, Array $dev_sites, String $host)
{
  $host = empty($host) ? "127.0.0.1" : $host;
  $listen = 1010;
  $sites_array = [];
  $output = "";
  foreach($dev_sites as $site) {
    if(basename($file) === "000-default.conf") {
      $listen++;
      $output .= "listen *:$listen
<VirtualHost *:80 *:$listen>
  ServerName $site
  DocumentRoot /var/www/html/$site
</VirtualHost>".PHP_EOL;
    } else {
      $sites_array[] = $host . ' ' . $site;
    }
  }

  if(basename($file) === "000-default.conf") {
    $str_array = preg_split('/$\R?^/m', $output);
    $sites_array = array_merge($sites_array, $str_array);
  }

  return $sites_array;
}

/**
 * Initialize development host
 *
 * @return void
 *
 **/
function initialize(String $file, $argv)
{
  $argv = !is_null($argv) ? '' : $argv[1];

  $sites = getSites();
  createFile($file);
  $directory_array = createDirectories($file, $sites, $argv);
  $file_array = buildFile($file);
  $file_array = array_merge($file_array, $directory_array);

  print_r($file_array);

  file_put_contents($file, implode(PHP_EOL, $file_array));
};

$files = [
// $conf_file = '/etc/apache2/sites-available/000-default.conf';
// $hosts_file = '/etc/hosts';
  '000-default.conf',
  'hosts'
];
foreach ($files as $key => $file) {
  initialize($file, $argv);
}

// Restart apache
echo PHP_EOL."Restarting apache2".PHP_EOL;
shell_exec('sudo service apache2 restart');
{'fraction_non_alphanumeric': 0.1775768535262206, 'fraction_numerical': 0.014285714285714285, 'mean_word_length': 4.834388185654008, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7416108', 'n_tokens_mistral': 2131, 'n_tokens_neox': 2017, 'n_words': 389}