Skip to content

Instantly share code, notes, and snippets.

@JackNoordhuis
Created October 8, 2017 09:50
Show Gist options
  • Select an option

  • Save JackNoordhuis/b1122dd4f691662cc9133e7c1c2babd7 to your computer and use it in GitHub Desktop.

Select an option

Save JackNoordhuis/b1122dd4f691662cc9133e7c1c2babd7 to your computer and use it in GitHub Desktop.
Script to quickly generate an aliases.php stub file for the Laravel Framework
<?php
/*
* Script to quickly generate the aliases.php stub file
*/
$root_path = realpath(__DIR__ . "/../../.."); // path to the project root directory
$config = preg_replace("/\s+/", "", file_get_contents($root_path . "/config/app.php")); // get the app config and strip all white space
preg_match('/"aliases"=>\[(.*?)\,],/', $config, $matches); // find the aliases array entry
preg_match_all('/"(.*?)\=>(.*?)::class,/', $matches[1], $result); // scrape the array for all alias entries
$stub = "<?php\n\n/*\n * Stub used to help IDE's use the laravel frameworks class alias feature.\n *\n * This file was generated automatically, DO NOT edit it manually.\n */\n"; // PHP file declaration and file header/comment
for($count = count($result[0]), $i = 0; $i < $count; $i++) { // loop over all the alias entries
$stub .= "\nclass " . trim($result[1][$i], '"') . " extends " . trim($result[2][$i], '"') . "{}\n"; // create a global dummy class with the alias name that extends the aliased class
}
file_put_contents($root_path . "/stub/aliases.php", rtrim($stub, "\n")); // write the resulting stub to our stub file and let the IDE index the result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment