The Wonderful World of Composer

Video loading...

  • 0:15
    The Wonderful World of Composer
  • 0:17
    with Leanna Pelham
  • 0:26
    [Common Web Languages]
  • 0:27
    Welcome to the brand new world of PHP with Composer.
  • 0:30
    One of the great things about PHP is that we're huge,
  • 0:34
    but how big are we really?
  • 0:36
    PHP consists of a lot of successful but extremely isolated
  • 0:41
    libraries, like Wordpress, Drupal, Joomla,
  • 0:44
    and all of the PHP frameworks, like Symfony and Zend.
  • 0:48
    Historically, these different groups share almost nothing,
  • 0:51
    which means that in reality we're all pretty small islands.
  • 0:55
    That's right.
  • 0:55
    Even though we're the biggest group in the world,
  • 0:57
    we've somehow turned ourselves into the underdogs.
  • 1:01
    Well, it's time for a whole new beautiful era where
  • 1:04
    your community is all of the PHP world.
  • 1:08
    PHP developers haven't typically shared code or used
  • 1:11
    outside libraries because, well, it sucked.
  • 1:14
    To use just one outside library, you'd
  • 1:16
    need to tackle at least three major issues.
  • 1:20
    First, how do autoload the PHP classes in the library?
  • 1:24
    Autoloading is the background machine
  • 1:25
    that makes it possible to reference PHP classes without using
  • 1:29
    'require' or 'include' statements.
  • 1:32
    When I bring in outside code, I either
  • 1:34
    need to figure out which files to include
  • 1:36
    or how to configure the library's autoloader.
  • 1:39
    Second, we need to know if this library depends
  • 1:41
    on any other libraries, and if it does-- oh gosh-- that's yet
  • 1:46
    another library I need to download and configure.
  • 1:49
    Finally, how should I store the library in my project?
  • 1:52
    Should I use SVN externals, kits of modules,
  • 1:56
    or just commit the whole darn thing into my project?
  • 1:59
    But, the answer to these three problems is Composer.
  • 2:03
    Want to do something crazy like bring Symfony's finder
  • 2:05
    component into a Drupal project?
  • 2:07
    We're about to learn just how easy that is.
  • 2:10
    But wait, before you non Drupal people run off,
  • 2:13
    the processing code you'll see have nothing to do with Drupal.
  • 2:16
    This is one of the first great things about using Composer.
  • 2:20
    The process for using external libraries
  • 2:22
    is the same now in any PHP project.
  • 2:26
    Move into the directory where your project lives.
  • 2:30
    Our first goal is to use the standalone Symfony component called
  • 2:33
    "finder" to list all the files in a directory
  • 2:35
    and print them onto the page.
  • 2:38
    Just to make things really exciting, we're
  • 2:40
    going to do this inside a custom Drupal module.
  • 2:43
    To start, I've created a function called get_current_files
  • 2:47
    deep inside my project, which returns an array of file names.
  • 2:51
    I've also gone far enough to print these out onto the screen.
  • 2:54
    OK, great.
  • 2:55
    Now, let's get to work.
  • 2:57
    The first thing you'll need to do is download the Composer executable.
  • 3:01
    Go to getcomposer.org.
  • 3:03
    Click 'Download', then copy one of the two code blocks,
  • 3:06
    depending if you have curl installed.
  • 3:09
    Composer itself is just an executable file,
  • 3:12
    and this fancy bit of code downloads the file
  • 3:14
    and makes sure your system is set up to use Composer.
  • 3:18
    If you see any errors or warnings during this step,
  • 3:21
    you may need to tweak your PHP configuration.
  • 3:24
    If everything went OK, you'll have a brand new composer.phar
  • 3:28
    sitting at the root of your project.
  • 3:30
    All right, let's put this guy to work.
  • 3:33
    Execute the Composer's script by typing php composer.phar.
  • 3:37
    This shows you a list of all the available Composer commands.
  • 3:41
    We'll get to know some of these in the next few minutes.
  • 3:45
    So far, we have a Composer.phar, but that's it.
  • 3:48
    Composer's main job is to download third party libraries
  • 3:52
    into a vendor directory in your project.
  • 3:54
    To tell composer which libraries you need,
  • 3:56
    your project needs to have a composer.json configuration file.
  • 4:01
    Instead of creating this file by hand,
  • 4:03
    let's use the first Composer command, init.
  • 4:07
    The init command will ask you several questions
  • 4:09
    about your project, but, unless you're planning to open source it,
  • 4:12
    don't worry too much about your answers.
  • 4:15
    Finally, it'll ask you to interactively
  • 4:17
    define your dependencies.
  • 4:20
    By dependencies, Composer is asking you which third party libraries
  • 4:24
    you want to include in your project.
  • 4:26
    To start, we're going to add a Symfony2 component called
  • 4:29
    'finder', and its documentation is at symfony.com.
  • 4:37
    At the prompt, simply type 'finder' and wait for the results.
  • 4:41
    Behind the scenes, Composer is searching
  • 4:43
    against a giant central repository of packages called Packagist, which
  • 4:47
    you can search directly at packagist.org.
  • 4:51
    In the language of Composer, a package
  • 4:53
    is just an individual directory that you
  • 4:55
    want to download into your project.
  • 4:58
    A typical package contains PHP classes,
  • 5:01
    but it can really contain anything.
  • 5:03
    Each package has a unique name and your first job
  • 5:06
    is to find out the name of the one you need.
  • 5:09
    Ideally, the name is included in the documentation for the library,
  • 5:12
    but even if it's not, you can often find it just by searching.
  • 5:16
    On Packagist, searching for 'finder' reveals a package called
  • 5:20
    'Symfony Finder', which is definitely the right one.
  • 5:25
    Back at the terminal, our search for 'finder' has returned a bunch
  • 5:28
    of results, including many versions of 'Symfony Finder'.
  • 5:31
    The second thing we need to figure out is which version we want.
  • 5:35
    The safest choice is to choose the latest stable release,
  • 5:38
    which should follow the x.x.x format.
  • 5:42
    In our case, this is v 2.1.2.
  • 5:46
    If a version ends in -dev, it's a development branch,
  • 5:49
    which may be stable or unstable based on the library.
  • 5:53
    The dev.master version is special, and it always
  • 5:57
    means the latest bleeding edge code.
  • 5:59
    If you use a lesser known package, dev.master may be your only option.
  • 6:04
    Let's choose to install v 2.1.2.
  • 6:08
    When it asks you to install dev dependencies interactively,
  • 6:11
    choose 'No'.
  • 6:12
    You probably won't need to worry about the dev dependencies,
  • 6:16
    but, if you're curious about them, check out Composer's documentation.
  • 6:21
    Finally, confirm generation and add vendor to your .gitignore file
  • 6:25
    if you're starting your project with Git.
  • 6:28
    The end product of the init command is the new composer.json
  • 6:31
    file that's now in your project.
  • 6:33
    Open it and check out the required key.
  • 6:36
    This is really the only important part of the file right now
  • 6:39
    and it simply tells Composer which packages your project needs.
  • 6:43
    We could have created this file by hand.
  • 6:45
    The init task, it's just there for convenience.
  • 6:48
    At this point we've downloaded the Composer executable
  • 6:51
    and created the composer.json config file.
  • 6:54
    To actually put Composer to work, run php composer.phar install.
  • 6:59
    This is the most important command.
  • 7:01
    It reads the composer.json file and downloads all the needed libraries
  • 7:05
    into the vendor directory.
  • 7:12
    And look, a vendor directory.
  • 7:14
    It contains a symfony directory that holds the finder library
  • 7:18
    and a few other things that help with autoloading, which is one
  • 7:21
    of the most powerful features of Composer.
  • 7:24
    Now that Composer has downloaded the Finder library, let's use it.
  • 7:29
    To keep things simple, I'll paste in some finder code that looks for all
  • 7:32
    the .gif files that have been modified within the past day.
  • 7:37
    This code should work, but when we refresh the page,
  • 7:40
    we get a class not found error.
  • 7:42
    Of course.
  • 7:44
    Even though Composer downloaded the Finder library for us,
  • 7:47
    we can't use any of its PHP classes without including them.
  • 7:52
    Fortunately, Composer solves this for us through autoloading.
  • 7:56
    The exact details of how autoloading works goes beyond the screen
  • 7:59
    cast, but the important thing is that Composer helps us out.
  • 8:03
    To use Composer's autoloader, simply include the vendor autoload.php
  • 8:07
    file somewhere in your project.
  • 8:09
    For now, let's put it right inside this function.
  • 8:24
    Refresh the page again.
  • 8:26
    It works.
  • 8:28
    By including Composer's autoloader, the Finder library, as well as
  • 8:32
    the other PHP classes for any other libraries we included via Composer,
  • 8:35
    are made available to us automatically.
  • 8:39
    To make a third party classes available anywhere,
  • 8:41
    it would be even better to include the autoload file
  • 8:44
    in some central bootstrap file in your project.
  • 8:47
    For Drupal, this might be the settings.php file.
  • 8:57
    When we refresh, everything still works.
  • 9:00
    Things are going so well that I think
  • 9:01
    we should add another library.
  • 9:03
    So let's get crazy.
  • 9:05
    Head back to packagist.org and find the library Symfony Filesystem.
  • 9:12
    To tell Composer that we want this package,
  • 9:14
    just edit the composer.json by hand.
  • 9:17
    Add a second entry under the require with the name of the library.
  • 9:21
    To make things more interesting, let's use the 2.1.x-dev version,
  • 9:27
    which will give us the latest commit on the 2.1 branch.
  • 9:31
    Next, we need to tell Composer to re-read this file
  • 9:33
    and download the new library.
  • 9:35
    Before, we used the install command to do this,
  • 9:38
    but if you try that command now, it prints out a few lines,
  • 9:41
    but doesn't actually do anything.
  • 9:43
    Why not?
  • 9:45
    When it comes to downloading the libraries we need,
  • 9:47
    Composer actually has two different commands, install and update.
  • 9:54
    When we ran the install command earlier, one of the things it did
  • 9:56
    was create a composer.lock file that recorded the exact versions
  • 10:00
    of all libraries that it downloaded at that exact moment.
  • 10:05
    Normally, the install command actually ignores the composer.json
  • 10:08
    file and reads all the information from the log file instead.
  • 10:13
    If you make a change to composer.json
  • 10:15
    and run php composer.phar install, that change won't be used.
  • 10:21
    The lock file is important because if multiple developers are using
  • 10:24
    a project, each one can run php composer.phar install and receive
  • 10:29
    identical versions of all libraries, even
  • 10:32
    if new commits have been added to them.
  • 10:35
    In fact, the only time that the installer command reads
  • 10:38
    the composer.json file is when you first start the project,
  • 10:41
    because the log file doesn't exist yet.
  • 10:44
    In this one case, install acts exactly like the update command,
  • 10:48
    which always ignores the lock file and reads
  • 10:51
    the composer.json file instead.
  • 10:54
    This checks and potentially upgrades all the libraries in composer.json
  • 10:58
    and updates composer.lock when it finishes.
  • 11:02
    What this ultimately means is that you should use a simple workflow.
  • 11:06
    Unless you're adding a new library or intentionally upgrading
  • 11:09
    something, always use composer.phar install.
  • 11:12
    When you do need to add a new library or upgrade something,
  • 11:16
    use composer.phar update.
  • 11:18
    You can be even more precise by calling 'update'
  • 11:20
    and passing it the name of the library that you're updating.
  • 11:24
    By doing this, Composer will only update
  • 11:27
    that library instead of all of them.
  • 11:30
    Also, Composer has a cool shortcut command for adding new libraries
  • 11:33
    into your project, php composer.phar require.
  • 11:39
    With the require command, you can search for the package you need
  • 11:42
    and composer will automatically update your composer.json for you
  • 11:45
    and run the update command to download the library.
  • 12:00
    In this case, when I included the Doctrine Devolve package,
  • 12:03
    an extra package called Doctrine Common was downloaded.
  • 12:07
    This is dependency management in action.
  • 12:10
    Composer is smart enough to know the Doctrine Devolve depends
  • 12:13
    on Doctrine Common and it downloads it for you.
  • 12:16
    Woo.
  • 12:17
    This is so awesome.
  • 12:20
    The lock file is especially important
  • 12:22
    if you have multiple developers so that you can be sure
  • 12:25
    that each person has identical vendor libraries.
  • 12:29
    To make this possible, commit both your composer.json
  • 12:32
    file and your composer.lock file.
  • 12:36
    Typically, composer.phar is ignored since each developer
  • 12:39
    can download it individually.
  • 12:54
    Now let's pretend like we're a new developer
  • 12:56
    that's pulling down the code base.
  • 13:06
    Notice that the project doesn't have a vendor directory yet
  • 13:10
    because we didn't commit the vendor files.
  • 13:12
    In fact, we ignore the vendor directory again
  • 13:15
    because composer can populate it for us.
  • 13:18
    I'll copy in the composer.phar file from the previous directory
  • 13:21
    and then run php composer.phar install.
  • 13:24
    This reads the composer.lock file and downloads everything
  • 13:27
    we need into the vendor directory.
  • 13:30
    And just like that, your new developer has a functional project.
  • 13:34
    There's a lot more the Composer can do,
  • 13:36
    but you already understand how to find libraries, manage
  • 13:38
    your composer.json file, use Composer's autoloader,
  • 13:42
    and download the external libraries with the update
  • 13:44
    or install commands.
  • 13:46
    If you'd like to learn more, check out
  • 13:48
    the documentation at getcomposer.org.
  • 13:56
    One interesting topic is scripts, which are callbacks that
  • 13:59
    are executed before or after packages are installed.
  • 14:03
    Other important topics include the dump autoload command, dev
  • 14:06
    dependencies, minimum stability, and installing composer globally.

The Wonderful World of Composer

Loading...

Welcome to the wonderful world of Composer, the Dependency Manager for PHP! In this tutorial, you'll learn how to install and configure Composer and use it to integrate third-party libraries into any PHP project. We'll walk through:

  • How Composer makes sharing awesome again
  • Downloading the composer.phar file
  • Creating the composer.json file
  • Installing the external libraries
  • Handling autoloading
  • Understanding the composer.lock file
  • The update versus install command
  • The require command
  • Storing in version control
Downloads: 
Log in or sign up to download companion files.