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

Familiarize With Project0:00

Because you're excited about writing the code, you may be tempted to jump right in, but stop and take the time to familiarize yourself with the project, because it will save you so much frustration in the long run. If you don't understand how the project works under the hood, you're going to make mistakes, and when you submit the PR, it will likely be in a state that the maintainer is not happy with. At best case, they give you a long list of instructions to fix, but more often than not, they don't have time for that, and they'll just close the pull request. So it's so important that you familiarize yourself. Let's spend a few minutes familiarizing ourselves with Laravel prompts. Most projects will have a .github folder. This contains all of the workflow files for GitHub Actions. You can skip that for now. The same with Art, which just contains the logo to show.

Exploring Playground Confirm0:46

Most projects will have a .github folder. This contains all of the workflow files for GitHub Actions. You can skip that for now. The same with Art, which just contains the logo to show on GitHub. We have a Playground. Well, that's different for this project. I haven't seen that in many projects before. And I imagine, let's take a look at Confirm. Yeah, essentially, it's showing an example of the prompt. If we're going to create steps, we'll also need to create a Playground file to fit in with how the rest of prompts is built. So it's a good job that we've seen that now. In fact, why don't we run that Confirm from the terminal? So php Playground/confirm.php. There we go. That's exactly what I'd expect to see. So we have a confirmation prompt. The label is would you like to install dependencies? The hint is dependencies are required to run the application, which is what we see here. And then there's a validation logic

Touring Prompt Source1:28

confirmation prompt. The label is would you like to install dependencies? The hint is dependencies are required to run the application, which is what we see here. And then there's a validation logic on this confirmation prompt, which essentially dictates that we have to confirm. So I would expect if I tried to select no here. Yeah, there we go. We see the you must install dependencies validation error. Whereas if I select yes, everything works. And we're seeing bool true there because at the end we're var_dumping whether or not you confirmed. Okay, that's pretty neat. Let's move on to source. All right, here is all of the underlying code for how prompts works. And we have some traits here that we can make use of. We have the output. I imagine, yeah, these are wrapping the symfony/console output. Essentially, this is how we write to the terminal wherever this might be used in prompts. And it's doing a little bit of additional logic over the

these are wrapping the symphony console output. Essentially, this is how we write to the terminal wherever this might be used in prompts. And it's doing a little bit of additional logic over the default symphony console output, which is why it's extending it. Okay, we have themes, contracts, default. So we have this idea of a renderer from the looks of things. I wonder how a renderer differs from a prompt. Let's take a look at confirm. Okay, let's go from the top. So we'll head back to our little confirm.php playground script and let's source dive from top to bottom. This is a great way to understand how things work. So we'll jump into confirm. Confirm receives parameters and it's passing those parameters into this confirm prompt instance and then it calls prompt on it. So confirm prompt receives these parameters. It's setting a confirmed state. Here we go, bool. And it will set it to whatever you pass as the default, which is true by default.

prompt on it. So confirm prompt receives these parameters. It's setting a confirmed state. Here we go, bool. And it will set it to whatever you pass as the default, which is true by default. And then it's registering, is that? Yeah, it's registering an event listener for a key press. And depending on what you press, so if you press Y, it will set it to true. If you press N, it will set it to false. Let's test that. I'll run the playground again. I'm going to press N and now I'll press Y. Yeah, that works nicely. Or you can use tab, up arrow, down arrow. You can even use H, J, K, L, and that will change the current output, the state of the prompt. So if I use the arrow keys, for example, yeah, I can move between the different states. If you click enter, it will submit the prompt and the default is that nothing happens. So it would seem when you press a key, we're not rendering anything here, right? You don't see any

Prompt Rendering Cycle3:54

