Managing dependencies and third-party code in PHP applications is a daily challenge, keeping everyone on the same versions during development and at deploy time a struggle. Meet Composer, a tool designed to help you maintain a consistent set of dependencies as well as share and discover new libraries. Let’s see how you can use this tool in your Applications and Libraries to wrangle your dependencies with a simple json configuration file and a command line interface.
22. Benefits
‣ Easily declare project dependencies
‣ Updating dependency versions extremely simple
‣ Ensures your team is on the same page
23. Benefits
‣ Easily declare project dependencies
‣ Updating dependency versions extremely simple
‣ Ensures your team is on the same page
‣ Tons of excellent related features
31. Keeping up to Date
$ composer self-update
You are using the latest composer version.
$ php composer.phar self-update
You are using the latest composer version.
or
32. Keeping up to Date
$ composer self-update
You are using the latest composer version.
(Always updates to the latest dev-master)
$ php composer.phar self-update
You are using the latest composer version.
or
51. composer.lock
‣ install writes a dependency lock file
‣ List of exact versions installed
‣ Commit both composer.lock and composer.json
52. composer.lock
‣ install writes a dependency lock file
‣ List of exact versions installed
‣ Commit both composer.lock and composer.json
‣ composer install now checks the lock file, not
composer.json
53. composer.lock
‣ install writes a dependency lock file
‣ List of exact versions installed
‣ Commit both composer.lock and composer.json
‣ composer install now checks the lock file, not
composer.json
‣ Update dependencies with composer update
63. Adding New Dependencies
Or use the command line
$ composer require ircmaxell/password-compat:1.0.3
composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing ircmaxell/password-compat (1.0.3)
Downloading: 100%
Writing lock file
Generating autoload files
composer require
69. Autoloading
$loader = require 'vendor/autoload.php';
$loader->add('AcmeTest', __DIR__);
Pro Tip
Grab an autoloader instance and
add more namespaces.
70. Kickstart a Project
$ composer create-project slim/slim-skeleton super-sweet-application
Installing slim/slim-skeleton (1.1.0)
- Installing slim/slim-skeleton (1.1.0)
Downloading: 100%
Created project in super-sweet-application
Loading composer repositories with package information
Installing dependencies (including require-dev)
- Installing slim/slim (2.3.0)
Downloading: 100%
- Installing slim/extras (2.0.3)
Loading from cache
- Installing twig/twig (v1.13.1)
Loading from cache
Writing lock file
Generating autoload files
composer create-project
74. Libraries
‣ Everyone has written a library of some sort
‣ Making up examples is not my strong suit
75. Libraries
‣ Everyone has written a library of some sort
‣ Making up examples is not my strong suit
‣ Let’s walk through a real library . . .
76. Libraries
‣ Everyone has written a library of some sort
‣ Making up examples is not my strong suit
‣ Let’s walk through a real library . . .
‣ . . . and check out some awesome Composer features
90. require-dev
‣ Lists packages required for development (think tests)
‣ Only applies to the root package
‣ Both install and update install require-dev by default
91. require-dev
‣ Lists packages required for development (think tests)
‣ Only applies to the root package
‣ Both install and update install require-dev by default
‣ (Use the --no-dev flag to skip installing dev
dependencies)
95. bin
‣ cli scripts to pass along to package users
‣ A set of files that should be treated as binaries
96. bin
‣ cli scripts to pass along to package users
‣ A set of files that should be treated as binaries
‣ Installs binaries to vendor/bin for any project that
depends on your project
97. bin
‣ cli scripts to pass along to package users
‣ A set of files that should be treated as binaries
‣ Installs binaries to vendor/bin for any project that
depends on your project
‣ php-domain-parser uses a bin to update the local copy
of the public suffix list
100. scripts
‣ Used to execute custom code during the Composer
execution process
101. scripts
‣ Used to execute custom code during the Composer
execution process
‣ PHP callback (defined as a static method) . . .
102. scripts
‣ Used to execute custom code during the Composer
execution process
‣ PHP callback (defined as a static method) . . .
‣ . . . or any command-line executable command
103. scripts
‣ Used to execute custom code during the Composer
execution process
‣ PHP callback (defined as a static method) . . .
‣ . . . or any command-line executable command
‣ Only scripts defined in the root composer.json are
executed
104. scripts
‣ Used to execute custom code during the Composer
execution process
‣ PHP callback (defined as a static method) . . .
‣ . . . or any command-line executable command
‣ Only scripts defined in the root composer.json are
executed
‣ If a script is defined in a dependency, you can use it in
your composer.json
108. scripts
‣ Composer fires “named events” during execution
‣ Named events include:
‣ pre- and post-install-cmd
109. scripts
‣ Composer fires “named events” during execution
‣ Named events include:
‣ pre- and post-install-cmd
‣ pre- and post-update-cmd
110. scripts
‣ Composer fires “named events” during execution
‣ Named events include:
‣ pre- and post-install-cmd
‣ pre- and post-update-cmd
‣ post-root-package-install
111. scripts
‣ Composer fires “named events” during execution
‣ Named events include:
‣ pre- and post-install-cmd
‣ pre- and post-update-cmd
‣ post-root-package-install
‣ Many more . . .
119. Push it to aVCS
‣ git, svn, or hg all work equally well
120. Push it to aVCS
‣ git, svn, or hg all work equally well
‣ For the sake of simplicity I’ll assume git/github
121. Push it to aVCS
‣ git, svn, or hg all work equally well
‣ For the sake of simplicity I’ll assume git/github
‣ Package versions:
122. Push it to aVCS
‣ git, svn, or hg all work equally well
‣ For the sake of simplicity I’ll assume git/github
‣ Package versions:
‣ Tags are package versions (1.0.0, v2.5.4, etc)
123. Push it to aVCS
‣ git, svn, or hg all work equally well
‣ For the sake of simplicity I’ll assume git/github
‣ Package versions:
‣ Tags are package versions (1.0.0, v2.5.4, etc)
‣ Branches are dev versions (dev-{branchname},
{branchname}-dev)
124. Push it to aVCS
‣ git, svn, or hg all work equally well
‣ For the sake of simplicity I’ll assume git/github
‣ Package versions:
‣ Tags are package versions (1.0.0, v2.5.4, etc)
‣ Branches are dev versions (dev-{branchname},
{branchname}-dev)
‣ For this reason, I strongly recommend using semantic
versioning: http://semver.org
129. Packagist
‣ The main package repository for Composer
‣ (Aside: Search Packagist before “rolling your own”)
130. Packagist
‣ The main package repository for Composer
‣ (Aside: Search Packagist before “rolling your own”)
‣ Packages published there need no special settings in
composer.json to be installable (see “repositories” key)
131. Packagist
‣ The main package repository for Composer
‣ (Aside: Search Packagist before “rolling your own”)
‣ Packages published there need no special settings in
composer.json to be installable (see “repositories” key)
‣ Submitting is dead simple
132. Packagist
‣ The main package repository for Composer
‣ (Aside: Search Packagist before “rolling your own”)
‣ Packages published there need no special settings in
composer.json to be installable (see “repositories” key)
‣ Submitting is dead simple
‣ Create account, hit big green submit button
155. Satis Demo Project
‣ Want to goof around with Satis?
‣ See: https://github.com/jeremykendall/satis-demo
156. Satis Demo Project
‣ Want to goof around with Satis?
‣ See: https://github.com/jeremykendall/satis-demo
‣ Clone, follow instructions in README, and WIN
157. Satis Demo Project
‣ Want to goof around with Satis?
‣ See: https://github.com/jeremykendall/satis-demo
‣ Clone, follow instructions in README, and WIN
‣ Satis really is dead simple to set up
168. Credits
‣ Thanks to Rafael Dohms, the Composer presenter
‣ Thanks to php[architect] for having me
169. Credits
‣ Thanks to Rafael Dohms, the Composer presenter
‣ Thanks to php[architect] for having me
‣ Thanks to Jordi Boggiano, Nils Adermann, and the
community for an amazing project (and all the
documentation I cribbed from)
170. Credits
‣ Thanks to Rafael Dohms, the Composer presenter
‣ Thanks to php[architect] for having me
‣ Thanks to Jordi Boggiano, Nils Adermann, and the
community for an amazing project (and all the
documentation I cribbed from)
‣ Thanks to our sponsors for help making this happen
171. Credits
‣ Thanks to Rafael Dohms, the Composer presenter
‣ Thanks to php[architect] for having me
‣ Thanks to Jordi Boggiano, Nils Adermann, and the
community for an amazing project (and all the
documentation I cribbed from)
‣ Thanks to our sponsors for help making this happen
‣ Thanks to all of you for being here