در حال بارگذاری ...

Introducing Process Facade0:00

Now, next up, we can have a look at Laravel 10's new Process Facade, which is really cool and super flexible. Okay, so have a look. I will import that here. And yeah, if you're not familiar, Symfony's Process component allows you to trigger or invoke any kind of external process, like a command line script. But of course, the Process Facade wraps that and then adds a bunch of bells and whistles and conveniences and test helpers. Okay, so let's take a look. I'm going to run a new command, and let's keep it nice and simple.

Running Simple Commands0:30

Okay, so let's take a look. I'm going to run a new command, and let's keep it nice and simple. How about ls the current files in the current directory, or in the working path directory. And then I can grab the output of that command, and let's simply return it from this route, and then we'll have a look in the browser. And yeah, there we go. It works. And granted, I get it. It's a little hard to parse here because of the formatting, but sure enough, you can see in the base directory, we have a favicon, we have index.php, we have robots.txt.

It's a little hard to parse here because of the formatting, but sure enough, you can see in the base directory, we have a favicon, we have index.php, we have robots.txt. And in fact, why don't we just run it from the command line? I like this tool called HTTPie that you can install through Brew. And I can say, make a GET request to layerfl10.test. And there we go. So this is something a little more familiar to you, right? This is the files within my public directory. Okay, cool. So let's take it a little further.

Handling Failures and Errors1:22

Okay, cool. So let's take it a little further. What if I try to run a command that ultimately is gibberish? Something like this. All right, well, if I switch back and I give it a refresh, then this might be a little confusing because I don't see anything at all. All right, so as it turns out, and as you can imagine, well, when you run a command like this, in fact, let's just try it directly from the command line. And yeah, there we go. We get command not found.

And yeah, there we go. We get commands not found. So in this case, because it failed, the output was written to standard error, right? And the exit code is probably, I can't remember, 120, 125, 126. Why don't we have a look ourselves? I can check the exit code by calling the exitCode method. Okay, so once again, we come back and we give it a refresh. And yeah, okay, 127. So 127 stands for commands not found. Okay, very useful.

So 127 stands for commands not found. Okay, very useful. But on top of that, why don't we just check to see if there is error output? So remember, on the command line, there's a difference between standard output and then error-specific output. So in this case, if I give it another refresh, yeah, now we don't get a blank screen. We can see the output that was written or passed to standard error. Okay, very cool. Now, rather than checking a specific exit code, yeah, often you'll simply want to determine whether or not the command was executed or invoked successfully.

Long-Running Build Processes3:14

But otherwise, if we, once again, list all of the files, come back, well, that was successful. So we take this pathway. Okay, cool. Moving on, there's a lot more to show here. What about maybe more long running processes? So what if we were to say process, run, and I don't know, what should we do here? Maybe yeah, why don't we build Vite? Okay. So you know what, I probably need to do this as part of a command. So something like this, and then grab the output, right?

So you know what, I probably need to do this as part of a command. So something like this, and then grab the output, right? But yeah, why don't we switch this over to an actual console command? So within routes, we can go into console, and I can quickly define one here. We'll call it test, get rid of purpose. And then yeah, here is where I can trigger that logic. Okay, so back to our routes file. Drop that, close it out, and paste it in. Okay, so now when I run php artisan test, that's actually going to pass that through to npm run build, and then generate the output.

Okay, so now when I run php artisan test, that's actually going to pass that through to npm run build, and then generate the output. And then I will pass that output to the info method for your command. All right, let's give it a shot and see what happens. php artisan test. We wait a couple seconds, because remember, to build Vite probably takes two to four seconds, and then we get the results. So again, just notice that until that completes, you're waiting two, three, four seconds, in this case, actually three seconds. But yeah, still, that's pretty cool.

Streaming Progress Output6:00

And that's good to know. Okay, but what about especially long running processes? Maybe it takes 30 seconds or a minute, and you want to provide some kind of feedback as that command is being run. Let me show you something. If I click on this run method, notice that we can provide a callable, which is what we did earlier, right? And let's see where this is being executed. So you don't really need to follow this. But sometimes it's useful to see that when you call that run method, behind the scenes,

