Would making 'Artisan make:?' auto open the file in an editor be useful?

Posted by Andy Huggins on April 13, 2018

ANOTHER UPDATE: This is now a package...or see it on Packagist.org. It's really easy to install, `composer require ahuggins/open-on-make` and if you use Sublime Text, you should be good to go, if you use PHPStorm or Atom, you will need to add `OPEN_ON_MAKE_EDITOR=pstorm` or `OPEN_ON_MAKE_EDITOR=atom` to your .env file for your Laravel project you just installed the package into.


UPDATE: Figured out that there is an Event raised when a Command Finishes execution. So now you only have to add an event/listener pair to EventServiceProvider, and then make a Listener, OpenInSublime.php. Here is the relevant code in a gist. NOTE: This is more of a proof of concept, setting a config value in a Listener is probably not a best practice. But the good news is there is no longer a need to change any core code to make this work.


I was re-watching Adam Wathan's "Cruddy by design" talk and noticed a kind of very tiny annoyance.

When we are working we often use the `Artisan make` commands to generate classes. Which is great, an example, `php artisan make:controller PublishedPodcastController`, which makes a namespaced PublishedPodcastController. It is often the case though, that when you are making a controller or model, you will probably have to edit it. So then we hit `command + tab` to switch to Sublime (or your editor of choice), hit `command + p` to type in 'PublishedPodcast' and hopefully Sublime has figured out we want to open that newly made controller and hit `enter`.

In that particular talk, Adam probably does this 6 or 7 times with various models/controllers. But one of the times, I had the thought...."wouldn't it be easier if the generate command just opened the file in the editor?"

And I thought for a little bit, well if the generate command raised an event, it would be super easy to tie into and customize for any editor. A simple pair (the event and the listener) in the EventServiceProvider and a simple `event(new ClassWasGenerated($path))` is the only line of code needed to raise the event.

I did a quick test in a local Laravel project, and threw the relevant code in a gist.

Really only saves you the app switching, and the opening shortcut in sublime, but considering that you have to type in enough of the name again and think about it...it could end up saving quite a few keystrokes over the course of a project. Or at a minimum would reduce cognitive load in a talk.

But is this something people want? It's kind of nice, but it's really small. Ultimately it's super tiny, so I think it's probably not worth it. But if you look at the gist linked above, all that really is needed to make this possible is a new Event, and for that event to be raised in a single class and it works for every command. That's a very small amount of code, for a bit of convenience.

Another thing to consider that if the Event was added, it doesn't mean that Laravel has to even worry about the code needed to open the file. It would be something that devs could add if they wanted since they would be able to control it in the EventServiceProvider. There is almost no "cost" involved with raising an Event that never is listened to, or at least it's pretty minimal.