تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Plan Artisan Generator0:00

In the last episode, we wrote a test to make the name property optional. If you don't include one, we will provide a sensible default based upon the current class name. That means if we go to our achievement types, here you can see it's the same name as the class, so I can get rid of that entirely. And then first thousand points, this one's a little different. So if we want to keep that, it would make sense, but I don't care either way. So we can remove it. And that leaves us something like this. So next, I'd like to have an artisan command to quickly whip up a new achievement.

Create Command Skeleton0:28

And that leaves us something like this. So next, I'd like to have an artisan command to quickly whip up a new achievement. I want this process to be as quick as possible. All right, I'm going to run php artisan make:command. And we'll call it GenerateAchievementCommand. All right, let's take a look at that. We could even move that into the achievements directory, and we might do that very thing in just a minute. But for now, the default location is okay. All right, so let's get started.

But for now, the default location is okay. All right, so let's get started. I'm going to close all these other ones. And the name and signature of the console command. I could put this under achievements. If I don't have too many commands, I could put it under my main domain namespace, whatever you want. I'm going to call it makeAchievement. I'll put it under the make namespace. But yeah, if I was distributing a package, I may not do that.

I'll put it under the make namespace. But yeah, if I was distributing a package, I may not do that. I might do something like that instead to have my own namespace. Anyways, here we'll say generate a new Achievement class stub. We don't need a constructor, but we could say this info getStarted. We're just going to try this out. And let's run it. php artisan make:achievement. Okay, Laravel will automatically pick up on that, by the way, because it's in my commands directory.

Add Achievement Stub1:37

Okay, Laravel will automatically pick up on that, by the way, because it's in my commands directory. But we do get the output. Let's do this very quickly. So I'm going to go to our Laracast mastery, that's kind of what it should look like. And I'm just going to grab this whole thing. And then, again, I'm going to put it in the achievements directory. Let's add a new directory. And we'll call it console. And then within here, I'll add our stub.

And we'll call it console. And then within here, I'll add our stub. So maybe achievement.stub, any convention you want. And we'll paste that in. Now, we don't want to hard code anything here. So I will leave those blank. Like so. And then this can just be something like class. All right, is that the stub we want? Yeah, I think it's fine.

All right, is that the stub we want? Yeah, I think it's fine. So now that we have that, let's quickly build this up. So if I want to get that stub, I could say, we could do file_get_contents, of course, we could do basic php, file_get_contents. Let's just do that. It's fine. app_path, go into the achievements console directory and grab achievement.stub. And we'll save that as stub. And quickly, I will dd that for you.

Accept Name Argument2:43

And we'll save that as stub. And quickly, I will die and dump that for you. So if I come and run that, sure enough, we have fetched that file. Next, it looks like we just need to prepare the class name and substitute it there. It sounds like we need to accept an argument, right? So I could say, makeAchievement, and we'll call it first, you know, maybe the first completed lesson or something like that. So we would need to take that and turn it into the name of the class up there. Okay. So as we scroll up, we're going to accept the argument, and we'll accept the achievement

Okay. So as we scroll up, we're going to accept the argument, and we'll accept the achievement name there. So now if we want to grab that argument, I could say $argumentName, and again, let's just quickly dd. And we have fetched that value. So now let's just do a quick str_replace. Look for class, replace it with whatever you gave us, using the stub as a base. And we will overwrite that. All right.

Generate and Write File3:42

And we will overwrite that. All right. Let's run it again. And there we go. We have our populated class. All right. So now that we have the contents, let's write the file and be done with it. File, putContents. It will be app_path, achievements, and again, I noted, I think I might want to have a type subdirectory, and that will only contain the specific achievement types.

It will be app path, achievements, and again, I noted, I think I might want to have a type subdirectory, and that will only contain the specific achievement types. But we haven't done that yet, so we'll come back to that later. Achievements and then the class name. So once again, this argument name, and finally, I will put that stub. All right. Let's give it a run. All right. Let's see. I have no output yet, but first completion.

Let's see. I have no output yet, but first completion. There we go. So we forgot to add the extension, but it does seem to be working. Great. So we'll say argument.php, and then let's do this now. arguments.info.app.achievements. And again, this argument name was created. Whoops. All right.

Whoops. All right. Let's give that a run. App\Achievements\First\Completion.php was completed, and now we have that file with our stub. So now let's make another one. Maybe you need an achievement for, maybe the User is a top 10 forum participant, top 10 User. All right. Let's grab that, and we can get started.

Refactor and Register Command5:15

So here's the basic code we wrote. Now if we want to clean things up a little bit, it looks like we're doing the final path two times. So why don't we say $path equals app.path.achievements, the name of the class, .php. And now I can grab all of that and write to the $path, and then the same thing here. So how about, yeah, the $path was created. Next, here we are fetching the stub and then basically compiling the template. So why don't we extract that to a method? How about called compileTemplate? Like so.

And I think we're all set to go. And actually, you know what? We could even inline that, can't we? A little bit cleaner. Anyways, the only remaining thing is if we want our command to be within the achievements directory like everything else, again, maybe you are distributing a package for others to use. And you would want to do this. So let's grab that. I'm going to move it to app/achievements into our console directory.

So let's grab that. I'm going to move it to app/Achievements into our console directory. All right. Now let's update the namespace. However, in this new location, Laravel will no longer auto detect the command. You won't see make:achievement there. OK, let's register it within the service provider right here. We'll do it within, yeah, here's where we set up the singleton. We'll do it down here at the bottom. We can use the commands method to register an artisan command.

We'll do it down here at the bottom. We can use the commands method to register an artisan command. MakeAchievement command. And that will be imported at the top. OK. Let's give it another run, and make Achievement is back. Make me an Achievement called thing. Let's make sure that's still working. There it is. And we're all done.

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