Skip to content

Instantly share code, notes, and snippets.

@vlakoff
Created July 31, 2013 04:47
Show Gist options
  • Save vlakoff/6119352 to your computer and use it in GitHub Desktop.
Save vlakoff/6119352 to your computer and use it in GitHub Desktop.
Change the hardcoded paths in bootstrap/compiled.php to dynamic paths. Make the compiled Laravel application really portable.
<?php
file_exists('./bootstrap/compiled.php') || exit('File compiled.php not found');
$content = file_get_contents('./bootstrap/compiled.php');
$root = realpath('.');
// foo\bar --> foo/bar
$root_unix = str_replace('\\', '/', $root);
// foo/bar et foo\bar --> foo\\bar
// en effet, les backslashes sont escapés dans les strings du fichier manipulé
$root_windows = str_replace(array('/', '\\'), '\\\\', $root);
$occurrences_before = substr_count($content, $root_unix) + substr_count($content, $root_windows);
// (?:foo/bar|foo\\bar)
$regex_root = '(?:' . preg_quote($root_unix, '@') . '|' . preg_quote($root_windows, '@') . ')';
// (?:/|\\)
$sep = '(?:' . preg_quote('/') . '|' . preg_quote('\\\\') . ')';
$regex = "'{$regex_root}{$sep}vendor(?:{$sep}|\w)+'";
$new_content = preg_replace_callback('@'.$regex.'@', function ($matches) {
$str = $matches[0];
$str_unix = str_replace('\\\\', '/', $str);
$root_unix = str_replace('\\', '/', realpath('.'));
// 'rootdir/vendor --> __DIR__ . '/../vendor
// dans le fichier manipulé, __DIR__ est rootdir/bootstrap
$new_str = str_replace("'{$root_unix}/vendor", "__DIR__ . '/../vendor", $str_unix);
echo 'Remplacement :', "\n";
echo ' ', substr($str, 0, 60), "\n";
echo ' ', substr($new_str, 0, 60), "\n\n";
return $new_str;
}, $content);
$occurrences_after = substr_count($new_content, $root_unix) + substr_count($new_content, $root_windows);
echo 'Nombre de chemins locaux avant nettoyage : ', $occurrences_before, "\n\n";
echo 'Nombre de chemins locaux apres nettoyage : ', $occurrences_after, "\n";
if ($occurrences_after > 0) {
exit("\n".'ERREUR : Echec du nettoyage de certains chemins'."\n");
}
if ($occurrences_before > 0) {
file_put_contents('./bootstrap/compiled.php', $new_content);
}
exit;
// le chemin complet est normalisé en "/", au lieu d'avoir un mélange de "/" et "\\"
// exemple de chemin à remplacer
'C:\\wamp\\www\\website\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation' . '/start.php';
// résultat actuel
__DIR__ . '/../vendor/laravel/framework/src/Illuminate/Foundation' . '/start.php';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment