Skip to content

Instantly share code, notes, and snippets.

@vfsoraki
Created October 18, 2019 22:57
Show Gist options
  • Save vfsoraki/28f4c85073d294ee7a1986931714e849 to your computer and use it in GitHub Desktop.
Save vfsoraki/28f4c85073d294ee7a1986931714e849 to your computer and use it in GitHub Desktop.
Run arbitrary scripts in context of a Laravel application
<?php
/**
* This Artisan command can run any file in the context of application.
* It can be useful to test some scripts in production/staging to see what's happening.
* You can also use `$this->*` functions in script.
* This reads scripts from `storage` folder, but nothing prevents you from changing it.
**/
Artisan::command('run:script {script}', function ($script) {
$full_path = storage_path($script);
if (file_exists($full_path)) {
require $full_path;
} else {
$this->error("File {$script} does not exist in `storage` folder, double check name.");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment