Create a gist now

Instantly share code, notes, and snippets.

Embed
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