Skip to content

Instantly share code, notes, and snippets.

@ahuggins
Last active April 13, 2018 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahuggins/50db0a6bcddfabe6cdb9935e6fc740bf to your computer and use it in GitHub Desktop.
Save ahuggins/50db0a6bcddfabe6cdb9935e6fc740bf to your computer and use it in GitHub Desktop.
Add an eventListener and have make: commands open file in editor
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
// Add the following to the $listen array
protected $listen = [
'Illuminate\Console\Events\CommandFinished' => [
'App\Listeners\OpenInSublime',
],
];
}
<?php
namespace App\Listeners;
class OpenInSublime
{
protected $paths = [
'channel' => 'app/Broadcasting/',
'command' => 'app/Console/Commands/',
'controller' => 'app/Http/Controllers/',
'event' => 'app/Events/',
'exception' => 'app/Exceptions/',
'factory' => 'database/factories/',
'job' => 'app/Jobs/',
'listener' => 'app/Listeners/',
'mail' => 'app/Mail/',
'middleware' => 'app/Http/Middleware/',
'migration' => '',
'model' => 'app/',
'notification' => 'app/Notifications/',
'policy' => 'app/Policies/',
'provider' => 'app/Providers/',
'request' => 'app/Http/Requests/',
'resource' => 'app/Http/Resources/',
'rule' => 'app/Rules/',
'seeder' => 'database/seeds/',
// 'test' => './tests/Jobs//',
];
public function handle($event)
{
if (
config('app.env') !== 'production'
&& str_contains($event->command, 'make:')
) {
$classType = explode('make:', $event->command)[1];
// make:auth is not really something that works here
// it's not generating a single class
// So just return early and move on
if ($classType == 'auth') {
return;
}
$path = base_path(
$this->paths[$classType] . $event->input->getArgument('name') . '.php'
);
if ($classType == 'migration') {
$path = $this->getLatestMigrationFile();
}
// open the file
// This may need to be customized to your system...PHPStorm or
// might be subl instead of sublime depending on your system.
exec('sublime -a ' . escapeshellcmd($path));
}
}
protected function getLatestMigrationFile()
{
app()->config["filesystems.disks.easyOpen"] = [
'driver' => 'local',
'root' => base_path(),
];
$newestMigration = collect(
\Storage::disk('easyOpen')->files('database/migrations')
)->pop();
unset(app()->config['filesystems.disks.easyOpen']);
return base_path(
$newestMigration
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment