Drupal 8 Development Mastery (11): Composer

In this 11th part of our Drupal 8 development mastery, we introduce the central component of the Drupal 8 third-party integration system: Composer.

Drupal 8 Development Mastery: Composer

According to the Composer official website: Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Essentially, the Composer tool lets you explicitly state which third-party codes/libraries your project depends on, then it will download/update them for you. It will also download the dependencies that those libraries depends on without you explicitly declaring it.

The Composer system is made up two parts: The first part is Packagist.com, the online PHP package repository of third-party libraries, which host external codes your can use within your project. The second part is the Composer command-line application which you install in you system to enable you manage (download/update) dependencies from within your project's root folder.

To use Composer to manage dependencies in your project, you would first declare the libraries dependencies your project need in a composer.json file. You would place this file at the root folder of your project.

Then from the command line you would run the composer install command which would go through your declarations in the composer.json file and download all declared dependencies. It would also download any additional dependencies those libraries would require (recursive dependency resolution).

You can also update previously downloaded libraries by running the composer update command from from the command line.

Composer is the dependency manager of choice within the PHP community

Composer In Drupal 8

With regards to Drupal 8, Composer has been adopted as the dependency manager for third-party PHP scripts. Previously (in Drupal 7 and below) you would have to manually, or using drush, download and extract the libraries into the sites/all/libraries folder. Drupal 8 requires that you use Composer for that instead.

It is also through the Composer dependency manager tool that Drupal 8 integrates all of the third-party libraries it now depends on.

Beside using Composer for dependency management in Drupal 8 as described above, it is also being used as a tool for developing and deploying fully functional Drupal projects.

Also, beside downloading and updating dependencies, Composer in Drupal 8 also generates autoload files for the third-party libraries. This means that because Drupal 8 is fully object-oriented and now strictly follows modern programming best practices, you are not expected to manually include these libraries in your project files. The Composer automatically includes them via autoload files within the Drupal system. (Remember PSR-0 and PSR-4 from part 6?).

This is why you are not required to include third-party libraries manually in Drupal 8. It's possible, but it will not be practical or efficient.

Composer can be used to download more than just third-party libraries in Drupal 8, it can also be used to download regular Drupal modules and themes. This is the secret of its use for developing and deploying full projects in Drupal 8, similar to the way Drush Make was used in Drupal 7.

I will not go into the technical details about how Composer is used in Drupal 8 or how to use it to manage dependencies in your own Drupal projects here. Instead I will leave the following references to where these are better explained.

References

https://getcomposer.org/doc/00-intro.md
https://drupalwatchdog.com/volume-3/issue-2/composer-sharing-wider
https://bojanz.wordpress.com/2015/09/18/d8-composer-definitive-intro/
https://www.lullabot.com/articles/goodbye-drush-make-hello-composer
http://www.vmdoh.com/blog/building-drupal-8-website-composer
http://tim.millwoodonline.co.uk/post/124913346140/drupal-8-composer

Next: Drupal 8 Development Mastery (12): YAML

Categories: 

Add new comment