If you click enter, it will submit the prompt and the default is that nothing happens. So it would seem when you press a key, we're not rendering anything here, right? You don't see any output to the console in this match statement. We're just changing the state of the prompt itself. So I imagine if we go back to helpers and we now take a look at this prompt method, which is being called, this is where the actual magic happens. I'm going to skip past this first part, which just seems to be doing setup checks. And here is what I'm interested in. We render. So we're rendering the prompt and also this piece here, right? So it looks like we're reading the key presses, we're handling those key presses, and then we're also rendering the prompt. So we have an initial render and then we have a key press render, shall we call it. Let's take a look at the key press render. So we handle the key press, then we render out. Render works by creating

have an initial render and then we have a key press render, shall we call it. Let's take a look at the key press render. So we handle the key press, then we render out. render works by creating a frame from renderTheme and then outputting it to the terminal. So this is where we actually write to the terminal. What happens in renderTheme? It gets the renderer and it calls it passing the prompt into the render itself. And this is interesting. Okay. So rendering takes place separately to a prompt state. So here in the themes, we have the confirmPrompt, which is the Prompt class itself, which links itself to the confirmPromptRenderer. And the confirmPromptRenderer receives that prompt with its state. And it's going to change the output in the terminal based on that state. You can see it returns a string, which is what's actually being written to the terminal by the Prompt class itself. Okay. This is really nice. And of course,

in the terminal based on that state. You can see it returns a string, which is what's actually being written to the terminal by the prompt class itself. Okay. This is really nice. And of course, it's using, if I find a submit again, it's using the label, which is essentially based on the confirm status of the prompt. It's using the label to actually write out. So here's submit, here's cancel. You can see it has an error canceled underneath. Why don't we test that? I'm going to use Control C to cancel the confirm prompt. Yep. There we go. There's the canceled error. There's one for standard error as well, which is a warning. I wonder what a warning looks like. Ah, yellow with an exclamation points. So I think I've seen that if we run this again from the terminal and then I go to no and click enter, we get that validation message. You must install dependencies. That is what we're seeing here, right? So the prompt error is being set by the

the terminal and then I go to no and click enter, we get that validation message. You must install dependencies. That is what we're seeing here, right? So the prompt error is being set by the validator, and then it's being output in yellow by the renderer. So it's super important that we get this overview because if we were to dive right in, we would have completely missed the fact that the state of a prompt is separate from the rendering of a prompt. And we might've combined the two in one way or another. Jess Archer, who maintains Laravel prompts would see that and say, no, that's not how we do things. Best case, she would have given you a load of things to fix, which would be frustrating for yourself, but probably she's not got time for that. She just closed the PR and your idea would be dead. So it's so important that we familiarize ourselves with how prompts work. All right. I think I've got the basic idea of how this works,

Summarizing Prompt Architecture7:03

She just closed the PR and your Idea would be dead. So it's so important that we familiarize ourselves with how prompts work. All right. I think I've got the basic idea of how this works, but let's just run it from top to bottom so that we can be sure. So you call one of the various functions that prompts provides in this case, confirm, confirm builds up a new prompt instance, passes the arguments in, and then calls the prompt method. The prompt method is, after all has been done, going to render that prompt out to the console. But to render that prompt out to the console, it will use the relevant renderer by calling render theme. So in this case, it would be the confirm prompt renderer. Confirm prompt renderer is going to return the relevant string based on the state of the confirm prompt. And then the prompt itself is going to go ahead and write that out to the terminal in one way or another. So if the state

Output Diff Optimization7:51

return the relevant string based on the state of the confirm prompt. And then the prompt itself is going to go ahead and write that out to the terminal in one way or another. So if the state is submit, for example, it will write it here. And then it's doing something very clever down here, where if the state is not submit or initial, it will diff the lines between the previous output and the current output. And it will only write what's necessary to the terminal rather than rewriting the entire prompt each and every time. And I'm sure there are some differences for different types of prompts. For example, a spinner probably has to do some clever magic, but for the most part, that is how prompts work. All right. So with that in mind, I have a few ideas for how I actually want to build steps out. Why don't we start playing around with that in the next episode.

ideas for how I actually want to build steps out. Why don't we start playing around with that in the next episode.

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