Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Composer is a powerful dependency manager, much like Bundler for Ruby or NPM for NodeJS, in that it helps developers share and implement shared code. You can specify exact versions, pick stability levels and run updates to get new versions of code, all with a few commands.
Composer is a powerful dependency manager
much like Bundler for
0:00
Ruby or NPM for Node.js.
0:04
All of these systems helped developers
share code they have built and
0:06
implement shared code.
0:09
Composer works in a very similar way.
0:10
You can specify exact versions of the code
that you would like to install.
0:13
This helps ensure that you don't
accidentally get
0:17
major updates which could break your
application, but still get bug fixes and
0:19
patch improvements when you run Composer
update.
0:22
Composer is available from
getcomposer.org.
0:25
When you click on the Getting Started link
down here,
0:28
we can see installation instructions for
popular operating systems.
0:30
Number five here, we have installation for
0:34
Windows, and at number four here, we have
installation for *nix.
0:36
It's a bit of a, a bit of a confusing
name, but it means that these
0:41
are the instructions for OS X, Linux, and
any other Unix-based system.
0:44
We already have Composer installed in
Workspaces, and
0:49
we can access it using the console
feature.
0:51
If we go back to our workspace, we can
take a look.
0:53
Now, we haven't used console before, but
we can access it from View > Show Console.
0:56
You might recognize this as looking very
similar to an OS X or
1:01
Linux terminal, and it is.
1:04
It works in very much the same way.
1:05
Now, from here, we can check that we have
Composer installed just by
1:07
running the composer command.
1:10
If we see information like this, then it
means it's installed.
1:12
Let's pretend for this lesson that we're
building an application that requires
1:15
reading files from a ZIP file, and maybe
in the future,
1:18
it also needs to support things like
Amazon S3 or FTP drives.
1:21
That would be rather complicated to write
ourselves, but luckily,
1:25
we can just install the Flysystem package
from The League of Extraordinary Packages.
1:28
Now, to do that, we simply run composer
require league/flysystem.
1:32
And what this will do, if there's no
composer.json file already,
1:41
it will create one.
1:46
Otherwise, it will append to it.
1:46
But we don't have one, so we can assume
it's going to create it.
1:48
When we run this, it will ask us which
version we want.
1:51
Normally, it would be bad idea to specify
any version at all, but for
1:54
now, it doesn't really matter and we can
make it more specific later.
1:58
So we put star.
2:02
It just means, get me any version you can
find.
2:03
[BLANK_AUDIO]
2:05
There we go.
Looks like it's installed version 0.5.6.
2:09
And it's just suggesting a few other
packages that we could install if we
2:14
wanted to, but we don't care about those.
2:17
So if we look with ls, we'll see we have
some new files.
2:19
Let's refresh our workspace files and see
what we've got.
2:23
So as I mentioned, a composer.json file
has been created, and
2:27
inside we just have some basic JSOB with
an array of stuff.
2:30
This array, we have require here, which is
where we list all of our dependencies.
2:34
On the left, we have the names of our
dependencies, and
2:39
on the right-hand side is the version.
2:41
So as I said, it's a little bit vague at
the moment.
2:44
It's the version, but we know that it's
installed 0.5.6.
2:46
So why don't we just say 0.5.*?
2:49
That means if updates come out, then it
won't completely break our code.
2:53
Composer's also created a vendor folder
with this autoload.php.
2:56
We'll look at that in a second.
2:59
Composer, which you don't need to look at.
3:01
This is some internal logic.
3:03
And it's created the league folder with
our Flysystem package.
3:05
And this is where all the code for
3:08
Flysystem lives, especially in the source
folder here.
3:10
So this is all the code that makes
Flysystem run.
3:13
We haven't had to copy and paste anything
or, or add anything.
3:17
It's just, it's just straight in there.
3:20
Finally, we can see a composer.lock file
has been added.
3:22
This is similar in theory to the composer
JSON file,
3:25
but it contains much more specific
information.
3:29
Unfortunately, we can't preview it in
Workspaces, but it contains information
3:32
like, was it installed from Git or was it
downloaded from a ZIP file?
3:35
If it was downloaded, what was the exact
checksum so
3:39
we can make sure that we are using the
exact same version as each other.
3:41
You never want to edit the lock file, but
whenever you want to compose a update,
3:45
.lock will be updated and you should
commit the lock file.
3:48
So, with Composer installed and our new
dependency Flysystem available to use,
3:51
let's have a look at how it all fits
together.
3:55
[BLANK_AUDIO]
3:57
So, in previous versions, we made our own
Bootstrap file,
4:00
which contained our own autoloader.
4:04
We don't need that anymore because we can
just include the vendor autoload.php file
4:06
like we're doing on line three here.
4:11
This includes Composer's autoloader.
4:13
That means we don't have to worry about
autoloading a tool.
4:15
As a side note, require is exactly the
same as include,
4:17
with the added benefit of if it can't find
the file, it will error.
4:20
So if you run your code and it says, line
three failed, require could not
4:24
find vendor/autoload.php, then you know
that you need to run composer install.
4:28
So, let's move this console down a little
bit so we can look at our code better.
4:33
Okay, we've got some new stuff here.
4:38
On line seven, we have use
League\Flysystem\Filesystem.
4:40
This avoids us having to reference the
entire class name with all of
4:45
its namespace prefixed each time we use
it.
4:47
We can now just reference Filesystem and
PHP will know which class we mean.
4:51
On line eight, we have another use
statement,
4:54
which lets us alias the Zip class to the
global space.
4:57
This means we can reference Adapter and
not Zip, which is a very common name and
5:00
might conflict with something else.
5:03
On line ten, we are instantiating an
object as an instance of Filesystem,
5:05
containing an instance of Zip as a
constructor.
5:10
That Zip class is then taking the location
of the ZIP file as we want to read,
5:13
as a string argument, and that location is
in the current directory /archive.zip.
5:17
Then, line 13 uses a Filesystem object
method named listContents, and
5:24
we output everything into some HTML at the
bottom.
5:29
This HTML makes an unordered list on line
24, and
5:33
proceeds to loop through on lines 25 to 27
down here.
5:36
This little for each will go through our
contents, and
5:41
then make a content variable for each item
it finds.
5:43
Finally, line 26 outputs a few values from
the content array.
5:46
The type will say file or directory is a
string.
5:50
And the path value will be the name and
5:54
location of the file inside our
archive.zip.
5:56
So, let's run this and see what we get.
5:58
And there we have it.
5:59
A complex task made super easy just by
installing a Composer package.
6:02
You need to sign up for Treehouse in order to download course files.
Sign up