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

Git save/rollback aliases0:00

Just before we start, let me show you an alias that I use all the time, it's super useful. If I type alias WIP, you'll see I have a bash alias setup that adds all changed files to git and then commits them with a message of work in progress. This essentially makes it incredibly easy at any point during development to type WIP, hit enter, and have confidence that my work is saved before I move to the next section. I also have an alias called NOPE, which is going to git reset, git clean and git checkout HEAD, in other words, go right back to the last commit, so that at any point, if I'm not happy with my work, I can NOPE right out of there and start fresh. I encourage you to set up similar aliases or at least know those commands, because it's very important that you're able to work in stages and feel free to roll back at any point.

Start steps TDD loop0:47

I encourage you to set up similar aliases or at least know those commands, because it's very important that you're able to work in stages and feel free to roll back at any point. So before you go any further, make sure you've saved to git. Alright, now let's get started. I don't want to get caught up in architecture at this point, I don't want to worry about how things look, and I don't want to jump between files. So I'm actually going to declare a function at the top of this file called steps. And at the moment, well, it's not going to do anything. Then in the terminal, I'm going to run our steps.php file again, and I'm almost going to use this script as a form of TDD.

Then in the terminal, I'm going to run our steps.php file again, and I'm almost going to use this script as a form of TDD. I'll keep executing this script with each change until it works as expected. This is, again, another very nice way to work when building a PR like this. So the error is that there is no function called addOnNull, why don't we return a new anonymous class? Again, I'm not worried about structure, I'm just building a solution. And inside here, we can add a public function called add. Let's run the script again. Now we have another error, unnamed parameter revert.

Store steps and reverts1:53

Let's run the script again. Now we have another error, unnamed parameter revert. So why don't we add the parameters to this method? We'll add a closure for the actual step, and then we'll add another closure which can also be false, called revert, and let's by default set that to null. Then inside here, we'll need to store those steps, so we'll introduce a protected array which is steps, and is empty by default. And then inside here, we can say $this->steps, and we can add this new step, which we'll do as an array for now, passing in the step, and revert. And seeing as I don't want to have to deal with a null value, let's use some primitive.

do as an array for now, passing in the step, and revert. And seeing as I don't want to have to deal with a null value, let's use some primitive form of null object syntax to say, well, if revert is equal to null, then revert is actually going to be equal to a shorthand closure that returns null. So now I know that steps either contains a closure or false. I don't have to worry about a null value. Okay, let's carry on. So we set the step. In order to chain, we're going to have to return this. Let's go back to the terminal and run our script again.

Add run and responses2:56

In order to chain, we're going to have to return this. Let's go back to the terminal and run our script again. Call to undefined method run. All right, so let's add a new method onto here, public function run, and it's going to return an array. Let's return this responses, which we'll need to add to the class. Here we go, protected array responses equals an empty array by default. And then finally, let's rerun the script again. And we have our first working script. It's not returning anything useful.

And we have our first working script. It's not returning anything useful. In fact, it's not doing anything at all, really, but it is working without error. So now we'll just continue to add to the script until it does something we're happy with. For now, it makes sense that we'd loop over all of the steps. So in run, we'll say foreach this steps as index, and then we'll destructure the array and ask for the step and the revert process. And then inside, we can set the response at the current index to be equal to, well, whatever you do when you execute the step, passing in the current responses. Let's see how that works.

you do when you execute the step, passing in the current responses. Let's see how that works. So we'll rerun. And there we go. We see our prompts. Welcome to Laravel is the intro prompt. Then we have what is your name? Why don't we use James Brooks? Where should we create your project? We'll use path for now.

Where should we create your project? We'll use path for now. Provide a password. We'll set up JavaScript. I'm fine with those tools. We're not going to install dependencies. And here is the output of all of the responses. And in fact, if we go a little further up, you'll see this note here, and it actually includes the path variable. So we can be sure essentially that passing the responses in from previous questions works.

Add keyed responses4:36

includes the path variable. So we can be sure essentially that passing the responses in from previous questions works correctly. That's actually given me a pretty cool idea out of the box. And that is it would be nice if instead of having to ask for a particular index, let me find this note. Here we go. Instead of having to ask for a particular index like responses[2], I really want to be able to say responses.path with a string. So what if when we add a new step, and let's come up to the path step, here we go, what

be able to say responses path with a string. So what if when we add a new step, and let's come up to the path step, here we go, what if we could also optionally specify a key? And in this case, we'll say that the key is called path. Can we add that? Can we make that work? Well, let's rerun the script. I imagine an error will appear. Yes, it does. Unknown named parameter key.

Yes, it does. Unknown named parameter key. So we could come back up to our implementation code and where we use add, we could introduce a new parameter string key equals null by default. And then when we create the step, why don't we also pass in the key? Okay, so now when we run through the steps, we have a step, we have the revert process, and we have a key. So when we set the response index, we could say, well, if there's a key, use the key, otherwise use the index. And I'm pretty sure that would work just fine.

otherwise use the index. And I'm pretty sure that would work just fine. So if we rerun playground/steps, let's run through it. We can use Drees for this one. Let's say project, provide a password. We'll use JavaScript, accept, install dependencies. It's going to install dependencies for three seconds, and then it will finish. And hopefully, yeah, it worked just fine. And I think that is a much nicer way to work with certain responses inside steps. You can provide a key, and then you can access that key in the current responses.

Extract StepBuilder class6:17

And I think that is a much nicer way to work with certain responses inside steps. You can provide a key, and then you can access that key in the current responses. Very nice. Although we don't yet have support for reverting steps, I think this structure is nice, and it's a great building block. So before we move forward, let's now move this out of the file into its own class so that we can carry on working with it in a clean manner. Let's create a new file inside source, and well, we'll call this StepBuilder. We can always change it down the line if we want. So StepBuilder, and StepBuilder is basically going to have everything inside the anonymous

We can always change it down the line if we want. So stepBuilder, and stepBuilder is basically going to have everything inside the anonymous class that we created. So we'll pull that out, paste it in, and yeah, everything's good to go. Let's just import closures correctly so that that works. Make sure we have no other errors in here. Nice. And then we can refactor steps here, not to return a new class, but to return a new stepBuilder like so. Let's make sure that nothing's broken by rerunning the script.

Move steps helper function7:18

builder like so. Let's make sure that nothing's broken by rerunning the script. Yep, looks like it's working absolutely fine. So now let's move steps out of here into the helpers file that prompts provides. I'll copy this. We'll paste it into here. Okay, that looks great. Put that down on a separate line. Let's add a return type of step builder. And then finally, we can come back into here and we can import that step function instead.

Let's add a return type of step builder. And then finally, we can come back into here and we can import that step function instead. Okay, let's rerun the script one last time. Looks like everything works absolutely fine. So let's move on to the next feature.

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