Review Laravel Installer0:46
installer tool. Now, before we dig in, if you're not familiar with it, once we install it, it gives us a Laravel executable here. And really, all that it does is it gives us a very quick way to get set up with a new Laravel application. So, notice if I say, laravel new myProject, it will download everything we need. And if we cd into it, there's our Laravel app. So, in this video, we are going to reproduce this exact thing exclusively for learning purposes. So, let me go ahead and delete that folder. And we'll dig in. Now, if I run laravel here, notice that we have a new command here. Let's see how Taylor set that up. If we switch back to the Git repo, we can see that here's our entry point, so to speak, where we require the autoloader. We set up a new instance of a console application. And then notice we add to our application a command called new command. So, let's look at
where we require the autoloader. We set up a new instance of a console application. And then notice we add to our application a command called new command. So, let's look at that. Go into the source directory. And this looks pretty familiar, right? We have that configure method where we set up the description and the name and any arguments that might be necessary. And then, once again, we have the execute method to actually process the command. And then, indentation aside, if we read over this, it actually makes perfect sense. We begin by asserting that an application doesn't already exist. That way, you don't accidentally overwrite an existing folder. Next, we send some output to the user. And then we download a nightly of Laravel. And we extract it to the directory that we give it. And then we do a little bit of cleanup where we delete
Set Up New Command2:20
Next, we send some output to the user. And then we download a nightly of Laravel. And we extract it to the directory that we give it. And then we do a little bit of cleanup where we delete any temporary files. Finally, we're all finished, so we alert you once again. Alright, so now that we've reviewed it and we understand how it's working, let's reproduce it on our own as an exercise. If I switch back to the editor, let's keep it as LaraCasts, but we are going to update the command to newCommand. Alright, let's go within here and rename that newCommand, like so. Next, if we hide the sidebar, we'll set the name. We will then update the description, and I'll just paste that in. And we do require one argument, but no options. And then finally, the name is pretty self-explanatory, so we can leave that off entirely. Next, it's time to execute this. So let's get rid of our boilerplate from the
but no options. And then finally, the name is pretty self-explanatory, so we can leave that off entirely. Next, it's time to execute this. So let's get rid of our boilerplate from the previous lesson, and we can get started. Now, to begin, I'm going to add a couple comments here just so I understand what needs to happen. So I want to assert that the folder that we request doesn't already exist. And next, we want to download the nightly version of Laravel. So download nightly version of Laravel. We then need to extract that zip file. So extract zip file. And then finally, alert the user that they are ready to go. Okay, simple game plan here. Let's get started. To begin, we need to assert that the folder doesn't already exist. So let's say this assertApplicationDoesNotExist. Okay, let's go ahead and create that method here. And that will be private in this case. So how would we go about doing this? Well, it sounds like we
Validate Target Directory3:57
So let's say this assert application does not exist. Okay, let's go ahead and create that method here. And that will be private in this case. So how would we go about doing this? Well, it sounds like we need to accept the name of the directory that we're trying to create. That way, we can say if the name that we pass it already is a directory, well, we don't want to override that. Instead, we just want to exit. We can abort it using the exit function, and I will pass it 1, which is just a general error status code for something went wrong. Now, this is fine, but we also want to alert the user in the process. So with that in mind, why don't we pass through the output interface, like so. And then we can say output.writeLine(application already exists). All right, let's try this out. We need to pass through the directory, and we'll set that up shortly, as well as the output interface. We can fetch the directory by saying get the current working directory with a /,
We need to pass through the directory, and we'll set that up shortly, as well as the output interface. We can fetch the directory by saying get the current working directory with a slash, and then finally, the name that we provided from the console. Input, get argument, name. All right, so we have our directory name. We assert that an application with that name doesn't already exist. So if there is a directory, we output some information for the user, and we exit. Let's try it out. Let's assume we already have a directory called foo. If I then run laracast new foo, and we get class not found, we must have forgotten some kind of import. And yep, there it is, new command. Okay, one more time, and there we go. There's our output. Now, if you want this to be styled a little bit, we can go back, and we can wrap this like so, using these little HTML-like tags. Now, if we run it, you'll see it looks much more like an error here, which is cool. However,
Download With Guzzle5:41
to be styled a little bit, we can go back, and we can wrap this like so, using these little HTML-like tags. Now, if we run it, you'll see it looks much more like an error here, which is cool. However, if we try something else, we don't get that error, which means everything's working. Cool. Let's move on. So if I scroll up, this first part is done. Next, we need to download a nightly version of Laravel. We want to extract it, and then alert the user. Now, if you review the official source code for the Laravel installer, you can see that Taylor treats it sort of like a fluent API. So we will adopt that as well. This, download, and then extract. And let's see how we can do that. So right here, we have our download method, and once again, that can be private. Now, there's a cron job that runs every day. So if you request this URL, you'll get a nightly version of the framework. So with that in mind, what's the best way to go about making this request using php?
cron job that runs every day. So if you request this URL, you'll get a nightly version of the framework. So with that in mind, what's the best way to go about making this request using php? And generally, the de facto standard these days is to use Guzzle. So let's pull that in through Composer. composer require guzzlehttp/guzzle. And give that just a minute. Okay, that's now available to us, so let's go ahead and pull it in. And in this case, we'll pull it in through the constructor. Now, we want to use GuzzleHttp\ClientInterface. So let's update that, ClientInterface. And then I'll use a little macro here to save ourselves some time. Now, if I scroll down, when we download the file, I can just say $this->client->get, and I'll paste this in. So I will save that to a variable called $response, and of course, we do want to get the body from the request. But now, we want to save this to a temporary zip file that we
I'll paste this in. So I will save that to a variable called response, and of course, we do want to get the body from the request. But now, we want to save this to a temporary zip file that we will then open and extract. So that means the download method should accept some kind of zip file variable, and we'll go ahead and pass that through. So right here, the way Laravel does it, is it generates a unique file name. So we'll say this, makeFileName, and we'll do that right here. Method, makeFileName. And here, we're going to return the current working directory, and then we will call it laravel_, and here's where we will make it unique, by simply running md5 on the current time, and then also appending to it a unique ID. Finally, it will be a zip file, so we will tack on .zip. That means when we call the download method, zipFile will be equal to whatever this is. So now, I can just say,
Finally, it will be a zip file, so we will tack on .zip. That means when we call the download method, zip file will be equal to whatever this is. So now, I can just say, file_put_contents to this unique zip file, the response. Finally, if we scroll up, don't forget that we want this fluent style interface, so we need to make sure that we return this. That way, we can continue chaining. All right, so why don't we try this out and see what we get. But before we do that, if you're working along, look over everything, and see if there's something that might cause this to fail. Let's try it out. We run laracast new foobar, and yeah, argument one to the constructor must be an instance of ClientInterface, but we didn't give it anything. So we're leveraging dependency injection here, but when we created the object, we forgot to pass that in. So we'll do that, and then also,
but we didn't give it anything. So we're leveraging dependency injection here, but when we created the object, we forgot to pass that in. So we'll do that, and then also, by the way, we want to make sure that we call the parent class's constructor as well. Okay, so let's go to laracasts, and we will pass in GuzzleHttp\Client. Let's try it out now. One more time, we run it, and yes, we got an error because we haven't yet set up the extract method. However, if we list the files, you can see that zip file right there. And if you want to take a look, let's open that up, and if we inspect it, you'll see that we have the contents of a Laravel app. So we're on the right track here. Let's now switch back and add the extract method here. Right down at the bottom, extract, and this will be pretty easy as well. To open up and extract a zip file, we can actually use native PHP here. So at the top,
Extract Zip Archive9:59
Right down at the bottom, extract, and this will be pretty easy as well. To open up and extract a zip file, we can actually use native php here. So at the top, I'm going to use ZipArchive, and then here's the basic usage. $archive equals new ZipArchive. Next, we need to open the zip file, so $archive-open($zipFile), and we'll have to accept that when we call it. Next, we need to extract it, so $archive-extractTo($directory), and it looks like we'll also have to pass through a directory as well. And then finally, we're done, so we can close everything out. $archive-close(), and return this just in case we need to continue chaining. So let's make sure that extract is given the proper arguments here, the $zipFile and the $directory. So that means we'll have to save this, and then we can pass it through here, and also give it the directory name. Finally, we can see alert the user that they are ready to go. Let's go ahead and tackle that as
Cleanup and Final Output10:55
we'll have to save this, and then we can pass it through here, and also give it the directory name. Finally, we can see alert the user that they are ready to go. Let's go ahead and tackle that as well. Output, writeLine, and this will be a comment here. Application ready. So why don't we run this and see what else we need to do. php artisan new foobar. Give that just a second, and there we go. List the files, and there's our foobar folder. List foobar, and we have a new Laravel install. But one issue is we still have that temporary zip file, so it looks like we need one additional method that does just a little bit of cleanup. All right, no problem. Right here, we will continue chaining, and we will call cleanup and pass through the zip file. All right, right down here at the bottom, cleanup, give it the zip file, and then once again, this should be private. Now, we will begin by unlink the file, and honestly, that's probably
All right, right down here at the bottom, clean up, give it the zip file, and then once again, this should be private. Now, we will begin by unlinking the file, and honestly, that's probably all that's enough. But just in case, we're going to change the permissions as well to ensure that we do have the ability to delete this file. So change the permissions for the zip file to 0777, and then we will also make sure that we suppress any warnings, since they wouldn't be relevant in this case. Finally, we're done. We will return this just in case there's any other chaining to do, but I think mostly that takes care of it. So let's go over it one more time, and then we'll try it out. When we execute this command, we figure out the directory name that we want to use. We then assert that a application or a directory name of that type doesn't already exist. And if we get beyond that, we download the nightly version of FlareFL and save it to.
we want to use. We then assert that a application or a directory name of that type doesn't already exist. And if we get beyond that, we download the nightly version of FlareFL and save it to this zip file. And then we extract that zip file to the given directory. And then finally, we do some cleanup to remove that temporary zip file, and we alert the user. Now the only other thing that the official Laravel installer does is it notifies you right away, just because it takes a few seconds to install, and you don't want the person waiting there without knowing what's going on. So that's why we might have something like this. Crafting application dot dot dot. All right, let's try it out one last time. laracasts new my awesome application. We're crafting it, we download it, we extract it. So now if we list the files, there's our new folder. If we cd in, a fresh install of Laravel is ready to go. All right, so I think that does it for this lesson.
