Composure is a powerful dependency manager much like bundler for Ruby or NPM for nodejs All of these systems help develop a share code. They have built an implement shared code composer works in a very similar way you Can specify exact versions of the code that you would like to install this helps ensure that you don’t accidentally get major updates Which could break your application, but still get bug fixes and patching provements when you run composer update? Composer is available from get composer org and we click on the getting started link down here We can see installation instructions for popular operating systems Number five here we have installation for Windows and a number four here. We have installation for star Nix It’s a bit of a bit of a confusing name But it means that these are the instructions for OS X Linux and any other UNIX based system We already have composer installed in workspaces, and we can access it using the console feature If we go back to our workspace we can take a look And we haven’t used console before but we can access it from view show console you might recognize This as looking very similar to an OS X or Linux terminal And it is it works in very much the same way now from here We can check that we have composer installed just by running the composer command If we see information like this then it means it’s installed Let’s pretend for this lesson that we’re building an application that requires Reading files from a zip file and maybe in the future it also needs to support things like Amazon s3 or FTP drives That’ll be rather complicated to write ourselves But luckily we can just install the fly system package from the League of Extraordinary packages now to do that We simply run composer require League slash fly system And what this will do if there’s no composer JSON file already it will create one otherwise it will append to it But we don’t have one so we can assume it’s going to create it When we run this it will ask us which version we want Normally, it would be a bad idea to specify any version at all But for now it doesn’t really matter and we can make it more specific later So we put star it just means get me any version you can find Here we go looks like it’s installed version not five six and It’s just suggesting a few other packages that we could install if we wanted to but we don’t care about those So if we look with LS we’ll see we have some new files. Let’s refresh our workspace files and see what we’ve got So as I mentioned a composer dot JSON file has been created and inside We just have some basic jason with an array of stuff this array We have require here, which is where we list all of our dependencies and on the Left We have the names of our dependencies and on the right hand side is the version So as I said, it’s a little bit vague at the moment as to the version But we know that it’s installed nor point 5.6. So why don’t we just say not 0.5 points star? That means if updates come out, then it won’t completely break our code Composers also created a vendor folder with this auto load dot PHP. We’ll look at that in a second Composer which you don’t need to look out This is some internal logic, and it’s created the league folder with our fleiss’s and package and this is where all the code for Fly system lives especially in the source folder here So this is all the code that makes fly system run. We haven’t had to copy and paste anything or edit anything It’s just it’s just straight in there Finally we can see a composer doclock file has been added This is similar in theory to the composer JSON file, but it contains much more specific information Unfortunately we can’t preview it in workspaces But it contains information like was it installed from git or was it downloaded from a zip file if it was downloaded What was the exact checksum so we can make sure that we’re using the exact same versions each other? You never want to edit the lock file, but whenever you run composer update dot lock will be updated And you should commit the lock file So with composer installed and our new dependency fly system available to use let’s have a look at how it all fits together So in previous versions we made our own bootstrap file which contained our own auto loader We don’t need that anymore because we can just include the vendor auto load dot PHP file like we’re doing on line 3 here This includes composers auto loader and means we’d have to worry about auto loading a tool as a slight note Require is exactly the same as include with the added benefit of if it can’t find the file ill error So if you run your code, and it says line three failed require could not find a vendor slash auto load PHP Then you know that you need to run composer install So let’s move this console down a little bit so we can look at our code better Okay, we’ve got some new stuff here on line seven we have a use league fly system file system this avoids us having to reference The entire class name with all of its namespace prefix each time we use it We can now just reference file system and PHP will know which class we mean on line eight We have another use statement Which lets us alias the zip class to the global space this means we can reference adapter And not zip which is a very common name and might conflict with something else on line 10 We are instantiating an object as an instance of file system containing an instance of zip as a constructor that zip class is then taking the location of the zip file as we want to read as a string argument and That location is in the current directory slash archive zip Then line thirteen uses a file system object method named list contents, and we output everything in some HTML at the bottom This HTML makes an unordered list on line 24 and proceeds to loop through on lines 25 to 27 down here This little for each will go through our Contents and then make a content variable for each, item it finds finally line 26 outputs a few values from the content array the type will say file or directory is a string and The path value will be the name and location of the file inside our archive dot zip So let’s run this and see what we get and there we have it a complex made super easy just by installing a composer package I Promised we would look through psr-4 as part of composer Well now that we know how to use composer we can look at how we build components with it Will use psr-4 as the standard to do this along with a few other standards all of that will be covered in the next stage

Composer – PHP Standard and Best Practices Part 11
Tagged on:                                 

Leave a Reply

Your email address will not be published. Required fields are marked *