3. COMPOSER
INTRO
▸ Dependency Manager for PHP
▸ It allows you to declare the libraries your project depends
on and it will manage (install/update) them for you.
▸ Composer handles dependency resolution automatically
▸ Composer handles autoloading automatically
3
4. COMPOSER
DEPENDENCY MANAGEMENT
▸ Dependency management is actually a pretty simple concept. Let's
assume you're creating a one-page website using the Bootstrap
framework for your Javascript and CSS needs. How do you make sure
that Bootstrap is added to your project?
▸ The usual approach is to go to the website, download the package
and place it somewhere within your project. So far so good. Now, what
do you do when you want to update to the latest version? You repeat
the same thing, overwriting the old version.
▸ Let's assume this goes on for a while and you realize something is
broken. They've changed something in Bootstrap and now you have
to roll back, but where to? You'll need to find the older versions and
start applying them until you find the right one.
4
5. COMPOSER
DEPENDENCY MANAGEMENT
▸ Even if you sort all that out, let's say you move onto someone else's project. Are
they using Bootstrap as well? If so, where is it installed and what version is it?
▸ These may not seem like huge issues for a small project but imagine a project
with 8-10 dependencies - which still isn't a lot. Managing everything modularly
becomes impossible or - at the very least - a waste of time.
▸ Dependency management solves these problems by automating and
standardizing. The retrieval of dependencies such as Bootstrap, jQuery, Twig,
Symphony, logging modules and so on can be done programmatically.
Preferred versions can also be designated which protects against conflicts.
▸ A dependency manager standardizes the way the packages are stored and
where they are used from. In practice this means that every project that uses
the same dependency manager will follow the same structure - at least for
dependencies.
5
6. COMPOSER
INSTALLING COMPOSER (WINDOWS)
▸ Using the Installer
▸ This is the easiest way to get Composer set up on your
machine.
▸ Download and run Composer-Setup.exe. It will install
the latest Composer version and set up your PATH so
that you can just call composer from any directory in
your command line.
6
7. COMPOSER
BASIC USAGE
▸ For our basic usage introduction, we will be installing
monolog/monolog, a logging library.
▸ To start using Composer in your project, all you need is a
composer.json file. This file describes the dependencies
of your project and may contain other metadata as well.
▸ The first (and often only) thing you specify in
composer.json is the require key. You are simply telling
Composer which packages your project depends on.
7
9. COMPOSER
BASIC USAGE
▸ Package Names
▸ The package name consists of a vendor name and the
project's name.
▸ Package Version Constraints
▸ In our example, we are requesting the Monolog
package with the version constraint 1.0.*. This means
any version in the 1.0 development branch, or any
version that is greater than or equal to 1.0 and less than
1.1 (>=1.0 <1.1).
9
10. COMPOSER
BASIC USAGE
▸ Installing Dependencies
▸ To install the defined dependencies for your project, just
run the install command.
▸ composer install
▸ Updating Dependencies to their Latest Versions
▸ composer update
▸ composer update monolog/monolog
10
11. COMPOSER
BASIC USAGE
▸ Packagist
▸ Packagist.org is the main Composer repository.
▸ A Composer repository is basically a package source: a
place where you can get packages from.
▸ Packagist aims to be the central repository that
everybody uses.
▸ This means that you can automatically require any
package that is available there, without further specifying
where Composer should look for the package.
11
12. COMPOSER
BASIC USAGE
▸ Platform packages
▸ Composer has platform packages, which are virtual packages for things that are
installed on the system but are not actually installable by Composer. This includes PHP
itself, PHP extensions and some system libraries.
▸ php represents the PHP version of the user, allowing you to apply constraints, e.g.
>=5.4.0. To require a 64bit version of php, you can require the php-64bit
package.
▸ hhvm represents the version of the HHVM runtime and allows you to apply a
constraint, e.g., '>=2.3.3'.
▸ ext-<name> allows you to require PHP extensions (includes core extensions).
Versioning can be quite inconsistent here, so it's often a good idea to just set the
constraint to *. An example of an extension package name is ext-gd.
▸ lib-<name> allows constraints to be made on versions of libraries used by PHP. The
following are available: curl, iconv, icu, libxml, openssl, pcre, uuid, xsl.
12
13. COMPOSER
BASIC USAGE
▸ Autoloading
▸ For libraries that specify autoload information,
Composer generates a vendor/autoload.php file.
require __DIR__ . '/vendor/autoload.php';
$log = new MonologLogger('name');
$log->pushHandler(new
MonologHandlerStreamHandler('app.log',
MonologLogger::WARNING));
$log->addWarning('Foo');
13
14. COMPOSER
BASIC USAGE
▸ You can even add your own code to the autoloader by adding an autoload field to
composer.json.
{
"autoload": {
"psr-4": {"Acme": "src/"}
}
}
▸ Composer will register a PSR-4 autoloader for the Acme namespace.
▸ You define a mapping from namespaces to directories. The src directory would be in your
project root, on the same level as vendor directory is. An example filename would be src/
Foo.php containing an AcmeFoo class.
▸ After adding the autoload field, you have to re-run dump-autoload to re-generate the
vendor/autoload.php file.
14
15. COMPOSER
LIBRARIES
▸ Every project is a package
▸ As soon as you have a composer.json in a directory, that
directory is a package. When you add a require to a
project, you are making a package that depends on
other packages. The only difference between your
project and a library is that your project is a package
without a name.
▸ In order to make that package installable you need to
give it a name. You do this by adding the name property
in composer.json:
15
17. COMPOSER
COMPOSER GLOBAL
‣ You can install packages globally rather than locally into your project.
$ composer global require phpunit/phpunit
$ composer global require phpunit/dbunit
$ composer global require phing/phing
$ composer global require phpdocumentor/phpdocumentor
$ composer global require sebastian/phpcpd
$ composer global require phploc/phploc
$ composer global require phpmd/phpmd
$ composer global require squizlabs/php_codesniffer
‣ This will install PHPUnit and all its dependencies into the ~/.composer/vendor/ directory
and, most importantly, the phpunit CLI tools are installed into ~/.composer/vendor/bin/.
‣ Simply add this directory to your PATH
17
18. COMPOSER
COMPOSER RESOURCES
‣ Composer
‣ https://getcomposer.org/doc/
‣ Building maintainable PHP apps using Composer
‣ https://laravel-news.com/building-apps-composer
18