So you don't really need to follow this. But sometimes it's useful to see that when you call that run method, behind the scenes, if a callable is passed, that's just deferring to a start method, and then a wait method. Okay, so let's see what we can do with that. If I switch back to the console, that means I could say process start npm run build. And then I could say process, and then I could say process wait, and then I could do process output. So this would be another way that we could prepare that. So if I give it a run, one, two, three, we wait for the output, and then we render it all at once.

So if I give it a run, one, two, three, we wait for the output, and then we render it all at once. Okay, so that means while we're waiting, maybe we could do something. For example, I could say while the process is running, do something. You know, we could say info, and again, whatever needs to be done. In this case, I'll just say working to give you an idea. So I imagine if I run this, I'm going to have a full screen of working info messages. And yeah, there we go. But at the very end, once it completes, we get the output. Or we could say down here, you know, info, all done, whatever you need to do.

Faking and Asserting Processes8:09

Okay, really good to know. Okay, so what else? I don't want to overwhelm you too much here for a what's new series. We should probably dedicate a whole video to this. We could do a deep dive into it. There's things like concurrency and process pooling and lots of fun stuff. But why don't we wrap up with a quick discussion around testability, which is one of the key features here. So let's start from scratch. And I can say process, and like many of Laravel's components, there is a fake method on the

So let's start from scratch. And I can say process, and like many of Laravel's components, there is a fake method on the Facade. So now we're saying, let's just fake the process. Anything you pass here, don't worry about, don't actually execute. So if we give this a run, you'll notice that it completes instantly, because we aren't actually deferring to that command. Okay, so that's incredibly useful if you're writing a test that defers to the process Facade, but you don't actually want to execute the command. Instead, just fake it.

facade, but you don't actually want to execute the command. Instead, just fake it. Or there might be situations where you want to fake only certain commands, but not others. So for example, let's do this. Let's have two here. One of them, once again, just defers to list the files or maybe git log or something like that. In this case, process fake would fake every single command. So you'll see this completes instantly. However, you could also just say, no, fake git log and have that return a fake git log,

So you'll see this completes instantly. However, you could also just say, no, fake git log and have that return a fake git log, whatever you want your fake response to be. But anything not listed here as part of the array will be triggered as usual. So in this case, we'll have a fake git log, but then it will also, as you see here, run the full npm run build command. And you don't see any output here, again, because I didn't show any. And then actually, let's do the exact same thing here. Okay, that'll make it a little more clear. So we run it, and here is our git log fakery, but then we run the actual npm run build command.

Okay, that'll make it a little more clear. So we run it, and here is our git log fakery, but then we run the actual npm run build command. All right, good to know. And then what else? Because we are faking the process component here, we now have various assertions that we can write on top of that. So for example, let's keep it nice and simple here. Let's just say something like that, you know, list the files. Okay. If we were to fake that, a fake list, I can then say process->assert, and notice the various

Okay. If we were to fake that, a fake list, I can then say assert, and notice the various assertions. Well, assert that list of files was executed. And of course, in this case, it was. So if we run this, it works. But why don't we assert that something else ran, like git log? Well, in this case, of course, that's going to fail, as you see there. So if I scroll up, failed asserting that false is true, which is what we want. And yeah, then, of course, we have others like assert didn't run or assert that it ran.

So if I scroll up, failed asserting that false is true, which is what we want. And yeah, then, of course, we have others like assert didn't run or assert that it ran a certain number of times, most of the things that you might expect here, and that's really going to be useful within your tests. Yeah, but otherwise, the key thing to know here, at least initially, is that there's a new Process facade in Laravel 10 that allows you to run any sort of commands that you might need to trigger programmatically from a PHP file. You can then run the output method to get the standard output. You can fetch the exit code. You can check if it ran successfully.

Process FacadeProcess Faking

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